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