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