Merge "Refactor Watchlist code so mobile can be more consistent"
[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 */
29 class SpecialAllPages extends IncludableSpecialPage {
30
31 /**
32 * Maximum number of pages to show on single subpage.
33 *
34 * @var int $maxPerPage
35 */
36 protected $maxPerPage = 345;
37
38 /**
39 * Determines, which message describes the input field 'nsfrom'.
40 *
41 * @var string $nsfromMsg
42 */
43 protected $nsfromMsg = 'allpagesfrom';
44
45 /**
46 * Constructor
47 *
48 * @param string $name Name of the special page, as seen in links and URLs (default: 'Allpages')
49 */
50 function __construct( $name = 'Allpages' ) {
51 parent::__construct( $name );
52 }
53
54 /**
55 * Entry point : initialise variables and call subfunctions.
56 *
57 * @param string $par Becomes "FOO" when called like Special:Allpages/FOO (default null)
58 */
59 function execute( $par ) {
60 $request = $this->getRequest();
61 $out = $this->getOutput();
62
63 $this->setHeaders();
64 $this->outputHeader();
65 $out->allowClickjacking();
66
67 # GET values
68 $from = $request->getVal( 'from', null );
69 $to = $request->getVal( 'to', null );
70 $namespace = $request->getInt( 'namespace' );
71 $hideredirects = $request->getBool( 'hideredirects', false );
72
73 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
74
75 $out->setPageTitle(
76 ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) ) ?
77 $this->msg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) :
78 $this->msg( 'allarticles' )
79 );
80 $out->addModuleStyles( 'mediawiki.special' );
81
82 if ( $par !== null ) {
83 $this->showChunk( $namespace, $par, $to, $hideredirects );
84 } elseif ( $from !== null && $to === null ) {
85 $this->showChunk( $namespace, $from, $to, $hideredirects );
86 } else {
87 $this->showToplevel( $namespace, $from, $to, $hideredirects );
88 }
89 }
90
91 /**
92 * HTML for the top form
93 *
94 * @param int $namespace A namespace constant (default NS_MAIN).
95 * @param string $from DbKey we are starting listing at.
96 * @param string $to DbKey we are ending listing at.
97 * @param bool $hideredirects Dont show redirects (default false)
98 * @return string
99 */
100 function namespaceForm( $namespace = NS_MAIN, $from = '', $to = '', $hideredirects = false ) {
101 $t = $this->getPageTitle();
102
103 $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
104 $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $this->getConfig()->get( 'Script' ) ) );
105 $out .= Html::hidden( 'title', $t->getPrefixedText() );
106 $out .= Xml::openElement( 'fieldset' );
107 $out .= Xml::element( 'legend', null, $this->msg( 'allpages' )->text() );
108 $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
109 $out .= "<tr>
110 <td class='mw-label'>" .
111 Xml::label( $this->msg( 'allpagesfrom' )->text(), 'nsfrom' ) .
112 " </td>
113 <td class='mw-input'>" .
114 Xml::input( 'from', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
115 " </td>
116 </tr>
117 <tr>
118 <td class='mw-label'>" .
119 Xml::label( $this->msg( 'allpagesto' )->text(), 'nsto' ) .
120 " </td>
121 <td class='mw-input'>" .
122 Xml::input( 'to', 30, str_replace( '_', ' ', $to ), array( 'id' => 'nsto' ) ) .
123 " </td>
124 </tr>
125 <tr>
126 <td class='mw-label'>" .
127 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
128 " </td>
129 <td class='mw-input'>" .
130 Html::namespaceSelector(
131 array( 'selected' => $namespace ),
132 array( 'name' => 'namespace', 'id' => 'namespace' )
133 ) . ' ' .
134 Xml::checkLabel(
135 $this->msg( 'allpages-hide-redirects' )->text(),
136 'hideredirects',
137 'hideredirects',
138 $hideredirects
139 ) . ' ' .
140 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
141 " </td>
142 </tr>";
143 $out .= Xml::closeElement( 'table' );
144 $out .= Xml::closeElement( 'fieldset' );
145 $out .= Xml::closeElement( 'form' );
146 $out .= Xml::closeElement( 'div' );
147
148 return $out;
149 }
150
151 /**
152 * @param int $namespace (default NS_MAIN)
153 * @param string $from List all pages from this name
154 * @param string $to List all pages to this name
155 * @param bool $hideredirects Dont show redirects (default false)
156 */
157 function showToplevel( $namespace = NS_MAIN, $from = '', $to = '', $hideredirects = false ) {
158 $from = Title::makeTitleSafe( $namespace, $from );
159 $to = Title::makeTitleSafe( $namespace, $to );
160 $from = ( $from && $from->isLocal() ) ? $from->getDBkey() : null;
161 $to = ( $to && $to->isLocal() ) ? $to->getDBkey() : null;
162
163 $this->showChunk( $namespace, $from, $to, $hideredirects );
164 }
165
166 /**
167 * @param int $namespace Namespace (Default NS_MAIN)
168 * @param string $from List all pages from this name (default false)
169 * @param string $to List all pages to this name (default false)
170 * @param bool $hideredirects Dont show redirects (default false)
171 */
172 function showChunk( $namespace = NS_MAIN, $from = false, $to = false, $hideredirects = false ) {
173 $output = $this->getOutput();
174
175 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
176 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
177 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
178 $n = 0;
179
180 if ( !$fromList || !$toList ) {
181 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
182 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
183 // Show errormessage and reset to NS_MAIN
184 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
185 $namespace = NS_MAIN;
186 } else {
187 list( $namespace, $fromKey, $from ) = $fromList;
188 list( , $toKey, $to ) = $toList;
189
190 $dbr = wfGetDB( DB_SLAVE );
191 $conds = array(
192 'page_namespace' => $namespace,
193 'page_title >= ' . $dbr->addQuotes( $fromKey )
194 );
195
196 if ( $hideredirects ) {
197 $conds['page_is_redirect'] = 0;
198 }
199
200 if ( $toKey !== "" ) {
201 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
202 }
203
204 $res = $dbr->select( 'page',
205 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
206 $conds,
207 __METHOD__,
208 array(
209 'ORDER BY' => 'page_title',
210 'LIMIT' => $this->maxPerPage + 1,
211 'USE INDEX' => 'name_title',
212 )
213 );
214
215 if ( $res->numRows() > 0 ) {
216 $out = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-chunk' ) );
217 while ( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
218 $t = Title::newFromRow( $s );
219 if ( $t ) {
220 $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
221 Linker::link( $t ) .
222 ( $s->page_is_redirect ? '</div>' : '' );
223 } else {
224 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
225 }
226
227 if ( $n % 3 == 0 ) {
228 $out .= '<tr>';
229 }
230
231 $out .= "<td style=\"width:33%\">$link</td>";
232 $n++;
233 if ( $n % 3 == 0 ) {
234 $out .= "</tr>\n";
235 }
236 }
237
238 if ( ( $n % 3 ) != 0 ) {
239 $out .= "</tr>\n";
240 }
241 $out .= Xml::closeElement( 'table' );
242 } else {
243 $out = '';
244 }
245 }
246
247 if ( $this->including() ) {
248 $output->addHTML( $out );
249 return;
250 }
251
252 if ( $from == '' ) {
253 // First chunk; no previous link.
254 $prevTitle = null;
255 } else {
256 # Get the last title from previous chunk
257 $dbr = wfGetDB( DB_SLAVE );
258 $res_prev = $dbr->select(
259 'page',
260 'page_title',
261 array( 'page_namespace' => $namespace, 'page_title < ' . $dbr->addQuotes( $from ) ),
262 __METHOD__,
263 array( 'ORDER BY' => 'page_title DESC',
264 'LIMIT' => $this->maxPerPage, 'OFFSET' => ( $this->maxPerPage - 1 )
265 )
266 );
267
268 # Get first title of previous complete chunk
269 if ( $dbr->numrows( $res_prev ) >= $this->maxPerPage ) {
270 $pt = $dbr->fetchObject( $res_prev );
271 $prevTitle = Title::makeTitle( $namespace, $pt->page_title );
272 } else {
273 # The previous chunk is not complete, need to link to the very first title
274 # available in the database
275 $options = array( 'LIMIT' => 1 );
276 if ( !$dbr->implicitOrderby() ) {
277 $options['ORDER BY'] = 'page_title';
278 }
279 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
280 array( 'page_namespace' => $namespace ), __METHOD__, $options );
281 # Show the previous link if it s not the current requested chunk
282 if ( $from != $reallyFirstPage_title ) {
283 $prevTitle = Title::makeTitle( $namespace, $reallyFirstPage_title );
284 } else {
285 $prevTitle = null;
286 }
287 }
288 }
289
290 $self = $this->getPageTitle();
291
292 $topLinks = array(
293 Linker::link( $self, $this->msg( 'allpages' )->escaped() )
294 );
295 $bottomLinks = array();
296
297 # Do we put a previous link ?
298 if ( $prevTitle && $pt = $prevTitle->getText() ) {
299 $query = array( 'from' => $prevTitle->getText() );
300
301 if ( $namespace ) {
302 $query['namespace'] = $namespace;
303 }
304
305 if ( $hideredirects ) {
306 $query['hideredirects'] = $hideredirects;
307 }
308
309 $prevLink = Linker::linkKnown(
310 $self,
311 $this->msg( 'prevpage', $pt )->escaped(),
312 array(),
313 $query
314 );
315 $topLinks[] = $prevLink;
316 $bottomLinks[] = $prevLink;
317 }
318
319 if ( $n == $this->maxPerPage && $s = $res->fetchObject() ) {
320 # $s is the first link of the next chunk
321 $t = Title::makeTitle( $namespace, $s->page_title );
322 $query = array( 'from' => $t->getText() );
323
324 if ( $namespace ) {
325 $query['namespace'] = $namespace;
326 }
327
328 if ( $hideredirects ) {
329 $query['hideredirects'] = $hideredirects;
330 }
331
332 $nextLink = Linker::linkKnown(
333 $self,
334 $this->msg( 'nextpage', $t->getText() )->escaped(),
335 array(),
336 $query
337 );
338 $topLinks[] = $nextLink;
339 $bottomLinks[] = $nextLink;
340 }
341
342 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
343 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
344 '<tr>
345 <td>' .
346 $nsForm .
347 '</td>
348 <td class="mw-allpages-nav">' .
349 $this->getLanguage()->pipeList( $topLinks ) .
350 '</td></tr></table>';
351
352 $output->addHTML( $out2 . $out );
353
354 if ( count( $bottomLinks ) ) {
355 $output->addHTML(
356 Html::element( 'hr' ) .
357 Html::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
358 $this->getLanguage()->pipeList( $bottomLinks )
359 )
360 );
361 }
362 }
363
364 /**
365 * @param int $ns The namespace of the article
366 * @param string $text The name of the article
367 * @return array( int namespace, string dbkey, string pagename ) or null on error
368 */
369 protected function getNamespaceKeyAndText( $ns, $text ) {
370 if ( $text == '' ) {
371 # shortcut for common case
372 return array( $ns, '', '' );
373 }
374
375 $t = Title::makeTitleSafe( $ns, $text );
376 if ( $t && $t->isLocal() ) {
377 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
378 } elseif ( $t ) {
379 return null;
380 }
381
382 # try again, in case the problem was an empty pagename
383 $text = preg_replace( '/(#|$)/', 'X$1', $text );
384 $t = Title::makeTitleSafe( $ns, $text );
385 if ( $t && $t->isLocal() ) {
386 return array( $t->getNamespace(), '', '' );
387 } else {
388 return null;
389 }
390 }
391
392 protected function getGroupName() {
393 return 'pages';
394 }
395 }