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