0c1a941a9a349025d66838fbffb41cd314ebcb5c
[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 $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
104 $out .= Xml::openElement(
105 'form',
106 array( 'method' => 'get', 'action' => $this->getConfig()->get( 'Script' ) )
107 );
108 $out .= Html::hidden( 'title', $t->getPrefixedText() );
109 $out .= Xml::openElement( 'fieldset' );
110 $out .= Xml::element( 'legend', null, $this->msg( 'allpages' )->text() );
111 $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
112 $out .= "<tr>
113 <td class='mw-label'>" .
114 Xml::label( $this->msg( 'allpagesfrom' )->text(), 'nsfrom' ) .
115 " </td>
116 <td class='mw-input'>" .
117 Xml::input( 'from', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
118 " </td>
119 </tr>
120 <tr>
121 <td class='mw-label'>" .
122 Xml::label( $this->msg( 'allpagesto' )->text(), 'nsto' ) .
123 " </td>
124 <td class='mw-input'>" .
125 Xml::input( 'to', 30, str_replace( '_', ' ', $to ), array( 'id' => 'nsto' ) ) .
126 " </td>
127 </tr>
128 <tr>
129 <td class='mw-label'>" .
130 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
131 " </td>
132 <td class='mw-input'>" .
133 Html::namespaceSelector(
134 array( 'selected' => $namespace ),
135 array( 'name' => 'namespace', 'id' => 'namespace' )
136 ) . ' ' .
137 Xml::checkLabel(
138 $this->msg( 'allpages-hide-redirects' )->text(),
139 'hideredirects',
140 'hideredirects',
141 $hideredirects
142 ) . ' ' .
143 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
144 " </td>
145 </tr>";
146 $out .= Xml::closeElement( 'table' );
147 $out .= Xml::closeElement( 'fieldset' );
148 $out .= Xml::closeElement( 'form' );
149 $out .= Xml::closeElement( 'div' );
150 return $out;
151 }
152
153 /**
154 * @param int $namespace (default NS_MAIN)
155 * @param string $from List all pages from this name
156 * @param string $to List all pages to this name
157 * @param bool $hideredirects Dont show redirects (default false)
158 */
159 function showToplevel( $namespace = NS_MAIN, $from = '', $to = '', $hideredirects = false ) {
160 $from = Title::makeTitleSafe( $namespace, $from );
161 $to = Title::makeTitleSafe( $namespace, $to );
162 $from = ( $from && $from->isLocal() ) ? $from->getDBkey() : null;
163 $to = ( $to && $to->isLocal() ) ? $to->getDBkey() : null;
164
165 $this->showChunk( $namespace, $from, $to, $hideredirects );
166 }
167
168 /**
169 * @param int $namespace Namespace (Default NS_MAIN)
170 * @param string $from List all pages from this name (default false)
171 * @param string $to List all pages to this name (default false)
172 * @param bool $hideredirects Dont show redirects (default false)
173 */
174 function showChunk( $namespace = NS_MAIN, $from = false, $to = false, $hideredirects = false ) {
175 $output = $this->getOutput();
176
177 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
178 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
179 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
180 $n = 0;
181 $prevTitle = null;
182
183 if ( !$fromList || !$toList ) {
184 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
185 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
186 // Show errormessage and reset to NS_MAIN
187 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
188 $namespace = NS_MAIN;
189 } else {
190 list( $namespace, $fromKey, $from ) = $fromList;
191 list( , $toKey, $to ) = $toList;
192
193 $dbr = wfGetDB( DB_SLAVE );
194 $filterConds = array( 'page_namespace' => $namespace );
195 if ( $hideredirects ) {
196 $filterConds['page_is_redirect'] = 0;
197 }
198
199 $conds = $filterConds;
200 $conds[] = 'page_title >= ' . $dbr->addQuotes( $fromKey );
201 if ( $toKey !== "" ) {
202 $conds[] = 'page_title <= ' . $dbr->addQuotes( $toKey );
203 }
204
205 $res = $dbr->select( 'page',
206 array( 'page_namespace', 'page_title', 'page_is_redirect', 'page_id' ),
207 $conds,
208 __METHOD__,
209 array(
210 'ORDER BY' => 'page_title',
211 'LIMIT' => $this->maxPerPage + 1,
212 'USE INDEX' => 'name_title',
213 )
214 );
215
216 if ( $res->numRows() > 0 ) {
217 $out = Html::openElement( 'div', array( 'class' => 'mw-allpages-body' ) );
218 $out .= Html::openElement( 'ul', array( 'class' => 'mw-allpages-chunk' ) );
219
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 .= Html::closeElement( 'ul' );
234 $out .= Html::closeElement( 'div' );
235 } else {
236 $out = '';
237 }
238
239 if ( $fromKey !== '' && !$this->including() ) {
240 # Get the first title from previous chunk
241 $prevConds = $filterConds;
242 $prevConds[] = 'page_title < ' . $dbr->addQuotes( $fromKey );
243 $prevKey = $dbr->selectField(
244 'page',
245 'page_title',
246 $prevConds,
247 __METHOD__,
248 array( 'ORDER BY' => 'page_title DESC', 'OFFSET' => $this->maxPerPage - 1 )
249 );
250
251 if ( $prevKey === false ) {
252 # The previous chunk is not complete, need to link to the very first title
253 # available in the database
254 $prevKey = $dbr->selectField(
255 'page',
256 'page_title',
257 $prevConds,
258 __METHOD__,
259 array( 'ORDER BY' => 'page_title' )
260 );
261 }
262
263 if ( $prevKey !== false ) {
264 $prevTitle = Title::makeTitle( $namespace, $prevKey );
265 }
266 }
267 }
268
269 if ( $this->including() ) {
270 $output->addHTML( $out );
271 return;
272 }
273
274 $navLinks = array();
275 $self = $this->getPageTitle();
276
277 // Generate a "previous page" link if needed
278 if ( $prevTitle ) {
279 $query = array( 'from' => $prevTitle->getText() );
280
281 if ( $namespace ) {
282 $query['namespace'] = $namespace;
283 }
284
285 if ( $hideredirects ) {
286 $query['hideredirects'] = $hideredirects;
287 }
288
289 $navLinks[] = Linker::linkKnown(
290 $self,
291 $this->msg( 'prevpage', $prevTitle->getText() )->escaped(),
292 array(),
293 $query
294 );
295
296 }
297
298 // Generate a "next page" link if needed
299 if ( $n == $this->maxPerPage && $s = $res->fetchObject() ) {
300 # $s is the first link of the next chunk
301 $t = Title::makeTitle( $namespace, $s->page_title );
302 $query = array( 'from' => $t->getText() );
303
304 if ( $namespace ) {
305 $query['namespace'] = $namespace;
306 }
307
308 if ( $hideredirects ) {
309 $query['hideredirects'] = $hideredirects;
310 }
311
312 $navLinks[] = Linker::linkKnown(
313 $self,
314 $this->msg( 'nextpage', $t->getText() )->escaped(),
315 array(),
316 $query
317 );
318 }
319
320 $topOut = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
321
322 if ( count( $navLinks ) ) {
323 // Add pagination links
324 $pagination = Html::rawElement( 'div',
325 array( 'class' => 'mw-allpages-nav' ),
326 $this->getLanguage()->pipeList( $navLinks )
327 );
328
329 $topOut .= $pagination;
330 $out .= Html::element( 'hr' ) . $pagination; // Footer
331 }
332
333 $output->addHTML( $topOut . $out );
334 }
335
336 /**
337 * @param int $ns The namespace of the article
338 * @param string $text The name of the article
339 * @return array( int namespace, string dbkey, string pagename ) or null on error
340 */
341 protected function getNamespaceKeyAndText( $ns, $text ) {
342 if ( $text == '' ) {
343 # shortcut for common case
344 return array( $ns, '', '' );
345 }
346
347 $t = Title::makeTitleSafe( $ns, $text );
348 if ( $t && $t->isLocal() ) {
349 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
350 } elseif ( $t ) {
351 return null;
352 }
353
354 # try again, in case the problem was an empty pagename
355 $text = preg_replace( '/(#|$)/', 'X$1', $text );
356 $t = Title::makeTitleSafe( $ns, $text );
357 if ( $t && $t->isLocal() ) {
358 return array( $t->getNamespace(), '', '' );
359 } else {
360 return null;
361 }
362 }
363
364 protected function getGroupName() {
365 return 'pages';
366 }
367 }