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