Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / specials / SpecialNewpages.php
blob994a2e51545e8d76dcfac5d7a75a5624b4365b3c
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 if ( !$this->including() ) {
131 // Settings
132 $this->form();
134 $feedType = $this->opts->getValue( 'feed' );
135 if ( $feedType ) {
136 $this->feed( $feedType );
138 return;
141 $allValues = $this->opts->getAllValues();
142 unset( $allValues['feed'] );
143 $out->setFeedAppendQuery( wfArrayToCgi( $allValues ) );
146 $pager = new NewPagesPager( $this, $this->opts );
147 $pager->mLimit = $this->opts->getValue( 'limit' );
148 $pager->mOffset = $this->opts->getValue( 'offset' );
150 if ( $pager->getNumRows() ) {
151 $navigation = '';
152 if ( $this->showNavigation ) {
153 $navigation = $pager->getNavigationBar();
155 $out->addHTML( $navigation . $pager->getBody() . $navigation );
156 } else {
157 $out->addWikiMsg( 'specialpage-empty' );
161 protected function filterLinks() {
162 // show/hide links
163 $showhide = array( $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() );
165 // Option value -> message mapping
166 $filters = array(
167 'hideliu' => 'rcshowhideliu',
168 'hidepatrolled' => 'rcshowhidepatr',
169 'hidebots' => 'rcshowhidebots',
170 'hideredirs' => 'whatlinkshere-hideredirs'
172 foreach ( $this->customFilters as $key => $params ) {
173 $filters[$key] = $params['msg'];
176 // Disable some if needed
177 if ( !User::groupHasPermission( '*', 'createpage' ) ) {
178 unset( $filters['hideliu'] );
180 if ( !$this->getUser()->useNPPatrol() ) {
181 unset( $filters['hidepatrolled'] );
184 $links = array();
185 $changed = $this->opts->getChangedValues();
186 unset( $changed['offset'] ); // Reset offset if query type changes
188 $self = $this->getPageTitle();
189 foreach ( $filters as $key => $msg ) {
190 $onoff = 1 - $this->opts->getValue( $key );
191 $link = Linker::link( $self, $showhide[$onoff], array(),
192 array( $key => $onoff ) + $changed
194 $links[$key] = $this->msg( $msg )->rawParams( $link )->escaped();
197 return $this->getLanguage()->pipeList( $links );
200 protected function form() {
201 $out = $this->getOutput();
202 $out->addModules( 'mediawiki.userSuggest' );
204 // Consume values
205 $this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW
206 $namespace = $this->opts->consumeValue( 'namespace' );
207 $username = $this->opts->consumeValue( 'username' );
208 $tagFilterVal = $this->opts->consumeValue( 'tagfilter' );
209 $nsinvert = $this->opts->consumeValue( 'invert' );
211 // Check username input validity
212 $ut = Title::makeTitleSafe( NS_USER, $username );
213 $userText = $ut ? $ut->getText() : '';
215 // Store query values in hidden fields so that form submission doesn't lose them
216 $hidden = array();
217 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
218 $hidden[] = Html::hidden( $key, $value );
220 $hidden = implode( "\n", $hidden );
222 $form = array(
223 'namespace' => array(
224 'type' => 'namespaceselect',
225 'name' => 'namespace',
226 'label-message' => 'namespace',
227 'default' => $namespace,
229 'nsinvert' => array(
230 'type' => 'check',
231 'name' => 'invert',
232 'label-message' => 'invert',
233 'default' => $nsinvert,
234 'tooltip' => $this->msg( 'tooltip-invert' )->text(),
236 'tagFilter' => array(
237 'type' => 'tagfilter',
238 'name' => 'tagfilter',
239 'label-raw' => $this->msg( 'tag-filter' )->parse(),
240 'default' => $tagFilterVal,
242 'username' => array(
243 'type' => 'text',
244 'name' => 'username',
245 'label-message' => 'newpages-username',
246 'default' => $userText,
247 'id' => 'mw-np-username',
248 'size' => 30,
249 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
253 $htmlForm = new HTMLForm( $form, $this->getContext() );
255 $htmlForm->setSubmitText( $this->msg( 'allpagessubmit' )->text() );
256 $htmlForm->setSubmitProgressive();
257 // The form should be visible on each request (inclusive requests with submitted forms), so
258 // return always false here.
259 $htmlForm->setSubmitCallback(
260 function () {
261 return false;
264 $htmlForm->setMethod( 'get' );
266 $out->addHtml( Xml::fieldset( $this->msg( 'newpages' )->text() ) );
268 $htmlForm->show();
270 $out->addHtml(
271 Html::rawElement(
272 'div',
273 null,
274 $this->filterLinks()
276 Xml::closeElement( 'fieldset' )
281 * Format a row, providing the timestamp, links to the page/history,
282 * size, user links, and a comment
284 * @param object $result Result row
285 * @return string
287 public function formatRow( $result ) {
288 $title = Title::newFromRow( $result );
290 # Revision deletion works on revisions, so we should cast one
291 $row = array(
292 'comment' => $result->rc_comment,
293 'deleted' => $result->rc_deleted,
294 'user_text' => $result->rc_user_text,
295 'user' => $result->rc_user,
297 $rev = new Revision( $row );
298 $rev->setTitle( $title );
300 $classes = array();
302 $lang = $this->getLanguage();
303 $dm = $lang->getDirMark();
305 $spanTime = Html::element( 'span', array( 'class' => 'mw-newpages-time' ),
306 $lang->userTimeAndDate( $result->rc_timestamp, $this->getUser() )
308 $time = Linker::linkKnown(
309 $title,
310 $spanTime,
311 array(),
312 array( 'oldid' => $result->rc_this_oldid ),
313 array()
316 $query = array( 'redirect' => 'no' );
318 // Linker::linkKnown() uses 'known' and 'noclasses' options.
319 // This breaks the colouration for stubs.
320 $plink = Linker::link(
321 $title,
322 null,
323 array( 'class' => 'mw-newpages-pagename' ),
324 $query,
325 array( 'known' )
327 $histLink = Linker::linkKnown(
328 $title,
329 $this->msg( 'hist' )->escaped(),
330 array(),
331 array( 'action' => 'history' )
333 $hist = Html::rawElement( 'span', array( 'class' => 'mw-newpages-history' ),
334 $this->msg( 'parentheses' )->rawParams( $histLink )->escaped() );
336 $length = Html::rawElement(
337 'span',
338 array( 'class' => 'mw-newpages-length' ),
339 $this->msg( 'brackets' )->rawParams(
340 $this->msg( 'nbytes' )->numParams( $result->length )->escaped()
341 )->escaped()
344 $ulink = Linker::revUserTools( $rev );
345 $comment = Linker::revComment( $rev );
347 if ( $this->patrollable( $result ) ) {
348 $classes[] = 'not-patrolled';
351 # Add a class for zero byte pages
352 if ( $result->length == 0 ) {
353 $classes[] = 'mw-newpages-zero-byte-page';
356 # Tags, if any.
357 if ( isset( $result->ts_tags ) ) {
358 list( $tagDisplay, $newClasses ) = ChangeTags::formatSummaryRow(
359 $result->ts_tags,
360 'newpages'
362 $classes = array_merge( $classes, $newClasses );
363 } else {
364 $tagDisplay = '';
367 $css = count( $classes ) ? ' class="' . implode( ' ', $classes ) . '"' : '';
369 # Display the old title if the namespace/title has been changed
370 $oldTitleText = '';
371 $oldTitle = Title::makeTitle( $result->rc_namespace, $result->rc_title );
373 if ( !$title->equals( $oldTitle ) ) {
374 $oldTitleText = $oldTitle->getPrefixedText();
375 $oldTitleText = $this->msg( 'rc-old-title' )->params( $oldTitleText )->escaped();
378 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} "
379 . "{$dm}{$ulink} {$comment} {$tagDisplay} {$oldTitleText}</li>\n";
383 * Should a specific result row provide "patrollable" links?
385 * @param object $result Result row
386 * @return bool
388 protected function patrollable( $result ) {
389 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled );
393 * Output a subscription feed listing recent edits to this page.
395 * @param string $type
397 protected function feed( $type ) {
398 if ( !$this->getConfig()->get( 'Feed' ) ) {
399 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
401 return;
404 $feedClasses = $this->getConfig()->get( 'FeedClasses' );
405 if ( !isset( $feedClasses[$type] ) ) {
406 $this->getOutput()->addWikiMsg( 'feed-invalid' );
408 return;
411 $feed = new $feedClasses[$type](
412 $this->feedTitle(),
413 $this->msg( 'tagline' )->text(),
414 $this->getPageTitle()->getFullURL()
417 $pager = new NewPagesPager( $this, $this->opts );
418 $limit = $this->opts->getValue( 'limit' );
419 $pager->mLimit = min( $limit, $this->getConfig()->get( 'FeedLimit' ) );
421 $feed->outHeader();
422 if ( $pager->getNumRows() > 0 ) {
423 foreach ( $pager->mResult as $row ) {
424 $feed->outItem( $this->feedItem( $row ) );
427 $feed->outFooter();
430 protected function feedTitle() {
431 $desc = $this->getDescription();
432 $code = $this->getConfig()->get( 'LanguageCode' );
433 $sitename = $this->getConfig()->get( 'Sitename' );
435 return "$sitename - $desc [$code]";
438 protected function feedItem( $row ) {
439 $title = Title::makeTitle( intval( $row->rc_namespace ), $row->rc_title );
440 if ( $title ) {
441 $date = $row->rc_timestamp;
442 $comments = $title->getTalkPage()->getFullURL();
444 return new FeedItem(
445 $title->getPrefixedText(),
446 $this->feedItemDesc( $row ),
447 $title->getFullURL(),
448 $date,
449 $this->feedItemAuthor( $row ),
450 $comments
452 } else {
453 return null;
457 protected function feedItemAuthor( $row ) {
458 return isset( $row->rc_user_text ) ? $row->rc_user_text : '';
461 protected function feedItemDesc( $row ) {
462 $revision = Revision::newFromId( $row->rev_id );
463 if ( $revision ) {
464 //XXX: include content model/type in feed item?
465 return '<p>' . htmlspecialchars( $revision->getUserText() ) .
466 $this->msg( 'colon-separator' )->inContentLanguage()->escaped() .
467 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
468 "</p>\n<hr />\n<div>" .
469 nl2br( htmlspecialchars( $revision->getContent()->serialize() ) ) . "</div>";
472 return '';
475 protected function getGroupName() {
476 return 'changes';
481 * @ingroup SpecialPage Pager
483 class NewPagesPager extends ReverseChronologicalPager {
484 // Stored opts
485 protected $opts;
488 * @var HtmlForm
490 protected $mForm;
492 function __construct( $form, FormOptions $opts ) {
493 parent::__construct( $form->getContext() );
494 $this->mForm = $form;
495 $this->opts = $opts;
498 function getQueryInfo() {
499 $conds = array();
500 $conds['rc_new'] = 1;
502 $namespace = $this->opts->getValue( 'namespace' );
503 $namespace = ( $namespace === 'all' ) ? false : intval( $namespace );
505 $username = $this->opts->getValue( 'username' );
506 $user = Title::makeTitleSafe( NS_USER, $username );
508 $rcIndexes = array();
510 if ( $namespace !== false ) {
511 if ( $this->opts->getValue( 'invert' ) ) {
512 $conds[] = 'rc_namespace != ' . $this->mDb->addQuotes( $namespace );
513 } else {
514 $conds['rc_namespace'] = $namespace;
518 if ( $user ) {
519 $conds['rc_user_text'] = $user->getText();
520 $rcIndexes = 'rc_user_text';
521 } elseif ( User::groupHasPermission( '*', 'createpage' ) &&
522 $this->opts->getValue( 'hideliu' )
524 # If anons cannot make new pages, don't "exclude logged in users"!
525 $conds['rc_user'] = 0;
528 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
529 if ( $this->opts->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
530 $conds['rc_patrolled'] = 0;
533 if ( $this->opts->getValue( 'hidebots' ) ) {
534 $conds['rc_bot'] = 0;
537 if ( $this->opts->getValue( 'hideredirs' ) ) {
538 $conds['page_is_redirect'] = 0;
541 // Allow changes to the New Pages query
542 $tables = array( 'recentchanges', 'page' );
543 $fields = array(
544 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
545 'rc_comment', 'rc_timestamp', 'rc_patrolled', 'rc_id', 'rc_deleted',
546 'length' => 'page_len', 'rev_id' => 'page_latest', 'rc_this_oldid',
547 'page_namespace', 'page_title'
549 $join_conds = array( 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ) );
551 Hooks::run( 'SpecialNewpagesConditions',
552 array( &$this, $this->opts, &$conds, &$tables, &$fields, &$join_conds ) );
554 $options = array();
556 if ( $rcIndexes ) {
557 $options = array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) );
560 $info = array(
561 'tables' => $tables,
562 'fields' => $fields,
563 'conds' => $conds,
564 'options' => $options,
565 'join_conds' => $join_conds
568 // Modify query for tags
569 ChangeTags::modifyDisplayQuery(
570 $info['tables'],
571 $info['fields'],
572 $info['conds'],
573 $info['join_conds'],
574 $info['options'],
575 $this->opts['tagfilter']
578 return $info;
581 function getIndexField() {
582 return 'rc_timestamp';
585 function formatRow( $row ) {
586 return $this->mForm->formatRow( $row );
589 function getStartBody() {
590 # Do a batch existence check on pages
591 $linkBatch = new LinkBatch();
592 foreach ( $this->mResult as $row ) {
593 $linkBatch->add( NS_USER, $row->rc_user_text );
594 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
595 $linkBatch->add( $row->rc_namespace, $row->rc_title );
597 $linkBatch->execute();
599 return '<ul>';
602 function getEndBody() {
603 return '</ul>';