Merge "Fix the (un)watch token to include the namespace name."
[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 // Consume values
203 $this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW
204 $namespace = $this->opts->consumeValue( 'namespace' );
205 $username = $this->opts->consumeValue( 'username' );
206 $tagFilterVal = $this->opts->consumeValue( 'tagfilter' );
207 $nsinvert = $this->opts->consumeValue( 'invert' );
208
209 // Check username input validity
210 $ut = Title::makeTitleSafe( NS_USER, $username );
211 $userText = $ut ? $ut->getText() : '';
212
213 // Store query values in hidden fields so that form submission doesn't lose them
214 $hidden = array();
215 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
216 $hidden[] = Html::hidden( $key, $value );
217 }
218 $hidden = implode( "\n", $hidden );
219
220 $tagFilter = ChangeTags::buildTagFilterSelector( $tagFilterVal );
221 if ( $tagFilter ) {
222 list( $tagFilterLabel, $tagFilterSelector ) = $tagFilter;
223 }
224
225 $form = Xml::openElement( 'form', array( 'action' => wfScript() ) ) .
226 Html::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
227 Xml::fieldset( $this->msg( 'newpages' )->text() ) .
228 Xml::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
229 '<tr>
230 <td class="mw-label">' .
231 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
232 '</td>
233 <td class="mw-input">' .
234 Html::namespaceSelector(
235 array(
236 'selected' => $namespace,
237 'all' => 'all',
238 ), array(
239 'name' => 'namespace',
240 'id' => 'namespace',
241 'class' => 'namespaceselector',
242 )
243 ) . '&#160;' .
244 Xml::checkLabel(
245 $this->msg( 'invert' )->text(),
246 'invert',
247 'nsinvert',
248 $nsinvert,
249 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
250 ) .
251 '</td>
252 </tr>' . ( $tagFilter ? (
253 '<tr>
254 <td class="mw-label">' .
255 $tagFilterLabel .
256 '</td>
257 <td class="mw-input">' .
258 $tagFilterSelector .
259 '</td>
260 </tr>' ) : '' ) .
261 '<tr>
262 <td class="mw-label">' .
263 Xml::label( $this->msg( 'newpages-username' )->text(), 'mw-np-username' ) .
264 '</td>
265 <td class="mw-input">' .
266 Xml::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
267 '</td>
268 </tr>' .
269 '<tr> <td></td>
270 <td class="mw-submit">' .
271 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
272 '</td>
273 </tr>' .
274 '<tr>
275 <td></td>
276 <td class="mw-input">' .
277 $this->filterLinks() .
278 '</td>
279 </tr>' .
280 Xml::closeElement( 'table' ) .
281 Xml::closeElement( 'fieldset' ) .
282 $hidden .
283 Xml::closeElement( 'form' );
284
285 $this->getOutput()->addHTML( $form );
286 }
287
288 /**
289 * Format a row, providing the timestamp, links to the page/history,
290 * size, user links, and a comment
291 *
292 * @param object $result Result row
293 * @return string
294 */
295 public function formatRow( $result ) {
296 $title = Title::newFromRow( $result );
297
298 # Revision deletion works on revisions, so we should cast one
299 $row = array(
300 'comment' => $result->rc_comment,
301 'deleted' => $result->rc_deleted,
302 'user_text' => $result->rc_user_text,
303 'user' => $result->rc_user,
304 );
305 $rev = new Revision( $row );
306 $rev->setTitle( $title );
307
308 $classes = array();
309
310 $lang = $this->getLanguage();
311 $dm = $lang->getDirMark();
312
313 $spanTime = Html::element( 'span', array( 'class' => 'mw-newpages-time' ),
314 $lang->userTimeAndDate( $result->rc_timestamp, $this->getUser() )
315 );
316 $time = Linker::linkKnown(
317 $title,
318 $spanTime,
319 array(),
320 array( 'oldid' => $result->rc_this_oldid ),
321 array()
322 );
323
324 $query = array( 'redirect' => 'no' );
325
326 // Linker::linkKnown() uses 'known' and 'noclasses' options.
327 // This breaks the colouration for stubs.
328 $plink = Linker::link(
329 $title,
330 null,
331 array( 'class' => 'mw-newpages-pagename' ),
332 $query,
333 array( 'known' )
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(
345 'span',
346 array( 'class' => 'mw-newpages-length' ),
347 $this->msg( 'brackets' )->params( $this->msg( 'nbytes' )
348 ->numParams( $result->length )->text()
349 )
350 );
351
352 $ulink = Linker::revUserTools( $rev );
353 $comment = Linker::revComment( $rev );
354
355 if ( $this->patrollable( $result ) ) {
356 $classes[] = 'not-patrolled';
357 }
358
359 # Add a class for zero byte pages
360 if ( $result->length == 0 ) {
361 $classes[] = 'mw-newpages-zero-byte-page';
362 }
363
364 # Tags, if any.
365 if ( isset( $result->ts_tags ) ) {
366 list( $tagDisplay, $newClasses ) = ChangeTags::formatSummaryRow(
367 $result->ts_tags,
368 'newpages'
369 );
370 $classes = array_merge( $classes, $newClasses );
371 } else {
372 $tagDisplay = '';
373 }
374
375 $css = count( $classes ) ? ' class="' . implode( ' ', $classes ) . '"' : '';
376
377 # Display the old title if the namespace/title has been changed
378 $oldTitleText = '';
379 $oldTitle = Title::makeTitle( $result->rc_namespace, $result->rc_title );
380
381 if ( !$title->equals( $oldTitle ) ) {
382 $oldTitleText = $oldTitle->getPrefixedText();
383 $oldTitleText = $this->msg( 'rc-old-title' )->params( $oldTitleText )->escaped();
384 }
385
386 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} "
387 . "{$dm}{$ulink} {$comment} {$tagDisplay} {$oldTitleText}</li>\n";
388 }
389
390 /**
391 * Should a specific result row provide "patrollable" links?
392 *
393 * @param object $result Result row
394 * @return bool
395 */
396 protected function patrollable( $result ) {
397 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled );
398 }
399
400 /**
401 * Output a subscription feed listing recent edits to this page.
402 *
403 * @param string $type
404 */
405 protected function feed( $type ) {
406 if ( !$this->getConfig()->get( 'Feed' ) ) {
407 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
408
409 return;
410 }
411
412 $feedClasses = $this->getConfig()->get( 'FeedClasses' );
413 if ( !isset( $feedClasses[$type] ) ) {
414 $this->getOutput()->addWikiMsg( 'feed-invalid' );
415
416 return;
417 }
418
419 $feed = new $feedClasses[$type](
420 $this->feedTitle(),
421 $this->msg( 'tagline' )->text(),
422 $this->getPageTitle()->getFullURL()
423 );
424
425 $pager = new NewPagesPager( $this, $this->opts );
426 $limit = $this->opts->getValue( 'limit' );
427 $pager->mLimit = min( $limit, $this->getConfig()->get( 'FeedLimit' ) );
428
429 $feed->outHeader();
430 if ( $pager->getNumRows() > 0 ) {
431 foreach ( $pager->mResult as $row ) {
432 $feed->outItem( $this->feedItem( $row ) );
433 }
434 }
435 $feed->outFooter();
436 }
437
438 protected function feedTitle() {
439 $desc = $this->getDescription();
440 $code = $this->getConfig()->get( 'LanguageCode' );
441 $sitename = $this->getConfig()->get( 'Sitename' );
442
443 return "$sitename - $desc [$code]";
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
480 return '';
481 }
482
483 protected function getGroupName() {
484 return 'changes';
485 }
486 }
487
488 /**
489 * @ingroup SpecialPage Pager
490 */
491 class NewPagesPager extends ReverseChronologicalPager {
492 // Stored opts
493 protected $opts;
494
495 /**
496 * @var HtmlForm
497 */
498 protected $mForm;
499
500 function __construct( $form, FormOptions $opts ) {
501 parent::__construct( $form->getContext() );
502 $this->mForm = $form;
503 $this->opts = $opts;
504 }
505
506 function getQueryInfo() {
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 $rcIndexes = array();
517
518 if ( $namespace !== false ) {
519 if ( $this->opts->getValue( 'invert' ) ) {
520 $conds[] = 'rc_namespace != ' . $this->mDb->addQuotes( $namespace );
521 } else {
522 $conds['rc_namespace'] = $namespace;
523 }
524 }
525
526 if ( $user ) {
527 $conds['rc_user_text'] = $user->getText();
528 $rcIndexes = 'rc_user_text';
529 } elseif ( User::groupHasPermission( '*', 'createpage' ) &&
530 $this->opts->getValue( 'hideliu' )
531 ) {
532 # If anons cannot make new pages, don't "exclude logged in users"!
533 $conds['rc_user'] = 0;
534 }
535
536 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
537 if ( $this->opts->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
538 $conds['rc_patrolled'] = 0;
539 }
540
541 if ( $this->opts->getValue( 'hidebots' ) ) {
542 $conds['rc_bot'] = 0;
543 }
544
545 if ( $this->opts->getValue( 'hideredirs' ) ) {
546 $conds['page_is_redirect'] = 0;
547 }
548
549 // Allow changes to the New Pages query
550 $tables = array( 'recentchanges', 'page' );
551 $fields = array(
552 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
553 'rc_comment', 'rc_timestamp', 'rc_patrolled', 'rc_id', 'rc_deleted',
554 'length' => 'page_len', 'rev_id' => 'page_latest', 'rc_this_oldid',
555 'page_namespace', 'page_title'
556 );
557 $join_conds = array( 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ) );
558
559 wfRunHooks( 'SpecialNewpagesConditions',
560 array( &$this, $this->opts, &$conds, &$tables, &$fields, &$join_conds ) );
561
562 $options = array();
563
564 if ( $rcIndexes ) {
565 $options = array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) );
566 }
567
568 $info = array(
569 'tables' => $tables,
570 'fields' => $fields,
571 'conds' => $conds,
572 'options' => $options,
573 'join_conds' => $join_conds
574 );
575
576 // Modify query for tags
577 ChangeTags::modifyDisplayQuery(
578 $info['tables'],
579 $info['fields'],
580 $info['conds'],
581 $info['join_conds'],
582 $info['options'],
583 $this->opts['tagfilter']
584 );
585
586 return $info;
587 }
588
589 function getIndexField() {
590 return 'rc_timestamp';
591 }
592
593 function formatRow( $row ) {
594 return $this->mForm->formatRow( $row );
595 }
596
597 function getStartBody() {
598 # Do a batch existence check on pages
599 $linkBatch = new LinkBatch();
600 foreach ( $this->mResult as $row ) {
601 $linkBatch->add( NS_USER, $row->rc_user_text );
602 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
603 $linkBatch->add( $row->rc_namespace, $row->rc_title );
604 }
605 $linkBatch->execute();
606
607 return '<ul>';
608 }
609
610 function getEndBody() {
611 return '</ul>';
612 }
613 }