Merge "Remove not used private member variable mParserWarnings from OutputPage"
[mediawiki.git] / includes / specials / SpecialNewpages.php
blob5d3700d5ea7287f89efb732d26fb88b1c3560833
1 <?php
2 /**
3 * Implements Special:Newpages
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * A special page that list newly created pages
27 * @ingroup SpecialPage
29 class SpecialNewpages extends IncludableSpecialPage {
30 /**
31 * @var FormOptions
33 protected $opts;
34 protected $customFilters;
36 protected $showNavigation = false;
38 public function __construct() {
39 parent::__construct( 'Newpages' );
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 );
58 $this->customFilters = array();
59 Hooks::run( 'SpecialNewPagesFilters', array( $this, &$this->customFilters ) );
60 foreach ( $this->customFilters as $key => $params ) {
61 $opts->add( $key, $params['default'] );
64 // Set values
65 $opts->fetchValuesFromRequest( $this->getRequest() );
66 if ( $par ) {
67 $this->parseParams( $par );
70 // Validate
71 $opts->validateIntBounds( 'limit', 0, 5000 );
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;
80 if ( 'hideliu' === $bit ) {
81 $this->opts->setValue( 'hideliu', true );
83 if ( 'hidepatrolled' == $bit ) {
84 $this->opts->setValue( 'hidepatrolled', true );
86 if ( 'hidebots' == $bit ) {
87 $this->opts->setValue( 'hidebots', true );
89 if ( 'showredirs' == $bit ) {
90 $this->opts->setValue( 'hideredirs', false );
92 if ( is_numeric( $bit ) ) {
93 $this->opts->setValue( 'limit', intval( $bit ) );
96 $m = array();
97 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
98 $this->opts->setValue( 'limit', intval( $m[1] ) );
100 // PG offsets not just digits!
101 if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) ) {
102 $this->opts->setValue( 'offset', intval( $m[1] ) );
104 if ( preg_match( '/^username=(.*)$/', $bit, $m ) ) {
105 $this->opts->setValue( 'username', $m[1] );
107 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
108 $ns = $this->getLanguage()->getNsIndex( $m[1] );
109 if ( $ns !== false ) {
110 $this->opts->setValue( 'namespace', $ns );
117 * Show a form for filtering namespace and username
119 * @param string $par
121 public function execute( $par ) {
122 $out = $this->getOutput();
124 $this->setHeaders();
125 $this->outputHeader();
127 $this->showNavigation = !$this->including(); // Maybe changed in setup
128 $this->setup( $par );
130 $this->addHelpLink( 'Help:New pages' );
132 if ( !$this->including() ) {
133 // Settings
134 $this->form();
136 $feedType = $this->opts->getValue( 'feed' );
137 if ( $feedType ) {
138 $this->feed( $feedType );
140 return;
143 $allValues = $this->opts->getAllValues();
144 unset( $allValues['feed'] );
145 $out->setFeedAppendQuery( wfArrayToCgi( $allValues ) );
148 $pager = new NewPagesPager( $this, $this->opts );
149 $pager->mLimit = $this->opts->getValue( 'limit' );
150 $pager->mOffset = $this->opts->getValue( 'offset' );
152 if ( $pager->getNumRows() ) {
153 $navigation = '';
154 if ( $this->showNavigation ) {
155 $navigation = $pager->getNavigationBar();
157 $out->addHTML( $navigation . $pager->getBody() . $navigation );
158 } else {
159 $out->addWikiMsg( 'specialpage-empty' );
163 protected function filterLinks() {
164 // show/hide links
165 $showhide = array( $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() );
167 // Option value -> message mapping
168 $filters = array(
169 'hideliu' => 'rcshowhideliu',
170 'hidepatrolled' => 'rcshowhidepatr',
171 'hidebots' => 'rcshowhidebots',
172 'hideredirs' => 'whatlinkshere-hideredirs'
174 foreach ( $this->customFilters as $key => $params ) {
175 $filters[$key] = $params['msg'];
178 // Disable some if needed
179 if ( !User::groupHasPermission( '*', 'createpage' ) ) {
180 unset( $filters['hideliu'] );
182 if ( !$this->getUser()->useNPPatrol() ) {
183 unset( $filters['hidepatrolled'] );
186 $links = array();
187 $changed = $this->opts->getChangedValues();
188 unset( $changed['offset'] ); // Reset offset if query type changes
190 $self = $this->getPageTitle();
191 foreach ( $filters as $key => $msg ) {
192 $onoff = 1 - $this->opts->getValue( $key );
193 $link = Linker::link( $self, $showhide[$onoff], array(),
194 array( $key => $onoff ) + $changed
196 $links[$key] = $this->msg( $msg )->rawParams( $link )->escaped();
199 return $this->getLanguage()->pipeList( $links );
202 protected function form() {
203 $out = $this->getOutput();
204 $out->addModules( 'mediawiki.userSuggest' );
206 // Consume values
207 $this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW
208 $namespace = $this->opts->consumeValue( 'namespace' );
209 $username = $this->opts->consumeValue( 'username' );
210 $tagFilterVal = $this->opts->consumeValue( 'tagfilter' );
211 $nsinvert = $this->opts->consumeValue( 'invert' );
213 // Check username input validity
214 $ut = Title::makeTitleSafe( NS_USER, $username );
215 $userText = $ut ? $ut->getText() : '';
217 // Store query values in hidden fields so that form submission doesn't lose them
218 $hidden = array();
219 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
220 $hidden[] = Html::hidden( $key, $value );
222 $hidden = implode( "\n", $hidden );
224 $form = array(
225 'namespace' => array(
226 'type' => 'namespaceselect',
227 'name' => 'namespace',
228 'label-message' => 'namespace',
229 'default' => $namespace,
231 'nsinvert' => array(
232 'type' => 'check',
233 'name' => 'invert',
234 'label-message' => 'invert',
235 'default' => $nsinvert,
236 'tooltip' => 'invert',
238 'tagFilter' => array(
239 'type' => 'tagfilter',
240 'name' => 'tagfilter',
241 'label-raw' => $this->msg( 'tag-filter' )->parse(),
242 'default' => $tagFilterVal,
244 'username' => array(
245 'type' => 'text',
246 'name' => 'username',
247 'label-message' => 'newpages-username',
248 'default' => $userText,
249 'id' => 'mw-np-username',
250 'size' => 30,
251 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
255 $htmlForm = new HTMLForm( $form, $this->getContext() );
257 $htmlForm->setSubmitText( $this->msg( 'newpages-submit' )->text() );
258 $htmlForm->setSubmitProgressive();
259 // The form should be visible on each request (inclusive requests with submitted forms), so
260 // return always false here.
261 $htmlForm->setSubmitCallback(
262 function () {
263 return false;
266 $htmlForm->setMethod( 'get' );
268 $out->addHtml( Xml::fieldset( $this->msg( 'newpages' )->text() ) );
270 $htmlForm->show();
272 $out->addHtml(
273 Html::rawElement(
274 'div',
275 null,
276 $this->filterLinks()
278 Xml::closeElement( 'fieldset' )
283 * Format a row, providing the timestamp, links to the page/history,
284 * size, user links, and a comment
286 * @param object $result Result row
287 * @return string
289 public function formatRow( $result ) {
290 $title = Title::newFromRow( $result );
292 # Revision deletion works on revisions, so we should cast one
293 $row = array(
294 'comment' => $result->rc_comment,
295 'deleted' => $result->rc_deleted,
296 'user_text' => $result->rc_user_text,
297 'user' => $result->rc_user,
299 $rev = new Revision( $row );
300 $rev->setTitle( $title );
302 $classes = array();
304 $lang = $this->getLanguage();
305 $dm = $lang->getDirMark();
307 $spanTime = Html::element( 'span', array( 'class' => 'mw-newpages-time' ),
308 $lang->userTimeAndDate( $result->rc_timestamp, $this->getUser() )
310 $time = Linker::linkKnown(
311 $title,
312 $spanTime,
313 array(),
314 array( 'oldid' => $result->rc_this_oldid ),
315 array()
318 $query = $title->isRedirect() ? array( 'redirect' => 'no' ) : array();
320 // Linker::linkKnown() uses 'known' and 'noclasses' options.
321 // This breaks the colouration for stubs.
322 $plink = Linker::link(
323 $title,
324 null,
325 array( 'class' => 'mw-newpages-pagename' ),
326 $query,
327 array( 'known' )
329 $histLink = Linker::linkKnown(
330 $title,
331 $this->msg( 'hist' )->escaped(),
332 array(),
333 array( 'action' => 'history' )
335 $hist = Html::rawElement( 'span', array( 'class' => 'mw-newpages-history' ),
336 $this->msg( 'parentheses' )->rawParams( $histLink )->escaped() );
338 $length = Html::rawElement(
339 'span',
340 array( 'class' => 'mw-newpages-length' ),
341 $this->msg( 'brackets' )->rawParams(
342 $this->msg( 'nbytes' )->numParams( $result->length )->escaped()
343 )->escaped()
346 $ulink = Linker::revUserTools( $rev );
347 $comment = Linker::revComment( $rev );
349 if ( $this->patrollable( $result ) ) {
350 $classes[] = 'not-patrolled';
353 # Add a class for zero byte pages
354 if ( $result->length == 0 ) {
355 $classes[] = 'mw-newpages-zero-byte-page';
358 # Tags, if any.
359 if ( isset( $result->ts_tags ) ) {
360 list( $tagDisplay, $newClasses ) = ChangeTags::formatSummaryRow(
361 $result->ts_tags,
362 'newpages'
364 $classes = array_merge( $classes, $newClasses );
365 } else {
366 $tagDisplay = '';
369 $css = count( $classes ) ? ' class="' . implode( ' ', $classes ) . '"' : '';
371 # Display the old title if the namespace/title has been changed
372 $oldTitleText = '';
373 $oldTitle = Title::makeTitle( $result->rc_namespace, $result->rc_title );
375 if ( !$title->equals( $oldTitle ) ) {
376 $oldTitleText = $oldTitle->getPrefixedText();
377 $oldTitleText = $this->msg( 'rc-old-title' )->params( $oldTitleText )->escaped();
380 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} "
381 . "{$dm}{$ulink} {$comment} {$tagDisplay} {$oldTitleText}</li>\n";
385 * Should a specific result row provide "patrollable" links?
387 * @param object $result Result row
388 * @return bool
390 protected function patrollable( $result ) {
391 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled );
395 * Output a subscription feed listing recent edits to this page.
397 * @param string $type
399 protected function feed( $type ) {
400 if ( !$this->getConfig()->get( 'Feed' ) ) {
401 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
403 return;
406 $feedClasses = $this->getConfig()->get( 'FeedClasses' );
407 if ( !isset( $feedClasses[$type] ) ) {
408 $this->getOutput()->addWikiMsg( 'feed-invalid' );
410 return;
413 $feed = new $feedClasses[$type](
414 $this->feedTitle(),
415 $this->msg( 'tagline' )->text(),
416 $this->getPageTitle()->getFullURL()
419 $pager = new NewPagesPager( $this, $this->opts );
420 $limit = $this->opts->getValue( 'limit' );
421 $pager->mLimit = min( $limit, $this->getConfig()->get( 'FeedLimit' ) );
423 $feed->outHeader();
424 if ( $pager->getNumRows() > 0 ) {
425 foreach ( $pager->mResult as $row ) {
426 $feed->outItem( $this->feedItem( $row ) );
429 $feed->outFooter();
432 protected function feedTitle() {
433 $desc = $this->getDescription();
434 $code = $this->getConfig()->get( 'LanguageCode' );
435 $sitename = $this->getConfig()->get( 'Sitename' );
437 return "$sitename - $desc [$code]";
440 protected function feedItem( $row ) {
441 $title = Title::makeTitle( intval( $row->rc_namespace ), $row->rc_title );
442 if ( $title ) {
443 $date = $row->rc_timestamp;
444 $comments = $title->getTalkPage()->getFullURL();
446 return new FeedItem(
447 $title->getPrefixedText(),
448 $this->feedItemDesc( $row ),
449 $title->getFullURL(),
450 $date,
451 $this->feedItemAuthor( $row ),
452 $comments
454 } else {
455 return null;
459 protected function feedItemAuthor( $row ) {
460 return isset( $row->rc_user_text ) ? $row->rc_user_text : '';
463 protected function feedItemDesc( $row ) {
464 $revision = Revision::newFromId( $row->rev_id );
465 if ( $revision ) {
466 // XXX: include content model/type in feed item?
467 return '<p>' . htmlspecialchars( $revision->getUserText() ) .
468 $this->msg( 'colon-separator' )->inContentLanguage()->escaped() .
469 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
470 "</p>\n<hr />\n<div>" .
471 nl2br( htmlspecialchars( $revision->getContent()->serialize() ) ) . "</div>";
474 return '';
477 protected function getGroupName() {
478 return 'changes';
483 * @ingroup SpecialPage Pager
485 class NewPagesPager extends ReverseChronologicalPager {
486 // Stored opts
487 protected $opts;
490 * @var HtmlForm
492 protected $mForm;
494 function __construct( $form, FormOptions $opts ) {
495 parent::__construct( $form->getContext() );
496 $this->mForm = $form;
497 $this->opts = $opts;
500 function getQueryInfo() {
501 $conds = array();
502 $conds['rc_new'] = 1;
504 $namespace = $this->opts->getValue( 'namespace' );
505 $namespace = ( $namespace === 'all' ) ? false : intval( $namespace );
507 $username = $this->opts->getValue( 'username' );
508 $user = Title::makeTitleSafe( NS_USER, $username );
510 $rcIndexes = array();
512 if ( $namespace !== false ) {
513 if ( $this->opts->getValue( 'invert' ) ) {
514 $conds[] = 'rc_namespace != ' . $this->mDb->addQuotes( $namespace );
515 } else {
516 $conds['rc_namespace'] = $namespace;
520 if ( $user ) {
521 $conds['rc_user_text'] = $user->getText();
522 $rcIndexes = 'rc_user_text';
523 } elseif ( User::groupHasPermission( '*', 'createpage' ) &&
524 $this->opts->getValue( 'hideliu' )
526 # If anons cannot make new pages, don't "exclude logged in users"!
527 $conds['rc_user'] = 0;
530 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
531 if ( $this->opts->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
532 $conds['rc_patrolled'] = 0;
535 if ( $this->opts->getValue( 'hidebots' ) ) {
536 $conds['rc_bot'] = 0;
539 if ( $this->opts->getValue( 'hideredirs' ) ) {
540 $conds['page_is_redirect'] = 0;
543 // Allow changes to the New Pages query
544 $tables = array( 'recentchanges', 'page' );
545 $fields = array(
546 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
547 'rc_comment', 'rc_timestamp', 'rc_patrolled', 'rc_id', 'rc_deleted',
548 'length' => 'page_len', 'rev_id' => 'page_latest', 'rc_this_oldid',
549 'page_namespace', 'page_title'
551 $join_conds = array( 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ) );
553 Hooks::run( 'SpecialNewpagesConditions',
554 array( &$this, $this->opts, &$conds, &$tables, &$fields, &$join_conds ) );
556 $options = array();
558 if ( $rcIndexes ) {
559 $options = array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) );
562 $info = array(
563 'tables' => $tables,
564 'fields' => $fields,
565 'conds' => $conds,
566 'options' => $options,
567 'join_conds' => $join_conds
570 // Modify query for tags
571 ChangeTags::modifyDisplayQuery(
572 $info['tables'],
573 $info['fields'],
574 $info['conds'],
575 $info['join_conds'],
576 $info['options'],
577 $this->opts['tagfilter']
580 return $info;
583 function getIndexField() {
584 return 'rc_timestamp';
587 function formatRow( $row ) {
588 return $this->mForm->formatRow( $row );
591 function getStartBody() {
592 # Do a batch existence check on pages
593 $linkBatch = new LinkBatch();
594 foreach ( $this->mResult as $row ) {
595 $linkBatch->add( NS_USER, $row->rc_user_text );
596 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
597 $linkBatch->add( $row->page_namespace, $row->page_title );
599 $linkBatch->execute();
601 return '<ul>';
604 function getEndBody() {
605 return '</ul>';