MediaWiki master
SpecialPrefixIndex.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Specials;
25
26use HTMLCheckField;
27use LinkCache;
34
41
46 protected $stripPrefix = false;
47
48 protected $hideRedirects = false;
49
50 // Inherit $maxPerPage
51
52 private IConnectionProvider $dbProvider;
53 private LinkCache $linkCache;
54
59 public function __construct(
60 IConnectionProvider $dbProvider,
61 LinkCache $linkCache
62 ) {
63 parent::__construct( $dbProvider );
64 $this->mName = 'Prefixindex';
65 $this->dbProvider = $dbProvider;
66 $this->linkCache = $linkCache;
67 }
68
73 public function execute( $par ) {
74 $this->setHeaders();
75 $this->outputHeader();
76
77 $out = $this->getOutput();
78 $out->addModuleStyles( 'mediawiki.special' );
79
80 # GET values
81 $request = $this->getRequest();
82 $from = $request->getVal( 'from', '' );
83 $prefix = $request->getVal( 'prefix', '' );
84 $ns = $request->getIntOrNull( 'namespace' );
85 $namespace = (int)$ns; // if no namespace given, use 0 (NS_MAIN).
86 $this->hideRedirects = $request->getBool( 'hideredirects', $this->hideRedirects );
87 $this->stripPrefix = $request->getBool( 'stripprefix', $this->stripPrefix );
88
89 $namespaces = $this->getContentLanguage()->getNamespaces();
90 $out->setPageTitleMsg(
91 ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) )
92 ? $this->msg( 'prefixindex-namespace' )->plaintextParams(
93 str_replace( '_', ' ', $namespaces[$namespace] )
94 )
95 : $this->msg( 'prefixindex' )
96 );
97
98 $showme = '';
99 if ( $par !== null ) {
100 $showme = $par;
101 } elseif ( $prefix != '' ) {
102 $showme = $prefix;
103 } elseif ( $from != '' && $ns === null ) {
104 // For back-compat with Special:Allpages
105 // Don't do this if namespace is passed, so paging works when doing NS views.
106 $showme = $from;
107 }
108
109 // T29864: if transcluded, show all pages instead of the form.
110 if ( $this->including() || $showme != '' || $ns !== null ) {
111 $this->showPrefixChunk( $namespace, $showme, $from );
112 } else {
113 $out->addHTML( $this->namespacePrefixForm( $namespace, '' )->getHTML( false ) );
114 }
115 }
116
123 protected function namespacePrefixForm( $namespace = NS_MAIN, $from = '' ): HTMLForm {
124 $formDescriptor = [
125 'prefix' => [
126 'label-message' => 'allpagesprefix',
127 'name' => 'prefix',
128 'id' => 'nsfrom',
129 'type' => 'text',
130 'size' => '30',
131 'default' => str_replace( '_', ' ', $from ),
132 ],
133 'namespace' => [
134 'type' => 'namespaceselect',
135 'name' => 'namespace',
136 'id' => 'namespace',
137 'label-message' => 'namespace',
138 'all' => null,
139 'default' => $namespace,
140 ],
141 'hidedirects' => [
142 'class' => HTMLCheckField::class,
143 'name' => 'hideredirects',
144 'label-message' => 'allpages-hide-redirects',
145 ],
146 'stripprefix' => [
147 'class' => HTMLCheckField::class,
148 'name' => 'stripprefix',
149 'label-message' => 'prefixindex-strip',
150 ],
151 ];
152
153 $this->getHookRunner()->onSpecialPrefixIndexGetFormFilters( $this->getContext(), $formDescriptor );
154
155 $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
156 ->setMethod( 'get' )
157 ->setTitle( $this->getPageTitle() ) // Remove subpage
158 ->setWrapperLegendMsg( 'prefixindex' )
159 ->setSubmitTextMsg( 'prefixindex-submit' );
160
161 return $htmlForm->prepareForm();
162 }
163
169 protected function showPrefixChunk( $namespace, $prefix, $from = null ) {
170 if ( $from === null ) {
171 $from = $prefix;
172 }
173
174 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
175 $prefixList = $this->getNamespaceKeyAndText( $namespace, $prefix );
176 $namespaces = $this->getContentLanguage()->getNamespaces();
177 $res = null;
178 $n = 0;
179 $nextRow = null;
180 $preparedHtmlForm = $this->namespacePrefixForm( $namespace, $prefix );
181
182 if ( !$prefixList || !$fromList ) {
183 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
184 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
185 // Show errormessage and reset to NS_MAIN
186 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
187 $namespace = NS_MAIN;
188 } else {
189 [ $namespace, $prefixKey, $prefix ] = $prefixList;
190 [ /* $fromNS */, $fromKey, ] = $fromList;
191
192 # ## @todo FIXME: Should complain if $fromNs != $namespace
193
194 $dbr = $this->dbProvider->getReplicaDatabase();
195 $queryBuiler = $dbr->newSelectQueryBuilder()
196 ->select( LinkCache::getSelectFields() )
197 ->from( 'page' )
198 ->where( [
199 'page_namespace' => $namespace,
200 $dbr->expr(
201 'page_title',
202 IExpression::LIKE,
203 new LikeValue( $prefixKey, $dbr->anyString() )
204 ),
205 $dbr->expr( 'page_title', '>=', $fromKey ),
206 ] )
207 ->orderBy( 'page_title' )
208 ->limit( $this->maxPerPage + 1 )
209 ->useIndex( 'page_name_title' );
210
211 if ( $this->hideRedirects ) {
212 $queryBuiler->andWhere( [ 'page_is_redirect' => 0 ] );
213 }
214
215 $this->getHookRunner()->onSpecialPrefixIndexQuery( $preparedHtmlForm->mFieldData, $queryBuiler );
216
217 $res = $queryBuiler->caller( __METHOD__ )->fetchResultSet();
218
219 // @todo FIXME: Side link to previous
220
221 if ( $res->numRows() > 0 ) {
222 $out = Html::openElement( 'ul', [ 'class' => 'mw-prefixindex-list' ] );
223
224 $prefixLength = strlen( $prefix );
225 foreach ( $res as $row ) {
226 if ( $n >= $this->maxPerPage ) {
227 $nextRow = $row;
228 break;
229 }
230 $title = Title::newFromRow( $row );
231 // Make sure it gets into LinkCache
232 $this->linkCache->addGoodLinkObjFromRow( $title, $row );
233 $displayed = $title->getText();
234 // Try not to generate unclickable links
235 if ( $this->stripPrefix && $prefixLength !== strlen( $displayed ) ) {
236 $displayed = substr( $displayed, $prefixLength );
237 }
238 $link = ( $title->isRedirect() ? '<div class="allpagesredirect">' : '' ) .
239 $this->getLinkRenderer()->makeKnownLink(
240 $title,
241 $displayed
242 ) .
243 ( $title->isRedirect() ? '</div>' : '' );
244
245 $out .= "<li>$link</li>\n";
246 $n++;
247
248 }
249 $out .= Html::closeElement( 'ul' );
250
251 if ( $res->numRows() > 2 ) {
252 // Only apply CSS column styles if there are more than 2 entries.
253 // Otherwise, rendering is broken as "mw-prefixindex-body"'s CSS column count is 3.
254 $out = Html::rawElement( 'div', [ 'class' => 'mw-prefixindex-body' ], $out );
255 }
256 } else {
257 $out = '';
258 }
259 }
260
261 $output = $this->getOutput();
262
263 if ( $this->including() ) {
264 // We don't show the nav-links and the form when included in other
265 // pages, so let's just finish here.
266 $output->addHTML( $out );
267 return;
268 }
269
270 $topOut = $preparedHtmlForm->getHTML( false );
271
272 if ( $res && ( $n == $this->maxPerPage ) && $nextRow ) {
273 $query = [
274 'from' => $nextRow->page_title,
275 'prefix' => $prefix,
276 'hideredirects' => $this->hideRedirects,
277 'stripprefix' => $this->stripPrefix,
278 ];
279
280 if ( $namespace || $prefix == '' ) {
281 // Keep the namespace even if it's 0 for empty prefixes.
282 // This tells us we're not just a holdover from old links.
283 $query['namespace'] = $namespace;
284 }
285
286 $nextLink = $this->getLinkRenderer()->makeKnownLink(
287 $this->getPageTitle(),
288 $this->msg( 'nextpage', str_replace( '_', ' ', $nextRow->page_title ) )->text(),
289 [],
290 $query
291 );
292
293 // Link shown at the top of the page below the form
294 $topOut .= Html::rawElement( 'div',
295 [ 'class' => 'mw-prefixindex-nav' ],
296 $nextLink
297 );
298
299 // Link shown at the footer
300 $out .= "\n" . Html::element( 'hr' ) .
301 Html::rawElement(
302 'div',
303 [ 'class' => 'mw-prefixindex-nav' ],
304 $nextLink
305 );
306
307 }
308
309 $output->addHTML( $topOut . $out );
310 }
311
312 protected function getGroupName() {
313 return 'pages';
314 }
315}
316
321class_alias( SpecialPrefixIndex::class, 'SpecialPrefixindex' );
const NS_MAIN
Definition Defines.php:64
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition LinkCache.php:45
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:206
setMethod( $method='post')
Set the method used to submit the form.
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getPageTitle( $subpage=false)
Get a self-referential title object.
getContext()
Gets the context this SpecialPage is executed in.
getRequest()
Get the WebRequest being used for this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
getContentLanguage()
Shortcut to get content language.
including( $x=null)
Whether the special page is being evaluated via transclusion.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
Implements Special:Allpages.
Implements Special:Prefixindex.
namespacePrefixForm( $namespace=NS_MAIN, $from='')
Prepared HTMLForm object for the top form.
__construct(IConnectionProvider $dbProvider, LinkCache $linkCache)
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
showPrefixChunk( $namespace, $prefix, $from=null)
execute( $par)
Entry point: initialise variables and call subfunctions.
$stripPrefix
Whether to remove the searched prefix from the displayed link.
Represents a title within MediaWiki.
Definition Title.php:78
Content of like value.
Definition LikeValue.php:14
Provide primary and replica IDatabase connections.
element(SerializerNode $parent, SerializerNode $node, $contents)
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...