Standardised file description headers; first path
[mediawiki.git] / includes / specials / SpecialNewpages.php
blob6c261fd3368d7f5494889ac245730f6a2c2c9c8b
1 <?php
2 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
20 /**
21 * implements Special:Newpages
22 * @ingroup SpecialPage
24 class SpecialNewpages extends IncludableSpecialPage {
26 // Stored objects
27 protected $opts, $skin;
29 // Some internal settings
30 protected $showNavigation = false;
32 public function __construct() {
33 parent::__construct( 'Newpages' );
36 protected function setup( $par ) {
37 global $wgRequest, $wgUser, $wgEnableNewpagesUserFilter;
39 // Options
40 $opts = new FormOptions();
41 $this->opts = $opts; // bind
42 $opts->add( 'hideliu', false );
43 $opts->add( 'hidepatrolled', $wgUser->getBoolOption( 'newpageshidepatrolled' ) );
44 $opts->add( 'hidebots', false );
45 $opts->add( 'hideredirs', true );
46 $opts->add( 'limit', (int)$wgUser->getOption( 'rclimit' ) );
47 $opts->add( 'offset', '' );
48 $opts->add( 'namespace', '0' );
49 $opts->add( 'username', '' );
50 $opts->add( 'feed', '' );
51 $opts->add( 'tagfilter', '' );
53 // Set values
54 $opts->fetchValuesFromRequest( $wgRequest );
55 if ( $par ) $this->parseParams( $par );
57 // Validate
58 $opts->validateIntBounds( 'limit', 0, 5000 );
59 if( !$wgEnableNewpagesUserFilter ) {
60 $opts->setValue( 'username', '' );
63 // Store some objects
64 $this->skin = $wgUser->getSkin();
67 protected function parseParams( $par ) {
68 global $wgLang;
69 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
70 foreach ( $bits as $bit ) {
71 if ( 'shownav' == $bit )
72 $this->showNavigation = true;
73 if ( 'hideliu' === $bit )
74 $this->opts->setValue( 'hideliu', true );
75 if ( 'hidepatrolled' == $bit )
76 $this->opts->setValue( 'hidepatrolled', true );
77 if ( 'hidebots' == $bit )
78 $this->opts->setValue( 'hidebots', true );
79 if ( 'showredirs' == $bit )
80 $this->opts->setValue( 'hideredirs', false );
81 if ( is_numeric( $bit ) )
82 $this->opts->setValue( 'limit', intval( $bit ) );
84 $m = array();
85 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) )
86 $this->opts->setValue( 'limit', intval($m[1]) );
87 // PG offsets not just digits!
88 if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) )
89 $this->opts->setValue( 'offset', intval($m[1]) );
90 if ( preg_match( '/^username=(.*)$/', $bit, $m ) )
91 $this->opts->setValue( 'username', $m[1] );
92 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
93 $ns = $wgLang->getNsIndex( $m[1] );
94 if( $ns !== false ) {
95 $this->opts->setValue( 'namespace', $ns );
102 * Show a form for filtering namespace and username
104 * @param $par String
105 * @return String
107 public function execute( $par ) {
108 global $wgOut;
110 $this->setHeaders();
111 $this->outputHeader();
113 $this->showNavigation = !$this->including(); // Maybe changed in setup
114 $this->setup( $par );
116 if( !$this->including() ) {
117 // Settings
118 $this->form();
120 $this->setSyndicated();
121 $feedType = $this->opts->getValue( 'feed' );
122 if( $feedType ) {
123 return $this->feed( $feedType );
127 $pager = new NewPagesPager( $this, $this->opts );
128 $pager->mLimit = $this->opts->getValue( 'limit' );
129 $pager->mOffset = $this->opts->getValue( 'offset' );
131 if( $pager->getNumRows() ) {
132 $navigation = '';
133 if ( $this->showNavigation ) $navigation = $pager->getNavigationBar();
134 $wgOut->addHTML( $navigation . $pager->getBody() . $navigation );
135 } else {
136 $wgOut->addWikiMsg( 'specialpage-empty' );
140 protected function filterLinks() {
141 global $wgGroupPermissions, $wgUser, $wgLang;
143 // show/hide links
144 $showhide = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ) );
146 // Option value -> message mapping
147 $filters = array(
148 'hideliu' => 'rcshowhideliu',
149 'hidepatrolled' => 'rcshowhidepatr',
150 'hidebots' => 'rcshowhidebots',
151 'hideredirs' => 'whatlinkshere-hideredirs'
154 // Disable some if needed
155 # FIXME: throws E_NOTICEs if not set; and doesn't obey hooks etc
156 if ( $wgGroupPermissions['*']['createpage'] !== true )
157 unset($filters['hideliu']);
159 if ( !$wgUser->useNPPatrol() )
160 unset($filters['hidepatrolled']);
162 $links = array();
163 $changed = $this->opts->getChangedValues();
164 unset($changed['offset']); // Reset offset if query type changes
166 $self = $this->getTitle();
167 foreach ( $filters as $key => $msg ) {
168 $onoff = 1 - $this->opts->getValue($key);
169 $link = $this->skin->link( $self, $showhide[$onoff], array(),
170 array( $key => $onoff ) + $changed
172 $links[$key] = wfMsgHtml( $msg, $link );
175 return $wgLang->pipeList( $links );
178 protected function form() {
179 global $wgOut, $wgEnableNewpagesUserFilter, $wgScript;
181 // Consume values
182 $this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW
183 $namespace = $this->opts->consumeValue( 'namespace' );
184 $username = $this->opts->consumeValue( 'username' );
185 $tagFilterVal = $this->opts->consumeValue( 'tagfilter' );
187 // Check username input validity
188 $ut = Title::makeTitleSafe( NS_USER, $username );
189 $userText = $ut ? $ut->getText() : '';
191 // Store query values in hidden fields so that form submission doesn't lose them
192 $hidden = array();
193 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
194 $hidden[] = Xml::hidden( $key, $value );
196 $hidden = implode( "\n", $hidden );
198 $tagFilter = ChangeTags::buildTagFilterSelector( $tagFilterVal );
199 if ($tagFilter)
200 list( $tagFilterLabel, $tagFilterSelector ) = $tagFilter;
202 $form = Xml::openElement( 'form', array( 'action' => $wgScript ) ) .
203 Xml::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
204 Xml::fieldset( wfMsg( 'newpages' ) ) .
205 Xml::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
206 "<tr>
207 <td class='mw-label'>" .
208 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
209 "</td>
210 <td class='mw-input'>" .
211 Xml::namespaceSelector( $namespace, 'all' ) .
212 "</td>
213 </tr>" . ( $tagFilter ? (
214 "<tr>
215 <td class='mw-label'>" .
216 $tagFilterLabel .
217 "</td>
218 <td class='mw-input'>" .
219 $tagFilterSelector .
220 "</td>
221 </tr>" ) : '' ) .
222 ($wgEnableNewpagesUserFilter ?
223 "<tr>
224 <td class='mw-label'>" .
225 Xml::label( wfMsg( 'newpages-username' ), 'mw-np-username' ) .
226 "</td>
227 <td class='mw-input'>" .
228 Xml::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
229 "</td>
230 </tr>" : "" ) .
231 "<tr> <td></td>
232 <td class='mw-submit'>" .
233 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
234 "</td>
235 </tr>" .
236 "<tr>
237 <td></td>
238 <td class='mw-input'>" .
239 $this->filterLinks() .
240 "</td>
241 </tr>" .
242 Xml::closeElement( 'table' ) .
243 Xml::closeElement( 'fieldset' ) .
244 $hidden .
245 Xml::closeElement( 'form' );
247 $wgOut->addHTML( $form );
250 protected function setSyndicated() {
251 global $wgOut;
252 $wgOut->setSyndicated( true );
253 $wgOut->setFeedAppendQuery( wfArrayToCGI( $this->opts->getAllValues() ) );
257 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
259 * @param $result Result row
260 * @return String
262 public function formatRow( $result ) {
263 global $wgLang, $wgContLang;
265 $classes = array();
267 $dm = $wgContLang->getDirMark();
269 $title = Title::makeTitleSafe( $result->rc_namespace, $result->rc_title );
270 $time = Html::element( 'span', array( 'class' => 'mw-newpages-time' ),
271 $wgLang->timeAndDate( $result->rc_timestamp, true )
274 $query = array( 'redirect' => 'no' );
276 if( $this->patrollable( $result ) )
277 $query['rcid'] = $result->rc_id;
279 $plink = $this->skin->linkKnown(
280 $title,
281 null,
282 array( 'class' => 'mw-newpages-pagename' ),
283 $query,
284 array( 'known' ) // Set explicitly to avoid the default of 'known','noclasses'. This breaks the colouration for stubs
286 $histLink = $this->skin->linkKnown(
287 $title,
288 wfMsgHtml( 'hist' ),
289 array(),
290 array( 'action' => 'history' )
292 $hist = Html::rawElement( 'span', array( 'class' => 'mw-newpages-history' ), wfMsg( 'parentheses', $histLink ) );
294 $length = Html::rawElement( 'span', array( 'class' => 'mw-newpages-length' ),
295 '[' . wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ), $wgLang->formatNum( $result->length ) ) .
298 $ulink = $this->skin->userLink( $result->rc_user, $result->rc_user_text ) . ' ' .
299 $this->skin->userToolLinks( $result->rc_user, $result->rc_user_text );
300 $comment = $this->skin->commentBlock( $result->rc_comment );
302 if ( $this->patrollable( $result ) ) {
303 $classes[] = 'not-patrolled';
306 # Add a class for zero byte pages
307 if ( $result->length == 0 ) {
308 $classes[] = 'mw-newpages-zero-byte-page';
311 # Tags, if any. check for including due to bug 23293
312 if ( !$this->including() ) {
313 list( $tagDisplay, $newClasses ) = ChangeTags::formatSummaryRow( $result->ts_tags, 'newpages' );
314 $classes = array_merge( $classes, $newClasses );
315 } else {
316 $tagDisplay = '';
319 $css = count($classes) ? ' class="'.implode( " ", $classes).'"' : '';
321 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} {$dm}{$ulink} {$comment} {$tagDisplay}</li>\n";
325 * Should a specific result row provide "patrollable" links?
327 * @param $result Result row
328 * @return Boolean
330 protected function patrollable( $result ) {
331 global $wgUser;
332 return ( $wgUser->useNPPatrol() && !$result->rc_patrolled );
336 * Output a subscription feed listing recent edits to this page.
338 * @param $type String
340 protected function feed( $type ) {
341 global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgOut;
343 if ( !$wgFeed ) {
344 $wgOut->addWikiMsg( 'feed-unavailable' );
345 return;
348 if( !isset( $wgFeedClasses[$type] ) ) {
349 $wgOut->addWikiMsg( 'feed-invalid' );
350 return;
353 $feed = new $wgFeedClasses[$type](
354 $this->feedTitle(),
355 wfMsgExt( 'tagline', 'parsemag' ),
356 $this->getTitle()->getFullUrl() );
358 $pager = new NewPagesPager( $this, $this->opts );
359 $limit = $this->opts->getValue( 'limit' );
360 $pager->mLimit = min( $limit, $wgFeedLimit );
362 $feed->outHeader();
363 if( $pager->getNumRows() > 0 ) {
364 while( $row = $pager->mResult->fetchObject() ) {
365 $feed->outItem( $this->feedItem( $row ) );
368 $feed->outFooter();
371 protected function feedTitle() {
372 global $wgContLanguageCode, $wgSitename;
373 $page = SpecialPage::getPage( 'Newpages' );
374 $desc = $page->getDescription();
375 return "$wgSitename - $desc [$wgContLanguageCode]";
378 protected function feedItem( $row ) {
379 $title = Title::MakeTitle( intval( $row->rc_namespace ), $row->rc_title );
380 if( $title ) {
381 $date = $row->rc_timestamp;
382 $comments = $title->getTalkPage()->getFullURL();
384 return new FeedItem(
385 $title->getPrefixedText(),
386 $this->feedItemDesc( $row ),
387 $title->getFullURL(),
388 $date,
389 $this->feedItemAuthor( $row ),
390 $comments);
391 } else {
392 return null;
396 protected function feedItemAuthor( $row ) {
397 return isset( $row->rc_user_text ) ? $row->rc_user_text : '';
400 protected function feedItemDesc( $row ) {
401 $revision = Revision::newFromId( $row->rev_id );
402 if( $revision ) {
403 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
404 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
405 "</p>\n<hr />\n<div>" .
406 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
408 return '';
413 * @ingroup SpecialPage Pager
415 class NewPagesPager extends ReverseChronologicalPager {
416 // Stored opts
417 protected $opts, $mForm;
419 function __construct( $form, FormOptions $opts ) {
420 parent::__construct();
421 $this->mForm = $form;
422 $this->opts = $opts;
425 function getTitle() {
426 static $title = null;
427 if ( $title === null )
428 $title = $this->mForm->getTitle();
429 return $title;
432 function getQueryInfo() {
433 global $wgEnableNewpagesUserFilter, $wgGroupPermissions, $wgUser;
434 $conds = array();
435 $conds['rc_new'] = 1;
437 $namespace = $this->opts->getValue( 'namespace' );
438 $namespace = ( $namespace === 'all' ) ? false : intval( $namespace );
440 $username = $this->opts->getValue( 'username' );
441 $user = Title::makeTitleSafe( NS_USER, $username );
443 if( $namespace !== false ) {
444 $conds['rc_namespace'] = $namespace;
445 $rcIndexes = array( 'new_name_timestamp' );
446 } else {
447 $rcIndexes = array( 'rc_timestamp' );
450 # $wgEnableNewpagesUserFilter - temp WMF hack
451 if( $wgEnableNewpagesUserFilter && $user ) {
452 $conds['rc_user_text'] = $user->getText();
453 $rcIndexes = 'rc_user_text';
454 # If anons cannot make new pages, don't "exclude logged in users"!
455 } elseif( $wgGroupPermissions['*']['createpage'] && $this->opts->getValue( 'hideliu' ) ) {
456 $conds['rc_user'] = 0;
458 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
459 if( $this->opts->getValue( 'hidepatrolled' ) && $wgUser->useNPPatrol() ) {
460 $conds['rc_patrolled'] = 0;
462 if( $this->opts->getValue( 'hidebots' ) ) {
463 $conds['rc_bot'] = 0;
466 if ( $this->opts->getValue( 'hideredirs' ) ) {
467 $conds['page_is_redirect'] = 0;
470 // Allow changes to the New Pages query
471 wfRunHooks('SpecialNewpagesConditions', array(&$this, $this->opts, &$conds));
473 $info = array(
474 'tables' => array( 'recentchanges', 'page' ),
475 'fields' => 'rc_namespace,rc_title, rc_cur_id, rc_user,rc_user_text,rc_comment,
476 rc_timestamp,rc_patrolled,rc_id,page_len as length, page_latest as rev_id, ts_tags',
477 'conds' => $conds,
478 'options' => array( 'USE INDEX' => array('recentchanges' => $rcIndexes) ),
479 'join_conds' => array(
480 'page' => array('INNER JOIN', 'page_id=rc_cur_id'),
484 ## Empty array for fields, it'll be set by us anyway.
485 $fields = array();
487 ## Modify query for tags
488 ChangeTags::modifyDisplayQuery( $info['tables'],
489 $fields,
490 $info['conds'],
491 $info['join_conds'],
492 $info['options'],
493 $this->opts['tagfilter'] );
495 return $info;
498 function getIndexField() {
499 return 'rc_timestamp';
502 function formatRow( $row ) {
503 return $this->mForm->formatRow( $row );
506 function getStartBody() {
507 # Do a batch existence check on pages
508 $linkBatch = new LinkBatch();
509 while( $row = $this->mResult->fetchObject() ) {
510 $linkBatch->add( NS_USER, $row->rc_user_text );
511 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
512 $linkBatch->add( $row->rc_namespace, $row->rc_title );
514 $linkBatch->execute();
515 return "<ul>";
518 function getEndBody() {
519 return "</ul>";