Add SpecialPage::getGroupName and use it
[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', $this->getUser()->getIntOption( 'rclimit' ) );
57 $opts->add( 'offset', '' );
58 $opts->add( 'namespace', '0' );
59 $opts->add( 'username', '' );
60 $opts->add( 'feed', '' );
61 $opts->add( 'tagfilter', '' );
62 $opts->add( 'invert', false );
63
64 $this->customFilters = array();
65 wfRunHooks( 'SpecialNewPagesFilters', array( $this, &$this->customFilters ) );
66 foreach( $this->customFilters as $key => $params ) {
67 $opts->add( $key, $params['default'] );
68 }
69
70 // Set values
71 $opts->fetchValuesFromRequest( $this->getRequest() );
72 if ( $par ) $this->parseParams( $par );
73
74 // Validate
75 $opts->validateIntBounds( 'limit', 0, 5000 );
76 if( !$wgEnableNewpagesUserFilter ) {
77 $opts->setValue( 'username', '' );
78 }
79 }
80
81 protected function parseParams( $par ) {
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 = $this->getLanguage()->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 $feedType = $this->opts->getValue( 'feed' );
143 if( $feedType ) {
144 $this->feed( $feedType );
145 return;
146 }
147
148 $allValues = $this->opts->getAllValues();
149 unset( $allValues['feed'] );
150 $out->setFeedAppendQuery( wfArrayToCgi( $allValues ) );
151 }
152
153 $pager = new NewPagesPager( $this, $this->opts );
154 $pager->mLimit = $this->opts->getValue( 'limit' );
155 $pager->mOffset = $this->opts->getValue( 'offset' );
156
157 if( $pager->getNumRows() ) {
158 $navigation = '';
159 if ( $this->showNavigation ) {
160 $navigation = $pager->getNavigationBar();
161 }
162 $out->addHTML( $navigation . $pager->getBody() . $navigation );
163 } else {
164 $out->addWikiMsg( 'specialpage-empty' );
165 }
166 }
167
168 protected function filterLinks() {
169 // show/hide links
170 $showhide = array( $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() );
171
172 // Option value -> message mapping
173 $filters = array(
174 'hideliu' => 'rcshowhideliu',
175 'hidepatrolled' => 'rcshowhidepatr',
176 'hidebots' => 'rcshowhidebots',
177 'hideredirs' => 'whatlinkshere-hideredirs'
178 );
179 foreach ( $this->customFilters as $key => $params ) {
180 $filters[$key] = $params['msg'];
181 }
182
183 // Disable some if needed
184 if ( !User::groupHasPermission( '*', 'createpage' ) ) {
185 unset( $filters['hideliu'] );
186 }
187 if ( !$this->getUser()->useNPPatrol() ) {
188 unset( $filters['hidepatrolled'] );
189 }
190
191 $links = array();
192 $changed = $this->opts->getChangedValues();
193 unset( $changed['offset'] ); // Reset offset if query type changes
194
195 $self = $this->getTitle();
196 foreach ( $filters as $key => $msg ) {
197 $onoff = 1 - $this->opts->getValue( $key );
198 $link = Linker::link( $self, $showhide[$onoff], array(),
199 array( $key => $onoff ) + $changed
200 );
201 $links[$key] = $this->msg( $msg )->rawParams( $link )->escaped();
202 }
203
204 return $this->getLanguage()->pipeList( $links );
205 }
206
207 protected function form() {
208 global $wgEnableNewpagesUserFilter, $wgScript;
209
210 // Consume values
211 $this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW
212 $namespace = $this->opts->consumeValue( 'namespace' );
213 $username = $this->opts->consumeValue( 'username' );
214 $tagFilterVal = $this->opts->consumeValue( 'tagfilter' );
215 $nsinvert = $this->opts->consumeValue( 'invert' );
216
217 // Check username input validity
218 $ut = Title::makeTitleSafe( NS_USER, $username );
219 $userText = $ut ? $ut->getText() : '';
220
221 // Store query values in hidden fields so that form submission doesn't lose them
222 $hidden = array();
223 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
224 $hidden[] = Html::hidden( $key, $value );
225 }
226 $hidden = implode( "\n", $hidden );
227
228 $tagFilter = ChangeTags::buildTagFilterSelector( $tagFilterVal );
229 if ( $tagFilter ) {
230 list( $tagFilterLabel, $tagFilterSelector ) = $tagFilter;
231 }
232
233 $form = Xml::openElement( 'form', array( 'action' => $wgScript ) ) .
234 Html::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
235 Xml::fieldset( $this->msg( 'newpages' )->text() ) .
236 Xml::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
237 '<tr>
238 <td class="mw-label">' .
239 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
240 '</td>
241 <td class="mw-input">' .
242 Html::namespaceSelector(
243 array(
244 'selected' => $namespace,
245 'all' => 'all',
246 ), array(
247 'name' => 'namespace',
248 'id' => 'namespace',
249 'class' => 'namespaceselector',
250 )
251 ) . '&#160;' .
252 Xml::checkLabel(
253 $this->msg( 'invert' )->text(),
254 'invert',
255 'nsinvert',
256 $nsinvert,
257 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
258 ) .
259 '</td>
260 </tr>' . ( $tagFilter ? (
261 '<tr>
262 <td class="mw-label">' .
263 $tagFilterLabel .
264 '</td>
265 <td class="mw-input">' .
266 $tagFilterSelector .
267 '</td>
268 </tr>' ) : '' ) .
269 ( $wgEnableNewpagesUserFilter ?
270 '<tr>
271 <td class="mw-label">' .
272 Xml::label( $this->msg( 'newpages-username' )->text(), 'mw-np-username' ) .
273 '</td>
274 <td class="mw-input">' .
275 Xml::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
276 '</td>
277 </tr>' : '' ) .
278 '<tr> <td></td>
279 <td class="mw-submit">' .
280 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
281 '</td>
282 </tr>' .
283 '<tr>
284 <td></td>
285 <td class="mw-input">' .
286 $this->filterLinks() .
287 '</td>
288 </tr>' .
289 Xml::closeElement( 'table' ) .
290 Xml::closeElement( 'fieldset' ) .
291 $hidden .
292 Xml::closeElement( 'form' );
293
294 $this->getOutput()->addHTML( $form );
295 }
296
297 /**
298 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
299 *
300 * @param $result Result row
301 * @return String
302 */
303 public function formatRow( $result ) {
304 $title = Title::newFromRow( $result );
305
306 # Revision deletion works on revisions, so we should cast one
307 $row = array(
308 'comment' => $result->rc_comment,
309 'deleted' => $result->rc_deleted,
310 'user_text' => $result->rc_user_text,
311 'user' => $result->rc_user,
312 );
313 $rev = new Revision( $row );
314 $rev->setTitle( $title );
315
316 $classes = array();
317
318 $lang = $this->getLanguage();
319 $dm = $lang->getDirMark();
320
321 $spanTime = Html::element( 'span', array( 'class' => 'mw-newpages-time' ),
322 $lang->userTimeAndDate( $result->rc_timestamp, $this->getUser() )
323 );
324 $time = Linker::linkKnown(
325 $title,
326 $spanTime,
327 array(),
328 array( 'oldid' => $result->rc_this_oldid ),
329 array()
330 );
331
332 $query = array( 'redirect' => 'no' );
333
334 if( $this->patrollable( $result ) ) {
335 $query['rcid'] = $result->rc_id;
336 }
337
338 // Linker::linkKnown() uses 'known' and 'noclasses' options. This breaks the colouration for stubs.
339 $plink = Linker::link(
340 $title,
341 null,
342 array( 'class' => 'mw-newpages-pagename' ),
343 $query,
344 array( 'known' )
345 );
346 $histLink = Linker::linkKnown(
347 $title,
348 $this->msg( 'hist' )->escaped(),
349 array(),
350 array( 'action' => 'history' )
351 );
352 $hist = Html::rawElement( 'span', array( 'class' => 'mw-newpages-history' ),
353 $this->msg( 'parentheses' )->rawParams( $histLink )->escaped() );
354
355 $length = Html::element( 'span', array( 'class' => 'mw-newpages-length' ),
356 $this->msg( 'brackets' )->params( $this->msg( 'nbytes' )->numParams( $result->length )->text() )
357 );
358
359 $ulink = Linker::revUserTools( $rev );
360 $comment = Linker::revComment( $rev );
361
362 if ( $this->patrollable( $result ) ) {
363 $classes[] = 'not-patrolled';
364 }
365
366 # Add a class for zero byte pages
367 if ( $result->length == 0 ) {
368 $classes[] = 'mw-newpages-zero-byte-page';
369 }
370
371 # Tags, if any.
372 if( isset( $result->ts_tags ) ) {
373 list( $tagDisplay, $newClasses ) = ChangeTags::formatSummaryRow( $result->ts_tags, 'newpages' );
374 $classes = array_merge( $classes, $newClasses );
375 } else {
376 $tagDisplay = '';
377 }
378
379 $css = count( $classes ) ? ' class="' . implode( ' ', $classes ) . '"' : '';
380
381 # Display the old title if the namespace/title has been changed
382 $oldTitleText = '';
383 $oldTitle = Title::makeTitle( $result->rc_namespace, $result->rc_title );
384 if ( !$title->equals( $oldTitle ) ) {
385 $oldTitleText = $this->msg( 'rc-old-title' )->params( $oldTitle->getPrefixedText() )->escaped();
386 }
387
388 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} {$dm}{$ulink} {$comment} {$tagDisplay} {$oldTitleText}</li>\n";
389 }
390
391 /**
392 * Should a specific result row provide "patrollable" links?
393 *
394 * @param $result Result row
395 * @return Boolean
396 */
397 protected function patrollable( $result ) {
398 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled );
399 }
400
401 /**
402 * Output a subscription feed listing recent edits to this page.
403 *
404 * @param $type String
405 */
406 protected function feed( $type ) {
407 global $wgFeed, $wgFeedClasses, $wgFeedLimit;
408
409 if ( !$wgFeed ) {
410 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
411 return;
412 }
413
414 if( !isset( $wgFeedClasses[$type] ) ) {
415 $this->getOutput()->addWikiMsg( 'feed-invalid' );
416 return;
417 }
418
419 $feed = new $wgFeedClasses[$type](
420 $this->feedTitle(),
421 $this->msg( 'tagline' )->text(),
422 $this->getTitle()->getFullUrl()
423 );
424
425 $pager = new NewPagesPager( $this, $this->opts );
426 $limit = $this->opts->getValue( 'limit' );
427 $pager->mLimit = min( $limit, $wgFeedLimit );
428
429 $feed->outHeader();
430 if( $pager->getNumRows() > 0 ) {
431 foreach ( $pager->mResult as $row ) {
432 $feed->outItem( $this->feedItem( $row ) );
433 }
434 }
435 $feed->outFooter();
436 }
437
438 protected function feedTitle() {
439 global $wgLanguageCode, $wgSitename;
440 $desc = $this->getDescription();
441 return "$wgSitename - $desc [$wgLanguageCode]";
442 }
443
444 protected function feedItem( $row ) {
445 $title = Title::makeTitle( intval( $row->rc_namespace ), $row->rc_title );
446 if( $title ) {
447 $date = $row->rc_timestamp;
448 $comments = $title->getTalkPage()->getFullURL();
449
450 return new FeedItem(
451 $title->getPrefixedText(),
452 $this->feedItemDesc( $row ),
453 $title->getFullURL(),
454 $date,
455 $this->feedItemAuthor( $row ),
456 $comments
457 );
458 } else {
459 return null;
460 }
461 }
462
463 protected function feedItemAuthor( $row ) {
464 return isset( $row->rc_user_text ) ? $row->rc_user_text : '';
465 }
466
467 protected function feedItemDesc( $row ) {
468 $revision = Revision::newFromId( $row->rev_id );
469 if( $revision ) {
470 //XXX: include content model/type in feed item?
471 return '<p>' . htmlspecialchars( $revision->getUserText() ) .
472 $this->msg( 'colon-separator' )->inContentLanguage()->escaped() .
473 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
474 "</p>\n<hr />\n<div>" .
475 nl2br( htmlspecialchars( $revision->getContent()->serialize() ) ) . "</div>";
476 }
477 return '';
478 }
479
480 protected function getGroupName() {
481 return 'changes';
482 }
483 }
484
485 /**
486 * @ingroup SpecialPage Pager
487 */
488 class NewPagesPager extends ReverseChronologicalPager {
489 // Stored opts
490 protected $opts;
491
492 /**
493 * @var HtmlForm
494 */
495 protected $mForm;
496
497 function __construct( $form, FormOptions $opts ) {
498 parent::__construct( $form->getContext() );
499 $this->mForm = $form;
500 $this->opts = $opts;
501 }
502
503 function getQueryInfo() {
504 global $wgEnableNewpagesUserFilter;
505 $conds = array();
506 $conds['rc_new'] = 1;
507
508 $namespace = $this->opts->getValue( 'namespace' );
509 $namespace = ( $namespace === 'all' ) ? false : intval( $namespace );
510
511 $username = $this->opts->getValue( 'username' );
512 $user = Title::makeTitleSafe( NS_USER, $username );
513
514 if( $namespace !== false ) {
515 if ( $this->opts->getValue( 'invert' ) ) {
516 $conds[] = 'rc_namespace != ' . $this->mDb->addQuotes( $namespace );
517 } else {
518 $conds['rc_namespace'] = $namespace;
519 }
520 $rcIndexes = array( 'new_name_timestamp' );
521 } else {
522 $rcIndexes = array( 'rc_timestamp' );
523 }
524
525 # $wgEnableNewpagesUserFilter - temp WMF hack
526 if( $wgEnableNewpagesUserFilter && $user ) {
527 $conds['rc_user_text'] = $user->getText();
528 $rcIndexes = 'rc_user_text';
529 # If anons cannot make new pages, don't "exclude logged in users"!
530 } elseif( User::groupHasPermission( '*', 'createpage' ) && $this->opts->getValue( 'hideliu' ) ) {
531 $conds['rc_user'] = 0;
532 }
533 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
534 if( $this->opts->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
535 $conds['rc_patrolled'] = 0;
536 }
537 if( $this->opts->getValue( 'hidebots' ) ) {
538 $conds['rc_bot'] = 0;
539 }
540
541 if ( $this->opts->getValue( 'hideredirs' ) ) {
542 $conds['page_is_redirect'] = 0;
543 }
544
545 // Allow changes to the New Pages query
546 $tables = array( 'recentchanges', 'page' );
547 $fields = array(
548 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
549 'rc_comment', 'rc_timestamp', 'rc_patrolled','rc_id', 'rc_deleted',
550 'length' => 'page_len', 'rev_id' => 'page_latest', 'rc_this_oldid',
551 'page_namespace', 'page_title'
552 );
553 $join_conds = array( 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ) );
554
555 wfRunHooks( 'SpecialNewpagesConditions',
556 array( &$this, $this->opts, &$conds, &$tables, &$fields, &$join_conds ) );
557
558 $info = array(
559 'tables' => $tables,
560 'fields' => $fields,
561 'conds' => $conds,
562 'options' => array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) ),
563 'join_conds' => $join_conds
564 );
565
566 // Modify query for tags
567 ChangeTags::modifyDisplayQuery(
568 $info['tables'],
569 $info['fields'],
570 $info['conds'],
571 $info['join_conds'],
572 $info['options'],
573 $this->opts['tagfilter']
574 );
575
576 return $info;
577 }
578
579 function getIndexField() {
580 return 'rc_timestamp';
581 }
582
583 function formatRow( $row ) {
584 return $this->mForm->formatRow( $row );
585 }
586
587 function getStartBody() {
588 # Do a batch existence check on pages
589 $linkBatch = new LinkBatch();
590 foreach ( $this->mResult as $row ) {
591 $linkBatch->add( NS_USER, $row->rc_user_text );
592 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
593 $linkBatch->add( $row->rc_namespace, $row->rc_title );
594 }
595 $linkBatch->execute();
596 return '<ul>';
597 }
598
599 function getEndBody() {
600 return '</ul>';
601 }
602 }