* Made IndexPager extend ContextSource
[lhc/web/wiklou.git] / includes / specials / SpecialNewpages.php
1 <?php
2 /**
3 * Implements Special:Newpages
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 * A special page that list newly created pages
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialNewpages extends IncludableSpecialPage {
30
31 // Stored objects
32
33 /**
34 * @var FormOptions
35 */
36 protected $opts;
37 protected $customFilters;
38
39 // Some internal settings
40 protected $showNavigation = false;
41
42 public function __construct() {
43 parent::__construct( 'Newpages' );
44 }
45
46 protected function setup( $par ) {
47 global $wgEnableNewpagesUserFilter;
48
49 // Options
50 $opts = new FormOptions();
51 $this->opts = $opts; // bind
52 $opts->add( 'hideliu', false );
53 $opts->add( 'hidepatrolled', $this->getUser()->getBoolOption( 'newpageshidepatrolled' ) );
54 $opts->add( 'hidebots', false );
55 $opts->add( 'hideredirs', true );
56 $opts->add( 'limit', (int)$this->getUser()->getOption( 'rclimit' ) );
57 $opts->add( 'offset', '' );
58 $opts->add( 'namespace', '0' );
59 $opts->add( 'username', '' );
60 $opts->add( 'feed', '' );
61 $opts->add( 'tagfilter', '' );
62
63 $this->customFilters = array();
64 wfRunHooks( 'SpecialNewPagesFilters', array( $this, &$this->customFilters ) );
65 foreach( $this->customFilters as $key => $params ) {
66 $opts->add( $key, $params['default'] );
67 }
68
69 // Set values
70 $opts->fetchValuesFromRequest( $this->getRequest() );
71 if ( $par ) $this->parseParams( $par );
72
73 // Validate
74 $opts->validateIntBounds( 'limit', 0, 5000 );
75 if( !$wgEnableNewpagesUserFilter ) {
76 $opts->setValue( 'username', '' );
77 }
78 }
79
80 protected function parseParams( $par ) {
81 global $wgLang;
82 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
83 foreach ( $bits as $bit ) {
84 if ( 'shownav' == $bit ) {
85 $this->showNavigation = true;
86 }
87 if ( 'hideliu' === $bit ) {
88 $this->opts->setValue( 'hideliu', true );
89 }
90 if ( 'hidepatrolled' == $bit ) {
91 $this->opts->setValue( 'hidepatrolled', true );
92 }
93 if ( 'hidebots' == $bit ) {
94 $this->opts->setValue( 'hidebots', true );
95 }
96 if ( 'showredirs' == $bit ) {
97 $this->opts->setValue( 'hideredirs', false );
98 }
99 if ( is_numeric( $bit ) ) {
100 $this->opts->setValue( 'limit', intval( $bit ) );
101 }
102
103 $m = array();
104 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
105 $this->opts->setValue( 'limit', intval( $m[1] ) );
106 }
107 // PG offsets not just digits!
108 if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) ) {
109 $this->opts->setValue( 'offset', intval( $m[1] ) );
110 }
111 if ( preg_match( '/^username=(.*)$/', $bit, $m ) ) {
112 $this->opts->setValue( 'username', $m[1] );
113 }
114 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
115 $ns = $wgLang->getNsIndex( $m[1] );
116 if( $ns !== false ) {
117 $this->opts->setValue( 'namespace', $ns );
118 }
119 }
120 }
121 }
122
123 /**
124 * Show a form for filtering namespace and username
125 *
126 * @param $par String
127 * @return String
128 */
129 public function execute( $par ) {
130 $out = $this->getOutput();
131
132 $this->setHeaders();
133 $this->outputHeader();
134
135 $this->showNavigation = !$this->including(); // Maybe changed in setup
136 $this->setup( $par );
137
138 if( !$this->including() ) {
139 // Settings
140 $this->form();
141
142 $this->setSyndicated();
143 $feedType = $this->opts->getValue( 'feed' );
144 if( $feedType ) {
145 return $this->feed( $feedType );
146 }
147 }
148
149 $pager = new NewPagesPager( $this, $this->opts );
150 $pager->mLimit = $this->opts->getValue( 'limit' );
151 $pager->mOffset = $this->opts->getValue( 'offset' );
152
153 if( $pager->getNumRows() ) {
154 $navigation = '';
155 if ( $this->showNavigation ) {
156 $navigation = $pager->getNavigationBar();
157 }
158 $out->addHTML( $navigation . $pager->getBody() . $navigation );
159 } else {
160 $out->addWikiMsg( 'specialpage-empty' );
161 }
162 }
163
164 protected function filterLinks() {
165 global $wgGroupPermissions, $wgLang;
166
167 // show/hide links
168 $showhide = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ) );
169
170 // Option value -> message mapping
171 $filters = array(
172 'hideliu' => 'rcshowhideliu',
173 'hidepatrolled' => 'rcshowhidepatr',
174 'hidebots' => 'rcshowhidebots',
175 'hideredirs' => 'whatlinkshere-hideredirs'
176 );
177 foreach ( $this->customFilters as $key => $params ) {
178 $filters[$key] = $params['msg'];
179 }
180
181 // Disable some if needed
182 # @todo FIXME: Throws E_NOTICEs if not set; and doesn't obey hooks etc.
183 if ( $wgGroupPermissions['*']['createpage'] !== true ) {
184 unset( $filters['hideliu'] );
185 }
186 if ( !$this->getUser()->useNPPatrol() ) {
187 unset( $filters['hidepatrolled'] );
188 }
189
190 $links = array();
191 $changed = $this->opts->getChangedValues();
192 unset( $changed['offset'] ); // Reset offset if query type changes
193
194 $self = $this->getTitle();
195 foreach ( $filters as $key => $msg ) {
196 $onoff = 1 - $this->opts->getValue( $key );
197 $link = Linker::link( $self, $showhide[$onoff], array(),
198 array( $key => $onoff ) + $changed
199 );
200 $links[$key] = wfMsgHtml( $msg, $link );
201 }
202
203 return $wgLang->pipeList( $links );
204 }
205
206 protected function form() {
207 global $wgEnableNewpagesUserFilter, $wgScript;
208
209 // Consume values
210 $this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW
211 $namespace = $this->opts->consumeValue( 'namespace' );
212 $username = $this->opts->consumeValue( 'username' );
213 $tagFilterVal = $this->opts->consumeValue( 'tagfilter' );
214
215 // Check username input validity
216 $ut = Title::makeTitleSafe( NS_USER, $username );
217 $userText = $ut ? $ut->getText() : '';
218
219 // Store query values in hidden fields so that form submission doesn't lose them
220 $hidden = array();
221 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
222 $hidden[] = Html::hidden( $key, $value );
223 }
224 $hidden = implode( "\n", $hidden );
225
226 $tagFilter = ChangeTags::buildTagFilterSelector( $tagFilterVal );
227 if ( $tagFilter ) {
228 list( $tagFilterLabel, $tagFilterSelector ) = $tagFilter;
229 }
230
231 $form = Xml::openElement( 'form', array( 'action' => $wgScript ) ) .
232 Html::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
233 Xml::fieldset( wfMsg( 'newpages' ) ) .
234 Xml::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
235 '<tr>
236 <td class="mw-label">' .
237 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
238 '</td>
239 <td class="mw-input">' .
240 Xml::namespaceSelector( $namespace, 'all' ) .
241 '</td>
242 </tr>' . ( $tagFilter ? (
243 '<tr>
244 <td class="mw-label">' .
245 $tagFilterLabel .
246 '</td>
247 <td class="mw-input">' .
248 $tagFilterSelector .
249 '</td>
250 </tr>' ) : '' ) .
251 ( $wgEnableNewpagesUserFilter ?
252 '<tr>
253 <td class="mw-label">' .
254 Xml::label( wfMsg( 'newpages-username' ), 'mw-np-username' ) .
255 '</td>
256 <td class="mw-input">' .
257 Xml::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
258 '</td>
259 </tr>' : '' ) .
260 '<tr> <td></td>
261 <td class="mw-submit">' .
262 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
263 '</td>
264 </tr>' .
265 '<tr>
266 <td></td>
267 <td class="mw-input">' .
268 $this->filterLinks() .
269 '</td>
270 </tr>' .
271 Xml::closeElement( 'table' ) .
272 Xml::closeElement( 'fieldset' ) .
273 $hidden .
274 Xml::closeElement( 'form' );
275
276 $this->getOutput()->addHTML( $form );
277 }
278
279 protected function setSyndicated() {
280 $out = $this->getOutput();
281 $out->setSyndicated( true );
282 $out->setFeedAppendQuery( wfArrayToCGI( $this->opts->getAllValues() ) );
283 }
284
285 /**
286 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
287 *
288 * @param $result Result row
289 * @return String
290 */
291 public function formatRow( $result ) {
292 global $wgLang;
293
294 # Revision deletion works on revisions, so we should cast one
295 $row = array(
296 'comment' => $result->rc_comment,
297 'deleted' => $result->rc_deleted,
298 'user_text' => $result->rc_user_text,
299 'user' => $result->rc_user,
300 );
301 $rev = new Revision( $row );
302
303 $classes = array();
304
305 $dm = $wgLang->getDirMark();
306
307 $title = Title::makeTitleSafe( $result->rc_namespace, $result->rc_title );
308 $time = Html::element( 'span', array( 'class' => 'mw-newpages-time' ),
309 $wgLang->timeAndDate( $result->rc_timestamp, true )
310 );
311
312 $query = array( 'redirect' => 'no' );
313
314 if( $this->patrollable( $result ) ) {
315 $query['rcid'] = $result->rc_id;
316 }
317
318 $plink = Linker::linkKnown(
319 $title,
320 null,
321 array( 'class' => 'mw-newpages-pagename' ),
322 $query,
323 array( 'known' ) // Set explicitly to avoid the default of 'known','noclasses'. This breaks the colouration for stubs
324 );
325 $histLink = Linker::linkKnown(
326 $title,
327 wfMsgHtml( 'hist' ),
328 array(),
329 array( 'action' => 'history' )
330 );
331 $hist = Html::rawElement( 'span', array( 'class' => 'mw-newpages-history' ), wfMsg( 'parentheses', $histLink ) );
332
333 $length = Html::rawElement( 'span', array( 'class' => 'mw-newpages-length' ),
334 '[' . wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ), $wgLang->formatNum( $result->length ) ) .
335 ']'
336 );
337
338 $ulink = Linker::revUserTools( $rev );
339 $comment = Linker::revComment( $rev );
340
341 if ( $this->patrollable( $result ) ) {
342 $classes[] = 'not-patrolled';
343 }
344
345 # Add a class for zero byte pages
346 if ( $result->length == 0 ) {
347 $classes[] = 'mw-newpages-zero-byte-page';
348 }
349
350 # Tags, if any. check for including due to bug 23293
351 if ( !$this->including() ) {
352 list( $tagDisplay, $newClasses ) = ChangeTags::formatSummaryRow( $result->ts_tags, 'newpages' );
353 $classes = array_merge( $classes, $newClasses );
354 } else {
355 $tagDisplay = '';
356 }
357
358 $css = count( $classes ) ? ' class="' . implode( ' ', $classes ) . '"' : '';
359
360 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} {$dm}{$ulink} {$comment} {$tagDisplay}</li>\n";
361 }
362
363 /**
364 * Should a specific result row provide "patrollable" links?
365 *
366 * @param $result Result row
367 * @return Boolean
368 */
369 protected function patrollable( $result ) {
370 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled );
371 }
372
373 /**
374 * Output a subscription feed listing recent edits to this page.
375 *
376 * @param $type String
377 */
378 protected function feed( $type ) {
379 global $wgFeed, $wgFeedClasses, $wgFeedLimit;
380
381 if ( !$wgFeed ) {
382 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
383 return;
384 }
385
386 if( !isset( $wgFeedClasses[$type] ) ) {
387 $this->getOutput()->addWikiMsg( 'feed-invalid' );
388 return;
389 }
390
391 $feed = new $wgFeedClasses[$type](
392 $this->feedTitle(),
393 wfMsgExt( 'tagline', 'parsemag' ),
394 $this->getTitle()->getFullUrl()
395 );
396
397 $pager = new NewPagesPager( $this, $this->opts );
398 $limit = $this->opts->getValue( 'limit' );
399 $pager->mLimit = min( $limit, $wgFeedLimit );
400
401 $feed->outHeader();
402 if( $pager->getNumRows() > 0 ) {
403 foreach ( $pager->mResult as $row ) {
404 $feed->outItem( $this->feedItem( $row ) );
405 }
406 }
407 $feed->outFooter();
408 }
409
410 protected function feedTitle() {
411 global $wgLanguageCode, $wgSitename;
412 $desc = $this->getDescription();
413 return "$wgSitename - $desc [$wgLanguageCode]";
414 }
415
416 protected function feedItem( $row ) {
417 $title = Title::MakeTitle( intval( $row->rc_namespace ), $row->rc_title );
418 if( $title ) {
419 $date = $row->rc_timestamp;
420 $comments = $title->getTalkPage()->getFullURL();
421
422 return new FeedItem(
423 $title->getPrefixedText(),
424 $this->feedItemDesc( $row ),
425 $title->getFullURL(),
426 $date,
427 $this->feedItemAuthor( $row ),
428 $comments
429 );
430 } else {
431 return null;
432 }
433 }
434
435 protected function feedItemAuthor( $row ) {
436 return isset( $row->rc_user_text ) ? $row->rc_user_text : '';
437 }
438
439 protected function feedItemDesc( $row ) {
440 $revision = Revision::newFromId( $row->rev_id );
441 if( $revision ) {
442 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
443 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
444 "</p>\n<hr />\n<div>" .
445 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
446 }
447 return '';
448 }
449 }
450
451 /**
452 * @ingroup SpecialPage Pager
453 */
454 class NewPagesPager extends ReverseChronologicalPager {
455 // Stored opts
456 protected $opts;
457
458 /**
459 * @var HtmlForm
460 */
461 protected $mForm;
462
463 function __construct( $form, FormOptions $opts ) {
464 parent::__construct( $form->getContext() );
465 $this->mForm = $form;
466 $this->opts = $opts;
467 }
468
469 function getQueryInfo() {
470 global $wgEnableNewpagesUserFilter, $wgGroupPermissions;
471 $conds = array();
472 $conds['rc_new'] = 1;
473
474 $namespace = $this->opts->getValue( 'namespace' );
475 $namespace = ( $namespace === 'all' ) ? false : intval( $namespace );
476
477 $username = $this->opts->getValue( 'username' );
478 $user = Title::makeTitleSafe( NS_USER, $username );
479
480 if( $namespace !== false ) {
481 $conds['rc_namespace'] = $namespace;
482 $rcIndexes = array( 'new_name_timestamp' );
483 } else {
484 $rcIndexes = array( 'rc_timestamp' );
485 }
486
487 # $wgEnableNewpagesUserFilter - temp WMF hack
488 if( $wgEnableNewpagesUserFilter && $user ) {
489 $conds['rc_user_text'] = $user->getText();
490 $rcIndexes = 'rc_user_text';
491 # If anons cannot make new pages, don't "exclude logged in users"!
492 } elseif( $wgGroupPermissions['*']['createpage'] && $this->opts->getValue( 'hideliu' ) ) {
493 $conds['rc_user'] = 0;
494 }
495 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
496 if( $this->opts->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
497 $conds['rc_patrolled'] = 0;
498 }
499 if( $this->opts->getValue( 'hidebots' ) ) {
500 $conds['rc_bot'] = 0;
501 }
502
503 if ( $this->opts->getValue( 'hideredirs' ) ) {
504 $conds['page_is_redirect'] = 0;
505 }
506
507 // Allow changes to the New Pages query
508 $tables = array( 'recentchanges', 'page' );
509 $fields = array(
510 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
511 'rc_comment', 'rc_timestamp', 'rc_patrolled','rc_id', 'rc_deleted',
512 'page_len AS length', 'page_latest AS rev_id', 'ts_tags'
513 );
514 $join_conds = array( 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ) );
515
516 wfRunHooks( 'SpecialNewpagesConditions',
517 array( &$this, $this->opts, &$conds, &$tables, &$fields, &$join_conds ) );
518
519 $info = array(
520 'tables' => $tables,
521 'fields' => $fields,
522 'conds' => $conds,
523 'options' => array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) ),
524 'join_conds' => $join_conds
525 );
526
527 // Empty array for fields, it'll be set by us anyway.
528 $fields = array();
529
530 // Modify query for tags
531 ChangeTags::modifyDisplayQuery(
532 $info['tables'],
533 $fields,
534 $info['conds'],
535 $info['join_conds'],
536 $info['options'],
537 $this->opts['tagfilter']
538 );
539
540 return $info;
541 }
542
543 function getIndexField() {
544 return 'rc_timestamp';
545 }
546
547 function formatRow( $row ) {
548 return $this->mForm->formatRow( $row );
549 }
550
551 function getStartBody() {
552 # Do a batch existence check on pages
553 $linkBatch = new LinkBatch();
554 foreach ( $this->mResult as $row ) {
555 $linkBatch->add( NS_USER, $row->rc_user_text );
556 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
557 $linkBatch->add( $row->rc_namespace, $row->rc_title );
558 }
559 $linkBatch->execute();
560 return '<ul>';
561 }
562
563 function getEndBody() {
564 return '</ul>';
565 }
566 }