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
{
34 protected $customFilters;
36 protected $showNavigation = false;
38 public function __construct() {
39 parent
::__construct( 'Newpages' );
42 protected function setup( $par ) {
44 $opts = new FormOptions();
45 $this->opts
= $opts; // bind
46 $opts->add( 'hideliu', false );
47 $opts->add( 'hidepatrolled', $this->getUser()->getBoolOption( 'newpageshidepatrolled' ) );
48 $opts->add( 'hidebots', false );
49 $opts->add( 'hideredirs', true );
50 $opts->add( 'limit', $this->getUser()->getIntOption( 'rclimit' ) );
51 $opts->add( 'offset', '' );
52 $opts->add( 'namespace', '0' );
53 $opts->add( 'username', '' );
54 $opts->add( 'feed', '' );
55 $opts->add( 'tagfilter', '' );
56 $opts->add( 'invert', false );
58 $this->customFilters
= array();
59 Hooks
::run( 'SpecialNewPagesFilters', array( $this, &$this->customFilters
) );
60 foreach ( $this->customFilters
as $key => $params ) {
61 $opts->add( $key, $params['default'] );
65 $opts->fetchValuesFromRequest( $this->getRequest() );
67 $this->parseParams( $par );
71 $opts->validateIntBounds( 'limit', 0, 5000 );
74 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 = $this->getLanguage()->getNsIndex( $m[1] );
109 if ( $ns !== false ) {
110 $this->opts
->setValue( 'namespace', $ns );
117 * Show a form for filtering namespace and username
121 public function execute( $par ) {
122 $out = $this->getOutput();
125 $this->outputHeader();
127 $this->showNavigation
= !$this->including(); // Maybe changed in setup
128 $this->setup( $par );
130 $this->addHelpLink( 'Help:New pages' );
132 if ( !$this->including() ) {
136 $feedType = $this->opts
->getValue( 'feed' );
138 $this->feed( $feedType );
143 $allValues = $this->opts
->getAllValues();
144 unset( $allValues['feed'] );
145 $out->setFeedAppendQuery( wfArrayToCgi( $allValues ) );
148 $pager = new NewPagesPager( $this, $this->opts
);
149 $pager->mLimit
= $this->opts
->getValue( 'limit' );
150 $pager->mOffset
= $this->opts
->getValue( 'offset' );
152 if ( $pager->getNumRows() ) {
154 if ( $this->showNavigation
) {
155 $navigation = $pager->getNavigationBar();
157 $out->addHTML( $navigation . $pager->getBody() . $navigation );
159 $out->addWikiMsg( 'specialpage-empty' );
163 protected function filterLinks() {
165 $showhide = array( $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() );
167 // Option value -> message mapping
169 'hideliu' => 'rcshowhideliu',
170 'hidepatrolled' => 'rcshowhidepatr',
171 'hidebots' => 'rcshowhidebots',
172 'hideredirs' => 'whatlinkshere-hideredirs'
174 foreach ( $this->customFilters
as $key => $params ) {
175 $filters[$key] = $params['msg'];
178 // Disable some if needed
179 if ( !User
::groupHasPermission( '*', 'createpage' ) ) {
180 unset( $filters['hideliu'] );
182 if ( !$this->getUser()->useNPPatrol() ) {
183 unset( $filters['hidepatrolled'] );
187 $changed = $this->opts
->getChangedValues();
188 unset( $changed['offset'] ); // Reset offset if query type changes
190 $self = $this->getPageTitle();
191 foreach ( $filters as $key => $msg ) {
192 $onoff = 1 - $this->opts
->getValue( $key );
193 $link = Linker
::link( $self, $showhide[$onoff], array(),
194 array( $key => $onoff ) +
$changed
196 $links[$key] = $this->msg( $msg )->rawParams( $link )->escaped();
199 return $this->getLanguage()->pipeList( $links );
202 protected function form() {
203 $out = $this->getOutput();
204 $out->addModules( 'mediawiki.userSuggest' );
207 $this->opts
->consumeValue( 'offset' ); // don't carry offset, DWIW
208 $namespace = $this->opts
->consumeValue( 'namespace' );
209 $username = $this->opts
->consumeValue( 'username' );
210 $tagFilterVal = $this->opts
->consumeValue( 'tagfilter' );
211 $nsinvert = $this->opts
->consumeValue( 'invert' );
213 // Check username input validity
214 $ut = Title
::makeTitleSafe( NS_USER
, $username );
215 $userText = $ut ?
$ut->getText() : '';
217 // Store query values in hidden fields so that form submission doesn't lose them
219 foreach ( $this->opts
->getUnconsumedValues() as $key => $value ) {
220 $hidden[] = Html
::hidden( $key, $value );
222 $hidden = implode( "\n", $hidden );
225 'namespace' => array(
226 'type' => 'namespaceselect',
227 'name' => 'namespace',
228 'label-message' => 'namespace',
229 'default' => $namespace,
234 'label-message' => 'invert',
235 'default' => $nsinvert,
236 'tooltip' => 'invert',
238 'tagFilter' => array(
239 'type' => 'tagfilter',
240 'name' => 'tagfilter',
241 'label-raw' => $this->msg( 'tag-filter' )->parse(),
242 'default' => $tagFilterVal,
246 'name' => 'username',
247 'label-message' => 'newpages-username',
248 'default' => $userText,
249 'id' => 'mw-np-username',
251 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
255 $htmlForm = new HTMLForm( $form, $this->getContext() );
257 $htmlForm->setSubmitText( $this->msg( 'newpages-submit' )->text() );
258 $htmlForm->setSubmitProgressive();
259 // The form should be visible on each request (inclusive requests with submitted forms), so
260 // return always false here.
261 $htmlForm->setSubmitCallback(
266 $htmlForm->setMethod( 'get' );
268 $out->addHtml( Xml
::fieldset( $this->msg( 'newpages' )->text() ) );
278 Xml
::closeElement( 'fieldset' )
283 * Format a row, providing the timestamp, links to the page/history,
284 * size, user links, and a comment
286 * @param object $result Result row
289 public function formatRow( $result ) {
290 $title = Title
::newFromRow( $result );
292 # Revision deletion works on revisions, so we should cast one
294 'comment' => $result->rc_comment
,
295 'deleted' => $result->rc_deleted
,
296 'user_text' => $result->rc_user_text
,
297 'user' => $result->rc_user
,
299 $rev = new Revision( $row );
300 $rev->setTitle( $title );
304 $lang = $this->getLanguage();
305 $dm = $lang->getDirMark();
307 $spanTime = Html
::element( 'span', array( 'class' => 'mw-newpages-time' ),
308 $lang->userTimeAndDate( $result->rc_timestamp
, $this->getUser() )
310 $time = Linker
::linkKnown(
314 array( 'oldid' => $result->rc_this_oldid
),
318 $query = $title->isRedirect() ?
array( 'redirect' => 'no' ) : array();
320 // Linker::linkKnown() uses 'known' and 'noclasses' options.
321 // This breaks the colouration for stubs.
322 $plink = Linker
::link(
325 array( 'class' => 'mw-newpages-pagename' ),
329 $histLink = Linker
::linkKnown(
331 $this->msg( 'hist' )->escaped(),
333 array( 'action' => 'history' )
335 $hist = Html
::rawElement( 'span', array( 'class' => 'mw-newpages-history' ),
336 $this->msg( 'parentheses' )->rawParams( $histLink )->escaped() );
338 $length = Html
::rawElement(
340 array( 'class' => 'mw-newpages-length' ),
341 $this->msg( 'brackets' )->rawParams(
342 $this->msg( 'nbytes' )->numParams( $result->length
)->escaped()
346 $ulink = Linker
::revUserTools( $rev );
347 $comment = Linker
::revComment( $rev );
349 if ( $this->patrollable( $result ) ) {
350 $classes[] = 'not-patrolled';
353 # Add a class for zero byte pages
354 if ( $result->length
== 0 ) {
355 $classes[] = 'mw-newpages-zero-byte-page';
359 if ( isset( $result->ts_tags
) ) {
360 list( $tagDisplay, $newClasses ) = ChangeTags
::formatSummaryRow(
364 $classes = array_merge( $classes, $newClasses );
369 $css = count( $classes ) ?
' class="' . implode( ' ', $classes ) . '"' : '';
371 # Display the old title if the namespace/title has been changed
373 $oldTitle = Title
::makeTitle( $result->rc_namespace
, $result->rc_title
);
375 if ( !$title->equals( $oldTitle ) ) {
376 $oldTitleText = $oldTitle->getPrefixedText();
377 $oldTitleText = $this->msg( 'rc-old-title' )->params( $oldTitleText )->escaped();
380 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} "
381 . "{$dm}{$ulink} {$comment} {$tagDisplay} {$oldTitleText}</li>\n";
385 * Should a specific result row provide "patrollable" links?
387 * @param object $result Result row
390 protected function patrollable( $result ) {
391 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled
);
395 * Output a subscription feed listing recent edits to this page.
397 * @param string $type
399 protected function feed( $type ) {
400 if ( !$this->getConfig()->get( 'Feed' ) ) {
401 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
406 $feedClasses = $this->getConfig()->get( 'FeedClasses' );
407 if ( !isset( $feedClasses[$type] ) ) {
408 $this->getOutput()->addWikiMsg( 'feed-invalid' );
413 $feed = new $feedClasses[$type](
415 $this->msg( 'tagline' )->text(),
416 $this->getPageTitle()->getFullURL()
419 $pager = new NewPagesPager( $this, $this->opts
);
420 $limit = $this->opts
->getValue( 'limit' );
421 $pager->mLimit
= min( $limit, $this->getConfig()->get( 'FeedLimit' ) );
424 if ( $pager->getNumRows() > 0 ) {
425 foreach ( $pager->mResult
as $row ) {
426 $feed->outItem( $this->feedItem( $row ) );
432 protected function feedTitle() {
433 $desc = $this->getDescription();
434 $code = $this->getConfig()->get( 'LanguageCode' );
435 $sitename = $this->getConfig()->get( 'Sitename' );
437 return "$sitename - $desc [$code]";
440 protected function feedItem( $row ) {
441 $title = Title
::makeTitle( intval( $row->rc_namespace
), $row->rc_title
);
443 $date = $row->rc_timestamp
;
444 $comments = $title->getTalkPage()->getFullURL();
447 $title->getPrefixedText(),
448 $this->feedItemDesc( $row ),
449 $title->getFullURL(),
451 $this->feedItemAuthor( $row ),
459 protected function feedItemAuthor( $row ) {
460 return isset( $row->rc_user_text
) ?
$row->rc_user_text
: '';
463 protected function feedItemDesc( $row ) {
464 $revision = Revision
::newFromId( $row->rev_id
);
466 // XXX: include content model/type in feed item?
467 return '<p>' . htmlspecialchars( $revision->getUserText() ) .
468 $this->msg( 'colon-separator' )->inContentLanguage()->escaped() .
469 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
470 "</p>\n<hr />\n<div>" .
471 nl2br( htmlspecialchars( $revision->getContent()->serialize() ) ) . "</div>";
477 protected function getGroupName() {
483 * @ingroup SpecialPage Pager
485 class NewPagesPager
extends ReverseChronologicalPager
{
494 function __construct( $form, FormOptions
$opts ) {
495 parent
::__construct( $form->getContext() );
496 $this->mForm
= $form;
500 function getQueryInfo() {
502 $conds['rc_new'] = 1;
504 $namespace = $this->opts
->getValue( 'namespace' );
505 $namespace = ( $namespace === 'all' ) ?
false : intval( $namespace );
507 $username = $this->opts
->getValue( 'username' );
508 $user = Title
::makeTitleSafe( NS_USER
, $username );
510 $rcIndexes = array();
512 if ( $namespace !== false ) {
513 if ( $this->opts
->getValue( 'invert' ) ) {
514 $conds[] = 'rc_namespace != ' . $this->mDb
->addQuotes( $namespace );
516 $conds['rc_namespace'] = $namespace;
521 $conds['rc_user_text'] = $user->getText();
522 $rcIndexes = 'rc_user_text';
523 } elseif ( User
::groupHasPermission( '*', 'createpage' ) &&
524 $this->opts
->getValue( 'hideliu' )
526 # If anons cannot make new pages, don't "exclude logged in users"!
527 $conds['rc_user'] = 0;
530 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
531 if ( $this->opts
->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
532 $conds['rc_patrolled'] = 0;
535 if ( $this->opts
->getValue( 'hidebots' ) ) {
536 $conds['rc_bot'] = 0;
539 if ( $this->opts
->getValue( 'hideredirs' ) ) {
540 $conds['page_is_redirect'] = 0;
543 // Allow changes to the New Pages query
544 $tables = array( 'recentchanges', 'page' );
546 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
547 'rc_comment', 'rc_timestamp', 'rc_patrolled', 'rc_id', 'rc_deleted',
548 'length' => 'page_len', 'rev_id' => 'page_latest', 'rc_this_oldid',
549 'page_namespace', 'page_title'
551 $join_conds = array( 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ) );
553 Hooks
::run( 'SpecialNewpagesConditions',
554 array( &$this, $this->opts
, &$conds, &$tables, &$fields, &$join_conds ) );
559 $options = array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) );
566 'options' => $options,
567 'join_conds' => $join_conds
570 // Modify query for tags
571 ChangeTags
::modifyDisplayQuery(
577 $this->opts
['tagfilter']
583 function getIndexField() {
584 return 'rc_timestamp';
587 function formatRow( $row ) {
588 return $this->mForm
->formatRow( $row );
591 function getStartBody() {
592 # Do a batch existence check on pages
593 $linkBatch = new LinkBatch();
594 foreach ( $this->mResult
as $row ) {
595 $linkBatch->add( NS_USER
, $row->rc_user_text
);
596 $linkBatch->add( NS_USER_TALK
, $row->rc_user_text
);
597 $linkBatch->add( $row->page_namespace
, $row->page_title
);
599 $linkBatch->execute();
604 function getEndBody() {