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