Removes 'languageshtml' property in mediawiki API's 'parse' action
[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 global $wgScript;
102 $t = $this->getPageTitle();
103
104 $out = Xml::openElement( 'div', array( 'class' => 'namespaceoptions' ) );
105 $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
106 $out .= Html::hidden( 'title', $t->getPrefixedText() );
107 $out .= Xml::openElement( 'fieldset' );
108 $out .= Xml::element( 'legend', null, $this->msg( 'allpages' )->text() );
109 $out .= Xml::openElement( 'table', array( 'id' => 'nsselect', 'class' => 'allpages' ) );
110 $out .= "<tr>
111 <td class='mw-label'>" .
112 Xml::label( $this->msg( 'allpagesfrom' )->text(), 'nsfrom' ) .
113 " </td>
114 <td class='mw-input'>" .
115 Xml::input( 'from', 30, str_replace( '_', ' ', $from ), array( 'id' => 'nsfrom' ) ) .
116 " </td>
117 </tr>
118 <tr>
119 <td class='mw-label'>" .
120 Xml::label( $this->msg( 'allpagesto' )->text(), 'nsto' ) .
121 " </td>
122 <td class='mw-input'>" .
123 Xml::input( 'to', 30, str_replace( '_', ' ', $to ), array( 'id' => 'nsto' ) ) .
124 " </td>
125 </tr>
126 <tr>
127 <td class='mw-label'>" .
128 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
129 " </td>
130 <td class='mw-input'>" .
131 Html::namespaceSelector(
132 array( 'selected' => $namespace ),
133 array( 'name' => 'namespace', 'id' => 'namespace' )
134 ) . ' ' .
135 Xml::checkLabel(
136 $this->msg( 'allpages-hide-redirects' )->text(),
137 'hideredirects',
138 'hideredirects',
139 $hideredirects
140 ) . ' ' .
141 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
142 " </td>
143 </tr>";
144 $out .= Xml::closeElement( 'table' );
145 $out .= Xml::closeElement( 'fieldset' );
146 $out .= Xml::closeElement( 'form' );
147 $out .= Xml::closeElement( 'div' );
148
149 return $out;
150 }
151
152 /**
153 * @param int $namespace (default NS_MAIN)
154 * @param string $from List all pages from this name
155 * @param string $to List all pages to this name
156 * @param bool $hideredirects Dont show redirects (default false)
157 */
158 function showToplevel( $namespace = NS_MAIN, $from = '', $to = '', $hideredirects = false ) {
159 $from = Title::makeTitleSafe( $namespace, $from );
160 $to = Title::makeTitleSafe( $namespace, $to );
161 $from = ( $from && $from->isLocal() ) ? $from->getDBkey() : null;
162 $to = ( $to && $to->isLocal() ) ? $to->getDBkey() : null;
163
164 $this->showChunk( $namespace, $from, $to, $hideredirects );
165 }
166
167 /**
168 * @param int $namespace Namespace (Default NS_MAIN)
169 * @param string $from List all pages from this name (default false)
170 * @param string $to List all pages to this name (default false)
171 * @param bool $hideredirects Dont show redirects (default false)
172 */
173 function showChunk( $namespace = NS_MAIN, $from = false, $to = false, $hideredirects = false ) {
174 $output = $this->getOutput();
175
176 $fromList = $this->getNamespaceKeyAndText( $namespace, $from );
177 $toList = $this->getNamespaceKeyAndText( $namespace, $to );
178 $namespaces = $this->getContext()->getLanguage()->getNamespaces();
179 $n = 0;
180
181 if ( !$fromList || !$toList ) {
182 $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock();
183 } elseif ( !array_key_exists( $namespace, $namespaces ) ) {
184 // Show errormessage and reset to NS_MAIN
185 $out = $this->msg( 'allpages-bad-ns', $namespace )->parse();
186 $namespace = NS_MAIN;
187 } else {
188 list( $namespace, $fromKey, $from ) = $fromList;
189 list( , $toKey, $to ) = $toList;
190
191 $dbr = wfGetDB( DB_SLAVE );
192 $conds = array(
193 'page_namespace' => $namespace,
194 'page_title >= ' . $dbr->addQuotes( $fromKey )
195 );
196
197 if ( $hideredirects ) {
198 $conds['page_is_redirect'] = 0;
199 }
200
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 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-chunk' ) );
218 while ( ( $n < $this->maxPerPage ) && ( $s = $res->fetchObject() ) ) {
219 $t = Title::newFromRow( $s );
220 if ( $t ) {
221 $link = ( $s->page_is_redirect ? '<div class="allpagesredirect">' : '' ) .
222 Linker::link( $t ) .
223 ( $s->page_is_redirect ? '</div>' : '' );
224 } else {
225 $link = '[[' . htmlspecialchars( $s->page_title ) . ']]';
226 }
227
228 if ( $n % 3 == 0 ) {
229 $out .= '<tr>';
230 }
231
232 $out .= "<td style=\"width:33%\">$link</td>";
233 $n++;
234 if ( $n % 3 == 0 ) {
235 $out .= "</tr>\n";
236 }
237 }
238
239 if ( ( $n % 3 ) != 0 ) {
240 $out .= "</tr>\n";
241 }
242 $out .= Xml::closeElement( 'table' );
243 } else {
244 $out = '';
245 }
246 }
247
248 if ( $this->including() ) {
249 $output->addHTML( $out );
250 return;
251 }
252
253 if ( $from == '' ) {
254 // First chunk; no previous link.
255 $prevTitle = null;
256 } else {
257 # Get the last title from previous chunk
258 $dbr = wfGetDB( DB_SLAVE );
259 $res_prev = $dbr->select(
260 'page',
261 'page_title',
262 array( 'page_namespace' => $namespace, 'page_title < ' . $dbr->addQuotes( $from ) ),
263 __METHOD__,
264 array( 'ORDER BY' => 'page_title DESC',
265 'LIMIT' => $this->maxPerPage, 'OFFSET' => ( $this->maxPerPage - 1 )
266 )
267 );
268
269 # Get first title of previous complete chunk
270 if ( $dbr->numrows( $res_prev ) >= $this->maxPerPage ) {
271 $pt = $dbr->fetchObject( $res_prev );
272 $prevTitle = Title::makeTitle( $namespace, $pt->page_title );
273 } else {
274 # The previous chunk is not complete, need to link to the very first title
275 # available in the database
276 $options = array( 'LIMIT' => 1 );
277 if ( !$dbr->implicitOrderby() ) {
278 $options['ORDER BY'] = 'page_title';
279 }
280 $reallyFirstPage_title = $dbr->selectField( 'page', 'page_title',
281 array( 'page_namespace' => $namespace ), __METHOD__, $options );
282 # Show the previous link if it s not the current requested chunk
283 if ( $from != $reallyFirstPage_title ) {
284 $prevTitle = Title::makeTitle( $namespace, $reallyFirstPage_title );
285 } else {
286 $prevTitle = null;
287 }
288 }
289 }
290
291 $self = $this->getPageTitle();
292
293 $topLinks = array(
294 Linker::link( $self, $this->msg( 'allpages' )->escaped() )
295 );
296 $bottomLinks = array();
297
298 # Do we put a previous link ?
299 if ( $prevTitle && $pt = $prevTitle->getText() ) {
300 $query = array( 'from' => $prevTitle->getText() );
301
302 if ( $namespace ) {
303 $query['namespace'] = $namespace;
304 }
305
306 if ( $hideredirects ) {
307 $query['hideredirects'] = $hideredirects;
308 }
309
310 $prevLink = Linker::linkKnown(
311 $self,
312 $this->msg( 'prevpage', $pt )->escaped(),
313 array(),
314 $query
315 );
316 $topLinks[] = $prevLink;
317 $bottomLinks[] = $prevLink;
318 }
319
320 if ( $n == $this->maxPerPage && $s = $res->fetchObject() ) {
321 # $s is the first link of the next chunk
322 $t = Title::makeTitle( $namespace, $s->page_title );
323 $query = array( 'from' => $t->getText() );
324
325 if ( $namespace ) {
326 $query['namespace'] = $namespace;
327 }
328
329 if ( $hideredirects ) {
330 $query['hideredirects'] = $hideredirects;
331 }
332
333 $nextLink = Linker::linkKnown(
334 $self,
335 $this->msg( 'nextpage', $t->getText() )->escaped(),
336 array(),
337 $query
338 );
339 $topLinks[] = $nextLink;
340 $bottomLinks[] = $nextLink;
341 }
342
343 $nsForm = $this->namespaceForm( $namespace, $from, $to, $hideredirects );
344 $out2 = Xml::openElement( 'table', array( 'class' => 'mw-allpages-table-form' ) ) .
345 '<tr>
346 <td>' .
347 $nsForm .
348 '</td>
349 <td class="mw-allpages-nav">' .
350 $this->getLanguage()->pipeList( $topLinks ) .
351 '</td></tr></table>';
352
353 $output->addHTML( $out2 . $out );
354
355 if ( count( $bottomLinks ) ) {
356 $output->addHTML(
357 Html::element( 'hr' ) .
358 Html::rawElement( 'div', array( 'class' => 'mw-allpages-nav' ),
359 $this->getLanguage()->pipeList( $bottomLinks )
360 )
361 );
362 }
363 }
364
365 /**
366 * @param int $ns The namespace of the article
367 * @param string $text The name of the article
368 * @return array( int namespace, string dbkey, string pagename ) or null on error
369 */
370 protected function getNamespaceKeyAndText( $ns, $text ) {
371 if ( $text == '' ) {
372 # shortcut for common case
373 return array( $ns, '', '' );
374 }
375
376 $t = Title::makeTitleSafe( $ns, $text );
377 if ( $t && $t->isLocal() ) {
378 return array( $t->getNamespace(), $t->getDBkey(), $t->getText() );
379 } elseif ( $t ) {
380 return null;
381 }
382
383 # try again, in case the problem was an empty pagename
384 $text = preg_replace( '/(#|$)/', 'X$1', $text );
385 $t = Title::makeTitleSafe( $ns, $text );
386 if ( $t && $t->isLocal() ) {
387 return array( $t->getNamespace(), '', '' );
388 } else {
389 return null;
390 }
391 }
392
393 protected function getGroupName() {
394 return 'pages';
395 }
396 }