Merge "jquery.tablesorter: Reset unaffected columns' sort counts when sorting"
[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 // Stored objects
31
32 /**
33 * @var FormOptions
34 */
35 protected $opts;
36 protected $customFilters;
37
38 // Some internal settings
39 protected $showNavigation = false;
40
41 public function __construct() {
42 parent::__construct( 'Newpages' );
43 }
44
45 protected function setup( $par ) {
46 global $wgEnableNewpagesUserFilter;
47
48 // Options
49 $opts = new FormOptions();
50 $this->opts = $opts; // bind
51 $opts->add( 'hideliu', false );
52 $opts->add( 'hidepatrolled', $this->getUser()->getBoolOption( 'newpageshidepatrolled' ) );
53 $opts->add( 'hidebots', false );
54 $opts->add( 'hideredirs', true );
55 $opts->add( 'limit', $this->getUser()->getIntOption( 'rclimit' ) );
56 $opts->add( 'offset', '' );
57 $opts->add( 'namespace', '0' );
58 $opts->add( 'username', '' );
59 $opts->add( 'feed', '' );
60 $opts->add( 'tagfilter', '' );
61 $opts->add( 'invert', false );
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 ) {
72 $this->parseParams( $par );
73 }
74
75 // Validate
76 $opts->validateIntBounds( 'limit', 0, 5000 );
77 if ( !$wgEnableNewpagesUserFilter ) {
78 $opts->setValue( 'username', '' );
79 }
80 }
81
82 protected function parseParams( $par ) {
83 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
84 foreach ( $bits as $bit ) {
85 if ( 'shownav' == $bit ) {
86 $this->showNavigation = true;
87 }
88 if ( 'hideliu' === $bit ) {
89 $this->opts->setValue( 'hideliu', true );
90 }
91 if ( 'hidepatrolled' == $bit ) {
92 $this->opts->setValue( 'hidepatrolled', true );
93 }
94 if ( 'hidebots' == $bit ) {
95 $this->opts->setValue( 'hidebots', true );
96 }
97 if ( 'showredirs' == $bit ) {
98 $this->opts->setValue( 'hideredirs', false );
99 }
100 if ( is_numeric( $bit ) ) {
101 $this->opts->setValue( 'limit', intval( $bit ) );
102 }
103
104 $m = array();
105 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
106 $this->opts->setValue( 'limit', intval( $m[1] ) );
107 }
108 // PG offsets not just digits!
109 if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) ) {
110 $this->opts->setValue( 'offset', intval( $m[1] ) );
111 }
112 if ( preg_match( '/^username=(.*)$/', $bit, $m ) ) {
113 $this->opts->setValue( 'username', $m[1] );
114 }
115 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
116 $ns = $this->getLanguage()->getNsIndex( $m[1] );
117 if ( $ns !== false ) {
118 $this->opts->setValue( 'namespace', $ns );
119 }
120 }
121 }
122 }
123
124 /**
125 * Show a form for filtering namespace and username
126 *
127 * @param $par String
128 * @return String
129 */
130 public function execute( $par ) {
131 $out = $this->getOutput();
132
133 $this->setHeaders();
134 $this->outputHeader();
135
136 $this->showNavigation = !$this->including(); // Maybe changed in setup
137 $this->setup( $par );
138
139 if ( !$this->including() ) {
140 // Settings
141 $this->form();
142
143 $feedType = $this->opts->getValue( 'feed' );
144 if ( $feedType ) {
145 $this->feed( $feedType );
146
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,
301 * size, user links, and a comment
302 *
303 * @param object $result Result row
304 * @return String
305 */
306 public function formatRow( $result ) {
307 $title = Title::newFromRow( $result );
308
309 # Revision deletion works on revisions, so we should cast one
310 $row = array(
311 'comment' => $result->rc_comment,
312 'deleted' => $result->rc_deleted,
313 'user_text' => $result->rc_user_text,
314 'user' => $result->rc_user,
315 );
316 $rev = new Revision( $row );
317 $rev->setTitle( $title );
318
319 $classes = array();
320
321 $lang = $this->getLanguage();
322 $dm = $lang->getDirMark();
323
324 $spanTime = Html::element( 'span', array( 'class' => 'mw-newpages-time' ),
325 $lang->userTimeAndDate( $result->rc_timestamp, $this->getUser() )
326 );
327 $time = Linker::linkKnown(
328 $title,
329 $spanTime,
330 array(),
331 array( 'oldid' => $result->rc_this_oldid ),
332 array()
333 );
334
335 $query = array( 'redirect' => 'no' );
336
337 if( $this->patrollable( $result ) ) {
338 // Tell Article.php that we want to patrol the first revision
339 // and not the current one. Has effect if both recentchages and new page
340 // patrolling are enabled, we set it everytime for link consistency though.
341 $query['patrolpage'] = 1;
342 }
343
344 // Linker::linkKnown() uses 'known' and 'noclasses' options.
345 // This breaks the colouration for stubs.
346 $plink = Linker::link(
347 $title,
348 null,
349 array( 'class' => 'mw-newpages-pagename' ),
350 $query,
351 array( 'known' )
352 );
353 $histLink = Linker::linkKnown(
354 $title,
355 $this->msg( 'hist' )->escaped(),
356 array(),
357 array( 'action' => 'history' )
358 );
359 $hist = Html::rawElement( 'span', array( 'class' => 'mw-newpages-history' ),
360 $this->msg( 'parentheses' )->rawParams( $histLink )->escaped() );
361
362 $length = Html::element(
363 'span',
364 array( 'class' => 'mw-newpages-length' ),
365 $this->msg( 'brackets' )->params( $this->msg( 'nbytes' )
366 ->numParams( $result->length )->text()
367 )
368 );
369
370 $ulink = Linker::revUserTools( $rev );
371 $comment = Linker::revComment( $rev );
372
373 if ( $this->patrollable( $result ) ) {
374 $classes[] = 'not-patrolled';
375 }
376
377 # Add a class for zero byte pages
378 if ( $result->length == 0 ) {
379 $classes[] = 'mw-newpages-zero-byte-page';
380 }
381
382 # Tags, if any.
383 if ( isset( $result->ts_tags ) ) {
384 list( $tagDisplay, $newClasses ) = ChangeTags::formatSummaryRow(
385 $result->ts_tags,
386 'newpages'
387 );
388 $classes = array_merge( $classes, $newClasses );
389 } else {
390 $tagDisplay = '';
391 }
392
393 $css = count( $classes ) ? ' class="' . implode( ' ', $classes ) . '"' : '';
394
395 # Display the old title if the namespace/title has been changed
396 $oldTitleText = '';
397 $oldTitle = Title::makeTitle( $result->rc_namespace, $result->rc_title );
398
399 if ( !$title->equals( $oldTitle ) ) {
400 $oldTitleText = $oldTitle->getPrefixedText();
401 $oldTitleText = $this->msg( 'rc-old-title' )->params( $oldTitleText )->escaped();
402 }
403
404 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} {$dm}{$ulink} {$comment} {$tagDisplay} {$oldTitleText}</li>\n";
405 }
406
407 /**
408 * Should a specific result row provide "patrollable" links?
409 *
410 * @param object $result Result row
411 * @return Boolean
412 */
413 protected function patrollable( $result ) {
414 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled );
415 }
416
417 /**
418 * Output a subscription feed listing recent edits to this page.
419 *
420 * @param $type String
421 */
422 protected function feed( $type ) {
423 global $wgFeed, $wgFeedClasses, $wgFeedLimit;
424
425 if ( !$wgFeed ) {
426 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
427
428 return;
429 }
430
431 if ( !isset( $wgFeedClasses[$type] ) ) {
432 $this->getOutput()->addWikiMsg( 'feed-invalid' );
433
434 return;
435 }
436
437 $feed = new $wgFeedClasses[$type](
438 $this->feedTitle(),
439 $this->msg( 'tagline' )->text(),
440 $this->getTitle()->getFullURL()
441 );
442
443 $pager = new NewPagesPager( $this, $this->opts );
444 $limit = $this->opts->getValue( 'limit' );
445 $pager->mLimit = min( $limit, $wgFeedLimit );
446
447 $feed->outHeader();
448 if ( $pager->getNumRows() > 0 ) {
449 foreach ( $pager->mResult as $row ) {
450 $feed->outItem( $this->feedItem( $row ) );
451 }
452 }
453 $feed->outFooter();
454 }
455
456 protected function feedTitle() {
457 global $wgLanguageCode, $wgSitename;
458 $desc = $this->getDescription();
459
460 return "$wgSitename - $desc [$wgLanguageCode]";
461 }
462
463 protected function feedItem( $row ) {
464 $title = Title::makeTitle( intval( $row->rc_namespace ), $row->rc_title );
465 if ( $title ) {
466 $date = $row->rc_timestamp;
467 $comments = $title->getTalkPage()->getFullURL();
468
469 return new FeedItem(
470 $title->getPrefixedText(),
471 $this->feedItemDesc( $row ),
472 $title->getFullURL(),
473 $date,
474 $this->feedItemAuthor( $row ),
475 $comments
476 );
477 } else {
478 return null;
479 }
480 }
481
482 protected function feedItemAuthor( $row ) {
483 return isset( $row->rc_user_text ) ? $row->rc_user_text : '';
484 }
485
486 protected function feedItemDesc( $row ) {
487 $revision = Revision::newFromId( $row->rev_id );
488 if ( $revision ) {
489 //XXX: include content model/type in feed item?
490 return '<p>' . htmlspecialchars( $revision->getUserText() ) .
491 $this->msg( 'colon-separator' )->inContentLanguage()->escaped() .
492 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
493 "</p>\n<hr />\n<div>" .
494 nl2br( htmlspecialchars( $revision->getContent()->serialize() ) ) . "</div>";
495 }
496
497 return '';
498 }
499
500 protected function getGroupName() {
501 return 'changes';
502 }
503 }
504
505 /**
506 * @ingroup SpecialPage Pager
507 */
508 class NewPagesPager extends ReverseChronologicalPager {
509 // Stored opts
510 protected $opts;
511
512 /**
513 * @var HtmlForm
514 */
515 protected $mForm;
516
517 function __construct( $form, FormOptions $opts ) {
518 parent::__construct( $form->getContext() );
519 $this->mForm = $form;
520 $this->opts = $opts;
521 }
522
523 function getQueryInfo() {
524 global $wgEnableNewpagesUserFilter;
525 $conds = array();
526 $conds['rc_new'] = 1;
527
528 $namespace = $this->opts->getValue( 'namespace' );
529 $namespace = ( $namespace === 'all' ) ? false : intval( $namespace );
530
531 $username = $this->opts->getValue( 'username' );
532 $user = Title::makeTitleSafe( NS_USER, $username );
533
534 if ( $namespace !== false ) {
535 if ( $this->opts->getValue( 'invert' ) ) {
536 $conds[] = 'rc_namespace != ' . $this->mDb->addQuotes( $namespace );
537 } else {
538 $conds['rc_namespace'] = $namespace;
539 }
540 $rcIndexes = array( 'new_name_timestamp' );
541 } else {
542 $rcIndexes = array( 'rc_timestamp' );
543 }
544
545 # $wgEnableNewpagesUserFilter - temp WMF hack
546 if ( $wgEnableNewpagesUserFilter && $user ) {
547 $conds['rc_user_text'] = $user->getText();
548 $rcIndexes = 'rc_user_text';
549 } elseif ( User::groupHasPermission( '*', 'createpage' ) &&
550 $this->opts->getValue( 'hideliu' )
551 ) {
552 # If anons cannot make new pages, don't "exclude logged in users"!
553 $conds['rc_user'] = 0;
554 }
555
556 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
557 if ( $this->opts->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
558 $conds['rc_patrolled'] = 0;
559 }
560
561 if ( $this->opts->getValue( 'hidebots' ) ) {
562 $conds['rc_bot'] = 0;
563 }
564
565 if ( $this->opts->getValue( 'hideredirs' ) ) {
566 $conds['page_is_redirect'] = 0;
567 }
568
569 // Allow changes to the New Pages query
570 $tables = array( 'recentchanges', 'page' );
571 $fields = array(
572 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
573 'rc_comment', 'rc_timestamp', 'rc_patrolled', 'rc_id', 'rc_deleted',
574 'length' => 'page_len', 'rev_id' => 'page_latest', 'rc_this_oldid',
575 'page_namespace', 'page_title'
576 );
577 $join_conds = array( 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ) );
578
579 wfRunHooks( 'SpecialNewpagesConditions',
580 array( &$this, $this->opts, &$conds, &$tables, &$fields, &$join_conds ) );
581
582 $info = array(
583 'tables' => $tables,
584 'fields' => $fields,
585 'conds' => $conds,
586 'options' => array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) ),
587 'join_conds' => $join_conds
588 );
589
590 // Modify query for tags
591 ChangeTags::modifyDisplayQuery(
592 $info['tables'],
593 $info['fields'],
594 $info['conds'],
595 $info['join_conds'],
596 $info['options'],
597 $this->opts['tagfilter']
598 );
599
600 return $info;
601 }
602
603 function getIndexField() {
604 return 'rc_timestamp';
605 }
606
607 function formatRow( $row ) {
608 return $this->mForm->formatRow( $row );
609 }
610
611 function getStartBody() {
612 # Do a batch existence check on pages
613 $linkBatch = new LinkBatch();
614 foreach ( $this->mResult as $row ) {
615 $linkBatch->add( NS_USER, $row->rc_user_text );
616 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
617 $linkBatch->add( $row->rc_namespace, $row->rc_title );
618 }
619 $linkBatch->execute();
620
621 return '<ul>';
622 }
623
624 function getEndBody() {
625 return '</ul>';
626 }
627 }