Merge "Remove unreachable block"
[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->getContext()->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 * HTML for the top form
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 * @return string
100 */
101 function namespaceForm( $namespace = NS_MAIN, $from = '', $to = '', $hideredirects = false ) {
102 $t = $this->getPageTitle();
103
104 $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
105 $out .= Xml::openElement(
106 'form',
107 array( 'method' => 'get', 'action' => $this->getConfig()->get( 'Script' ) )
108 );
109 $out .= Html::hidden( 'title', $t->getPrefixedText() );
110 $out .= Xml::openElement( 'fieldset' );
111 $out .= Xml::element( 'legend', null, $this->msg( 'allpages' )->text() );
112 $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
113 $out .= "<tr>
114 <td class='mw-label'>" .
115 Xml::label( $this->msg( 'allpagesfrom' )->text(), 'nsfrom' ) .
116 " </td>
117 <td class='mw-input'>" .
118 Xml::input( 'from', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
119 " </td>
120 </tr>
121 <tr>
122 <td class='mw-label'>" .
123 Xml::label( $this->msg( 'allpagesto' )->text(), 'nsto' ) .
124 " </td>
125 <td class='mw-input'>" .
126 Xml::input( 'to', 30, str_replace( '_', ' ', $to ), array( 'id' => 'nsto' ) ) .
127 " </td>
128 </tr>
129 <tr>
130 <td class='mw-label'>" .
131 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
132 " </td>
133 <td class='mw-input'>" .
134 Html::namespaceSelector(
135 array( 'selected' => $namespace ),
136 array( 'name' => 'namespace', 'id' => 'namespace' )
137 ) . ' ' .
138 Xml::checkLabel(
139 $this->msg( 'allpages-hide-redirects' )->text(),
140 'hideredirects',
141 'hideredirects',
142 $hideredirects
143 ) . ' ' .
144 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
145 " </td>
146 </tr>";
147 $out .= Xml::closeElement( 'table' );
148 $out .= Xml::closeElement( 'fieldset' );
149 $out .= Xml::closeElement( 'form' );
150 $out .= Xml::closeElement( 'div' );
151
152 return $out;
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 Dont 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 $from List all pages from this name (default false)
173 * @param string $to List all pages to this name (default false)
174 * @param bool $hideredirects Dont 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_SLAVE );
196 $filterConds = array( '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 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
209 $conds,
210 __METHOD__,
211 array(
212 'ORDER BY' => 'page_title',
213 'LIMIT' => $this->maxPerPage + 1,
214 'USE INDEX' => 'name_title',
215 )
216 );
217
218 if ( $res->numRows() > 0 ) {
219 $out = Xml::openElement( 'ul', array( 'class' => 'mw-allpages-chunk' ) );
220 while ( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
221 $t = Title::newFromRow( $s );
222 if ( $t ) {
223 $out .= '<li' .
224 ( $s->page_is_redirect ? ' class="allpagesredirect"' : '' ) .
225 '>' .
226 Linker::link( $t ) .
227 "</li>\n";
228 } else {
229 $out .= '<li>[[' . htmlspecialchars( $s->page_title ) . "]]</li>\n";
230 }
231 $n++;
232 }
233 $out .= Xml::closeElement( 'ul' );
234 } else {
235 $out = '';
236 }
237
238 if ( $fromKey !== '' && !$this->including() ) {
239 # Get the first title from previous chunk
240 $prevConds = $filterConds;
241 $prevConds[] = 'page_title < ' . $dbr->addQuotes( $fromKey );
242 $prevKey = $dbr->selectField(
243 'page',
244 'page_title',
245 $prevConds,
246 __METHOD__,
247 array( 'ORDER BY' => 'page_title DESC', 'OFFSET' => $this->maxPerPage - 1 )
248 );
249
250 if ( $prevKey === false ) {
251 # The previous chunk is not complete, need to link to the very first title
252 # available in the database
253 $prevKey = $dbr->selectField(
254 'page',
255 'page_title',
256 $prevConds,
257 __METHOD__,
258 array( 'ORDER BY' => 'page_title' )
259 );
260 }
261
262 if ( $prevKey !== false ) {
263 $prevTitle = Title::makeTitle( $namespace, $prevKey );
264 }
265 }
266 }
267
268 if ( $this->including() ) {
269 $output->addHTML( $out );
270 return;
271 }
272
273 $self = $this->getPageTitle();
274
275 $topLinks = array(
276 Linker::link( $self, $this->msg( 'allpages' )->escaped() )
277 );
278 $bottomLinks = array();
279
280 # Do we put a previous link ?
281 if ( $prevTitle ) {
282 $query = array( 'from' => $prevTitle->getText() );
283
284 if ( $namespace ) {
285 $query['namespace'] = $namespace;
286 }
287
288 if ( $hideredirects ) {
289 $query['hideredirects'] = $hideredirects;
290 }
291
292 $prevLink = Linker::linkKnown(
293 $self,
294 $this->msg( 'prevpage', $prevTitle->getText() )->escaped(),
295 array(),
296 $query
297 );
298 $topLinks[] = $prevLink;
299 $bottomLinks[] = $prevLink;
300 }
301
302 if ( $n == $this->maxPerPage && $s = $res->fetchObject() ) {
303 # $s is the first link of the next chunk
304 $t = Title::makeTitle( $namespace, $s->page_title );
305 $query = array( 'from' => $t->getText() );
306
307 if ( $namespace ) {
308 $query['namespace'] = $namespace;
309 }
310
311 if ( $hideredirects ) {
312 $query['hideredirects'] = $hideredirects;
313 }
314
315 $nextLink = Linker::linkKnown(
316 $self,
317 $this->msg( 'nextpage', $t->getText() )->escaped(),
318 array(),
319 $query
320 );
321 $topLinks[] = $nextLink;
322 $bottomLinks[] = $nextLink;
323 }
324
325 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
326 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
327 '<tr>
328 <td>' .
329 $nsForm .
330 '</td>
331 <td class="mw-allpages-nav">' .
332 $this->getLanguage()->pipeList( $topLinks ) .
333 '</td></tr></table>';
334
335 $output->addHTML( $out2 . $out );
336
337 if ( count( $bottomLinks ) ) {
338 $output->addHTML(
339 Html::element( 'hr' ) .
340 Html::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
341 $this->getLanguage()->pipeList( $bottomLinks )
342 )
343 );
344 }
345 }
346
347 /**
348 * @param int $ns The namespace of the article
349 * @param string $text The name of the article
350 * @return array( int namespace, string dbkey, string pagename ) or null on error
351 */
352 protected function getNamespaceKeyAndText( $ns, $text ) {
353 if ( $text == '' ) {
354 # shortcut for common case
355 return array( $ns, '', '' );
356 }
357
358 $t = Title::makeTitleSafe( $ns, $text );
359 if ( $t && $t->isLocal() ) {
360 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
361 } elseif ( $t ) {
362 return null;
363 }
364
365 # try again, in case the problem was an empty pagename
366 $text = preg_replace( '/(#|$)/', 'X$1', $text );
367 $t = Title::makeTitleSafe( $ns, $text );
368 if ( $t && $t->isLocal() ) {
369 return array( $t->getNamespace(), '', '' );
370 } else {
371 return null;
372 }
373 }
374
375 protected function getGroupName() {
376 return 'pages';
377 }
378 }