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', false );
28 $opts->add( 'hidebots', false );
29 $opts->add( 'hideredirs', true );
30 $opts->add( 'limit', (int)$wgUser->getOption( 'rclimit' ) );
31 $opts->add( 'offset', '' );
32 $opts->add( 'namespace', '0' );
33 $opts->add( 'username', '' );
34 $opts->add( 'feed', '' );
37 $opts->fetchValuesFromRequest( $wgRequest );
38 if ( $par ) $this->parseParams( $par );
41 $opts->validateIntBounds( 'limit', 0, 5000 );
42 if( !$wgEnableNewpagesUserFilter ) {
43 $opts->setValue( 'username', '' );
47 $this->skin
= $wgUser->getSkin();
50 protected function parseParams( $par ) {
52 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
53 foreach ( $bits as $bit ) {
54 if ( 'shownav' == $bit )
55 $this->showNavigation
= true;
56 if ( 'hideliu' === $bit )
57 $this->opts
->setValue( 'hideliu', true );
58 if ( 'hidepatrolled' == $bit )
59 $this->opts
->setValue( 'hidepatrolled', true );
60 if ( 'hidebots' == $bit )
61 $this->opts
->setValue( 'hidebots', true );
62 if ( 'showredirs' == $bit )
63 $this->opts
->setValue( 'hideredirs', false );
64 if ( is_numeric( $bit ) )
65 $this->opts
->setValue( 'limit', intval( $bit ) );
68 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) )
69 $this->opts
->setValue( 'limit', intval($m[1]) );
70 // PG offsets not just digits!
71 if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) )
72 $this->opts
->setValue( 'offset', intval($m[1]) );
73 if ( preg_match( '/^username=(.*)$/', $bit, $m ) )
74 $this->opts
->setValue( 'username', $m[1] );
75 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
76 $ns = $wgLang->getNsIndex( $m[1] );
78 $this->opts
->setValue( 'namespace', $ns );
85 * Show a form for filtering namespace and username
90 public function execute( $par ) {
91 global $wgLang, $wgUser, $wgOut;
94 $this->outputHeader();
96 $this->showNavigation
= !$this->including(); // Maybe changed in setup
99 if( !$this->including() ) {
103 $this->setSyndicated();
104 $feedType = $this->opts
->getValue( 'feed' );
106 return $this->feed( $feedType );
110 $pager = new NewPagesPager( $this, $this->opts
);
111 $pager->mLimit
= $this->opts
->getValue( 'limit' );
112 $pager->mOffset
= $this->opts
->getValue( 'offset' );
114 if( $pager->getNumRows() ) {
116 if ( $this->showNavigation
) $navigation = $pager->getNavigationBar();
117 $wgOut->addHTML( $navigation . $pager->getBody() . $navigation );
119 $wgOut->addWikiMsg( 'specialpage-empty' );
123 protected function filterLinks() {
124 global $wgGroupPermissions, $wgUser;
127 $showhide = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ) );
129 // Option value -> message mapping
131 'hideliu' => 'rcshowhideliu',
132 'hidepatrolled' => 'rcshowhidepatr',
133 'hidebots' => 'rcshowhidebots',
134 'hideredirs' => 'whatlinkshere-hideredirs'
137 // Disable some if needed
138 if ( $wgGroupPermissions['*']['createpage'] !== true )
139 unset($filters['hideliu']);
141 if ( !$wgUser->useNPPatrol() )
142 unset($filters['hidepatrolled']);
145 $changed = $this->opts
->getChangedValues();
146 unset($changed['offset']); // Reset offset if query type changes
148 $self = $this->getTitle();
149 foreach ( $filters as $key => $msg ) {
150 $onoff = 1 - $this->opts
->getValue($key);
151 $link = $this->skin
->link( $self, $showhide[$onoff], array(),
152 array( $key => $onoff ) +
$changed
154 $links[$key] = wfMsgHtml( $msg, $link );
157 return implode( ' | ', $links );
160 protected function form() {
161 global $wgOut, $wgEnableNewpagesUserFilter, $wgScript;
164 $this->opts
->consumeValue( 'offset' ); // don't carry offset, DWIW
165 $namespace = $this->opts
->consumeValue( 'namespace' );
166 $username = $this->opts
->consumeValue( 'username' );
168 // Check username input validity
169 $ut = Title
::makeTitleSafe( NS_USER
, $username );
170 $userText = $ut ?
$ut->getText() : '';
172 // Store query values in hidden fields so that form submission doesn't lose them
174 foreach ( $this->opts
->getUnconsumedValues() as $key => $value ) {
175 $hidden[] = Xml
::hidden( $key, $value );
177 $hidden = implode( "\n", $hidden );
179 $form = Xml
::openElement( 'form', array( 'action' => $wgScript ) ) .
180 Xml
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
181 Xml
::fieldset( wfMsg( 'newpages' ) ) .
182 Xml
::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
184 <td class='mw-label'>" .
185 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) .
187 <td class='mw-input'>" .
188 Xml
::namespaceSelector( $namespace, 'all' ) .
191 ($wgEnableNewpagesUserFilter ?
193 <td class='mw-label'>" .
194 Xml
::label( wfMsg( 'newpages-username' ), 'mw-np-username' ) .
196 <td class='mw-input'>" .
197 Xml
::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
201 <td class='mw-submit'>" .
202 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) .
207 <td class='mw-input'>" .
208 $this->filterLinks() .
211 Xml
::closeElement( 'table' ) .
212 Xml
::closeElement( 'fieldset' ) .
214 Xml
::closeElement( 'form' );
216 $wgOut->addHTML( $form );
219 protected function setSyndicated() {
221 $queryParams = array(
222 'namespace' => $this->opts
->getValue( 'namespace' ),
223 'username' => $this->opts
->getValue( 'username' )
225 $wgOut->setSyndicated( true );
226 $wgOut->setFeedAppendQuery( wfArrayToCGI( $queryParams ) );
230 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
232 * @param $skin Skin to use
233 * @param $result Result row
236 public function formatRow( $result ) {
237 global $wgLang, $wgContLang, $wgUser;
238 $dm = $wgContLang->getDirMark();
240 $title = Title
::makeTitleSafe( $result->rc_namespace
, $result->rc_title
);
241 $time = $wgLang->timeAndDate( $result->rc_timestamp
, true );
242 $query = $this->patrollable( $result ) ?
"rcid={$result->rc_id}&redirect=no" : 'redirect=no';
243 $plink = $this->skin
->makeKnownLinkObj( $title, '', $query );
244 $hist = $this->skin
->makeKnownLinkObj( $title, wfMsgHtml( 'hist' ), 'action=history' );
245 $length = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
246 $wgLang->formatNum( $result->length
) );
247 $ulink = $this->skin
->userLink( $result->rc_user
, $result->rc_user_text
) . ' ' .
248 $this->skin
->userToolLinks( $result->rc_user
, $result->rc_user_text
);
249 $comment = $this->skin
->commentBlock( $result->rc_comment
);
250 $css = $this->patrollable( $result ) ?
" class='not-patrolled'" : '';
252 return "<li{$css}>{$time} {$dm}{$plink} ({$hist}) {$dm}[{$length}] {$dm}{$ulink} {$comment}</li>\n";
256 * Should a specific result row provide "patrollable" links?
258 * @param $result Result row
261 protected function patrollable( $result ) {
263 return ( $wgUser->useNPPatrol() && !$result->rc_patrolled
);
267 * Output a subscription feed listing recent edits to this page.
268 * @param string $type
270 protected function feed( $type ) {
271 global $wgFeed, $wgFeedClasses, $wgFeedLimit;
275 $wgOut->addWikiMsg( 'feed-unavailable' );
279 if( !isset( $wgFeedClasses[$type] ) ) {
281 $wgOut->addWikiMsg( 'feed-invalid' );
285 $feed = new $wgFeedClasses[$type](
287 wfMsgExt( 'tagline', 'parsemag' ),
288 $this->getTitle()->getFullUrl() );
290 $pager = new NewPagesPager( $this, $this->opts
);
291 $limit = $this->opts
->getValue( 'limit' );
292 $pager->mLimit
= min( $limit, $wgFeedLimit );
295 if( $pager->getNumRows() > 0 ) {
296 while( $row = $pager->mResult
->fetchObject() ) {
297 $feed->outItem( $this->feedItem( $row ) );
303 protected function feedTitle() {
304 global $wgContLanguageCode, $wgSitename;
305 $page = SpecialPage
::getPage( 'Newpages' );
306 $desc = $page->getDescription();
307 return "$wgSitename - $desc [$wgContLanguageCode]";
310 protected function feedItem( $row ) {
311 $title = Title
::MakeTitle( intval( $row->rc_namespace
), $row->rc_title
);
313 $date = $row->rc_timestamp
;
314 $comments = $title->getTalkPage()->getFullURL();
317 $title->getPrefixedText(),
318 $this->feedItemDesc( $row ),
319 $title->getFullURL(),
321 $this->feedItemAuthor( $row ),
328 protected function feedItemAuthor( $row ) {
329 return isset( $row->rc_user_text
) ?
$row->rc_user_text
: '';
332 protected function feedItemDesc( $row ) {
333 $revision = Revision
::newFromId( $row->rev_id
);
335 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
336 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
337 "</p>\n<hr />\n<div>" .
338 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
345 * @ingroup SpecialPage Pager
347 class NewPagesPager
extends ReverseChronologicalPager
{
349 protected $opts, $mForm;
351 function __construct( $form, FormOptions
$opts ) {
352 parent
::__construct();
353 $this->mForm
= $form;
357 function getTitle() {
358 static $title = null;
359 if ( $title === null )
360 $title = $this->mForm
->getTitle();
364 function getQueryInfo() {
365 global $wgEnableNewpagesUserFilter, $wgGroupPermissions, $wgUser;
367 $conds['rc_new'] = 1;
369 $namespace = $this->opts
->getValue( 'namespace' );
370 $namespace = ( $namespace === 'all' ) ?
false : intval( $namespace );
372 $username = $this->opts
->getValue( 'username' );
373 $user = Title
::makeTitleSafe( NS_USER
, $username );
375 if( $namespace !== false ) {
376 $conds['rc_namespace'] = $namespace;
377 $rcIndexes = array( 'new_name_timestamp' );
379 $rcIndexes = array( 'rc_timestamp' );
381 $conds[] = 'page_id = rc_cur_id';
383 # $wgEnableNewpagesUserFilter - temp WMF hack
384 if( $wgEnableNewpagesUserFilter && $user ) {
385 $conds['rc_user_text'] = $user->getText();
386 $rcIndexes = 'rc_user_text';
387 # If anons cannot make new pages, don't "exclude logged in users"!
388 } elseif( $wgGroupPermissions['*']['createpage'] && $this->opts
->getValue( 'hideliu' ) ) {
389 $conds['rc_user'] = 0;
391 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
392 if( $this->opts
->getValue( 'hidepatrolled' ) && $wgUser->useNPPatrol() ) {
393 $conds['rc_patrolled'] = 0;
395 if( $this->opts
->getValue( 'hidebots' ) ) {
396 $conds['rc_bot'] = 0;
399 if ( $this->opts
->getValue( 'hideredirs' ) ) {
400 $conds['page_is_redirect'] = 0;
404 'tables' => array( 'recentchanges', 'page' ),
405 'fields' => 'rc_namespace,rc_title, rc_cur_id, rc_user,rc_user_text,rc_comment,
406 rc_timestamp,rc_patrolled,rc_id,page_len as length, page_latest as rev_id',
408 'options' => array( 'USE INDEX' => array('recentchanges' => $rcIndexes) )
412 function getIndexField() {
413 return 'rc_timestamp';
416 function formatRow( $row ) {
417 return $this->mForm
->formatRow( $row );
420 function getStartBody() {
421 # Do a batch existence check on pages
422 $linkBatch = new LinkBatch();
423 while( $row = $this->mResult
->fetchObject() ) {
424 $linkBatch->add( NS_USER
, $row->rc_user_text
);
425 $linkBatch->add( NS_USER_TALK
, $row->rc_user_text
);
426 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
428 $linkBatch->execute();
432 function getEndBody() {