4 * implements Special:Newpages
7 class SpecialNewpages
extends SpecialPage
{
10 protected $opts, $skin;
12 // Some internal settings
13 protected $showNavigation = false;
15 public function __construct() {
16 parent
::__construct( 'Newpages' );
17 $this->includable( true );
20 protected function setup( $par ) {
21 global $wgRequest, $wgUser, $wgEnableNewpagesUserFilter;
24 $opts = new FormOptions();
25 $this->opts
= $opts; // bind
26 $opts->add( 'hideliu', false );
27 $opts->add( 'hidepatrolled', $wgUser->getBoolOption( 'newpageshidepatrolled' ) );
28 $opts->add( 'hidebots', $wgUser->getBoolOption( 'newpageshidebots' ) );
29 $opts->add( 'hideown', $wgUser->getBoolOption( 'newpageshideown' ) );
30 $opts->add( 'hideredirs', true );
31 $opts->add( 'limit', (int)$wgUser->getOption( 'rclimit' ) );
32 $opts->add( 'offset', '' );
33 $opts->add( 'namespace', '0' );
34 $opts->add( 'username', '' );
35 $opts->add( 'feed', '' );
36 $opts->add( 'tagfilter', '' );
39 $opts->fetchValuesFromRequest( $wgRequest );
40 if ( $par ) $this->parseParams( $par );
43 $opts->validateIntBounds( 'limit', 0, 5000 );
44 if( !$wgEnableNewpagesUserFilter ) {
45 $opts->setValue( 'username', '' );
49 $this->skin
= $wgUser->getSkin();
52 protected function parseParams( $par ) {
54 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
55 foreach ( $bits as $bit ) {
56 if ( 'shownav' == $bit )
57 $this->showNavigation
= true;
58 if ( 'hideliu' === $bit )
59 $this->opts
->setValue( 'hideliu', true );
60 if ( 'hidepatrolled' == $bit )
61 $this->opts
->setValue( 'hidepatrolled', true );
62 if ( 'hidebots' == $bit )
63 $this->opts
->setValue( 'hidebots', true );
64 if ( 'hideown' == $bit )
65 $this->opts
->setValue( 'hideown', true );
66 if ( 'showredirs' == $bit )
67 $this->opts
->setValue( 'hideredirs', false );
68 if ( is_numeric( $bit ) )
69 $this->opts
->setValue( 'limit', intval( $bit ) );
72 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) )
73 $this->opts
->setValue( 'limit', intval($m[1]) );
74 // PG offsets not just digits!
75 if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) )
76 $this->opts
->setValue( 'offset', intval($m[1]) );
77 if ( preg_match( '/^username=(.*)$/', $bit, $m ) )
78 $this->opts
->setValue( 'username', $m[1] );
79 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
80 $ns = $wgLang->getNsIndex( $m[1] );
82 $this->opts
->setValue( 'namespace', $ns );
89 * Show a form for filtering namespace and username
94 public function execute( $par ) {
95 global $wgLang, $wgUser, $wgOut;
98 $this->outputHeader();
100 $this->showNavigation
= !$this->including(); // Maybe changed in setup
101 $this->setup( $par );
103 if( !$this->including() ) {
107 $this->setSyndicated();
108 $feedType = $this->opts
->getValue( 'feed' );
110 return $this->feed( $feedType );
114 $pager = new NewPagesPager( $this, $this->opts
);
115 $pager->mLimit
= $this->opts
->getValue( 'limit' );
116 $pager->mOffset
= $this->opts
->getValue( 'offset' );
118 if( $pager->getNumRows() ) {
120 if ( $this->showNavigation
) $navigation = $pager->getNavigationBar();
121 $wgOut->addHTML( $navigation . $pager->getBody() . $navigation );
123 $wgOut->addWikiMsg( 'specialpage-empty' );
127 protected function filterLinks() {
128 global $wgGroupPermissions, $wgUser, $wgLang;
131 $showhide = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ) );
133 // Option value -> message mapping
135 'hideliu' => 'rcshowhideliu',
136 'hidepatrolled' => 'rcshowhidepatr',
137 'hidebots' => 'rcshowhidebots',
138 'hideown' => 'rcshowhidemine',
139 'hideredirs' => 'whatlinkshere-hideredirs'
142 // Disable some if needed
143 if ( $wgGroupPermissions['*']['createpage'] !== true )
144 unset($filters['hideliu']);
146 if ( !$wgUser->useNPPatrol() )
147 unset($filters['hidepatrolled']);
150 $changed = $this->opts
->getChangedValues();
151 unset($changed['offset']); // Reset offset if query type changes
153 $self = $this->getTitle();
154 foreach ( $filters as $key => $msg ) {
155 $onoff = 1 - $this->opts
->getValue($key);
156 $link = $this->skin
->link( $self, $showhide[$onoff], array(),
157 array( $key => $onoff ) +
$changed
159 $links[$key] = wfMsgHtml( $msg, $link );
162 return $wgLang->pipeList( $links );
165 protected function form() {
166 global $wgOut, $wgEnableNewpagesUserFilter, $wgScript;
169 $this->opts
->consumeValue( 'offset' ); // don't carry offset, DWIW
170 $namespace = $this->opts
->consumeValue( 'namespace' );
171 $username = $this->opts
->consumeValue( 'username' );
173 // Check username input validity
174 $ut = Title
::makeTitleSafe( NS_USER
, $username );
175 $userText = $ut ?
$ut->getText() : '';
177 // Store query values in hidden fields so that form submission doesn't lose them
179 foreach ( $this->opts
->getUnconsumedValues() as $key => $value ) {
180 $hidden[] = Xml
::hidden( $key, $value );
182 $hidden = implode( "\n", $hidden );
184 $tagFilter = ChangeTags
::buildTagFilterSelector( $this->opts
['tagfilter'] );
186 list( $tagFilterLabel, $tagFilterSelector ) = $tagFilter;
188 $form = Xml
::openElement( 'form', array( 'action' => $wgScript ) ) .
189 Xml
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
190 Xml
::fieldset( wfMsg( 'newpages' ) ) .
191 Xml
::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
193 <td class='mw-label'>" .
194 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) .
196 <td class='mw-input'>" .
197 Xml
::namespaceSelector( $namespace, 'all' ) .
199 </tr>" . ( $tagFilter ?
(
201 <td class='mw-label'>" .
204 <td class='mw-input'>" .
208 ($wgEnableNewpagesUserFilter ?
210 <td class='mw-label'>" .
211 Xml
::label( wfMsg( 'newpages-username' ), 'mw-np-username' ) .
213 <td class='mw-input'>" .
214 Xml
::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
218 <td class='mw-submit'>" .
219 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) .
224 <td class='mw-input'>" .
225 $this->filterLinks() .
228 Xml
::closeElement( 'table' ) .
229 Xml
::closeElement( 'fieldset' ) .
231 Xml
::closeElement( 'form' );
233 $wgOut->addHTML( $form );
236 protected function setSyndicated() {
238 $queryParams = array(
239 'namespace' => $this->opts
->getValue( 'namespace' ),
240 'username' => $this->opts
->getValue( 'username' )
242 $wgOut->setSyndicated( true );
243 $wgOut->setFeedAppendQuery( wfArrayToCGI( $queryParams ) );
247 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
249 * @param $skin Skin to use
250 * @param $result Result row
253 public function formatRow( $result ) {
254 global $wgLang, $wgContLang, $wgUser;
258 $dm = $wgContLang->getDirMark();
260 $title = Title
::makeTitleSafe( $result->rc_namespace
, $result->rc_title
);
261 $time = $wgLang->timeAndDate( $result->rc_timestamp
, true );
262 $query = $this->patrollable( $result ) ?
"rcid={$result->rc_id}&redirect=no" : 'redirect=no';
263 $plink = $this->skin
->makeKnownLinkObj( $title, '', $query );
264 $hist = $this->skin
->makeKnownLinkObj( $title, wfMsgHtml( 'hist' ), 'action=history' );
265 $length = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
266 $wgLang->formatNum( $result->length
) );
267 $ulink = $this->skin
->userLink( $result->rc_user
, $result->rc_user_text
) . ' ' .
268 $this->skin
->userToolLinks( $result->rc_user
, $result->rc_user_text
);
269 $comment = $this->skin
->commentBlock( $result->rc_comment
);
271 if ( $this->patrollable( $result ) )
272 $classes[] = 'not-patrolled';
275 list( $tagDisplay, $newClasses ) = ChangeTags
::formatSummaryRow( $result->ts_tags
, 'newpages' );
276 $classes = array_merge( $classes, $newClasses );
278 $css = count($classes) ?
' class="'.implode( " ", $classes).'"' : '';
280 return "<li{$css}>{$time} {$dm}{$plink} ({$hist}) {$dm}[{$length}] {$dm}{$ulink} {$comment} {$tagDisplay}</li>\n";
284 * Should a specific result row provide "patrollable" links?
286 * @param $result Result row
289 protected function patrollable( $result ) {
291 return ( $wgUser->useNPPatrol() && !$result->rc_patrolled
);
295 * Output a subscription feed listing recent edits to this page.
296 * @param string $type
298 protected function feed( $type ) {
299 global $wgFeed, $wgFeedClasses, $wgFeedLimit;
303 $wgOut->addWikiMsg( 'feed-unavailable' );
307 if( !isset( $wgFeedClasses[$type] ) ) {
309 $wgOut->addWikiMsg( 'feed-invalid' );
313 $feed = new $wgFeedClasses[$type](
315 wfMsgExt( 'tagline', 'parsemag' ),
316 $this->getTitle()->getFullUrl() );
318 $pager = new NewPagesPager( $this, $this->opts
);
319 $limit = $this->opts
->getValue( 'limit' );
320 $pager->mLimit
= min( $limit, $wgFeedLimit );
323 if( $pager->getNumRows() > 0 ) {
324 while( $row = $pager->mResult
->fetchObject() ) {
325 $feed->outItem( $this->feedItem( $row ) );
331 protected function feedTitle() {
332 global $wgContLanguageCode, $wgSitename;
333 $page = SpecialPage
::getPage( 'Newpages' );
334 $desc = $page->getDescription();
335 return "$wgSitename - $desc [$wgContLanguageCode]";
338 protected function feedItem( $row ) {
339 $title = Title
::MakeTitle( intval( $row->rc_namespace
), $row->rc_title
);
341 $date = $row->rc_timestamp
;
342 $comments = $title->getTalkPage()->getFullURL();
345 $title->getPrefixedText(),
346 $this->feedItemDesc( $row ),
347 $title->getFullURL(),
349 $this->feedItemAuthor( $row ),
356 protected function feedItemAuthor( $row ) {
357 return isset( $row->rc_user_text
) ?
$row->rc_user_text
: '';
360 protected function feedItemDesc( $row ) {
361 $revision = Revision
::newFromId( $row->rev_id
);
363 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
364 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
365 "</p>\n<hr />\n<div>" .
366 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
373 * @ingroup SpecialPage Pager
375 class NewPagesPager
extends ReverseChronologicalPager
{
377 protected $opts, $mForm;
379 function __construct( $form, FormOptions
$opts ) {
380 parent
::__construct();
381 $this->mForm
= $form;
385 function getTitle() {
386 static $title = null;
387 if ( $title === null )
388 $title = $this->mForm
->getTitle();
392 function getQueryInfo() {
393 global $wgEnableNewpagesUserFilter, $wgGroupPermissions, $wgUser;
394 $dbr = wfGetDB( DB_SLAVE
);
396 $conds['rc_new'] = 1;
398 $namespace = $this->opts
->getValue( 'namespace' );
399 $namespace = ( $namespace === 'all' ) ?
false : intval( $namespace );
401 $username = $this->opts
->getValue( 'username' );
402 $user = Title
::makeTitleSafe( NS_USER
, $username );
404 if( $namespace !== false ) {
405 $conds['rc_namespace'] = $namespace;
406 $rcIndexes = array( 'new_name_timestamp' );
408 $rcIndexes = array( 'rc_timestamp' );
411 # $wgEnableNewpagesUserFilter - temp WMF hack
412 if( $wgEnableNewpagesUserFilter && $user ) {
413 $conds['rc_user_text'] = $user->getText();
414 $rcIndexes = 'rc_user_text';
415 # If anons cannot make new pages, don't "exclude logged in users"!
416 } elseif( $wgGroupPermissions['*']['createpage'] && $this->opts
->getValue( 'hideliu' ) ) {
417 $conds['rc_user'] = 0;
419 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
420 if( $this->opts
->getValue( 'hidepatrolled' ) && $wgUser->useNPPatrol() ) {
421 $conds['rc_patrolled'] = 0;
423 if( $this->opts
->getValue( 'hidebots' ) ) {
424 $conds['rc_bot'] = 0;
426 if( $this->opts
->getValue( 'hideown' ) ) {
427 if( $wgUser->getId() ) {
428 $conds[] = 'rc_user != ' . $dbr->addQuotes( $wgUser->getId() );
430 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $wgUser->getName() );
433 if ( $this->opts
->getValue( 'hideredirs' ) ) {
434 $conds['page_is_redirect'] = 0;
438 'tables' => array( 'recentchanges', 'page' ),
439 'fields' => 'rc_namespace,rc_title, rc_cur_id, rc_user,rc_user_text,rc_comment,
440 rc_timestamp,rc_patrolled,rc_id,page_len as length, page_latest as rev_id, ts_tags',
442 'options' => array( 'USE INDEX' => array('recentchanges' => $rcIndexes) ),
443 'join_conds' => array(
444 'page' => array('INNER JOIN', 'page_id=rc_cur_id'),
448 ## Empty array for fields, it'll be set by us anyway.
451 ## Modify query for tags
452 ChangeTags
::modifyDisplayQuery( $info['tables'],
457 $this->opts
['tagfilter'] );
462 function getIndexField() {
463 return 'rc_timestamp';
466 function formatRow( $row ) {
467 return $this->mForm
->formatRow( $row );
470 function getStartBody() {
471 # Do a batch existence check on pages
472 $linkBatch = new LinkBatch();
473 while( $row = $this->mResult
->fetchObject() ) {
474 $linkBatch->add( NS_USER
, $row->rc_user_text
);
475 $linkBatch->add( NS_USER_TALK
, $row->rc_user_text
);
476 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
478 $linkBatch->execute();
482 function getEndBody() {