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
{
37 protected $customFilters;
39 // Some internal settings
40 protected $showNavigation = false;
42 public function __construct() {
43 parent
::__construct( 'Newpages' );
46 protected function setup( $par ) {
47 global $wgEnableNewpagesUserFilter;
50 $opts = new FormOptions();
51 $this->opts
= $opts; // bind
52 $opts->add( 'hideliu', false );
53 $opts->add( 'hidepatrolled', $this->getUser()->getBoolOption( 'newpageshidepatrolled' ) );
54 $opts->add( 'hidebots', false );
55 $opts->add( 'hideredirs', true );
56 $opts->add( 'limit', (int)$this->getUser()->getOption( 'rclimit' ) );
57 $opts->add( 'offset', '' );
58 $opts->add( 'namespace', '0' );
59 $opts->add( 'username', '' );
60 $opts->add( 'feed', '' );
61 $opts->add( 'tagfilter', '' );
63 $this->customFilters
= array();
64 wfRunHooks( 'SpecialNewPagesFilters', array( $this, &$this->customFilters
) );
65 foreach( $this->customFilters
as $key => $params ) {
66 $opts->add( $key, $params['default'] );
70 $opts->fetchValuesFromRequest( $this->getRequest() );
71 if ( $par ) $this->parseParams( $par );
74 $opts->validateIntBounds( 'limit', 0, 5000 );
75 if( !$wgEnableNewpagesUserFilter ) {
76 $opts->setValue( 'username', '' );
80 protected function parseParams( $par ) {
81 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
82 foreach ( $bits as $bit ) {
83 if ( 'shownav' == $bit ) {
84 $this->showNavigation
= true;
86 if ( 'hideliu' === $bit ) {
87 $this->opts
->setValue( 'hideliu', true );
89 if ( 'hidepatrolled' == $bit ) {
90 $this->opts
->setValue( 'hidepatrolled', true );
92 if ( 'hidebots' == $bit ) {
93 $this->opts
->setValue( 'hidebots', true );
95 if ( 'showredirs' == $bit ) {
96 $this->opts
->setValue( 'hideredirs', false );
98 if ( is_numeric( $bit ) ) {
99 $this->opts
->setValue( 'limit', intval( $bit ) );
103 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
104 $this->opts
->setValue( 'limit', intval( $m[1] ) );
106 // PG offsets not just digits!
107 if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) ) {
108 $this->opts
->setValue( 'offset', intval( $m[1] ) );
110 if ( preg_match( '/^username=(.*)$/', $bit, $m ) ) {
111 $this->opts
->setValue( 'username', $m[1] );
113 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
114 $ns = $this->getLanguage()->getNsIndex( $m[1] );
115 if( $ns !== false ) {
116 $this->opts
->setValue( 'namespace', $ns );
123 * Show a form for filtering namespace and username
128 public function execute( $par ) {
129 $out = $this->getOutput();
132 $this->outputHeader();
134 $this->showNavigation
= !$this->including(); // Maybe changed in setup
135 $this->setup( $par );
137 if( !$this->including() ) {
141 $feedType = $this->opts
->getValue( 'feed' );
143 return $this->feed( $feedType );
146 $out->setFeedAppendQuery( wfArrayToCGI( $this->opts
->getAllValues() ) );
149 $pager = new NewPagesPager( $this, $this->opts
);
150 $pager->mLimit
= $this->opts
->getValue( 'limit' );
151 $pager->mOffset
= $this->opts
->getValue( 'offset' );
153 if( $pager->getNumRows() ) {
155 if ( $this->showNavigation
) {
156 $navigation = $pager->getNavigationBar();
158 $out->addHTML( $navigation . $pager->getBody() . $navigation );
160 $out->addWikiMsg( 'specialpage-empty' );
164 protected function filterLinks() {
165 global $wgGroupPermissions;
168 $showhide = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ) );
170 // Option value -> message mapping
172 'hideliu' => 'rcshowhideliu',
173 'hidepatrolled' => 'rcshowhidepatr',
174 'hidebots' => 'rcshowhidebots',
175 'hideredirs' => 'whatlinkshere-hideredirs'
177 foreach ( $this->customFilters
as $key => $params ) {
178 $filters[$key] = $params['msg'];
181 // Disable some if needed
182 # @todo FIXME: Throws E_NOTICEs if not set; and doesn't obey hooks etc.
183 if ( $wgGroupPermissions['*']['createpage'] !== true ) {
184 unset( $filters['hideliu'] );
186 if ( !$this->getUser()->useNPPatrol() ) {
187 unset( $filters['hidepatrolled'] );
191 $changed = $this->opts
->getChangedValues();
192 unset( $changed['offset'] ); // Reset offset if query type changes
194 $self = $this->getTitle();
195 foreach ( $filters as $key => $msg ) {
196 $onoff = 1 - $this->opts
->getValue( $key );
197 $link = Linker
::link( $self, $showhide[$onoff], array(),
198 array( $key => $onoff ) +
$changed
200 $links[$key] = wfMsgHtml( $msg, $link );
203 return $this->getLanguage()->pipeList( $links );
206 protected function form() {
207 global $wgEnableNewpagesUserFilter, $wgScript;
210 $this->opts
->consumeValue( 'offset' ); // don't carry offset, DWIW
211 $namespace = $this->opts
->consumeValue( 'namespace' );
212 $username = $this->opts
->consumeValue( 'username' );
213 $tagFilterVal = $this->opts
->consumeValue( 'tagfilter' );
215 // Check username input validity
216 $ut = Title
::makeTitleSafe( NS_USER
, $username );
217 $userText = $ut ?
$ut->getText() : '';
219 // Store query values in hidden fields so that form submission doesn't lose them
221 foreach ( $this->opts
->getUnconsumedValues() as $key => $value ) {
222 $hidden[] = Html
::hidden( $key, $value );
224 $hidden = implode( "\n", $hidden );
226 $tagFilter = ChangeTags
::buildTagFilterSelector( $tagFilterVal );
228 list( $tagFilterLabel, $tagFilterSelector ) = $tagFilter;
231 $form = Xml
::openElement( 'form', array( 'action' => $wgScript ) ) .
232 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
233 Xml
::fieldset( wfMsg( 'newpages' ) ) .
234 Xml
::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
236 <td class="mw-label">' .
237 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) .
239 <td class="mw-input">' .
240 Xml
::namespaceSelector( $namespace, 'all' ) .
242 </tr>' . ( $tagFilter ?
(
244 <td class="mw-label">' .
247 <td class="mw-input">' .
251 ( $wgEnableNewpagesUserFilter ?
253 <td class="mw-label">' .
254 Xml
::label( wfMsg( 'newpages-username' ), 'mw-np-username' ) .
256 <td class="mw-input">' .
257 Xml
::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
261 <td class="mw-submit">' .
262 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) .
267 <td class="mw-input">' .
268 $this->filterLinks() .
271 Xml
::closeElement( 'table' ) .
272 Xml
::closeElement( 'fieldset' ) .
274 Xml
::closeElement( 'form' );
276 $this->getOutput()->addHTML( $form );
280 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
282 * @param $result Result row
285 public function formatRow( $result ) {
286 # Revision deletion works on revisions, so we should cast one
288 'comment' => $result->rc_comment
,
289 'deleted' => $result->rc_deleted
,
290 'user_text' => $result->rc_user_text
,
291 'user' => $result->rc_user
,
293 $rev = new Revision( $row );
297 $lang = $this->getLanguage();
298 $dm = $lang->getDirMark();
300 $title = Title
::newFromRow( $result );
301 $spanTime = Html
::element( 'span', array( 'class' => 'mw-newpages-time' ),
302 $lang->timeanddate( $result->rc_timestamp
, true )
304 $time = Linker
::linkKnown(
308 array( 'oldid' => $result->rc_this_oldid
),
312 $query = array( 'redirect' => 'no' );
314 if( $this->patrollable( $result ) ) {
315 $query['rcid'] = $result->rc_id
;
318 $plink = Linker
::linkKnown(
321 array( 'class' => 'mw-newpages-pagename' ),
323 array( 'known' ) // Set explicitly to avoid the default of 'known','noclasses'. This breaks the colouration for stubs
325 $histLink = Linker
::linkKnown(
329 array( 'action' => 'history' )
331 $hist = Html
::rawElement( 'span', array( 'class' => 'mw-newpages-history' ), wfMsg( 'parentheses', $histLink ) );
333 $length = Html
::element( 'span', array( 'class' => 'mw-newpages-length' ),
334 '[' . $this->msg( 'nbytes' )->numParams( $result->length
)->text() . ']'
337 $ulink = Linker
::revUserTools( $rev );
338 $comment = Linker
::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 # Display the old title if the namespace/title has been changed
361 $oldTitle = Title
::makeTitle( $result->rc_namespace
, $result->rc_title
);
362 if ( !$title->equals( $oldTitle ) ) {
363 $oldTitleText = wfMessage( 'rc-old-title' )->params( $oldTitle->getPrefixedText() )->escaped();
366 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} {$dm}{$ulink} {$comment} {$tagDisplay} {$oldTitleText}</li>\n";
370 * Should a specific result row provide "patrollable" links?
372 * @param $result Result row
375 protected function patrollable( $result ) {
376 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled
);
380 * Output a subscription feed listing recent edits to this page.
382 * @param $type String
384 protected function feed( $type ) {
385 global $wgFeed, $wgFeedClasses, $wgFeedLimit;
388 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
392 if( !isset( $wgFeedClasses[$type] ) ) {
393 $this->getOutput()->addWikiMsg( 'feed-invalid' );
397 $feed = new $wgFeedClasses[$type](
399 wfMsgExt( 'tagline', 'parsemag' ),
400 $this->getTitle()->getFullUrl()
403 $pager = new NewPagesPager( $this, $this->opts
);
404 $limit = $this->opts
->getValue( 'limit' );
405 $pager->mLimit
= min( $limit, $wgFeedLimit );
408 if( $pager->getNumRows() > 0 ) {
409 foreach ( $pager->mResult
as $row ) {
410 $feed->outItem( $this->feedItem( $row ) );
416 protected function feedTitle() {
417 global $wgLanguageCode, $wgSitename;
418 $desc = $this->getDescription();
419 return "$wgSitename - $desc [$wgLanguageCode]";
422 protected function feedItem( $row ) {
423 $title = Title
::MakeTitle( intval( $row->rc_namespace
), $row->rc_title
);
425 $date = $row->rc_timestamp
;
426 $comments = $title->getTalkPage()->getFullURL();
429 $title->getPrefixedText(),
430 $this->feedItemDesc( $row ),
431 $title->getFullURL(),
433 $this->feedItemAuthor( $row ),
441 protected function feedItemAuthor( $row ) {
442 return isset( $row->rc_user_text
) ?
$row->rc_user_text
: '';
445 protected function feedItemDesc( $row ) {
446 $revision = Revision
::newFromId( $row->rev_id
);
448 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
449 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
450 "</p>\n<hr />\n<div>" .
451 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
458 * @ingroup SpecialPage Pager
460 class NewPagesPager
extends ReverseChronologicalPager
{
469 function __construct( $form, FormOptions
$opts ) {
470 parent
::__construct( $form->getContext() );
471 $this->mForm
= $form;
475 function getQueryInfo() {
476 global $wgEnableNewpagesUserFilter, $wgGroupPermissions;
478 $conds['rc_new'] = 1;
480 $namespace = $this->opts
->getValue( 'namespace' );
481 $namespace = ( $namespace === 'all' ) ?
false : intval( $namespace );
483 $username = $this->opts
->getValue( 'username' );
484 $user = Title
::makeTitleSafe( NS_USER
, $username );
486 if( $namespace !== false ) {
487 $conds['rc_namespace'] = $namespace;
488 $rcIndexes = array( 'new_name_timestamp' );
490 $rcIndexes = array( 'rc_timestamp' );
493 # $wgEnableNewpagesUserFilter - temp WMF hack
494 if( $wgEnableNewpagesUserFilter && $user ) {
495 $conds['rc_user_text'] = $user->getText();
496 $rcIndexes = 'rc_user_text';
497 # If anons cannot make new pages, don't "exclude logged in users"!
498 } elseif( $wgGroupPermissions['*']['createpage'] && $this->opts
->getValue( 'hideliu' ) ) {
499 $conds['rc_user'] = 0;
501 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
502 if( $this->opts
->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
503 $conds['rc_patrolled'] = 0;
505 if( $this->opts
->getValue( 'hidebots' ) ) {
506 $conds['rc_bot'] = 0;
509 if ( $this->opts
->getValue( 'hideredirs' ) ) {
510 $conds['page_is_redirect'] = 0;
513 // Allow changes to the New Pages query
514 $tables = array( 'recentchanges', 'page' );
516 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
517 'rc_comment', 'rc_timestamp', 'rc_patrolled','rc_id', 'rc_deleted',
518 'page_len AS length', 'page_latest AS rev_id', 'ts_tags', 'rc_this_oldid',
519 'page_namespace', 'page_title'
521 $join_conds = array( 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ) );
523 wfRunHooks( 'SpecialNewpagesConditions',
524 array( &$this, $this->opts
, &$conds, &$tables, &$fields, &$join_conds ) );
530 'options' => array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) ),
531 'join_conds' => $join_conds
534 // Empty array for fields, it'll be set by us anyway.
537 // Modify query for tags
538 ChangeTags
::modifyDisplayQuery(
544 $this->opts
['tagfilter']
550 function getIndexField() {
551 return 'rc_timestamp';
554 function formatRow( $row ) {
555 return $this->mForm
->formatRow( $row );
558 function getStartBody() {
559 # Do a batch existence check on pages
560 $linkBatch = new LinkBatch();
561 foreach ( $this->mResult
as $row ) {
562 $linkBatch->add( NS_USER
, $row->rc_user_text
);
563 $linkBatch->add( NS_USER_TALK
, $row->rc_user_text
);
564 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
566 $linkBatch->execute();
570 function getEndBody() {