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
{
43 // Some internal settings
44 protected $showNavigation = false;
46 public function __construct() {
47 parent
::__construct( 'Newpages' );
50 protected function setup( $par ) {
51 global $wgRequest, $wgUser, $wgEnableNewpagesUserFilter;
54 $opts = new FormOptions();
55 $this->opts
= $opts; // bind
56 $opts->add( 'hideliu', false );
57 $opts->add( 'hidepatrolled', $wgUser->getBoolOption( 'newpageshidepatrolled' ) );
58 $opts->add( 'hidebots', false );
59 $opts->add( 'hideredirs', true );
60 $opts->add( 'limit', (int)$wgUser->getOption( 'rclimit' ) );
61 $opts->add( 'offset', '' );
62 $opts->add( 'namespace', '0' );
63 $opts->add( 'username', '' );
64 $opts->add( 'feed', '' );
65 $opts->add( 'tagfilter', '' );
68 $opts->fetchValuesFromRequest( $wgRequest );
69 if ( $par ) $this->parseParams( $par );
72 $opts->validateIntBounds( 'limit', 0, 5000 );
73 if( !$wgEnableNewpagesUserFilter ) {
74 $opts->setValue( 'username', '' );
78 $this->skin
= $wgUser->getSkin();
81 protected function parseParams( $par ) {
83 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
84 foreach ( $bits as $bit ) {
85 if ( 'shownav' == $bit ) {
86 $this->showNavigation
= true;
88 if ( 'hideliu' === $bit ) {
89 $this->opts
->setValue( 'hideliu', true );
91 if ( 'hidepatrolled' == $bit ) {
92 $this->opts
->setValue( 'hidepatrolled', true );
94 if ( 'hidebots' == $bit ) {
95 $this->opts
->setValue( 'hidebots', true );
97 if ( 'showredirs' == $bit ) {
98 $this->opts
->setValue( 'hideredirs', false );
100 if ( is_numeric( $bit ) ) {
101 $this->opts
->setValue( 'limit', intval( $bit ) );
105 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
106 $this->opts
->setValue( 'limit', intval( $m[1] ) );
108 // PG offsets not just digits!
109 if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) ) {
110 $this->opts
->setValue( 'offset', intval( $m[1] ) );
112 if ( preg_match( '/^username=(.*)$/', $bit, $m ) ) {
113 $this->opts
->setValue( 'username', $m[1] );
115 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
116 $ns = $wgLang->getNsIndex( $m[1] );
117 if( $ns !== false ) {
118 $this->opts
->setValue( 'namespace', $ns );
125 * Show a form for filtering namespace and username
130 public function execute( $par ) {
134 $this->outputHeader();
136 $this->showNavigation
= !$this->including(); // Maybe changed in setup
137 $this->setup( $par );
139 if( !$this->including() ) {
143 $this->setSyndicated();
144 $feedType = $this->opts
->getValue( 'feed' );
146 return $this->feed( $feedType );
150 $pager = new NewPagesPager( $this, $this->opts
);
151 $pager->mLimit
= $this->opts
->getValue( 'limit' );
152 $pager->mOffset
= $this->opts
->getValue( 'offset' );
154 if( $pager->getNumRows() ) {
156 if ( $this->showNavigation
) {
157 $navigation = $pager->getNavigationBar();
159 $wgOut->addHTML( $navigation . $pager->getBody() . $navigation );
161 $wgOut->addWikiMsg( 'specialpage-empty' );
165 protected function filterLinks() {
166 global $wgGroupPermissions, $wgUser, $wgLang;
169 $showhide = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ) );
171 // Option value -> message mapping
173 'hideliu' => 'rcshowhideliu',
174 'hidepatrolled' => 'rcshowhidepatr',
175 'hidebots' => 'rcshowhidebots',
176 'hideredirs' => 'whatlinkshere-hideredirs'
179 // Disable some if needed
180 # FIXME: throws E_NOTICEs if not set; and doesn't obey hooks etc.
181 if ( $wgGroupPermissions['*']['createpage'] !== true ) {
182 unset( $filters['hideliu'] );
185 if ( !$wgUser->useNPPatrol() ) {
186 unset( $filters['hidepatrolled'] );
190 $changed = $this->opts
->getChangedValues();
191 unset( $changed['offset'] ); // Reset offset if query type changes
193 $self = $this->getTitle();
194 foreach ( $filters as $key => $msg ) {
195 $onoff = 1 - $this->opts
->getValue( $key );
196 $link = $this->skin
->link( $self, $showhide[$onoff], array(),
197 array( $key => $onoff ) +
$changed
199 $links[$key] = wfMsgHtml( $msg, $link );
202 return $wgLang->pipeList( $links );
205 protected function form() {
206 global $wgOut, $wgEnableNewpagesUserFilter, $wgScript;
209 $this->opts
->consumeValue( 'offset' ); // don't carry offset, DWIW
210 $namespace = $this->opts
->consumeValue( 'namespace' );
211 $username = $this->opts
->consumeValue( 'username' );
212 $tagFilterVal = $this->opts
->consumeValue( 'tagfilter' );
214 // Check username input validity
215 $ut = Title
::makeTitleSafe( NS_USER
, $username );
216 $userText = $ut ?
$ut->getText() : '';
218 // Store query values in hidden fields so that form submission doesn't lose them
220 foreach ( $this->opts
->getUnconsumedValues() as $key => $value ) {
221 $hidden[] = Html
::hidden( $key, $value );
223 $hidden = implode( "\n", $hidden );
225 $tagFilter = ChangeTags
::buildTagFilterSelector( $tagFilterVal );
227 list( $tagFilterLabel, $tagFilterSelector ) = $tagFilter;
230 $form = Xml
::openElement( 'form', array( 'action' => $wgScript ) ) .
231 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
232 Xml
::fieldset( wfMsg( 'newpages' ) ) .
233 Xml
::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
235 <td class="mw-label">' .
236 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) .
238 <td class="mw-input">' .
239 Xml
::namespaceSelector( $namespace, 'all' ) .
241 </tr>' . ( $tagFilter ?
(
243 <td class="mw-label">' .
246 <td class="mw-input">' .
250 ( $wgEnableNewpagesUserFilter ?
252 <td class="mw-label">' .
253 Xml
::label( wfMsg( 'newpages-username' ), 'mw-np-username' ) .
255 <td class="mw-input">' .
256 Xml
::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
260 <td class="mw-submit">' .
261 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) .
266 <td class="mw-input">' .
267 $this->filterLinks() .
270 Xml
::closeElement( 'table' ) .
271 Xml
::closeElement( 'fieldset' ) .
273 Xml
::closeElement( 'form' );
275 $wgOut->addHTML( $form );
278 protected function setSyndicated() {
280 $wgOut->setSyndicated( true );
281 $wgOut->setFeedAppendQuery( wfArrayToCGI( $this->opts
->getAllValues() ) );
285 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
287 * @param $result Result row
290 public function formatRow( $result ) {
291 global $wgLang, $wgContLang;
293 # Revision deletion works on revisions, so we should cast one
295 'comment' => $result->rc_comment
,
296 'deleted' => $result->rc_deleted
,
297 'user_text' => $result->rc_user_text
,
298 'user' => $result->rc_user
,
300 $rev = new Revision( $row );
304 $dm = $wgContLang->getDirMark();
306 $title = Title
::makeTitleSafe( $result->rc_namespace
, $result->rc_title
);
307 $time = Html
::element( 'span', array( 'class' => 'mw-newpages-time' ),
308 $wgLang->timeAndDate( $result->rc_timestamp
, true )
311 $query = array( 'redirect' => 'no' );
313 if( $this->patrollable( $result ) ) {
314 $query['rcid'] = $result->rc_id
;
317 $plink = $this->skin
->linkKnown(
320 array( 'class' => 'mw-newpages-pagename' ),
322 array( 'known' ) // Set explicitly to avoid the default of 'known','noclasses'. This breaks the colouration for stubs
324 $histLink = $this->skin
->linkKnown(
328 array( 'action' => 'history' )
330 $hist = Html
::rawElement( 'span', array( 'class' => 'mw-newpages-history' ), wfMsg( 'parentheses', $histLink ) );
332 $length = Html
::rawElement( 'span', array( 'class' => 'mw-newpages-length' ),
333 '[' . wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ), $wgLang->formatNum( $result->length
) ) .
337 $ulink = $this->skin
->revUserTools( $rev );
338 $comment = $this->skin
->revComment( $rev );
340 if ( $this->patrollable( $result ) ) {
341 $classes[] = 'not-patrolled';
344 # Add a class for zero byte pages
345 if ( $result->length
== 0 ) {
346 $classes[] = 'mw-newpages-zero-byte-page';
349 # Tags, if any. check for including due to bug 23293
350 if ( !$this->including() ) {
351 list( $tagDisplay, $newClasses ) = ChangeTags
::formatSummaryRow( $result->ts_tags
, 'newpages' );
352 $classes = array_merge( $classes, $newClasses );
357 $css = count( $classes ) ?
' class="' . implode( ' ', $classes ) . '"' : '';
359 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} {$dm}{$ulink} {$comment} {$tagDisplay}</li>\n";
363 * Should a specific result row provide "patrollable" links?
365 * @param $result Result row
368 protected function patrollable( $result ) {
370 return ( $wgUser->useNPPatrol() && !$result->rc_patrolled
);
374 * Output a subscription feed listing recent edits to this page.
376 * @param $type String
378 protected function feed( $type ) {
379 global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgOut;
382 $wgOut->addWikiMsg( 'feed-unavailable' );
386 if( !isset( $wgFeedClasses[$type] ) ) {
387 $wgOut->addWikiMsg( 'feed-invalid' );
391 $feed = new $wgFeedClasses[$type](
393 wfMsgExt( 'tagline', 'parsemag' ),
394 $this->getTitle()->getFullUrl()
397 $pager = new NewPagesPager( $this, $this->opts
);
398 $limit = $this->opts
->getValue( 'limit' );
399 $pager->mLimit
= min( $limit, $wgFeedLimit );
402 if( $pager->getNumRows() > 0 ) {
403 foreach ( $pager->mResult
as $row ) {
404 $feed->outItem( $this->feedItem( $row ) );
410 protected function feedTitle() {
411 global $wgLanguageCode, $wgSitename;
412 $page = SpecialPage
::getPage( 'Newpages' );
413 $desc = $page->getDescription();
414 return "$wgSitename - $desc [$wgLanguageCode]";
417 protected function feedItem( $row ) {
418 $title = Title
::MakeTitle( intval( $row->rc_namespace
), $row->rc_title
);
420 $date = $row->rc_timestamp
;
421 $comments = $title->getTalkPage()->getFullURL();
424 $title->getPrefixedText(),
425 $this->feedItemDesc( $row ),
426 $title->getFullURL(),
428 $this->feedItemAuthor( $row ),
436 protected function feedItemAuthor( $row ) {
437 return isset( $row->rc_user_text
) ?
$row->rc_user_text
: '';
440 protected function feedItemDesc( $row ) {
441 $revision = Revision
::newFromId( $row->rev_id
);
443 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
444 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
445 "</p>\n<hr />\n<div>" .
446 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
453 * @ingroup SpecialPage Pager
455 class NewPagesPager
extends ReverseChronologicalPager
{
457 protected $opts, $mForm;
459 function __construct( $form, FormOptions
$opts ) {
460 parent
::__construct();
461 $this->mForm
= $form;
465 function getTitle() {
466 static $title = null;
467 if ( $title === null ) {
468 $title = $this->mForm
->getTitle();
473 function getQueryInfo() {
474 global $wgEnableNewpagesUserFilter, $wgGroupPermissions, $wgUser;
476 $conds['rc_new'] = 1;
478 $namespace = $this->opts
->getValue( 'namespace' );
479 $namespace = ( $namespace === 'all' ) ?
false : intval( $namespace );
481 $username = $this->opts
->getValue( 'username' );
482 $user = Title
::makeTitleSafe( NS_USER
, $username );
484 if( $namespace !== false ) {
485 $conds['rc_namespace'] = $namespace;
486 $rcIndexes = array( 'new_name_timestamp' );
488 $rcIndexes = array( 'rc_timestamp' );
491 # $wgEnableNewpagesUserFilter - temp WMF hack
492 if( $wgEnableNewpagesUserFilter && $user ) {
493 $conds['rc_user_text'] = $user->getText();
494 $rcIndexes = 'rc_user_text';
495 # If anons cannot make new pages, don't "exclude logged in users"!
496 } elseif( $wgGroupPermissions['*']['createpage'] && $this->opts
->getValue( 'hideliu' ) ) {
497 $conds['rc_user'] = 0;
499 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
500 if( $this->opts
->getValue( 'hidepatrolled' ) && $wgUser->useNPPatrol() ) {
501 $conds['rc_patrolled'] = 0;
503 if( $this->opts
->getValue( 'hidebots' ) ) {
504 $conds['rc_bot'] = 0;
507 if ( $this->opts
->getValue( 'hideredirs' ) ) {
508 $conds['page_is_redirect'] = 0;
511 // Allow changes to the New Pages query
512 wfRunHooks( 'SpecialNewpagesConditions', array( &$this, $this->opts
, &$conds ) );
515 'tables' => array( 'recentchanges', 'page' ),
517 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user',
518 'rc_user_text', 'rc_comment', 'rc_timestamp', 'rc_patrolled',
519 'rc_id', 'rc_deleted', 'page_len AS length', 'page_latest AS rev_id',
523 'options' => array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) ),
524 'join_conds' => array(
525 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ),
529 // Empty array for fields, it'll be set by us anyway.
532 // Modify query for tags
533 ChangeTags
::modifyDisplayQuery(
539 $this->opts
['tagfilter']
545 function getIndexField() {
546 return 'rc_timestamp';
549 function formatRow( $row ) {
550 return $this->mForm
->formatRow( $row );
553 function getStartBody() {
554 # Do a batch existence check on pages
555 $linkBatch = new LinkBatch();
556 foreach ( $this->mResult
as $row ) {
557 $linkBatch->add( NS_USER
, $row->rc_user_text
);
558 $linkBatch->add( NS_USER_TALK
, $row->rc_user_text
);
559 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
561 $linkBatch->execute();
565 function getEndBody() {