Add a way for packagers to override some installation details
[mediawiki.git] / includes / specials / SpecialNewpages.php
blob4c32c30d01453400a6d6f4892f3ebf160b759580
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 {
31 // Stored objects
33 /**
34 * @var FormOptions
36 protected $opts;
37 protected $customFilters;
39 // Some internal settings
40 protected $showNavigation = false;
42 public function __construct() {
43 parent::__construct( 'Newpages' );
46 protected function setup( $par ) {
47 global $wgEnableNewpagesUserFilter;
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', '' );
63 $this->customFilters = array();
64 wfRunHooks( 'SpecialNewPagesFilters', array( $this, &$this->customFilters ) );
65 foreach( $this->customFilters as $key => $params ) {
66 $opts->add( $key, $params['default'] );
69 // Set values
70 $opts->fetchValuesFromRequest( $this->getRequest() );
71 if ( $par ) $this->parseParams( $par );
73 // Validate
74 $opts->validateIntBounds( 'limit', 0, 5000 );
75 if( !$wgEnableNewpagesUserFilter ) {
76 $opts->setValue( 'username', '' );
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;
86 if ( 'hideliu' === $bit ) {
87 $this->opts->setValue( 'hideliu', true );
89 if ( 'hidepatrolled' == $bit ) {
90 $this->opts->setValue( 'hidepatrolled', true );
92 if ( 'hidebots' == $bit ) {
93 $this->opts->setValue( 'hidebots', true );
95 if ( 'showredirs' == $bit ) {
96 $this->opts->setValue( 'hideredirs', false );
98 if ( is_numeric( $bit ) ) {
99 $this->opts->setValue( 'limit', intval( $bit ) );
102 $m = array();
103 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
104 $this->opts->setValue( 'limit', intval( $m[1] ) );
106 // PG offsets not just digits!
107 if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) ) {
108 $this->opts->setValue( 'offset', intval( $m[1] ) );
110 if ( preg_match( '/^username=(.*)$/', $bit, $m ) ) {
111 $this->opts->setValue( 'username', $m[1] );
113 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
114 $ns = $this->getLanguage()->getNsIndex( $m[1] );
115 if( $ns !== false ) {
116 $this->opts->setValue( 'namespace', $ns );
123 * Show a form for filtering namespace and username
125 * @param $par String
126 * @return String
128 public function execute( $par ) {
129 $out = $this->getOutput();
131 $this->setHeaders();
132 $this->outputHeader();
134 $this->showNavigation = !$this->including(); // Maybe changed in setup
135 $this->setup( $par );
137 if( !$this->including() ) {
138 // Settings
139 $this->form();
141 $feedType = $this->opts->getValue( 'feed' );
142 if( $feedType ) {
143 return $this->feed( $feedType );
146 $allValues = $this->opts->getAllValues();
147 unset( $allValues['feed'] );
148 $out->setFeedAppendQuery( wfArrayToCGI( $allValues ) );
151 $pager = new NewPagesPager( $this, $this->opts );
152 $pager->mLimit = $this->opts->getValue( 'limit' );
153 $pager->mOffset = $this->opts->getValue( 'offset' );
155 if( $pager->getNumRows() ) {
156 $navigation = '';
157 if ( $this->showNavigation ) {
158 $navigation = $pager->getNavigationBar();
160 $out->addHTML( $navigation . $pager->getBody() . $navigation );
161 } else {
162 $out->addWikiMsg( 'specialpage-empty' );
166 protected function filterLinks() {
167 global $wgGroupPermissions;
169 // show/hide links
170 $showhide = array( $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() );
172 // Option value -> message mapping
173 $filters = array(
174 'hideliu' => 'rcshowhideliu',
175 'hidepatrolled' => 'rcshowhidepatr',
176 'hidebots' => 'rcshowhidebots',
177 'hideredirs' => 'whatlinkshere-hideredirs'
179 foreach ( $this->customFilters as $key => $params ) {
180 $filters[$key] = $params['msg'];
183 // Disable some if needed
184 # @todo FIXME: Throws E_NOTICEs if not set; and doesn't obey hooks etc.
185 if ( $wgGroupPermissions['*']['createpage'] !== true ) {
186 unset( $filters['hideliu'] );
188 if ( !$this->getUser()->useNPPatrol() ) {
189 unset( $filters['hidepatrolled'] );
192 $links = array();
193 $changed = $this->opts->getChangedValues();
194 unset( $changed['offset'] ); // Reset offset if query type changes
196 $self = $this->getTitle();
197 foreach ( $filters as $key => $msg ) {
198 $onoff = 1 - $this->opts->getValue( $key );
199 $link = Linker::link( $self, $showhide[$onoff], array(),
200 array( $key => $onoff ) + $changed
202 $links[$key] = $this->msg( $msg )->rawParams( $link )->escaped();
205 return $this->getLanguage()->pipeList( $links );
208 protected function form() {
209 global $wgEnableNewpagesUserFilter, $wgScript;
211 // Consume values
212 $this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW
213 $namespace = $this->opts->consumeValue( 'namespace' );
214 $username = $this->opts->consumeValue( 'username' );
215 $tagFilterVal = $this->opts->consumeValue( 'tagfilter' );
217 // Check username input validity
218 $ut = Title::makeTitleSafe( NS_USER, $username );
219 $userText = $ut ? $ut->getText() : '';
221 // Store query values in hidden fields so that form submission doesn't lose them
222 $hidden = array();
223 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
224 $hidden[] = Html::hidden( $key, $value );
226 $hidden = implode( "\n", $hidden );
228 $tagFilter = ChangeTags::buildTagFilterSelector( $tagFilterVal );
229 if ( $tagFilter ) {
230 list( $tagFilterLabel, $tagFilterSelector ) = $tagFilter;
233 $form = Xml::openElement( 'form', array( 'action' => $wgScript ) ) .
234 Html::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
235 Xml::fieldset( $this->msg( 'newpages' )->text() ) .
236 Xml::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
237 '<tr>
238 <td class="mw-label">' .
239 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
240 '</td>
241 <td class="mw-input">' .
242 Html::namespaceSelector(
243 array(
244 'selected' => $namespace,
245 'all' => 'all',
246 ), array(
247 'name' => 'namespace',
248 'id' => 'namespace',
249 'class' => 'namespaceselector',
252 '</td>
253 </tr>' . ( $tagFilter ? (
254 '<tr>
255 <td class="mw-label">' .
256 $tagFilterLabel .
257 '</td>
258 <td class="mw-input">' .
259 $tagFilterSelector .
260 '</td>
261 </tr>' ) : '' ) .
262 ( $wgEnableNewpagesUserFilter ?
263 '<tr>
264 <td class="mw-label">' .
265 Xml::label( $this->msg( 'newpages-username' )->text(), 'mw-np-username' ) .
266 '</td>
267 <td class="mw-input">' .
268 Xml::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
269 '</td>
270 </tr>' : '' ) .
271 '<tr> <td></td>
272 <td class="mw-submit">' .
273 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
274 '</td>
275 </tr>' .
276 '<tr>
277 <td></td>
278 <td class="mw-input">' .
279 $this->filterLinks() .
280 '</td>
281 </tr>' .
282 Xml::closeElement( 'table' ) .
283 Xml::closeElement( 'fieldset' ) .
284 $hidden .
285 Xml::closeElement( 'form' );
287 $this->getOutput()->addHTML( $form );
291 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
293 * @param $result Result row
294 * @return String
296 public function formatRow( $result ) {
297 $title = Title::newFromRow( $result );
299 # Revision deletion works on revisions, so we should cast one
300 $row = array(
301 'comment' => $result->rc_comment,
302 'deleted' => $result->rc_deleted,
303 'user_text' => $result->rc_user_text,
304 'user' => $result->rc_user,
306 $rev = new Revision( $row );
307 $rev->setTitle( $title );
309 $classes = array();
311 $lang = $this->getLanguage();
312 $dm = $lang->getDirMark();
314 $spanTime = Html::element( 'span', array( 'class' => 'mw-newpages-time' ),
315 $lang->userTimeAndDate( $result->rc_timestamp, $this->getUser() )
317 $time = Linker::linkKnown(
318 $title,
319 $spanTime,
320 array(),
321 array( 'oldid' => $result->rc_this_oldid ),
322 array()
325 $query = array( 'redirect' => 'no' );
327 if( $this->patrollable( $result ) ) {
328 $query['rcid'] = $result->rc_id;
331 $plink = Linker::linkKnown(
332 $title,
333 null,
334 array( 'class' => 'mw-newpages-pagename' ),
335 $query,
336 array( 'known' ) // Set explicitly to avoid the default of 'known','noclasses'. This breaks the colouration for stubs
338 $histLink = Linker::linkKnown(
339 $title,
340 $this->msg( 'hist' )->escaped(),
341 array(),
342 array( 'action' => 'history' )
344 $hist = Html::rawElement( 'span', array( 'class' => 'mw-newpages-history' ),
345 $this->msg( 'parentheses' )->rawParams( $histLink )->escaped() );
347 $length = Html::element( 'span', array( 'class' => 'mw-newpages-length' ),
348 '[' . $this->msg( 'nbytes' )->numParams( $result->length )->text() . ']'
351 $ulink = Linker::revUserTools( $rev );
352 $comment = Linker::revComment( $rev );
354 if ( $this->patrollable( $result ) ) {
355 $classes[] = 'not-patrolled';
358 # Add a class for zero byte pages
359 if ( $result->length == 0 ) {
360 $classes[] = 'mw-newpages-zero-byte-page';
363 # Tags, if any. check for including due to bug 23293
364 if ( !$this->including() ) {
365 list( $tagDisplay, $newClasses ) = ChangeTags::formatSummaryRow( $result->ts_tags, 'newpages' );
366 $classes = array_merge( $classes, $newClasses );
367 } else {
368 $tagDisplay = '';
371 $css = count( $classes ) ? ' class="' . implode( ' ', $classes ) . '"' : '';
373 # Display the old title if the namespace/title has been changed
374 $oldTitleText = '';
375 $oldTitle = Title::makeTitle( $result->rc_namespace, $result->rc_title );
376 if ( !$title->equals( $oldTitle ) ) {
377 $oldTitleText = $this->msg( 'rc-old-title' )->params( $oldTitle->getPrefixedText() )->escaped();
380 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} {$dm}{$ulink} {$comment} {$tagDisplay} {$oldTitleText}</li>\n";
384 * Should a specific result row provide "patrollable" links?
386 * @param $result Result row
387 * @return Boolean
389 protected function patrollable( $result ) {
390 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled );
394 * Output a subscription feed listing recent edits to this page.
396 * @param $type String
398 protected function feed( $type ) {
399 global $wgFeed, $wgFeedClasses, $wgFeedLimit;
401 if ( !$wgFeed ) {
402 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
403 return;
406 if( !isset( $wgFeedClasses[$type] ) ) {
407 $this->getOutput()->addWikiMsg( 'feed-invalid' );
408 return;
411 $feed = new $wgFeedClasses[$type](
412 $this->feedTitle(),
413 $this->msg( 'tagline' )->text(),
414 $this->getTitle()->getFullUrl()
417 $pager = new NewPagesPager( $this, $this->opts );
418 $limit = $this->opts->getValue( 'limit' );
419 $pager->mLimit = min( $limit, $wgFeedLimit );
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 global $wgLanguageCode, $wgSitename;
432 $desc = $this->getDescription();
433 return "$wgSitename - $desc [$wgLanguageCode]";
436 protected function feedItem( $row ) {
437 $title = Title::makeTitle( intval( $row->rc_namespace ), $row->rc_title );
438 if( $title ) {
439 $date = $row->rc_timestamp;
440 $comments = $title->getTalkPage()->getFullURL();
442 return new FeedItem(
443 $title->getPrefixedText(),
444 $this->feedItemDesc( $row ),
445 $title->getFullURL(),
446 $date,
447 $this->feedItemAuthor( $row ),
448 $comments
450 } else {
451 return null;
455 protected function feedItemAuthor( $row ) {
456 return isset( $row->rc_user_text ) ? $row->rc_user_text : '';
459 protected function feedItemDesc( $row ) {
460 $revision = Revision::newFromId( $row->rev_id );
461 if( $revision ) {
462 return '<p>' . htmlspecialchars( $revision->getUserText() ) .
463 $this->msg( 'colon-separator' )->inContentLanguage()->escaped() .
464 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
465 "</p>\n<hr />\n<div>" .
466 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
468 return '';
473 * @ingroup SpecialPage Pager
475 class NewPagesPager extends ReverseChronologicalPager {
476 // Stored opts
477 protected $opts;
480 * @var HtmlForm
482 protected $mForm;
484 function __construct( $form, FormOptions $opts ) {
485 parent::__construct( $form->getContext() );
486 $this->mForm = $form;
487 $this->opts = $opts;
490 function getQueryInfo() {
491 global $wgEnableNewpagesUserFilter, $wgGroupPermissions;
492 $conds = array();
493 $conds['rc_new'] = 1;
495 $namespace = $this->opts->getValue( 'namespace' );
496 $namespace = ( $namespace === 'all' ) ? false : intval( $namespace );
498 $username = $this->opts->getValue( 'username' );
499 $user = Title::makeTitleSafe( NS_USER, $username );
501 if( $namespace !== false ) {
502 $conds['rc_namespace'] = $namespace;
503 $rcIndexes = array( 'new_name_timestamp' );
504 } else {
505 $rcIndexes = array( 'rc_timestamp' );
508 # $wgEnableNewpagesUserFilter - temp WMF hack
509 if( $wgEnableNewpagesUserFilter && $user ) {
510 $conds['rc_user_text'] = $user->getText();
511 $rcIndexes = 'rc_user_text';
512 # If anons cannot make new pages, don't "exclude logged in users"!
513 } elseif( $wgGroupPermissions['*']['createpage'] && $this->opts->getValue( 'hideliu' ) ) {
514 $conds['rc_user'] = 0;
516 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
517 if( $this->opts->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
518 $conds['rc_patrolled'] = 0;
520 if( $this->opts->getValue( 'hidebots' ) ) {
521 $conds['rc_bot'] = 0;
524 if ( $this->opts->getValue( 'hideredirs' ) ) {
525 $conds['page_is_redirect'] = 0;
528 // Allow changes to the New Pages query
529 $tables = array( 'recentchanges', 'page' );
530 $fields = array(
531 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
532 'rc_comment', 'rc_timestamp', 'rc_patrolled','rc_id', 'rc_deleted',
533 'page_len AS length', 'page_latest AS rev_id', 'ts_tags', 'rc_this_oldid',
534 'page_namespace', 'page_title'
536 $join_conds = array( 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ) );
538 wfRunHooks( 'SpecialNewpagesConditions',
539 array( &$this, $this->opts, &$conds, &$tables, &$fields, &$join_conds ) );
541 $info = array(
542 'tables' => $tables,
543 'fields' => $fields,
544 'conds' => $conds,
545 'options' => array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) ),
546 'join_conds' => $join_conds
549 // Empty array for fields, it'll be set by us anyway.
550 $fields = array();
552 // Modify query for tags
553 ChangeTags::modifyDisplayQuery(
554 $info['tables'],
555 $fields,
556 $info['conds'],
557 $info['join_conds'],
558 $info['options'],
559 $this->opts['tagfilter']
562 return $info;
565 function getIndexField() {
566 return 'rc_timestamp';
569 function formatRow( $row ) {
570 return $this->mForm->formatRow( $row );
573 function getStartBody() {
574 # Do a batch existence check on pages
575 $linkBatch = new LinkBatch();
576 foreach ( $this->mResult as $row ) {
577 $linkBatch->add( NS_USER, $row->rc_user_text );
578 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
579 $linkBatch->add( $row->rc_namespace, $row->rc_title );
581 $linkBatch->execute();
582 return '<ul>';
585 function getEndBody() {
586 return '</ul>';