Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[lhc/web/wiklou.git] / includes / specials / SpecialAllPages.php
1 <?php
2 /**
3 * Implements Special:Allpages
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Implements Special:Allpages
26 *
27 * @ingroup SpecialPage
28 * @todo Rewrite using IndexPager
29 */
30 class SpecialAllPages extends IncludableSpecialPage {
31
32 /**
33 * Maximum number of pages to show on single subpage.
34 *
35 * @var int $maxPerPage
36 */
37 protected $maxPerPage = 345;
38
39 /**
40 * Determines, which message describes the input field 'nsfrom'.
41 *
42 * @var string $nsfromMsg
43 */
44 protected $nsfromMsg = 'allpagesfrom';
45
46 /**
47 * @param string $name Name of the special page, as seen in links and URLs (default: 'Allpages')
48 */
49 function __construct( $name = 'Allpages' ) {
50 parent::__construct( $name );
51 }
52
53 /**
54 * Entry point : initialise variables and call subfunctions.
55 *
56 * @param string $par Becomes "FOO" when called like Special:Allpages/FOO (default null)
57 */
58 function execute( $par ) {
59 $request = $this->getRequest();
60 $out = $this->getOutput();
61
62 $this->setHeaders();
63 $this->outputHeader();
64 $out->allowClickjacking();
65
66 # GET values
67 $from = $request->getVal( 'from', null );
68 $to = $request->getVal( 'to', null );
69 $namespace = $request->getInt( 'namespace' );
70
71 $miserMode = (bool)$this->getConfig()->get( 'MiserMode' );
72
73 // Redirects filter is disabled in MiserMode
74 $hideredirects = $request->getBool( 'hideredirects', false ) && !$miserMode;
75
76 $namespaces = $this->getLanguage()->getNamespaces();
77
78 $out->setPageTitle(
79 ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) ) ?
80 $this->msg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
81 $this->msg( 'allarticles' )
82 );
83 $out->addModuleStyles( 'mediawiki.special' );
84
85 if ( $par !== null ) {
86 $this->showChunk( $namespace, $par, $to, $hideredirects );
87 } elseif ( $from !== null && $to === null ) {
88 $this->showChunk( $namespace, $from, $to, $hideredirects );
89 } else {
90 $this->showToplevel( $namespace, $from, $to, $hideredirects );
91 }
92 }
93
94 /**
95 * Outputs the HTMLForm used on this page
96 *
97 * @param int $namespace A namespace constant (default NS_MAIN).
98 * @param string $from DbKey we are starting listing at.
99 * @param string $to DbKey we are ending listing at.
100 * @param bool $hideRedirects Don't show redirects (default false)
101 */
102 protected function outputHTMLForm( $namespace = NS_MAIN,
103 $from = '', $to = '', $hideRedirects = false
104 ) {
105 $miserMode = (bool)$this->getConfig()->get( 'MiserMode' );
106 $formDescriptor = [
107 'from' => [
108 'type' => 'text',
109 'name' => 'from',
110 'id' => 'nsfrom',
111 'size' => 30,
112 'label-message' => 'allpagesfrom',
113 'default' => str_replace( '_', ' ', $from ),
114 ],
115 'to' => [
116 'type' => 'text',
117 'name' => 'to',
118 'id' => 'nsto',
119 'size' => 30,
120 'label-message' => 'allpagesto',
121 'default' => str_replace( '_', ' ', $to ),
122 ],
123 'namespace' => [
124 'type' => 'namespaceselect',
125 'name' => 'namespace',
126 'id' => 'namespace',
127 'label-message' => 'namespace',
128 'all' => null,
129 'default' => $namespace,
130 ],
131 'hideredirects' => [
132 'type' => 'check',
133 'name' => 'hideredirects',
134 'id' => 'hidredirects',
135 'label-message' => 'allpages-hide-redirects',
136 'value' => $hideRedirects,
137 ],
138 ];
139
140 if ( $miserMode ) {
141 unset( $formDescriptor['hideredirects'] );
142 }
143
144 $context = new DerivativeContext( $this->getContext() );
145 $context->setTitle( $this->getPageTitle() ); // Remove subpage
146 $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $context );
147 $htmlForm
148 ->setMethod( 'get' )
149 ->setWrapperLegendMsg( 'allpages' )
150 ->setSubmitTextMsg( 'allpagessubmit' )
151 ->prepareForm()
152 ->displayForm( false );
153 }
154
155 /**
156 * @param int $namespace (default NS_MAIN)
157 * @param string $from List all pages from this name
158 * @param string $to List all pages to this name
159 * @param bool $hideredirects Don't show redirects (default false)
160 */
161 function showToplevel( $namespace = NS_MAIN, $from = '', $to = '', $hideredirects = false ) {
162 $from = Title::makeTitleSafe( $namespace, $from );
163 $to = Title::makeTitleSafe( $namespace, $to );
164 $from = ( $from && $from->isLocal() ) ? $from->getDBkey() : null;
165 $to = ( $to && $to->isLocal() ) ? $to->getDBkey() : null;
166
167 $this->showChunk( $namespace, $from, $to, $hideredirects );
168 }
169
170 /**
171 * @param int $namespace Namespace (Default NS_MAIN)
172 * @param string|false $from List all pages from this name (default false)
173 * @param string|false $to List all pages to this name (default false)
174 * @param bool $hideredirects Don't show redirects (default false)
175 */
176 function showChunk( $namespace = NS_MAIN, $from = false, $to = false, $hideredirects = false ) {
177 $output = $this->getOutput();
178
179 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
180 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
181 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
182 $n = 0;
183 $prevTitle = null;
184
185 if ( !$fromList || !$toList ) {
186 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
187 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
188 // Show errormessage and reset to NS_MAIN
189 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
190 $namespace = NS_MAIN;
191 } else {
192 list( $namespace, $fromKey, $from ) = $fromList;
193 list( , $toKey, $to ) = $toList;
194
195 $dbr = wfGetDB( DB_REPLICA );
196 $filterConds = [ 'page_namespace' => $namespace ];
197 if ( $hideredirects ) {
198 $filterConds['page_is_redirect'] = 0;
199 }
200
201 $conds = $filterConds;
202 $conds[] = 'page_title >= ' . $dbr->addQuotes( $fromKey );
203 if ( $toKey !== "" ) {
204 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
205 }
206
207 $res = $dbr->select( 'page',
208 [ 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ],
209 $conds,
210 __METHOD__,
211 [
212 'ORDER BY' => 'page_title',
213 'LIMIT' => $this->maxPerPage + 1,
214 'USE INDEX' => 'name_title',
215 ]
216 );
217
218 $linkRenderer = $this->getLinkRenderer();
219 if ( $res->numRows() > 0 ) {
220 $out = Html::openElement( 'ul', [ 'class' => 'mw-allpages-chunk' ] );
221
222 while ( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
223 $t = Title::newFromRow( $s );
224 if ( $t ) {
225 $out .= '<li' .
226 ( $s->page_is_redirect ? ' class="allpagesredirect"' : '' ) .
227 '>' .
228 $linkRenderer->makeLink( $t ) .
229 "</li>\n";
230 } else {
231 $out .= '<li>[[' . htmlspecialchars( $s->page_title ) . "]]</li>\n";
232 }
233 $n++;
234 }
235 $out .= Html::closeElement( 'ul' );
236
237 if ( $res->numRows() > 2 ) {
238 // Only apply CSS column styles if there's more than 2 entries.
239 // Otherwise, rendering is broken as "mw-allpages-body"'s CSS column count is 3.
240 $out = Html::rawElement( 'div', [ 'class' => 'mw-allpages-body' ], $out );
241 }
242 } else {
243 $out = '';
244 }
245
246 if ( $fromKey !== '' && !$this->including() ) {
247 # Get the first title from previous chunk
248 $prevConds = $filterConds;
249 $prevConds[] = 'page_title < ' . $dbr->addQuotes( $fromKey );
250 $prevKey = $dbr->selectField(
251 'page',
252 'page_title',
253 $prevConds,
254 __METHOD__,
255 [ 'ORDER BY' => 'page_title DESC', 'OFFSET' => $this->maxPerPage - 1 ]
256 );
257
258 if ( $prevKey === false ) {
259 # The previous chunk is not complete, need to link to the very first title
260 # available in the database
261 $prevKey = $dbr->selectField(
262 'page',
263 'page_title',
264 $prevConds,
265 __METHOD__,
266 [ 'ORDER BY' => 'page_title' ]
267 );
268 }
269
270 if ( $prevKey !== false ) {
271 $prevTitle = Title::makeTitle( $namespace, $prevKey );
272 }
273 }
274 }
275
276 if ( $this->including() ) {
277 $output->addHTML( $out );
278 return;
279 }
280
281 $navLinks = [];
282 $self = $this->getPageTitle();
283
284 $linkRenderer = $this->getLinkRenderer();
285 // Generate a "previous page" link if needed
286 if ( $prevTitle ) {
287 $query = [ 'from' => $prevTitle->getText() ];
288
289 if ( $namespace ) {
290 $query['namespace'] = $namespace;
291 }
292
293 if ( $hideredirects ) {
294 $query['hideredirects'] = $hideredirects;
295 }
296
297 $navLinks[] = $linkRenderer->makeKnownLink(
298 $self,
299 $this->msg( 'prevpage', $prevTitle->getText() )->text(),
300 [],
301 $query
302 );
303
304 }
305
306 // Generate a "next page" link if needed
307 if ( $n == $this->maxPerPage && $s = $res->fetchObject() ) {
308 # $s is the first link of the next chunk
309 $t = Title::makeTitle( $namespace, $s->page_title );
310 $query = [ 'from' => $t->getText() ];
311
312 if ( $namespace ) {
313 $query['namespace'] = $namespace;
314 }
315
316 if ( $hideredirects ) {
317 $query['hideredirects'] = $hideredirects;
318 }
319
320 $navLinks[] = $linkRenderer->makeKnownLink(
321 $self,
322 $this->msg( 'nextpage', $t->getText() )->text(),
323 [],
324 $query
325 );
326 }
327
328 $this->outputHTMLForm( $namespace, $from, $to, $hideredirects );
329
330 if ( count( $navLinks ) ) {
331 // Add pagination links
332 $pagination = Html::rawElement( 'div',
333 [ 'class' => 'mw-allpages-nav' ],
334 $this->getLanguage()->pipeList( $navLinks )
335 );
336
337 $output->addHTML( $pagination );
338 $out .= Html::element( 'hr' ) . $pagination; // Footer
339 }
340
341 $output->addHTML( $out );
342 }
343
344 /**
345 * @param int $ns The namespace of the article
346 * @param string $text The name of the article
347 * @return array|null [ int namespace, string dbkey, string pagename ] or null on error
348 */
349 protected function getNamespaceKeyAndText( $ns, $text ) {
350 if ( $text == '' ) {
351 # shortcut for common case
352 return [ $ns, '', '' ];
353 }
354
355 $t = Title::makeTitleSafe( $ns, $text );
356 if ( $t && $t->isLocal() ) {
357 return [ $t->getNamespace(), $t->getDBkey(), $t->getText() ];
358 } elseif ( $t ) {
359 return null;
360 }
361
362 # try again, in case the problem was an empty pagename
363 $text = preg_replace( '/(#|$)/', 'X$1', $text );
364 $t = Title::makeTitleSafe( $ns, $text );
365 if ( $t && $t->isLocal() ) {
366 return [ $t->getNamespace(), '', '' ];
367 } else {
368 return null;
369 }
370 }
371
372 /**
373 * Return an array of subpages beginning with $search that this special page will accept.
374 *
375 * @param string $search Prefix to search for
376 * @param int $limit Maximum number of results to return (usually 10)
377 * @param int $offset Number of results to skip (usually 0)
378 * @return string[] Matching subpages
379 */
380 public function prefixSearchSubpages( $search, $limit, $offset ) {
381 return $this->prefixSearchString( $search, $limit, $offset );
382 }
383
384 protected function getGroupName() {
385 return 'pages';
386 }
387 }