MWException: Don't send headers multiple times
[mediawiki.git] / includes / specials / SpecialNewpages.php
blob505a1ecc46c55858457e1671276887bbdbaa7543
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 global $wgEnableNewpagesUserFilter;
45 // Options
46 $opts = new FormOptions();
47 $this->opts = $opts; // bind
48 $opts->add( 'hideliu', false );
49 $opts->add( 'hidepatrolled', $this->getUser()->getBoolOption( 'newpageshidepatrolled' ) );
50 $opts->add( 'hidebots', false );
51 $opts->add( 'hideredirs', true );
52 $opts->add( 'limit', $this->getUser()->getIntOption( 'rclimit' ) );
53 $opts->add( 'offset', '' );
54 $opts->add( 'namespace', '0' );
55 $opts->add( 'username', '' );
56 $opts->add( 'feed', '' );
57 $opts->add( 'tagfilter', '' );
58 $opts->add( 'invert', false );
60 $this->customFilters = array();
61 wfRunHooks( 'SpecialNewPagesFilters', array( $this, &$this->customFilters ) );
62 foreach ( $this->customFilters as $key => $params ) {
63 $opts->add( $key, $params['default'] );
66 // Set values
67 $opts->fetchValuesFromRequest( $this->getRequest() );
68 if ( $par ) {
69 $this->parseParams( $par );
72 // Validate
73 $opts->validateIntBounds( 'limit', 0, 5000 );
74 if ( !$wgEnableNewpagesUserFilter ) {
75 $opts->setValue( 'username', '' );
79 protected function parseParams( $par ) {
80 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
81 foreach ( $bits as $bit ) {
82 if ( 'shownav' == $bit ) {
83 $this->showNavigation = true;
85 if ( 'hideliu' === $bit ) {
86 $this->opts->setValue( 'hideliu', true );
88 if ( 'hidepatrolled' == $bit ) {
89 $this->opts->setValue( 'hidepatrolled', true );
91 if ( 'hidebots' == $bit ) {
92 $this->opts->setValue( 'hidebots', true );
94 if ( 'showredirs' == $bit ) {
95 $this->opts->setValue( 'hideredirs', false );
97 if ( is_numeric( $bit ) ) {
98 $this->opts->setValue( 'limit', intval( $bit ) );
101 $m = array();
102 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
103 $this->opts->setValue( 'limit', intval( $m[1] ) );
105 // PG offsets not just digits!
106 if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) ) {
107 $this->opts->setValue( 'offset', intval( $m[1] ) );
109 if ( preg_match( '/^username=(.*)$/', $bit, $m ) ) {
110 $this->opts->setValue( 'username', $m[1] );
112 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
113 $ns = $this->getLanguage()->getNsIndex( $m[1] );
114 if ( $ns !== false ) {
115 $this->opts->setValue( 'namespace', $ns );
122 * Show a form for filtering namespace and username
124 * @param string $par
125 * @return string
127 public function execute( $par ) {
128 $out = $this->getOutput();
130 $this->setHeaders();
131 $this->outputHeader();
133 $this->showNavigation = !$this->including(); // Maybe changed in setup
134 $this->setup( $par );
136 if ( !$this->including() ) {
137 // Settings
138 $this->form();
140 $feedType = $this->opts->getValue( 'feed' );
141 if ( $feedType ) {
142 $this->feed( $feedType );
144 return;
147 $allValues = $this->opts->getAllValues();
148 unset( $allValues['feed'] );
149 $out->setFeedAppendQuery( wfArrayToCgi( $allValues ) );
152 $pager = new NewPagesPager( $this, $this->opts );
153 $pager->mLimit = $this->opts->getValue( 'limit' );
154 $pager->mOffset = $this->opts->getValue( 'offset' );
156 if ( $pager->getNumRows() ) {
157 $navigation = '';
158 if ( $this->showNavigation ) {
159 $navigation = $pager->getNavigationBar();
161 $out->addHTML( $navigation . $pager->getBody() . $navigation );
162 } else {
163 $out->addWikiMsg( 'specialpage-empty' );
167 protected function filterLinks() {
168 // show/hide links
169 $showhide = array( $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() );
171 // Option value -> message mapping
172 $filters = array(
173 'hideliu' => 'rcshowhideliu',
174 'hidepatrolled' => 'rcshowhidepatr',
175 'hidebots' => 'rcshowhidebots',
176 'hideredirs' => 'whatlinkshere-hideredirs'
178 foreach ( $this->customFilters as $key => $params ) {
179 $filters[$key] = $params['msg'];
182 // Disable some if needed
183 if ( !User::groupHasPermission( '*', 'createpage' ) ) {
184 unset( $filters['hideliu'] );
186 if ( !$this->getUser()->useNPPatrol() ) {
187 unset( $filters['hidepatrolled'] );
190 $links = array();
191 $changed = $this->opts->getChangedValues();
192 unset( $changed['offset'] ); // Reset offset if query type changes
194 $self = $this->getPageTitle();
195 foreach ( $filters as $key => $msg ) {
196 $onoff = 1 - $this->opts->getValue( $key );
197 $link = Linker::link( $self, $showhide[$onoff], array(),
198 array( $key => $onoff ) + $changed
200 $links[$key] = $this->msg( $msg )->rawParams( $link )->escaped();
203 return $this->getLanguage()->pipeList( $links );
206 protected function form() {
207 global $wgEnableNewpagesUserFilter, $wgScript;
209 // Consume values
210 $this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW
211 $namespace = $this->opts->consumeValue( 'namespace' );
212 $username = $this->opts->consumeValue( 'username' );
213 $tagFilterVal = $this->opts->consumeValue( 'tagfilter' );
214 $nsinvert = $this->opts->consumeValue( 'invert' );
216 // Check username input validity
217 $ut = Title::makeTitleSafe( NS_USER, $username );
218 $userText = $ut ? $ut->getText() : '';
220 // Store query values in hidden fields so that form submission doesn't lose them
221 $hidden = array();
222 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
223 $hidden[] = Html::hidden( $key, $value );
225 $hidden = implode( "\n", $hidden );
227 $tagFilter = ChangeTags::buildTagFilterSelector( $tagFilterVal );
228 if ( $tagFilter ) {
229 list( $tagFilterLabel, $tagFilterSelector ) = $tagFilter;
232 $form = Xml::openElement( 'form', array( 'action' => $wgScript ) ) .
233 Html::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
234 Xml::fieldset( $this->msg( 'newpages' )->text() ) .
235 Xml::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
236 '<tr>
237 <td class="mw-label">' .
238 Xml::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
239 '</td>
240 <td class="mw-input">' .
241 Html::namespaceSelector(
242 array(
243 'selected' => $namespace,
244 'all' => 'all',
245 ), array(
246 'name' => 'namespace',
247 'id' => 'namespace',
248 'class' => 'namespaceselector',
250 ) . '&#160;' .
251 Xml::checkLabel(
252 $this->msg( 'invert' )->text(),
253 'invert',
254 'nsinvert',
255 $nsinvert,
256 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
258 '</td>
259 </tr>' . ( $tagFilter ? (
260 '<tr>
261 <td class="mw-label">' .
262 $tagFilterLabel .
263 '</td>
264 <td class="mw-input">' .
265 $tagFilterSelector .
266 '</td>
267 </tr>' ) : '' ) .
268 ( $wgEnableNewpagesUserFilter ?
269 '<tr>
270 <td class="mw-label">' .
271 Xml::label( $this->msg( 'newpages-username' )->text(), 'mw-np-username' ) .
272 '</td>
273 <td class="mw-input">' .
274 Xml::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
275 '</td>
276 </tr>' : '' ) .
277 '<tr> <td></td>
278 <td class="mw-submit">' .
279 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
280 '</td>
281 </tr>' .
282 '<tr>
283 <td></td>
284 <td class="mw-input">' .
285 $this->filterLinks() .
286 '</td>
287 </tr>' .
288 Xml::closeElement( 'table' ) .
289 Xml::closeElement( 'fieldset' ) .
290 $hidden .
291 Xml::closeElement( 'form' );
293 $this->getOutput()->addHTML( $form );
297 * Format a row, providing the timestamp, links to the page/history,
298 * size, user links, and a comment
300 * @param object $result Result row
301 * @return string
303 public function formatRow( $result ) {
304 $title = Title::newFromRow( $result );
306 # Revision deletion works on revisions, so we should cast one
307 $row = array(
308 'comment' => $result->rc_comment,
309 'deleted' => $result->rc_deleted,
310 'user_text' => $result->rc_user_text,
311 'user' => $result->rc_user,
313 $rev = new Revision( $row );
314 $rev->setTitle( $title );
316 $classes = array();
318 $lang = $this->getLanguage();
319 $dm = $lang->getDirMark();
321 $spanTime = Html::element( 'span', array( 'class' => 'mw-newpages-time' ),
322 $lang->userTimeAndDate( $result->rc_timestamp, $this->getUser() )
324 $time = Linker::linkKnown(
325 $title,
326 $spanTime,
327 array(),
328 array( 'oldid' => $result->rc_this_oldid ),
329 array()
332 $query = array( 'redirect' => 'no' );
334 // Linker::linkKnown() uses 'known' and 'noclasses' options.
335 // This breaks the colouration for stubs.
336 $plink = Linker::link(
337 $title,
338 null,
339 array( 'class' => 'mw-newpages-pagename' ),
340 $query,
341 array( 'known' )
343 $histLink = Linker::linkKnown(
344 $title,
345 $this->msg( 'hist' )->escaped(),
346 array(),
347 array( 'action' => 'history' )
349 $hist = Html::rawElement( 'span', array( 'class' => 'mw-newpages-history' ),
350 $this->msg( 'parentheses' )->rawParams( $histLink )->escaped() );
352 $length = Html::element(
353 'span',
354 array( 'class' => 'mw-newpages-length' ),
355 $this->msg( 'brackets' )->params( $this->msg( 'nbytes' )
356 ->numParams( $result->length )->text()
360 $ulink = Linker::revUserTools( $rev );
361 $comment = Linker::revComment( $rev );
363 if ( $this->patrollable( $result ) ) {
364 $classes[] = 'not-patrolled';
367 # Add a class for zero byte pages
368 if ( $result->length == 0 ) {
369 $classes[] = 'mw-newpages-zero-byte-page';
372 # Tags, if any.
373 if ( isset( $result->ts_tags ) ) {
374 list( $tagDisplay, $newClasses ) = ChangeTags::formatSummaryRow(
375 $result->ts_tags,
376 'newpages'
378 $classes = array_merge( $classes, $newClasses );
379 } else {
380 $tagDisplay = '';
383 $css = count( $classes ) ? ' class="' . implode( ' ', $classes ) . '"' : '';
385 # Display the old title if the namespace/title has been changed
386 $oldTitleText = '';
387 $oldTitle = Title::makeTitle( $result->rc_namespace, $result->rc_title );
389 if ( !$title->equals( $oldTitle ) ) {
390 $oldTitleText = $oldTitle->getPrefixedText();
391 $oldTitleText = $this->msg( 'rc-old-title' )->params( $oldTitleText )->escaped();
394 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} "
395 . "{$dm}{$ulink} {$comment} {$tagDisplay} {$oldTitleText}</li>\n";
399 * Should a specific result row provide "patrollable" links?
401 * @param object $result Result row
402 * @return bool
404 protected function patrollable( $result ) {
405 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled );
409 * Output a subscription feed listing recent edits to this page.
411 * @param string $type
413 protected function feed( $type ) {
414 global $wgFeed, $wgFeedClasses, $wgFeedLimit;
416 if ( !$wgFeed ) {
417 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
419 return;
422 if ( !isset( $wgFeedClasses[$type] ) ) {
423 $this->getOutput()->addWikiMsg( 'feed-invalid' );
425 return;
428 $feed = new $wgFeedClasses[$type](
429 $this->feedTitle(),
430 $this->msg( 'tagline' )->text(),
431 $this->getPageTitle()->getFullURL()
434 $pager = new NewPagesPager( $this, $this->opts );
435 $limit = $this->opts->getValue( 'limit' );
436 $pager->mLimit = min( $limit, $wgFeedLimit );
438 $feed->outHeader();
439 if ( $pager->getNumRows() > 0 ) {
440 foreach ( $pager->mResult as $row ) {
441 $feed->outItem( $this->feedItem( $row ) );
444 $feed->outFooter();
447 protected function feedTitle() {
448 global $wgLanguageCode, $wgSitename;
449 $desc = $this->getDescription();
451 return "$wgSitename - $desc [$wgLanguageCode]";
454 protected function feedItem( $row ) {
455 $title = Title::makeTitle( intval( $row->rc_namespace ), $row->rc_title );
456 if ( $title ) {
457 $date = $row->rc_timestamp;
458 $comments = $title->getTalkPage()->getFullURL();
460 return new FeedItem(
461 $title->getPrefixedText(),
462 $this->feedItemDesc( $row ),
463 $title->getFullURL(),
464 $date,
465 $this->feedItemAuthor( $row ),
466 $comments
468 } else {
469 return null;
473 protected function feedItemAuthor( $row ) {
474 return isset( $row->rc_user_text ) ? $row->rc_user_text : '';
477 protected function feedItemDesc( $row ) {
478 $revision = Revision::newFromId( $row->rev_id );
479 if ( $revision ) {
480 //XXX: include content model/type in feed item?
481 return '<p>' . htmlspecialchars( $revision->getUserText() ) .
482 $this->msg( 'colon-separator' )->inContentLanguage()->escaped() .
483 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
484 "</p>\n<hr />\n<div>" .
485 nl2br( htmlspecialchars( $revision->getContent()->serialize() ) ) . "</div>";
488 return '';
491 protected function getGroupName() {
492 return 'changes';
497 * @ingroup SpecialPage Pager
499 class NewPagesPager extends ReverseChronologicalPager {
500 // Stored opts
501 protected $opts;
504 * @var HtmlForm
506 protected $mForm;
508 function __construct( $form, FormOptions $opts ) {
509 parent::__construct( $form->getContext() );
510 $this->mForm = $form;
511 $this->opts = $opts;
514 function getQueryInfo() {
515 global $wgEnableNewpagesUserFilter;
516 $conds = array();
517 $conds['rc_new'] = 1;
519 $namespace = $this->opts->getValue( 'namespace' );
520 $namespace = ( $namespace === 'all' ) ? false : intval( $namespace );
522 $username = $this->opts->getValue( 'username' );
523 $user = Title::makeTitleSafe( NS_USER, $username );
525 $rcIndexes = array();
527 if ( $namespace !== false ) {
528 if ( $this->opts->getValue( 'invert' ) ) {
529 $conds[] = 'rc_namespace != ' . $this->mDb->addQuotes( $namespace );
530 } else {
531 $conds['rc_namespace'] = $namespace;
535 # $wgEnableNewpagesUserFilter - temp WMF hack
536 if ( $wgEnableNewpagesUserFilter && $user ) {
537 $conds['rc_user_text'] = $user->getText();
538 $rcIndexes = 'rc_user_text';
539 } elseif ( User::groupHasPermission( '*', 'createpage' ) &&
540 $this->opts->getValue( 'hideliu' )
542 # If anons cannot make new pages, don't "exclude logged in users"!
543 $conds['rc_user'] = 0;
546 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
547 if ( $this->opts->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
548 $conds['rc_patrolled'] = 0;
551 if ( $this->opts->getValue( 'hidebots' ) ) {
552 $conds['rc_bot'] = 0;
555 if ( $this->opts->getValue( 'hideredirs' ) ) {
556 $conds['page_is_redirect'] = 0;
559 // Allow changes to the New Pages query
560 $tables = array( 'recentchanges', 'page' );
561 $fields = array(
562 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
563 'rc_comment', 'rc_timestamp', 'rc_patrolled', 'rc_id', 'rc_deleted',
564 'length' => 'page_len', 'rev_id' => 'page_latest', 'rc_this_oldid',
565 'page_namespace', 'page_title'
567 $join_conds = array( 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ) );
569 wfRunHooks( 'SpecialNewpagesConditions',
570 array( &$this, $this->opts, &$conds, &$tables, &$fields, &$join_conds ) );
572 $options = array();
574 if ( $rcIndexes ) {
575 $options = array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) );
578 $info = array(
579 'tables' => $tables,
580 'fields' => $fields,
581 'conds' => $conds,
582 'options' => $options,
583 'join_conds' => $join_conds
586 // Modify query for tags
587 ChangeTags::modifyDisplayQuery(
588 $info['tables'],
589 $info['fields'],
590 $info['conds'],
591 $info['join_conds'],
592 $info['options'],
593 $this->opts['tagfilter']
596 return $info;
599 function getIndexField() {
600 return 'rc_timestamp';
603 function formatRow( $row ) {
604 return $this->mForm->formatRow( $row );
607 function getStartBody() {
608 # Do a batch existence check on pages
609 $linkBatch = new LinkBatch();
610 foreach ( $this->mResult as $row ) {
611 $linkBatch->add( NS_USER, $row->rc_user_text );
612 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
613 $linkBatch->add( $row->rc_namespace, $row->rc_title );
615 $linkBatch->execute();
617 return '<ul>';
620 function getEndBody() {
621 return '</ul>';