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
{
38 // Some internal settings
39 protected $showNavigation = false;
41 public function __construct() {
42 parent
::__construct( 'Newpages' );
45 protected function setup( $par ) {
46 global $wgEnableNewpagesUserFilter;
49 $opts = new FormOptions();
50 $this->opts
= $opts; // bind
51 $opts->add( 'hideliu', false );
52 $opts->add( 'hidepatrolled', $this->getUser()->getBoolOption( 'newpageshidepatrolled' ) );
53 $opts->add( 'hidebots', false );
54 $opts->add( 'hideredirs', true );
55 $opts->add( 'limit', (int)$this->getUser()->getOption( 'rclimit' ) );
56 $opts->add( 'offset', '' );
57 $opts->add( 'namespace', '0' );
58 $opts->add( 'username', '' );
59 $opts->add( 'feed', '' );
60 $opts->add( 'tagfilter', '' );
63 $opts->fetchValuesFromRequest( $this->getRequest() );
64 if ( $par ) $this->parseParams( $par );
67 $opts->validateIntBounds( 'limit', 0, 5000 );
68 if( !$wgEnableNewpagesUserFilter ) {
69 $opts->setValue( 'username', '' );
73 protected function parseParams( $par ) {
75 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
76 foreach ( $bits as $bit ) {
77 if ( 'shownav' == $bit ) {
78 $this->showNavigation
= true;
80 if ( 'hideliu' === $bit ) {
81 $this->opts
->setValue( 'hideliu', true );
83 if ( 'hidepatrolled' == $bit ) {
84 $this->opts
->setValue( 'hidepatrolled', true );
86 if ( 'hidebots' == $bit ) {
87 $this->opts
->setValue( 'hidebots', true );
89 if ( 'showredirs' == $bit ) {
90 $this->opts
->setValue( 'hideredirs', false );
92 if ( is_numeric( $bit ) ) {
93 $this->opts
->setValue( 'limit', intval( $bit ) );
97 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
98 $this->opts
->setValue( 'limit', intval( $m[1] ) );
100 // PG offsets not just digits!
101 if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) ) {
102 $this->opts
->setValue( 'offset', intval( $m[1] ) );
104 if ( preg_match( '/^username=(.*)$/', $bit, $m ) ) {
105 $this->opts
->setValue( 'username', $m[1] );
107 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
108 $ns = $wgLang->getNsIndex( $m[1] );
109 if( $ns !== false ) {
110 $this->opts
->setValue( 'namespace', $ns );
117 * Show a form for filtering namespace and username
122 public function execute( $par ) {
123 $out = $this->getOutput();
126 $this->outputHeader();
128 $this->showNavigation
= !$this->including(); // Maybe changed in setup
129 $this->setup( $par );
131 if( !$this->including() ) {
135 $this->setSyndicated();
136 $feedType = $this->opts
->getValue( 'feed' );
138 return $this->feed( $feedType );
142 $pager = new NewPagesPager( $this, $this->opts
);
143 $pager->mLimit
= $this->opts
->getValue( 'limit' );
144 $pager->mOffset
= $this->opts
->getValue( 'offset' );
146 if( $pager->getNumRows() ) {
148 if ( $this->showNavigation
) {
149 $navigation = $pager->getNavigationBar();
151 $out->addHTML( $navigation . $pager->getBody() . $navigation );
153 $out->addWikiMsg( 'specialpage-empty' );
157 protected function filterLinks() {
158 global $wgGroupPermissions, $wgLang;
161 $showhide = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ) );
163 // Option value -> message mapping
165 'hideliu' => 'rcshowhideliu',
166 'hidepatrolled' => 'rcshowhidepatr',
167 'hidebots' => 'rcshowhidebots',
168 'hideredirs' => 'whatlinkshere-hideredirs'
171 // Disable some if needed
172 # FIXME: throws E_NOTICEs if not set; and doesn't obey hooks etc.
173 if ( $wgGroupPermissions['*']['createpage'] !== true ) {
174 unset( $filters['hideliu'] );
177 if ( !$this->getUser()->useNPPatrol() ) {
178 unset( $filters['hidepatrolled'] );
182 $changed = $this->opts
->getChangedValues();
183 unset( $changed['offset'] ); // Reset offset if query type changes
185 $self = $this->getTitle();
186 foreach ( $filters as $key => $msg ) {
187 $onoff = 1 - $this->opts
->getValue( $key );
188 $link = $this->getSkin()->link( $self, $showhide[$onoff], array(),
189 array( $key => $onoff ) +
$changed
191 $links[$key] = wfMsgHtml( $msg, $link );
194 return $wgLang->pipeList( $links );
197 protected function form() {
198 global $wgEnableNewpagesUserFilter, $wgScript;
201 $this->opts
->consumeValue( 'offset' ); // don't carry offset, DWIW
202 $namespace = $this->opts
->consumeValue( 'namespace' );
203 $username = $this->opts
->consumeValue( 'username' );
204 $tagFilterVal = $this->opts
->consumeValue( 'tagfilter' );
206 // Check username input validity
207 $ut = Title
::makeTitleSafe( NS_USER
, $username );
208 $userText = $ut ?
$ut->getText() : '';
210 // Store query values in hidden fields so that form submission doesn't lose them
212 foreach ( $this->opts
->getUnconsumedValues() as $key => $value ) {
213 $hidden[] = Html
::hidden( $key, $value );
215 $hidden = implode( "\n", $hidden );
217 $tagFilter = ChangeTags
::buildTagFilterSelector( $tagFilterVal );
219 list( $tagFilterLabel, $tagFilterSelector ) = $tagFilter;
222 $form = Xml
::openElement( 'form', array( 'action' => $wgScript ) ) .
223 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
224 Xml
::fieldset( wfMsg( 'newpages' ) ) .
225 Xml
::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
227 <td class="mw-label">' .
228 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) .
230 <td class="mw-input">' .
231 Xml
::namespaceSelector( $namespace, 'all' ) .
233 </tr>' . ( $tagFilter ?
(
235 <td class="mw-label">' .
238 <td class="mw-input">' .
242 ( $wgEnableNewpagesUserFilter ?
244 <td class="mw-label">' .
245 Xml
::label( wfMsg( 'newpages-username' ), 'mw-np-username' ) .
247 <td class="mw-input">' .
248 Xml
::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
252 <td class="mw-submit">' .
253 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) .
258 <td class="mw-input">' .
259 $this->filterLinks() .
262 Xml
::closeElement( 'table' ) .
263 Xml
::closeElement( 'fieldset' ) .
265 Xml
::closeElement( 'form' );
267 $this->getOutput()->addHTML( $form );
270 protected function setSyndicated() {
271 $out = $this->getOutput();
272 $out->setSyndicated( true );
273 $out->setFeedAppendQuery( wfArrayToCGI( $this->opts
->getAllValues() ) );
277 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
279 * @param $result Result row
282 public function formatRow( $result ) {
283 global $wgLang, $wgContLang;
285 # Revision deletion works on revisions, so we should cast one
287 'comment' => $result->rc_comment
,
288 'deleted' => $result->rc_deleted
,
289 'user_text' => $result->rc_user_text
,
290 'user' => $result->rc_user
,
292 $rev = new Revision( $row );
296 $dm = $wgContLang->getDirMark();
298 $title = Title
::makeTitleSafe( $result->rc_namespace
, $result->rc_title
);
299 $time = Html
::element( 'span', array( 'class' => 'mw-newpages-time' ),
300 $wgLang->timeAndDate( $result->rc_timestamp
, true )
303 $query = array( 'redirect' => 'no' );
305 if( $this->patrollable( $result ) ) {
306 $query['rcid'] = $result->rc_id
;
309 $plink = $this->getSkin()->linkKnown(
312 array( 'class' => 'mw-newpages-pagename' ),
314 array( 'known' ) // Set explicitly to avoid the default of 'known','noclasses'. This breaks the colouration for stubs
316 $histLink = $this->getSkin()->linkKnown(
320 array( 'action' => 'history' )
322 $hist = Html
::rawElement( 'span', array( 'class' => 'mw-newpages-history' ), wfMsg( 'parentheses', $histLink ) );
324 $length = Html
::rawElement( 'span', array( 'class' => 'mw-newpages-length' ),
325 '[' . wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ), $wgLang->formatNum( $result->length
) ) .
329 $ulink = $this->getSkin()->revUserTools( $rev );
330 $comment = $this->getSkin()->revComment( $rev );
332 if ( $this->patrollable( $result ) ) {
333 $classes[] = 'not-patrolled';
336 # Add a class for zero byte pages
337 if ( $result->length
== 0 ) {
338 $classes[] = 'mw-newpages-zero-byte-page';
341 # Tags, if any. check for including due to bug 23293
342 if ( !$this->including() ) {
343 list( $tagDisplay, $newClasses ) = ChangeTags
::formatSummaryRow( $result->ts_tags
, 'newpages' );
344 $classes = array_merge( $classes, $newClasses );
349 $css = count( $classes ) ?
' class="' . implode( ' ', $classes ) . '"' : '';
351 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} {$dm}{$ulink} {$comment} {$tagDisplay}</li>\n";
355 * Should a specific result row provide "patrollable" links?
357 * @param $result Result row
360 protected function patrollable( $result ) {
361 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled
);
365 * Output a subscription feed listing recent edits to this page.
367 * @param $type String
369 protected function feed( $type ) {
370 global $wgFeed, $wgFeedClasses, $wgFeedLimit;
373 $wgOut->addWikiMsg( 'feed-unavailable' );
377 if( !isset( $wgFeedClasses[$type] ) ) {
378 $this->getOut()->addWikiMsg( 'feed-invalid' );
382 $feed = new $wgFeedClasses[$type](
384 wfMsgExt( 'tagline', 'parsemag' ),
385 $this->getTitle()->getFullUrl()
388 $pager = new NewPagesPager( $this, $this->opts
);
389 $limit = $this->opts
->getValue( 'limit' );
390 $pager->mLimit
= min( $limit, $wgFeedLimit );
393 if( $pager->getNumRows() > 0 ) {
394 foreach ( $pager->mResult
as $row ) {
395 $feed->outItem( $this->feedItem( $row ) );
401 protected function feedTitle() {
402 global $wgLanguageCode, $wgSitename;
403 $desc = $this->getDescription();
404 return "$wgSitename - $desc [$wgLanguageCode]";
407 protected function feedItem( $row ) {
408 $title = Title
::MakeTitle( intval( $row->rc_namespace
), $row->rc_title
);
410 $date = $row->rc_timestamp
;
411 $comments = $title->getTalkPage()->getFullURL();
414 $title->getPrefixedText(),
415 $this->feedItemDesc( $row ),
416 $title->getFullURL(),
418 $this->feedItemAuthor( $row ),
426 protected function feedItemAuthor( $row ) {
427 return isset( $row->rc_user_text
) ?
$row->rc_user_text
: '';
430 protected function feedItemDesc( $row ) {
431 $revision = Revision
::newFromId( $row->rev_id
);
433 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
434 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
435 "</p>\n<hr />\n<div>" .
436 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
443 * @ingroup SpecialPage Pager
445 class NewPagesPager
extends ReverseChronologicalPager
{
447 protected $opts, $mForm;
449 function __construct( $form, FormOptions
$opts ) {
450 parent
::__construct();
451 $this->mForm
= $form;
455 function getTitle() {
456 static $title = null;
457 if ( $title === null ) {
458 $title = $this->mForm
->getTitle();
463 function getQueryInfo() {
464 global $wgEnableNewpagesUserFilter, $wgGroupPermissions;
466 $conds['rc_new'] = 1;
468 $namespace = $this->opts
->getValue( 'namespace' );
469 $namespace = ( $namespace === 'all' ) ?
false : intval( $namespace );
471 $username = $this->opts
->getValue( 'username' );
472 $user = Title
::makeTitleSafe( NS_USER
, $username );
474 if( $namespace !== false ) {
475 $conds['rc_namespace'] = $namespace;
476 $rcIndexes = array( 'new_name_timestamp' );
478 $rcIndexes = array( 'rc_timestamp' );
481 # $wgEnableNewpagesUserFilter - temp WMF hack
482 if( $wgEnableNewpagesUserFilter && $user ) {
483 $conds['rc_user_text'] = $user->getText();
484 $rcIndexes = 'rc_user_text';
485 # If anons cannot make new pages, don't "exclude logged in users"!
486 } elseif( $wgGroupPermissions['*']['createpage'] && $this->opts
->getValue( 'hideliu' ) ) {
487 $conds['rc_user'] = 0;
489 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
490 if( $this->opts
->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
491 $conds['rc_patrolled'] = 0;
493 if( $this->opts
->getValue( 'hidebots' ) ) {
494 $conds['rc_bot'] = 0;
497 if ( $this->opts
->getValue( 'hideredirs' ) ) {
498 $conds['page_is_redirect'] = 0;
501 // Allow changes to the New Pages query
502 wfRunHooks( 'SpecialNewpagesConditions', array( &$this, $this->opts
, &$conds ) );
505 'tables' => array( 'recentchanges', 'page' ),
507 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user',
508 'rc_user_text', 'rc_comment', 'rc_timestamp', 'rc_patrolled',
509 'rc_id', 'rc_deleted', 'page_len AS length', 'page_latest AS rev_id',
513 'options' => array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) ),
514 'join_conds' => array(
515 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ),
519 // Empty array for fields, it'll be set by us anyway.
522 // Modify query for tags
523 ChangeTags
::modifyDisplayQuery(
529 $this->opts
['tagfilter']
535 function getIndexField() {
536 return 'rc_timestamp';
539 function formatRow( $row ) {
540 return $this->mForm
->formatRow( $row );
543 function getStartBody() {
544 # Do a batch existence check on pages
545 $linkBatch = new LinkBatch();
546 foreach ( $this->mResult
as $row ) {
547 $linkBatch->add( NS_USER
, $row->rc_user_text
);
548 $linkBatch->add( NS_USER_TALK
, $row->rc_user_text
);
549 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
551 $linkBatch->execute();
555 function getEndBody() {