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