Merge "[FileBackend] Added getScopedLocksForOps() function."
[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 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
82 foreach ( $bits as $bit ) {
83 if ( 'shownav' == $bit ) {
84 $this->showNavigation = true;
85 }
86 if ( 'hideliu' === $bit ) {
87 $this->opts->setValue( 'hideliu', true );
88 }
89 if ( 'hidepatrolled' == $bit ) {
90 $this->opts->setValue( 'hidepatrolled', true );
91 }
92 if ( 'hidebots' == $bit ) {
93 $this->opts->setValue( 'hidebots', true );
94 }
95 if ( 'showredirs' == $bit ) {
96 $this->opts->setValue( 'hideredirs', false );
97 }
98 if ( is_numeric( $bit ) ) {
99 $this->opts->setValue( 'limit', intval( $bit ) );
100 }
101
102 $m = array();
103 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
104 $this->opts->setValue( 'limit', intval( $m[1] ) );
105 }
106 // PG offsets not just digits!
107 if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) ) {
108 $this->opts->setValue( 'offset', intval( $m[1] ) );
109 }
110 if ( preg_match( '/^username=(.*)$/', $bit, $m ) ) {
111 $this->opts->setValue( 'username', $m[1] );
112 }
113 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
114 $ns = $this->getLanguage()->getNsIndex( $m[1] );
115 if( $ns !== false ) {
116 $this->opts->setValue( 'namespace', $ns );
117 }
118 }
119 }
120 }
121
122 /**
123 * Show a form for filtering namespace and username
124 *
125 * @param $par String
126 * @return String
127 */
128 public function execute( $par ) {
129 $out = $this->getOutput();
130
131 $this->setHeaders();
132 $this->outputHeader();
133
134 $this->showNavigation = !$this->including(); // Maybe changed in setup
135 $this->setup( $par );
136
137 if( !$this->including() ) {
138 // Settings
139 $this->form();
140
141 $feedType = $this->opts->getValue( 'feed' );
142 if( $feedType ) {
143 return $this->feed( $feedType );
144 }
145
146 $allValues = $this->opts->getAllValues();
147 unset( $allValues['feed'] );
148 $out->setFeedAppendQuery( wfArrayToCGI( $allValues ) );
149 }
150
151 $pager = new NewPagesPager( $this, $this->opts );
152 $pager->mLimit = $this->opts->getValue( 'limit' );
153 $pager->mOffset = $this->opts->getValue( 'offset' );
154
155 if( $pager->getNumRows() ) {
156 $navigation = '';
157 if ( $this->showNavigation ) {
158 $navigation = $pager->getNavigationBar();
159 }
160 $out->addHTML( $navigation . $pager->getBody() . $navigation );
161 } else {
162 $out->addWikiMsg( 'specialpage-empty' );
163 }
164 }
165
166 protected function filterLinks() {
167 global $wgGroupPermissions;
168
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 # @todo FIXME: Throws E_NOTICEs if not set; and doesn't obey hooks etc.
185 if ( $wgGroupPermissions['*']['createpage'] !== true ) {
186 unset( $filters['hideliu'] );
187 }
188 if ( !$this->getUser()->useNPPatrol() ) {
189 unset( $filters['hidepatrolled'] );
190 }
191
192 $links = array();
193 $changed = $this->opts->getChangedValues();
194 unset( $changed['offset'] ); // Reset offset if query type changes
195
196 $self = $this->getTitle();
197 foreach ( $filters as $key => $msg ) {
198 $onoff = 1 - $this->opts->getValue( $key );
199 $link = Linker::link( $self, $showhide[$onoff], array(),
200 array( $key => $onoff ) + $changed
201 );
202 $links[$key] = $this->msg( $msg )->rawParams( $link )->escaped();
203 }
204
205 return $this->getLanguage()->pipeList( $links );
206 }
207
208 protected function form() {
209 global $wgEnableNewpagesUserFilter, $wgScript;
210
211 // Consume values
212 $this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW
213 $namespace = $this->opts->consumeValue( 'namespace' );
214 $username = $this->opts->consumeValue( 'username' );
215 $tagFilterVal = $this->opts->consumeValue( 'tagfilter' );
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 ) .
252 '</td>
253 </tr>' . ( $tagFilter ? (
254 '<tr>
255 <td class="mw-label">' .
256 $tagFilterLabel .
257 '</td>
258 <td class="mw-input">' .
259 $tagFilterSelector .
260 '</td>
261 </tr>' ) : '' ) .
262 ( $wgEnableNewpagesUserFilter ?
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, size, user links, and a comment
292 *
293 * @param $result Result row
294 * @return String
295 */
296 public function formatRow( $result ) {
297 # Revision deletion works on revisions, so we should cast one
298 $row = array(
299 'comment' => $result->rc_comment,
300 'deleted' => $result->rc_deleted,
301 'user_text' => $result->rc_user_text,
302 'user' => $result->rc_user,
303 );
304 $rev = new Revision( $row );
305
306 $classes = array();
307
308 $lang = $this->getLanguage();
309 $dm = $lang->getDirMark();
310
311 $title = Title::newFromRow( $result );
312 $spanTime = Html::element( 'span', array( 'class' => 'mw-newpages-time' ),
313 $lang->userTimeAndDate( $result->rc_timestamp, $this->getUser() )
314 );
315 $time = Linker::linkKnown(
316 $title,
317 $spanTime,
318 array(),
319 array( 'oldid' => $result->rc_this_oldid ),
320 array()
321 );
322
323 $query = array( 'redirect' => 'no' );
324
325 if( $this->patrollable( $result ) ) {
326 $query['rcid'] = $result->rc_id;
327 }
328
329 $plink = Linker::linkKnown(
330 $title,
331 null,
332 array( 'class' => 'mw-newpages-pagename' ),
333 $query,
334 array( 'known' ) // Set explicitly to avoid the default of 'known','noclasses'. This breaks the colouration for stubs
335 );
336 $histLink = Linker::linkKnown(
337 $title,
338 $this->msg( 'hist' )->escaped(),
339 array(),
340 array( 'action' => 'history' )
341 );
342 $hist = Html::rawElement( 'span', array( 'class' => 'mw-newpages-history' ),
343 $this->msg( 'parentheses' )->rawParams( $histLink )->escaped() );
344
345 $length = Html::element( 'span', array( 'class' => 'mw-newpages-length' ),
346 '[' . $this->msg( 'nbytes' )->numParams( $result->length )->text() . ']'
347 );
348
349 $ulink = Linker::revUserTools( $rev );
350 $comment = Linker::revComment( $rev );
351
352 if ( $this->patrollable( $result ) ) {
353 $classes[] = 'not-patrolled';
354 }
355
356 # Add a class for zero byte pages
357 if ( $result->length == 0 ) {
358 $classes[] = 'mw-newpages-zero-byte-page';
359 }
360
361 # Tags, if any. check for including due to bug 23293
362 if ( !$this->including() ) {
363 list( $tagDisplay, $newClasses ) = ChangeTags::formatSummaryRow( $result->ts_tags, 'newpages' );
364 $classes = array_merge( $classes, $newClasses );
365 } else {
366 $tagDisplay = '';
367 }
368
369 $css = count( $classes ) ? ' class="' . implode( ' ', $classes ) . '"' : '';
370
371 # Display the old title if the namespace/title has been changed
372 $oldTitleText = '';
373 $oldTitle = Title::makeTitle( $result->rc_namespace, $result->rc_title );
374 if ( !$title->equals( $oldTitle ) ) {
375 $oldTitleText = $this->msg( 'rc-old-title' )->params( $oldTitle->getPrefixedText() )->escaped();
376 }
377
378 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} {$dm}{$ulink} {$comment} {$tagDisplay} {$oldTitleText}</li>\n";
379 }
380
381 /**
382 * Should a specific result row provide "patrollable" links?
383 *
384 * @param $result Result row
385 * @return Boolean
386 */
387 protected function patrollable( $result ) {
388 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled );
389 }
390
391 /**
392 * Output a subscription feed listing recent edits to this page.
393 *
394 * @param $type String
395 */
396 protected function feed( $type ) {
397 global $wgFeed, $wgFeedClasses, $wgFeedLimit;
398
399 if ( !$wgFeed ) {
400 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
401 return;
402 }
403
404 if( !isset( $wgFeedClasses[$type] ) ) {
405 $this->getOutput()->addWikiMsg( 'feed-invalid' );
406 return;
407 }
408
409 $feed = new $wgFeedClasses[$type](
410 $this->feedTitle(),
411 $this->msg( 'tagline' )->text(),
412 $this->getTitle()->getFullUrl()
413 );
414
415 $pager = new NewPagesPager( $this, $this->opts );
416 $limit = $this->opts->getValue( 'limit' );
417 $pager->mLimit = min( $limit, $wgFeedLimit );
418
419 $feed->outHeader();
420 if( $pager->getNumRows() > 0 ) {
421 foreach ( $pager->mResult as $row ) {
422 $feed->outItem( $this->feedItem( $row ) );
423 }
424 }
425 $feed->outFooter();
426 }
427
428 protected function feedTitle() {
429 global $wgLanguageCode, $wgSitename;
430 $desc = $this->getDescription();
431 return "$wgSitename - $desc [$wgLanguageCode]";
432 }
433
434 protected function feedItem( $row ) {
435 $title = Title::makeTitle( intval( $row->rc_namespace ), $row->rc_title );
436 if( $title ) {
437 $date = $row->rc_timestamp;
438 $comments = $title->getTalkPage()->getFullURL();
439
440 return new FeedItem(
441 $title->getPrefixedText(),
442 $this->feedItemDesc( $row ),
443 $title->getFullURL(),
444 $date,
445 $this->feedItemAuthor( $row ),
446 $comments
447 );
448 } else {
449 return null;
450 }
451 }
452
453 protected function feedItemAuthor( $row ) {
454 return isset( $row->rc_user_text ) ? $row->rc_user_text : '';
455 }
456
457 protected function feedItemDesc( $row ) {
458 $revision = Revision::newFromId( $row->rev_id );
459 if( $revision ) {
460 return '<p>' . htmlspecialchars( $revision->getUserText() ) .
461 $this->msg( 'colon-separator' )->inContentLanguage()->escaped() .
462 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
463 "</p>\n<hr />\n<div>" .
464 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
465 }
466 return '';
467 }
468 }
469
470 /**
471 * @ingroup SpecialPage Pager
472 */
473 class NewPagesPager extends ReverseChronologicalPager {
474 // Stored opts
475 protected $opts;
476
477 /**
478 * @var HtmlForm
479 */
480 protected $mForm;
481
482 function __construct( $form, FormOptions $opts ) {
483 parent::__construct( $form->getContext() );
484 $this->mForm = $form;
485 $this->opts = $opts;
486 }
487
488 function getQueryInfo() {
489 global $wgEnableNewpagesUserFilter, $wgGroupPermissions;
490 $conds = array();
491 $conds['rc_new'] = 1;
492
493 $namespace = $this->opts->getValue( 'namespace' );
494 $namespace = ( $namespace === 'all' ) ? false : intval( $namespace );
495
496 $username = $this->opts->getValue( 'username' );
497 $user = Title::makeTitleSafe( NS_USER, $username );
498
499 if( $namespace !== false ) {
500 $conds['rc_namespace'] = $namespace;
501 $rcIndexes = array( 'new_name_timestamp' );
502 } else {
503 $rcIndexes = array( 'rc_timestamp' );
504 }
505
506 # $wgEnableNewpagesUserFilter - temp WMF hack
507 if( $wgEnableNewpagesUserFilter && $user ) {
508 $conds['rc_user_text'] = $user->getText();
509 $rcIndexes = 'rc_user_text';
510 # If anons cannot make new pages, don't "exclude logged in users"!
511 } elseif( $wgGroupPermissions['*']['createpage'] && $this->opts->getValue( 'hideliu' ) ) {
512 $conds['rc_user'] = 0;
513 }
514 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
515 if( $this->opts->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
516 $conds['rc_patrolled'] = 0;
517 }
518 if( $this->opts->getValue( 'hidebots' ) ) {
519 $conds['rc_bot'] = 0;
520 }
521
522 if ( $this->opts->getValue( 'hideredirs' ) ) {
523 $conds['page_is_redirect'] = 0;
524 }
525
526 // Allow changes to the New Pages query
527 $tables = array( 'recentchanges', 'page' );
528 $fields = array(
529 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
530 'rc_comment', 'rc_timestamp', 'rc_patrolled','rc_id', 'rc_deleted',
531 'page_len AS length', 'page_latest AS rev_id', 'ts_tags', 'rc_this_oldid',
532 'page_namespace', 'page_title'
533 );
534 $join_conds = array( 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ) );
535
536 wfRunHooks( 'SpecialNewpagesConditions',
537 array( &$this, $this->opts, &$conds, &$tables, &$fields, &$join_conds ) );
538
539 $info = array(
540 'tables' => $tables,
541 'fields' => $fields,
542 'conds' => $conds,
543 'options' => array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) ),
544 'join_conds' => $join_conds
545 );
546
547 // Empty array for fields, it'll be set by us anyway.
548 $fields = array();
549
550 // Modify query for tags
551 ChangeTags::modifyDisplayQuery(
552 $info['tables'],
553 $fields,
554 $info['conds'],
555 $info['join_conds'],
556 $info['options'],
557 $this->opts['tagfilter']
558 );
559
560 return $info;
561 }
562
563 function getIndexField() {
564 return 'rc_timestamp';
565 }
566
567 function formatRow( $row ) {
568 return $this->mForm->formatRow( $row );
569 }
570
571 function getStartBody() {
572 # Do a batch existence check on pages
573 $linkBatch = new LinkBatch();
574 foreach ( $this->mResult as $row ) {
575 $linkBatch->add( NS_USER, $row->rc_user_text );
576 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
577 $linkBatch->add( $row->rc_namespace, $row->rc_title );
578 }
579 $linkBatch->execute();
580 return '<ul>';
581 }
582
583 function getEndBody() {
584 return '</ul>';
585 }
586 }