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