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
21 * @ingroup SpecialPage
25 * A special page that list newly created pages
27 * @ingroup SpecialPage
29 class SpecialNewpages
extends IncludableSpecialPage
{
32 protected $opts, $skin;
34 // Some internal settings
35 protected $showNavigation = false;
37 public function __construct() {
38 parent
::__construct( 'Newpages' );
41 protected function setup( $par ) {
42 global $wgRequest, $wgUser, $wgEnableNewpagesUserFilter;
45 $opts = new FormOptions();
46 $this->opts
= $opts; // bind
47 $opts->add( 'hideliu', false );
48 $opts->add( 'hidepatrolled', $wgUser->getBoolOption( 'newpageshidepatrolled' ) );
49 $opts->add( 'hidebots', false );
50 $opts->add( 'hideredirs', true );
51 $opts->add( 'limit', (int)$wgUser->getOption( 'rclimit' ) );
52 $opts->add( 'offset', '' );
53 $opts->add( 'namespace', '0' );
54 $opts->add( 'username', '' );
55 $opts->add( 'feed', '' );
56 $opts->add( 'tagfilter', '' );
59 $opts->fetchValuesFromRequest( $wgRequest );
60 if ( $par ) $this->parseParams( $par );
63 $opts->validateIntBounds( 'limit', 0, 5000 );
64 if( !$wgEnableNewpagesUserFilter ) {
65 $opts->setValue( 'username', '' );
69 $this->skin
= $wgUser->getSkin();
72 protected function parseParams( $par ) {
74 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
75 foreach ( $bits as $bit ) {
76 if ( 'shownav' == $bit )
77 $this->showNavigation
= true;
78 if ( 'hideliu' === $bit )
79 $this->opts
->setValue( 'hideliu', true );
80 if ( 'hidepatrolled' == $bit )
81 $this->opts
->setValue( 'hidepatrolled', true );
82 if ( 'hidebots' == $bit )
83 $this->opts
->setValue( 'hidebots', true );
84 if ( 'showredirs' == $bit )
85 $this->opts
->setValue( 'hideredirs', false );
86 if ( is_numeric( $bit ) )
87 $this->opts
->setValue( 'limit', intval( $bit ) );
90 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) )
91 $this->opts
->setValue( 'limit', intval($m[1]) );
92 // PG offsets not just digits!
93 if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) )
94 $this->opts
->setValue( 'offset', intval($m[1]) );
95 if ( preg_match( '/^username=(.*)$/', $bit, $m ) )
96 $this->opts
->setValue( 'username', $m[1] );
97 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
98 $ns = $wgLang->getNsIndex( $m[1] );
100 $this->opts
->setValue( 'namespace', $ns );
107 * Show a form for filtering namespace and username
112 public function execute( $par ) {
116 $this->outputHeader();
118 $this->showNavigation
= !$this->including(); // Maybe changed in setup
119 $this->setup( $par );
121 if( !$this->including() ) {
125 $this->setSyndicated();
126 $feedType = $this->opts
->getValue( 'feed' );
128 return $this->feed( $feedType );
132 $pager = new NewPagesPager( $this, $this->opts
);
133 $pager->mLimit
= $this->opts
->getValue( 'limit' );
134 $pager->mOffset
= $this->opts
->getValue( 'offset' );
136 if( $pager->getNumRows() ) {
138 if ( $this->showNavigation
) $navigation = $pager->getNavigationBar();
139 $wgOut->addHTML( $navigation . $pager->getBody() . $navigation );
141 $wgOut->addWikiMsg( 'specialpage-empty' );
145 protected function filterLinks() {
146 global $wgGroupPermissions, $wgUser, $wgLang;
149 $showhide = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ) );
151 // Option value -> message mapping
153 'hideliu' => 'rcshowhideliu',
154 'hidepatrolled' => 'rcshowhidepatr',
155 'hidebots' => 'rcshowhidebots',
156 'hideredirs' => 'whatlinkshere-hideredirs'
159 // Disable some if needed
160 # FIXME: throws E_NOTICEs if not set; and doesn't obey hooks etc
161 if ( $wgGroupPermissions['*']['createpage'] !== true )
162 unset($filters['hideliu']);
164 if ( !$wgUser->useNPPatrol() )
165 unset($filters['hidepatrolled']);
168 $changed = $this->opts
->getChangedValues();
169 unset($changed['offset']); // Reset offset if query type changes
171 $self = $this->getTitle();
172 foreach ( $filters as $key => $msg ) {
173 $onoff = 1 - $this->opts
->getValue($key);
174 $link = $this->skin
->link( $self, $showhide[$onoff], array(),
175 array( $key => $onoff ) +
$changed
177 $links[$key] = wfMsgHtml( $msg, $link );
180 return $wgLang->pipeList( $links );
183 protected function form() {
184 global $wgOut, $wgEnableNewpagesUserFilter, $wgScript;
187 $this->opts
->consumeValue( 'offset' ); // don't carry offset, DWIW
188 $namespace = $this->opts
->consumeValue( 'namespace' );
189 $username = $this->opts
->consumeValue( 'username' );
190 $tagFilterVal = $this->opts
->consumeValue( 'tagfilter' );
192 // Check username input validity
193 $ut = Title
::makeTitleSafe( NS_USER
, $username );
194 $userText = $ut ?
$ut->getText() : '';
196 // Store query values in hidden fields so that form submission doesn't lose them
198 foreach ( $this->opts
->getUnconsumedValues() as $key => $value ) {
199 $hidden[] = Html
::hidden( $key, $value );
201 $hidden = implode( "\n", $hidden );
203 $tagFilter = ChangeTags
::buildTagFilterSelector( $tagFilterVal );
205 list( $tagFilterLabel, $tagFilterSelector ) = $tagFilter;
207 $form = Xml
::openElement( 'form', array( 'action' => $wgScript ) ) .
208 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
209 Xml
::fieldset( wfMsg( 'newpages' ) ) .
210 Xml
::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
212 <td class='mw-label'>" .
213 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) .
215 <td class='mw-input'>" .
216 Xml
::namespaceSelector( $namespace, 'all' ) .
218 </tr>" . ( $tagFilter ?
(
220 <td class='mw-label'>" .
223 <td class='mw-input'>" .
227 ($wgEnableNewpagesUserFilter ?
229 <td class='mw-label'>" .
230 Xml
::label( wfMsg( 'newpages-username' ), 'mw-np-username' ) .
232 <td class='mw-input'>" .
233 Xml
::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
237 <td class='mw-submit'>" .
238 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) .
243 <td class='mw-input'>" .
244 $this->filterLinks() .
247 Xml
::closeElement( 'table' ) .
248 Xml
::closeElement( 'fieldset' ) .
250 Xml
::closeElement( 'form' );
252 $wgOut->addHTML( $form );
255 protected function setSyndicated() {
257 $wgOut->setSyndicated( true );
258 $wgOut->setFeedAppendQuery( wfArrayToCGI( $this->opts
->getAllValues() ) );
262 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
264 * @param $result Result row
267 public function formatRow( $result ) {
268 global $wgLang, $wgContLang;
272 $dm = $wgContLang->getDirMark();
274 $title = Title
::makeTitleSafe( $result->rc_namespace
, $result->rc_title
);
275 $time = Html
::element( 'span', array( 'class' => 'mw-newpages-time' ),
276 $wgLang->timeAndDate( $result->rc_timestamp
, true )
279 $query = array( 'redirect' => 'no' );
281 if( $this->patrollable( $result ) )
282 $query['rcid'] = $result->rc_id
;
284 $plink = $this->skin
->linkKnown(
287 array( 'class' => 'mw-newpages-pagename' ),
289 array( 'known' ) // Set explicitly to avoid the default of 'known','noclasses'. This breaks the colouration for stubs
291 $histLink = $this->skin
->linkKnown(
295 array( 'action' => 'history' )
297 $hist = Html
::rawElement( 'span', array( 'class' => 'mw-newpages-history' ), wfMsg( 'parentheses', $histLink ) );
299 $length = Html
::rawElement( 'span', array( 'class' => 'mw-newpages-length' ),
300 '[' . wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ), $wgLang->formatNum( $result->length
) ) .
303 $ulink = $this->skin
->userLink( $result->rc_user
, $result->rc_user_text
) . ' ' .
304 $this->skin
->userToolLinks( $result->rc_user
, $result->rc_user_text
);
305 $comment = $this->skin
->commentBlock( $result->rc_comment
);
307 if ( $this->patrollable( $result ) ) {
308 $classes[] = 'not-patrolled';
311 # Add a class for zero byte pages
312 if ( $result->length
== 0 ) {
313 $classes[] = 'mw-newpages-zero-byte-page';
316 # Tags, if any. check for including due to bug 23293
317 if ( !$this->including() ) {
318 list( $tagDisplay, $newClasses ) = ChangeTags
::formatSummaryRow( $result->ts_tags
, 'newpages' );
319 $classes = array_merge( $classes, $newClasses );
324 $css = count($classes) ?
' class="'.implode( " ", $classes).'"' : '';
326 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} {$dm}{$ulink} {$comment} {$tagDisplay}</li>\n";
330 * Should a specific result row provide "patrollable" links?
332 * @param $result Result row
335 protected function patrollable( $result ) {
337 return ( $wgUser->useNPPatrol() && !$result->rc_patrolled
);
341 * Output a subscription feed listing recent edits to this page.
343 * @param $type String
345 protected function feed( $type ) {
346 global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgOut;
349 $wgOut->addWikiMsg( 'feed-unavailable' );
353 if( !isset( $wgFeedClasses[$type] ) ) {
354 $wgOut->addWikiMsg( 'feed-invalid' );
358 $feed = new $wgFeedClasses[$type](
360 wfMsgExt( 'tagline', 'parsemag' ),
361 $this->getTitle()->getFullUrl() );
363 $pager = new NewPagesPager( $this, $this->opts
);
364 $limit = $this->opts
->getValue( 'limit' );
365 $pager->mLimit
= min( $limit, $wgFeedLimit );
368 if( $pager->getNumRows() > 0 ) {
369 foreach ( $pager->mResult
->fetchObject() as $row ) {
370 $feed->outItem( $this->feedItem( $row ) );
376 protected function feedTitle() {
377 global $wgLanguageCode, $wgSitename;
378 $page = SpecialPage
::getPage( 'Newpages' );
379 $desc = $page->getDescription();
380 return "$wgSitename - $desc [$wgLanguageCode]";
383 protected function feedItem( $row ) {
384 $title = Title
::MakeTitle( intval( $row->rc_namespace
), $row->rc_title
);
386 $date = $row->rc_timestamp
;
387 $comments = $title->getTalkPage()->getFullURL();
390 $title->getPrefixedText(),
391 $this->feedItemDesc( $row ),
392 $title->getFullURL(),
394 $this->feedItemAuthor( $row ),
401 protected function feedItemAuthor( $row ) {
402 return isset( $row->rc_user_text
) ?
$row->rc_user_text
: '';
405 protected function feedItemDesc( $row ) {
406 $revision = Revision
::newFromId( $row->rev_id
);
408 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
409 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
410 "</p>\n<hr />\n<div>" .
411 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
418 * @ingroup SpecialPage Pager
420 class NewPagesPager
extends ReverseChronologicalPager
{
422 protected $opts, $mForm;
424 function __construct( $form, FormOptions
$opts ) {
425 parent
::__construct();
426 $this->mForm
= $form;
430 function getTitle() {
431 static $title = null;
432 if ( $title === null )
433 $title = $this->mForm
->getTitle();
437 function getQueryInfo() {
438 global $wgEnableNewpagesUserFilter, $wgGroupPermissions, $wgUser;
440 $conds['rc_new'] = 1;
442 $namespace = $this->opts
->getValue( 'namespace' );
443 $namespace = ( $namespace === 'all' ) ?
false : intval( $namespace );
445 $username = $this->opts
->getValue( 'username' );
446 $user = Title
::makeTitleSafe( NS_USER
, $username );
448 if( $namespace !== false ) {
449 $conds['rc_namespace'] = $namespace;
450 $rcIndexes = array( 'new_name_timestamp' );
452 $rcIndexes = array( 'rc_timestamp' );
455 # $wgEnableNewpagesUserFilter - temp WMF hack
456 if( $wgEnableNewpagesUserFilter && $user ) {
457 $conds['rc_user_text'] = $user->getText();
458 $rcIndexes = 'rc_user_text';
459 # If anons cannot make new pages, don't "exclude logged in users"!
460 } elseif( $wgGroupPermissions['*']['createpage'] && $this->opts
->getValue( 'hideliu' ) ) {
461 $conds['rc_user'] = 0;
463 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
464 if( $this->opts
->getValue( 'hidepatrolled' ) && $wgUser->useNPPatrol() ) {
465 $conds['rc_patrolled'] = 0;
467 if( $this->opts
->getValue( 'hidebots' ) ) {
468 $conds['rc_bot'] = 0;
471 if ( $this->opts
->getValue( 'hideredirs' ) ) {
472 $conds['page_is_redirect'] = 0;
475 // Allow changes to the New Pages query
476 wfRunHooks('SpecialNewpagesConditions', array(&$this, $this->opts
, &$conds));
479 'tables' => array( 'recentchanges', 'page' ),
480 'fields' => array( 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text', 'rc_comment',
481 'rc_timestamp', 'rc_patrolled', 'rc_id', 'page_len AS length', 'page_latest AS rev_id', 'ts_tags'),
483 'options' => array( 'USE INDEX' => array('recentchanges' => $rcIndexes) ),
484 'join_conds' => array(
485 'page' => array('INNER JOIN', 'page_id=rc_cur_id'),
489 ## Empty array for fields, it'll be set by us anyway.
492 ## Modify query for tags
493 ChangeTags
::modifyDisplayQuery( $info['tables'],
498 $this->opts
['tagfilter'] );
503 function getIndexField() {
504 return 'rc_timestamp';
507 function formatRow( $row ) {
508 return $this->mForm
->formatRow( $row );
511 function getStartBody() {
512 # Do a batch existence check on pages
513 $linkBatch = new LinkBatch();
514 foreach ( $this->mResult
as $row ) {
515 $linkBatch->add( NS_USER
, $row->rc_user_text
);
516 $linkBatch->add( NS_USER_TALK
, $row->rc_user_text
);
517 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
519 $linkBatch->execute();
523 function getEndBody() {