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;
79 if ( 'hideliu' === $bit ) {
80 $this->opts
->setValue( 'hideliu', true );
82 if ( 'hidepatrolled' == $bit ) {
83 $this->opts
->setValue( 'hidepatrolled', true );
85 if ( 'hidebots' == $bit ) {
86 $this->opts
->setValue( 'hidebots', true );
88 if ( 'showredirs' == $bit ) {
89 $this->opts
->setValue( 'hideredirs', false );
91 if ( is_numeric( $bit ) ) {
92 $this->opts
->setValue( 'limit', intval( $bit ) );
96 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
97 $this->opts
->setValue( 'limit', intval( $m[1] ) );
99 // PG offsets not just digits!
100 if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) ) {
101 $this->opts
->setValue( 'offset', intval( $m[1] ) );
103 if ( preg_match( '/^username=(.*)$/', $bit, $m ) ) {
104 $this->opts
->setValue( 'username', $m[1] );
106 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
107 $ns = $wgLang->getNsIndex( $m[1] );
108 if( $ns !== false ) {
109 $this->opts
->setValue( 'namespace', $ns );
116 * Show a form for filtering namespace and username
121 public function execute( $par ) {
125 $this->outputHeader();
127 $this->showNavigation
= !$this->including(); // Maybe changed in setup
128 $this->setup( $par );
130 if( !$this->including() ) {
134 $this->setSyndicated();
135 $feedType = $this->opts
->getValue( 'feed' );
137 return $this->feed( $feedType );
141 $pager = new NewPagesPager( $this, $this->opts
);
142 $pager->mLimit
= $this->opts
->getValue( 'limit' );
143 $pager->mOffset
= $this->opts
->getValue( 'offset' );
145 if( $pager->getNumRows() ) {
147 if ( $this->showNavigation
) {
148 $navigation = $pager->getNavigationBar();
150 $wgOut->addHTML( $navigation . $pager->getBody() . $navigation );
152 $wgOut->addWikiMsg( 'specialpage-empty' );
156 protected function filterLinks() {
157 global $wgGroupPermissions, $wgUser, $wgLang;
160 $showhide = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ) );
162 // Option value -> message mapping
164 'hideliu' => 'rcshowhideliu',
165 'hidepatrolled' => 'rcshowhidepatr',
166 'hidebots' => 'rcshowhidebots',
167 'hideredirs' => 'whatlinkshere-hideredirs'
170 // Disable some if needed
171 # FIXME: throws E_NOTICEs if not set; and doesn't obey hooks etc.
172 if ( $wgGroupPermissions['*']['createpage'] !== true ) {
173 unset( $filters['hideliu'] );
176 if ( !$wgUser->useNPPatrol() ) {
177 unset( $filters['hidepatrolled'] );
181 $changed = $this->opts
->getChangedValues();
182 unset( $changed['offset'] ); // Reset offset if query type changes
184 $self = $this->getTitle();
185 foreach ( $filters as $key => $msg ) {
186 $onoff = 1 - $this->opts
->getValue( $key );
187 $link = $this->skin
->link( $self, $showhide[$onoff], array(),
188 array( $key => $onoff ) +
$changed
190 $links[$key] = wfMsgHtml( $msg, $link );
193 return $wgLang->pipeList( $links );
196 protected function form() {
197 global $wgOut, $wgEnableNewpagesUserFilter, $wgScript;
200 $this->opts
->consumeValue( 'offset' ); // don't carry offset, DWIW
201 $namespace = $this->opts
->consumeValue( 'namespace' );
202 $username = $this->opts
->consumeValue( 'username' );
203 $tagFilterVal = $this->opts
->consumeValue( 'tagfilter' );
205 // Check username input validity
206 $ut = Title
::makeTitleSafe( NS_USER
, $username );
207 $userText = $ut ?
$ut->getText() : '';
209 // Store query values in hidden fields so that form submission doesn't lose them
211 foreach ( $this->opts
->getUnconsumedValues() as $key => $value ) {
212 $hidden[] = Html
::hidden( $key, $value );
214 $hidden = implode( "\n", $hidden );
216 $tagFilter = ChangeTags
::buildTagFilterSelector( $tagFilterVal );
218 list( $tagFilterLabel, $tagFilterSelector ) = $tagFilter;
221 $form = Xml
::openElement( 'form', array( 'action' => $wgScript ) ) .
222 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
223 Xml
::fieldset( wfMsg( 'newpages' ) ) .
224 Xml
::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
226 <td class='mw-label'>" .
227 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) .
229 <td class='mw-input'>" .
230 Xml
::namespaceSelector( $namespace, 'all' ) .
232 </tr>" . ( $tagFilter ?
(
234 <td class='mw-label'>" .
237 <td class='mw-input'>" .
241 ( $wgEnableNewpagesUserFilter ?
243 <td class='mw-label'>" .
244 Xml
::label( wfMsg( 'newpages-username' ), 'mw-np-username' ) .
246 <td class='mw-input'>" .
247 Xml
::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
251 <td class='mw-submit'>" .
252 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) .
257 <td class='mw-input'>" .
258 $this->filterLinks() .
261 Xml
::closeElement( 'table' ) .
262 Xml
::closeElement( 'fieldset' ) .
264 Xml
::closeElement( 'form' );
266 $wgOut->addHTML( $form );
269 protected function setSyndicated() {
271 $wgOut->setSyndicated( true );
272 $wgOut->setFeedAppendQuery( wfArrayToCGI( $this->opts
->getAllValues() ) );
276 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
278 * @param $result Result row
281 public function formatRow( $result ) {
282 global $wgLang, $wgContLang;
286 $dm = $wgContLang->getDirMark();
288 $title = Title
::makeTitleSafe( $result->rc_namespace
, $result->rc_title
);
289 $time = Html
::element( 'span', array( 'class' => 'mw-newpages-time' ),
290 $wgLang->timeAndDate( $result->rc_timestamp
, true )
293 $query = array( 'redirect' => 'no' );
295 if( $this->patrollable( $result ) ) {
296 $query['rcid'] = $result->rc_id
;
299 $plink = $this->skin
->linkKnown(
302 array( 'class' => 'mw-newpages-pagename' ),
304 array( 'known' ) // Set explicitly to avoid the default of 'known','noclasses'. This breaks the colouration for stubs
306 $histLink = $this->skin
->linkKnown(
310 array( 'action' => 'history' )
312 $hist = Html
::rawElement( 'span', array( 'class' => 'mw-newpages-history' ), wfMsg( 'parentheses', $histLink ) );
314 $length = Html
::rawElement( 'span', array( 'class' => 'mw-newpages-length' ),
315 '[' . wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ), $wgLang->formatNum( $result->length
) ) .
318 $ulink = $this->skin
->userLink( $result->rc_user
, $result->rc_user_text
) . ' ' .
319 $this->skin
->userToolLinks( $result->rc_user
, $result->rc_user_text
);
320 $comment = $this->skin
->commentBlock( $result->rc_comment
);
322 if ( $this->patrollable( $result ) ) {
323 $classes[] = 'not-patrolled';
326 # Add a class for zero byte pages
327 if ( $result->length
== 0 ) {
328 $classes[] = 'mw-newpages-zero-byte-page';
331 # Tags, if any. check for including due to bug 23293
332 if ( !$this->including() ) {
333 list( $tagDisplay, $newClasses ) = ChangeTags
::formatSummaryRow( $result->ts_tags
, 'newpages' );
334 $classes = array_merge( $classes, $newClasses );
339 $css = count( $classes ) ?
' class="' . implode( ' ', $classes ) . '"' : '';
341 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} {$dm}{$ulink} {$comment} {$tagDisplay}</li>\n";
345 * Should a specific result row provide "patrollable" links?
347 * @param $result Result row
350 protected function patrollable( $result ) {
352 return ( $wgUser->useNPPatrol() && !$result->rc_patrolled
);
356 * Output a subscription feed listing recent edits to this page.
358 * @param $type String
360 protected function feed( $type ) {
361 global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgOut;
364 $wgOut->addWikiMsg( 'feed-unavailable' );
368 if( !isset( $wgFeedClasses[$type] ) ) {
369 $wgOut->addWikiMsg( 'feed-invalid' );
373 $feed = new $wgFeedClasses[$type](
375 wfMsgExt( 'tagline', 'parsemag' ),
376 $this->getTitle()->getFullUrl()
379 $pager = new NewPagesPager( $this, $this->opts
);
380 $limit = $this->opts
->getValue( 'limit' );
381 $pager->mLimit
= min( $limit, $wgFeedLimit );
384 if( $pager->getNumRows() > 0 ) {
385 foreach ( $pager->mResult
as $row ) {
386 $feed->outItem( $this->feedItem( $row ) );
392 protected function feedTitle() {
393 global $wgLanguageCode, $wgSitename;
394 $page = SpecialPage
::getPage( 'Newpages' );
395 $desc = $page->getDescription();
396 return "$wgSitename - $desc [$wgLanguageCode]";
399 protected function feedItem( $row ) {
400 $title = Title
::MakeTitle( intval( $row->rc_namespace
), $row->rc_title
);
402 $date = $row->rc_timestamp
;
403 $comments = $title->getTalkPage()->getFullURL();
406 $title->getPrefixedText(),
407 $this->feedItemDesc( $row ),
408 $title->getFullURL(),
410 $this->feedItemAuthor( $row ),
418 protected function feedItemAuthor( $row ) {
419 return isset( $row->rc_user_text
) ?
$row->rc_user_text
: '';
422 protected function feedItemDesc( $row ) {
423 $revision = Revision
::newFromId( $row->rev_id
);
425 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
426 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
427 "</p>\n<hr />\n<div>" .
428 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
435 * @ingroup SpecialPage Pager
437 class NewPagesPager
extends ReverseChronologicalPager
{
439 protected $opts, $mForm;
441 function __construct( $form, FormOptions
$opts ) {
442 parent
::__construct();
443 $this->mForm
= $form;
447 function getTitle() {
448 static $title = null;
449 if ( $title === null ) {
450 $title = $this->mForm
->getTitle();
455 function getQueryInfo() {
456 global $wgEnableNewpagesUserFilter, $wgGroupPermissions, $wgUser;
458 $conds['rc_new'] = 1;
460 $namespace = $this->opts
->getValue( 'namespace' );
461 $namespace = ( $namespace === 'all' ) ?
false : intval( $namespace );
463 $username = $this->opts
->getValue( 'username' );
464 $user = Title
::makeTitleSafe( NS_USER
, $username );
466 if( $namespace !== false ) {
467 $conds['rc_namespace'] = $namespace;
468 $rcIndexes = array( 'new_name_timestamp' );
470 $rcIndexes = array( 'rc_timestamp' );
473 # $wgEnableNewpagesUserFilter - temp WMF hack
474 if( $wgEnableNewpagesUserFilter && $user ) {
475 $conds['rc_user_text'] = $user->getText();
476 $rcIndexes = 'rc_user_text';
477 # If anons cannot make new pages, don't "exclude logged in users"!
478 } elseif( $wgGroupPermissions['*']['createpage'] && $this->opts
->getValue( 'hideliu' ) ) {
479 $conds['rc_user'] = 0;
481 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
482 if( $this->opts
->getValue( 'hidepatrolled' ) && $wgUser->useNPPatrol() ) {
483 $conds['rc_patrolled'] = 0;
485 if( $this->opts
->getValue( 'hidebots' ) ) {
486 $conds['rc_bot'] = 0;
489 if ( $this->opts
->getValue( 'hideredirs' ) ) {
490 $conds['page_is_redirect'] = 0;
493 // Allow changes to the New Pages query
494 wfRunHooks( 'SpecialNewpagesConditions', array( &$this, $this->opts
, &$conds ) );
497 'tables' => array( 'recentchanges', 'page' ),
499 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user',
500 'rc_user_text', 'rc_comment', 'rc_timestamp', 'rc_patrolled',
501 'rc_id', 'page_len AS length', 'page_latest AS rev_id',
505 'options' => array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) ),
506 'join_conds' => array(
507 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ),
511 // Empty array for fields, it'll be set by us anyway.
514 // Modify query for tags
515 ChangeTags
::modifyDisplayQuery(
521 $this->opts
['tagfilter']
527 function getIndexField() {
528 return 'rc_timestamp';
531 function formatRow( $row ) {
532 return $this->mForm
->formatRow( $row );
535 function getStartBody() {
536 # Do a batch existence check on pages
537 $linkBatch = new LinkBatch();
538 foreach ( $this->mResult
as $row ) {
539 $linkBatch->add( NS_USER
, $row->rc_user_text
);
540 $linkBatch->add( NS_USER_TALK
, $row->rc_user_text
);
541 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
543 $linkBatch->execute();
547 function getEndBody() {