4348b14fbb5c3a1125a9b8b7959b06ddeea53176
[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 = Html::openElement( 'div', array( 'class' => 'mw-allpages-body' ) );
220 $out .= Html::openElement( 'ul', array( '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 Linker::link( $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 $out .= Html::closeElement( 'div' );
237 } else {
238 $out = '';
239 }
240
241 if ( $fromKey !== '' && !$this->including() ) {
242 # Get the first title from previous chunk
243 $prevConds = $filterConds;
244 $prevConds[] = 'page_title < ' . $dbr->addQuotes( $fromKey );
245 $prevKey = $dbr->selectField(
246 'page',
247 'page_title',
248 $prevConds,
249 __METHOD__,
250 array( 'ORDER BY' => 'page_title DESC', 'OFFSET' => $this->maxPerPage - 1 )
251 );
252
253 if ( $prevKey === false ) {
254 # The previous chunk is not complete, need to link to the very first title
255 # available in the database
256 $prevKey = $dbr->selectField(
257 'page',
258 'page_title',
259 $prevConds,
260 __METHOD__,
261 array( 'ORDER BY' => 'page_title' )
262 );
263 }
264
265 if ( $prevKey !== false ) {
266 $prevTitle = Title::makeTitle( $namespace, $prevKey );
267 }
268 }
269 }
270
271 if ( $this->including() ) {
272 $output->addHTML( $out );
273 return;
274 }
275
276 $self = $this->getPageTitle();
277
278 $topLinks = array(
279 Linker::link( $self, $this->msg( 'allpages' )->escaped() )
280 );
281 $bottomLinks = array();
282
283 # Do we put a previous link ?
284 if ( $prevTitle ) {
285 $query = array( 'from' => $prevTitle->getText() );
286
287 if ( $namespace ) {
288 $query['namespace'] = $namespace;
289 }
290
291 if ( $hideredirects ) {
292 $query['hideredirects'] = $hideredirects;
293 }
294
295 $prevLink = Linker::linkKnown(
296 $self,
297 $this->msg( 'prevpage', $prevTitle->getText() )->escaped(),
298 array(),
299 $query
300 );
301 $topLinks[] = $prevLink;
302 $bottomLinks[] = $prevLink;
303 }
304
305 if ( $n == $this->maxPerPage && $s = $res->fetchObject() ) {
306 # $s is the first link of the next chunk
307 $t = Title::makeTitle( $namespace, $s->page_title );
308 $query = array( 'from' => $t->getText() );
309
310 if ( $namespace ) {
311 $query['namespace'] = $namespace;
312 }
313
314 if ( $hideredirects ) {
315 $query['hideredirects'] = $hideredirects;
316 }
317
318 $nextLink = Linker::linkKnown(
319 $self,
320 $this->msg( 'nextpage', $t->getText() )->escaped(),
321 array(),
322 $query
323 );
324 $topLinks[] = $nextLink;
325 $bottomLinks[] = $nextLink;
326 }
327
328 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
329 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
330 '<tr>
331 <td>' .
332 $nsForm .
333 '</td>
334 <td class="mw-allpages-nav">' .
335 $this->getLanguage()->pipeList( $topLinks ) .
336 '</td></tr></table>';
337
338 $output->addHTML( $out2 . $out );
339
340 if ( count( $bottomLinks ) ) {
341 $output->addHTML(
342 Html::element( 'hr' ) .
343 Html::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
344 $this->getLanguage()->pipeList( $bottomLinks )
345 )
346 );
347 }
348 }
349
350 /**
351 * @param int $ns The namespace of the article
352 * @param string $text The name of the article
353 * @return array( int namespace, string dbkey, string pagename ) or null on error
354 */
355 protected function getNamespaceKeyAndText( $ns, $text ) {
356 if ( $text == '' ) {
357 # shortcut for common case
358 return array( $ns, '', '' );
359 }
360
361 $t = Title::makeTitleSafe( $ns, $text );
362 if ( $t && $t->isLocal() ) {
363 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
364 } elseif ( $t ) {
365 return null;
366 }
367
368 # try again, in case the problem was an empty pagename
369 $text = preg_replace( '/(#|$)/', 'X$1', $text );
370 $t = Title::makeTitleSafe( $ns, $text );
371 if ( $t && $t->isLocal() ) {
372 return array( $t->getNamespace(), '', '' );
373 } else {
374 return null;
375 }
376 }
377
378 protected function getGroupName() {
379 return 'pages';
380 }
381 }