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