Merge "Replaced some !count() with explicit and more efficient check"
[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 // show/hide links
168 $showhide = array( $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() );
169
170 // Option value -> message mapping
171 $filters = array(
172 'hideliu' => 'rcshowhideliu',
173 'hidepatrolled' => 'rcshowhidepatr',
174 'hidebots' => 'rcshowhidebots',
175 'hideredirs' => 'whatlinkshere-hideredirs'
176 );
177 foreach ( $this->customFilters as $key => $params ) {
178 $filters[$key] = $params['msg'];
179 }
180
181 // Disable some if needed
182 if ( !User::groupHasPermission( '*', 'createpage' ) ) {
183 unset( $filters['hideliu'] );
184 }
185 if ( !$this->getUser()->useNPPatrol() ) {
186 unset( $filters['hidepatrolled'] );
187 }
188
189 $links = array();
190 $changed = $this->opts->getChangedValues();
191 unset( $changed['offset'] ); // Reset offset if query type changes
192
193 $self = $this->getTitle();
194 foreach ( $filters as $key => $msg ) {
195 $onoff = 1 - $this->opts->getValue( $key );
196 $link = Linker::link( $self, $showhide[$onoff], array(),
197 array( $key => $onoff ) + $changed
198 );
199 $links[$key] = $this->msg( $msg )->rawParams( $link )->escaped();
200 }
201
202 return $this->getLanguage()->pipeList( $links );
203 }
204
205 protected function form() {
206 global $wgEnableNewpagesUserFilter, $wgScript;
207
208 // Consume values
209 $this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW
210 $namespace = $this->opts->consumeValue( 'namespace' );
211 $username = $this->opts->consumeValue( 'username' );
212 $tagFilterVal = $this->opts->consumeValue( 'tagfilter' );
213
214 // Check username input validity
215 $ut = Title::makeTitleSafe( NS_USER, $username );
216 $userText = $ut ? $ut->getText() : '';
217
218 // Store query values in hidden fields so that form submission doesn't lose them
219 $hidden = array();
220 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
221 $hidden[] = Html::hidden( $key, $value );
222 }
223 $hidden = implode( "\n", $hidden );
224
225 $tagFilter = ChangeTags::buildTagFilterSelector( $tagFilterVal );
226 if ( $tagFilter ) {
227 list( $tagFilterLabel, $tagFilterSelector ) = $tagFilter;
228 }
229
230 $form = Xml::openElement( 'form', array( 'action' => $wgScript ) ) .
231 Html::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
232 Xml::fieldset( $this->msg( 'newpages' )->text() ) .
233 Xml::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
234 '<tr>
235 <td class="mw-label">' .
236 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
237 '</td>
238 <td class="mw-input">' .
239 Html::namespaceSelector(
240 array(
241 'selected' => $namespace,
242 'all' => 'all',
243 ), array(
244 'name' => 'namespace',
245 'id' => 'namespace',
246 'class' => 'namespaceselector',
247 )
248 ) .
249 '</td>
250 </tr>' . ( $tagFilter ? (
251 '<tr>
252 <td class="mw-label">' .
253 $tagFilterLabel .
254 '</td>
255 <td class="mw-input">' .
256 $tagFilterSelector .
257 '</td>
258 </tr>' ) : '' ) .
259 ( $wgEnableNewpagesUserFilter ?
260 '<tr>
261 <td class="mw-label">' .
262 Xml::label( $this->msg( 'newpages-username' )->text(), 'mw-np-username' ) .
263 '</td>
264 <td class="mw-input">' .
265 Xml::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
266 '</td>
267 </tr>' : '' ) .
268 '<tr> <td></td>
269 <td class="mw-submit">' .
270 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
271 '</td>
272 </tr>' .
273 '<tr>
274 <td></td>
275 <td class="mw-input">' .
276 $this->filterLinks() .
277 '</td>
278 </tr>' .
279 Xml::closeElement( 'table' ) .
280 Xml::closeElement( 'fieldset' ) .
281 $hidden .
282 Xml::closeElement( 'form' );
283
284 $this->getOutput()->addHTML( $form );
285 }
286
287 /**
288 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
289 *
290 * @param $result Result row
291 * @return String
292 */
293 public function formatRow( $result ) {
294 $title = Title::newFromRow( $result );
295
296 # Revision deletion works on revisions, so we should cast one
297 $row = array(
298 'comment' => $result->rc_comment,
299 'deleted' => $result->rc_deleted,
300 'user_text' => $result->rc_user_text,
301 'user' => $result->rc_user,
302 );
303 $rev = new Revision( $row );
304 $rev->setTitle( $title );
305
306 $classes = array();
307
308 $lang = $this->getLanguage();
309 $dm = $lang->getDirMark();
310
311 $spanTime = Html::element( 'span', array( 'class' => 'mw-newpages-time' ),
312 $lang->userTimeAndDate( $result->rc_timestamp, $this->getUser() )
313 );
314 $time = Linker::linkKnown(
315 $title,
316 $spanTime,
317 array(),
318 array( 'oldid' => $result->rc_this_oldid ),
319 array()
320 );
321
322 $query = array( 'redirect' => 'no' );
323
324 if( $this->patrollable( $result ) ) {
325 $query['rcid'] = $result->rc_id;
326 }
327
328 $plink = Linker::linkKnown(
329 $title,
330 null,
331 array( 'class' => 'mw-newpages-pagename' ),
332 $query,
333 array( 'known' ) // Set explicitly to avoid the default of 'known','noclasses'. This breaks the colouration for stubs
334 );
335 $histLink = Linker::linkKnown(
336 $title,
337 $this->msg( 'hist' )->escaped(),
338 array(),
339 array( 'action' => 'history' )
340 );
341 $hist = Html::rawElement( 'span', array( 'class' => 'mw-newpages-history' ),
342 $this->msg( 'parentheses' )->rawParams( $histLink )->escaped() );
343
344 $length = Html::element( 'span', array( 'class' => 'mw-newpages-length' ),
345 $this->msg( 'brackets' )->params( $this->msg( 'nbytes' )->numParams( $result->length )->text() )
346 );
347
348 $ulink = Linker::revUserTools( $rev );
349 $comment = Linker::revComment( $rev );
350
351 if ( $this->patrollable( $result ) ) {
352 $classes[] = 'not-patrolled';
353 }
354
355 # Add a class for zero byte pages
356 if ( $result->length == 0 ) {
357 $classes[] = 'mw-newpages-zero-byte-page';
358 }
359
360 # Tags, if any.
361 if( isset( $result->ts_tags ) ) {
362 list( $tagDisplay, $newClasses ) = ChangeTags::formatSummaryRow( $result->ts_tags, 'newpages' );
363 $classes = array_merge( $classes, $newClasses );
364 } else {
365 $tagDisplay = '';
366 }
367
368 $css = count( $classes ) ? ' class="' . implode( ' ', $classes ) . '"' : '';
369
370 # Display the old title if the namespace/title has been changed
371 $oldTitleText = '';
372 $oldTitle = Title::makeTitle( $result->rc_namespace, $result->rc_title );
373 if ( !$title->equals( $oldTitle ) ) {
374 $oldTitleText = $this->msg( 'rc-old-title' )->params( $oldTitle->getPrefixedText() )->escaped();
375 }
376
377 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} {$dm}{$ulink} {$comment} {$tagDisplay} {$oldTitleText}</li>\n";
378 }
379
380 /**
381 * Should a specific result row provide "patrollable" links?
382 *
383 * @param $result Result row
384 * @return Boolean
385 */
386 protected function patrollable( $result ) {
387 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled );
388 }
389
390 /**
391 * Output a subscription feed listing recent edits to this page.
392 *
393 * @param $type String
394 */
395 protected function feed( $type ) {
396 global $wgFeed, $wgFeedClasses, $wgFeedLimit;
397
398 if ( !$wgFeed ) {
399 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
400 return;
401 }
402
403 if( !isset( $wgFeedClasses[$type] ) ) {
404 $this->getOutput()->addWikiMsg( 'feed-invalid' );
405 return;
406 }
407
408 $feed = new $wgFeedClasses[$type](
409 $this->feedTitle(),
410 $this->msg( 'tagline' )->text(),
411 $this->getTitle()->getFullUrl()
412 );
413
414 $pager = new NewPagesPager( $this, $this->opts );
415 $limit = $this->opts->getValue( 'limit' );
416 $pager->mLimit = min( $limit, $wgFeedLimit );
417
418 $feed->outHeader();
419 if( $pager->getNumRows() > 0 ) {
420 foreach ( $pager->mResult as $row ) {
421 $feed->outItem( $this->feedItem( $row ) );
422 }
423 }
424 $feed->outFooter();
425 }
426
427 protected function feedTitle() {
428 global $wgLanguageCode, $wgSitename;
429 $desc = $this->getDescription();
430 return "$wgSitename - $desc [$wgLanguageCode]";
431 }
432
433 protected function feedItem( $row ) {
434 $title = Title::makeTitle( intval( $row->rc_namespace ), $row->rc_title );
435 if( $title ) {
436 $date = $row->rc_timestamp;
437 $comments = $title->getTalkPage()->getFullURL();
438
439 return new FeedItem(
440 $title->getPrefixedText(),
441 $this->feedItemDesc( $row ),
442 $title->getFullURL(),
443 $date,
444 $this->feedItemAuthor( $row ),
445 $comments
446 );
447 } else {
448 return null;
449 }
450 }
451
452 protected function feedItemAuthor( $row ) {
453 return isset( $row->rc_user_text ) ? $row->rc_user_text : '';
454 }
455
456 protected function feedItemDesc( $row ) {
457 $revision = Revision::newFromId( $row->rev_id );
458 if( $revision ) {
459 //XXX: include content model/type in feed item?
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->getContent()->serialize() ) ) . "</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;
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( User::groupHasPermission( '*', '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 'length' => 'page_len', 'rev_id' => 'page_latest', '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 // Modify query for tags
548 ChangeTags::modifyDisplayQuery(
549 $info['tables'],
550 $info['fields'],
551 $info['conds'],
552 $info['join_conds'],
553 $info['options'],
554 $this->opts['tagfilter']
555 );
556
557 return $info;
558 }
559
560 function getIndexField() {
561 return 'rc_timestamp';
562 }
563
564 function formatRow( $row ) {
565 return $this->mForm->formatRow( $row );
566 }
567
568 function getStartBody() {
569 # Do a batch existence check on pages
570 $linkBatch = new LinkBatch();
571 foreach ( $this->mResult as $row ) {
572 $linkBatch->add( NS_USER, $row->rc_user_text );
573 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
574 $linkBatch->add( $row->rc_namespace, $row->rc_title );
575 }
576 $linkBatch->execute();
577 return '<ul>';
578 }
579
580 function getEndBody() {
581 return '</ul>';
582 }
583 }