Debian Codegraph @

includes/specials/SpecialPrefixIndex.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Specials;
22
23 use MediaWiki\Cache\LinkCache;
24 use MediaWiki\Html\Html;
25 use MediaWiki\HTMLForm\Field\HTMLCheckField;
26 use MediaWiki\HTMLForm\HTMLForm;
27 use MediaWiki\Title\Title;
28 use Wikimedia\Rdbms\IConnectionProvider;
29 use Wikimedia\Rdbms\IExpression;
30 use Wikimedia\Rdbms\LikeValue;
31
32 /**
33 * Implements Special:Prefixindex
34 *
35 * @ingroup SpecialPage
36 * @ingroup Search
37 */
38 class SpecialPrefixIndex extends SpecialAllPages {
39
40 /**
41 * Whether to remove the searched prefix from the displayed link. Useful
42 * for inclusion of a set of subpages in a root page.
43 * @var bool
44 */
45 protected $stripPrefix = false;
46
47 /** @var bool */
48 protected $hideRedirects = false;
49
50 // Inherit $maxPerPage
51
52 // phpcs:ignore MediaWiki.Commenting.PropertyDocumentation.WrongStyle
53 private IConnectionProvider $dbProvider;
54 private LinkCache $linkCache;
55
56 /**
57 * @param IConnectionProvider $dbProvider
58 * @param LinkCache $linkCache
59 */
60 public function __construct(
61 IConnectionProvider $dbProvider,
62 LinkCache $linkCache
63 ) {
64 parent::__construct( $dbProvider );
65 $this->mName = 'Prefixindex';
66 $this->dbProvider = $dbProvider;
67 $this->linkCache = $linkCache;
68 }
69
70 /**
71 * Entry point: initialise variables and call subfunctions.
72 * @param string|null $par Becomes "FOO" when called like Special:Prefixindex/FOO
73 */
74 public function execute( $par ) {
75 $this->setHeaders();
76 $this->outputHeader();
77
78 $out = $this->getOutput();
79 $out->addModuleStyles( 'mediawiki.special' );
80
81 # GET values
82 $request = $this->getRequest();
83 $from = $request->getVal( 'from', '' );
84 $prefix = $request->getVal( 'prefix', '' );
85 $ns = $request->getIntOrNull( 'namespace' );
86 $namespace = (int)$ns; // if no namespace given, use 0 (NS_MAIN).
87 $this->hideRedirects = $request->getBool( 'hideredirects', $this->hideRedirects );
88 $this->stripPrefix = $request->getBool( 'stripprefix', $this->stripPrefix );
89
90 $namespaces = $this->getContentLanguage()->getNamespaces();
91 $out->setPageTitleMsg(
92 ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) )
93 ? $this->msg( 'prefixindex-namespace' )->plaintextParams(
94 str_replace( '_', ' ', $namespaces[$namespace] )
95 )
96 : $this->msg( 'prefixindex' )
97 );
98
99 $showme = '';
100 if ( $par !== null ) {
101 $showme = $par;
102 } elseif ( $prefix != '' ) {
103 $showme = $prefix;
104 } elseif ( $from != '' && $ns === null ) {
105 // For back-compat with Special:Allpages
106 // Don't do this if namespace is passed, so paging works when doing NS views.
107 $showme = $from;
108 }
109
110 // T29864: if transcluded, show all pages instead of the form.
111 if ( $this->including() || $showme != '' || $ns !== null ) {
112 $this->showPrefixChunk( $namespace, $showme, $from );
113 } else {
114 $out->addHTML( $this->namespacePrefixForm( $namespace, '' )->getHTML( false ) );
115 }
116 }
117
118 /**
119 * Prepared HTMLForm object for the top form
120 * @param int $namespace A namespace constant (default NS_MAIN).
121 * @param string $from DbKey we are starting listing at.
122 * @return HTMLForm
123 */
124 protected function namespacePrefixForm( $namespace = NS_MAIN, $from = '' ): HTMLForm {
125 $formDescriptor = [
126 'prefix' => [
127 'label-message' => 'allpagesprefix',
128 'name' => 'prefix',
129 'id' => 'nsfrom',
130 'type' => 'text',
131 'size' => '30',
132 'default' => str_replace( '_', ' ', $from ),
133 ],
134 'namespace' => [
135 'type' => 'namespaceselect',
136 'name' => 'namespace',
137 'id' => 'namespace',
138 'label-message' => 'namespace',
139 'all' => null,
140 'default' => $namespace,
141 ],
142 'hidedirects' => [
143 'class' => HTMLCheckField::class,
144 'name' => 'hideredirects',
145 'label-message' => 'allpages-hide-redirects',
146 ],
147 'stripprefix' => [
148 'class' => HTMLCheckField::class,
149 'name' => 'stripprefix',
150 'label-message' => 'prefixindex-strip',
151 ],
152 ];
153
154 $this->getHookRunner()->onSpecialPrefixIndexGetFormFilters( $this->getContext(), $formDescriptor );
155
156 $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
157 ->setMethod( 'get' )
158 ->setTitle( $this->getPageTitle() ) // Remove subpage
159 ->setWrapperLegendMsg( 'prefixindex' )
160 ->setSubmitTextMsg( 'prefixindex-submit' );
161
162 return $htmlForm->prepareForm();
163 }
164
165 /**
166 * @param int $namespace
167 * @param string $prefix
168 * @param string|null $from List all pages from this name (default false)
169 */
170 protected function showPrefixChunk( $namespace, $prefix, $from = null ) {
171 $from ??= $prefix;
172
173 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
174 $prefixList = $this->getNamespaceKeyAndText( $namespace, $prefix );
175 $namespaces = $this->getContentLanguage()->getNamespaces();
176 $res = null;
177 $n = 0;
178 $nextRow = null;
179 $preparedHtmlForm = $this->namespacePrefixForm( $namespace, $prefix );
180
181 if ( !$prefixList || !$fromList ) {
182 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
183 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
184 // Show errormessage and reset to NS_MAIN
185 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
186 $namespace = NS_MAIN;
187 } else {
188 [ $namespace, $prefixKey, $prefix ] = $prefixList;
189 [ /* $fromNS */, $fromKey, ] = $fromList;
190
191 # ## @todo FIXME: Should complain if $fromNs != $namespace
192
193 $dbr = $this->dbProvider->getReplicaDatabase();
194 $queryBuiler = $dbr->newSelectQueryBuilder()
195 ->select( LinkCache::getSelectFields() )
196 ->from( 'page' )
197 ->where( [
198 'page_namespace' => $namespace,
199 $dbr->expr(
200 'page_title',
201 IExpression::LIKE,
202 new LikeValue( $prefixKey, $dbr->anyString() )
203 ),
204 $dbr->expr( 'page_title', '>=', $fromKey ),
205 ] )
206 ->orderBy( 'page_title' )
207 ->limit( $this->maxPerPage + 1 )
208 ->useIndex( 'page_name_title' );
209
210 if ( $this->hideRedirects ) {
211 $queryBuiler->andWhere( [ 'page_is_redirect' => 0 ] );
212 }
213
214 $this->getHookRunner()->onSpecialPrefixIndexQuery( $preparedHtmlForm->mFieldData, $queryBuiler );
215
216 $res = $queryBuiler->caller( __METHOD__ )->fetchResultSet();
217
218 // @todo FIXME: Side link to previous
219
220 if ( $res->numRows() > 0 ) {
221 $out = Html::openElement( 'ul', [ 'class' => 'mw-prefixindex-list' ] );
222
223 $prefixLength = strlen( $prefix );
224 foreach ( $res as $row ) {
225 if ( $n >= $this->maxPerPage ) {
226 $nextRow = $row;
227 break;
228 }
229 $title = Title::newFromRow( $row );
230 // Make sure it gets into LinkCache
231 $this->linkCache->addGoodLinkObjFromRow( $title, $row );
232 $displayed = $title->getText();
233 // Try not to generate unclickable links
234 if ( $this->stripPrefix && $prefixLength !== strlen( $displayed ) ) {
235 $displayed = substr( $displayed, $prefixLength );
236 }
237 $link = ( $title->isRedirect() ? '<div class="allpagesredirect">' : '' ) .
238 $this->getLinkRenderer()->makeKnownLink(
239 $title,
240 $displayed
241 ) .
242 ( $title->isRedirect() ? '</div>' : '' );
243
244 $out .= "<li>$link</li>\n";
245 $n++;
246
247 }
248 $out .= Html::closeElement( 'ul' );
249
250 if ( $res->numRows() > 2 ) {
251 // Only apply CSS column styles if there are more than 2 entries.
252 // Otherwise, rendering is broken as "mw-prefixindex-body"'s CSS column count is 3.
253 $out = Html::rawElement( 'div', [ 'class' => 'mw-prefixindex-body' ], $out );
254 }
255 } else {
256 $out = '';
257 }
258 }
259
260 $output = $this->getOutput();
261
262 if ( $this->including() ) {
263 // We don't show the nav-links and the form when included in other
264 // pages, so let's just finish here.
265 $output->addHTML( $out );
266 return;
267 }
268
269 $topOut = $preparedHtmlForm->getHTML( false );
270
271 if ( $res && ( $n == $this->maxPerPage ) && $nextRow ) {
272 $query = [
273 'from' => $nextRow->page_title,
274 'prefix' => $prefix,
275 'hideredirects' => $this->hideRedirects,
276 'stripprefix' => $this->stripPrefix,
277 ];
278
279 if ( $namespace || $prefix == '' ) {
280 // Keep the namespace even if it's 0 for empty prefixes.
281 // This tells us we're not just a holdover from old links.
282 $query['namespace'] = $namespace;
283 }
284
285 $nextLink = $this->getLinkRenderer()->makeKnownLink(
286 $this->getPageTitle(),
287 $this->msg( 'nextpage', str_replace( '_', ' ', $nextRow->page_title ) )->text(),
288 [],
289 $query
290 );
291
292 // Link shown at the top of the page below the form
293 $topOut .= Html::rawElement( 'div',
294 [ 'class' => 'mw-prefixindex-nav' ],
295 $nextLink
296 );
297
298 // Link shown at the footer
299 $out .= "\n" . Html::element( 'hr' ) .
300 Html::rawElement(
301 'div',
302 [ 'class' => 'mw-prefixindex-nav' ],
303 $nextLink
304 );
305
306 }
307
308 $output->addHTML( $topOut . $out );
309 }
310
311 protected function getGroupName() {
312 return 'pages';
313 }
314 }
315
316 /**
317 * Retain the old class name for backwards compatibility.
318 * @deprecated since 1.41
319 */
320 class_alias( SpecialPrefixIndex::class, 'SpecialPrefixindex' );