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