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 wfRunHooks( '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();
123 $out->addModules( 'mediawiki.userSuggest' );
126 $this->outputHeader();
128 $this->showNavigation
= !$this->including(); // Maybe changed in setup
129 $this->setup( $par );
131 if ( !$this->including() ) {
135 $feedType = $this->opts
->getValue( 'feed' );
137 $this->feed( $feedType );
142 $allValues = $this->opts
->getAllValues();
143 unset( $allValues['feed'] );
144 $out->setFeedAppendQuery( wfArrayToCgi( $allValues ) );
147 $pager = new NewPagesPager( $this, $this->opts
);
148 $pager->mLimit
= $this->opts
->getValue( 'limit' );
149 $pager->mOffset
= $this->opts
->getValue( 'offset' );
151 if ( $pager->getNumRows() ) {
153 if ( $this->showNavigation
) {
154 $navigation = $pager->getNavigationBar();
156 $out->addHTML( $navigation . $pager->getBody() . $navigation );
158 $out->addWikiMsg( 'specialpage-empty' );
162 protected function filterLinks() {
164 $showhide = array( $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() );
166 // Option value -> message mapping
168 'hideliu' => 'rcshowhideliu',
169 'hidepatrolled' => 'rcshowhidepatr',
170 'hidebots' => 'rcshowhidebots',
171 'hideredirs' => 'whatlinkshere-hideredirs'
173 foreach ( $this->customFilters
as $key => $params ) {
174 $filters[$key] = $params['msg'];
177 // Disable some if needed
178 if ( !User
::groupHasPermission( '*', 'createpage' ) ) {
179 unset( $filters['hideliu'] );
181 if ( !$this->getUser()->useNPPatrol() ) {
182 unset( $filters['hidepatrolled'] );
186 $changed = $this->opts
->getChangedValues();
187 unset( $changed['offset'] ); // Reset offset if query type changes
189 $self = $this->getPageTitle();
190 foreach ( $filters as $key => $msg ) {
191 $onoff = 1 - $this->opts
->getValue( $key );
192 $link = Linker
::link( $self, $showhide[$onoff], array(),
193 array( $key => $onoff ) +
$changed
195 $links[$key] = $this->msg( $msg )->rawParams( $link )->escaped();
198 return $this->getLanguage()->pipeList( $links );
201 protected function form() {
202 $out = $this->getOutput();
204 $this->opts
->consumeValue( 'offset' ); // don't carry offset, DWIW
205 $namespace = $this->opts
->consumeValue( 'namespace' );
206 $username = $this->opts
->consumeValue( 'username' );
207 $tagFilterVal = $this->opts
->consumeValue( 'tagfilter' );
208 $nsinvert = $this->opts
->consumeValue( 'invert' );
210 // Check username input validity
211 $ut = Title
::makeTitleSafe( NS_USER
, $username );
212 $userText = $ut ?
$ut->getText() : '';
214 // Store query values in hidden fields so that form submission doesn't lose them
216 foreach ( $this->opts
->getUnconsumedValues() as $key => $value ) {
217 $hidden[] = Html
::hidden( $key, $value );
219 $hidden = implode( "\n", $hidden );
222 'namespace' => array(
223 'type' => 'namespaceselect',
224 'name' => 'namespace',
225 'label-message' => 'namespace',
226 'default' => $namespace,
230 'name' => 'nsinvert',
231 'label-message' => 'invert',
232 'default' => $nsinvert,
233 'tooltip' => $this->msg( 'tooltip-invert' )->text(),
235 'tagFilter' => array(
236 'type' => 'tagfilter',
237 'name' => 'tagfilter',
238 'label-raw' => wfMessage( 'tag-filter' )->parse(),
239 'default' => $tagFilterVal,
243 'name' => 'username',
244 'label-message' => 'newpages-username',
245 'default' => $userText,
246 'id' => 'mw-np-username',
248 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
252 $htmlForm = new HTMLForm( $form, $this->getContext() );
254 $htmlForm->setSubmitText( $this->msg( 'allpagessubmit' )->text() );
255 $htmlForm->setSubmitProgressive();
256 // The form should be visible on each request (inclusive requests with submitted forms), so
257 // return always false here.
258 $htmlForm->setSubmitCallback(
263 $htmlForm->setMethod( 'get' );
265 $out->addHtml( Xml
::fieldset( $this->msg( 'newpages' )->text() ) );
275 Xml
::closeElement( 'fieldset' )
280 * Format a row, providing the timestamp, links to the page/history,
281 * size, user links, and a comment
283 * @param object $result Result row
286 public function formatRow( $result ) {
287 $title = Title
::newFromRow( $result );
289 # Revision deletion works on revisions, so we should cast one
291 'comment' => $result->rc_comment
,
292 'deleted' => $result->rc_deleted
,
293 'user_text' => $result->rc_user_text
,
294 'user' => $result->rc_user
,
296 $rev = new Revision( $row );
297 $rev->setTitle( $title );
301 $lang = $this->getLanguage();
302 $dm = $lang->getDirMark();
304 $spanTime = Html
::element( 'span', array( 'class' => 'mw-newpages-time' ),
305 $lang->userTimeAndDate( $result->rc_timestamp
, $this->getUser() )
307 $time = Linker
::linkKnown(
311 array( 'oldid' => $result->rc_this_oldid
),
315 $query = array( 'redirect' => 'no' );
317 // Linker::linkKnown() uses 'known' and 'noclasses' options.
318 // This breaks the colouration for stubs.
319 $plink = Linker
::link(
322 array( 'class' => 'mw-newpages-pagename' ),
326 $histLink = Linker
::linkKnown(
328 $this->msg( 'hist' )->escaped(),
330 array( 'action' => 'history' )
332 $hist = Html
::rawElement( 'span', array( 'class' => 'mw-newpages-history' ),
333 $this->msg( 'parentheses' )->rawParams( $histLink )->escaped() );
335 $length = Html
::element(
337 array( 'class' => 'mw-newpages-length' ),
338 $this->msg( 'brackets' )->params( $this->msg( 'nbytes' )
339 ->numParams( $result->length
)->text()
343 $ulink = Linker
::revUserTools( $rev );
344 $comment = Linker
::revComment( $rev );
346 if ( $this->patrollable( $result ) ) {
347 $classes[] = 'not-patrolled';
350 # Add a class for zero byte pages
351 if ( $result->length
== 0 ) {
352 $classes[] = 'mw-newpages-zero-byte-page';
356 if ( isset( $result->ts_tags
) ) {
357 list( $tagDisplay, $newClasses ) = ChangeTags
::formatSummaryRow(
361 $classes = array_merge( $classes, $newClasses );
366 $css = count( $classes ) ?
' class="' . implode( ' ', $classes ) . '"' : '';
368 # Display the old title if the namespace/title has been changed
370 $oldTitle = Title
::makeTitle( $result->rc_namespace
, $result->rc_title
);
372 if ( !$title->equals( $oldTitle ) ) {
373 $oldTitleText = $oldTitle->getPrefixedText();
374 $oldTitleText = $this->msg( 'rc-old-title' )->params( $oldTitleText )->escaped();
377 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} "
378 . "{$dm}{$ulink} {$comment} {$tagDisplay} {$oldTitleText}</li>\n";
382 * Should a specific result row provide "patrollable" links?
384 * @param object $result Result row
387 protected function patrollable( $result ) {
388 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled
);
392 * Output a subscription feed listing recent edits to this page.
394 * @param string $type
396 protected function feed( $type ) {
397 if ( !$this->getConfig()->get( 'Feed' ) ) {
398 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
403 $feedClasses = $this->getConfig()->get( 'FeedClasses' );
404 if ( !isset( $feedClasses[$type] ) ) {
405 $this->getOutput()->addWikiMsg( 'feed-invalid' );
410 $feed = new $feedClasses[$type](
412 $this->msg( 'tagline' )->text(),
413 $this->getPageTitle()->getFullURL()
416 $pager = new NewPagesPager( $this, $this->opts
);
417 $limit = $this->opts
->getValue( 'limit' );
418 $pager->mLimit
= min( $limit, $this->getConfig()->get( 'FeedLimit' ) );
421 if ( $pager->getNumRows() > 0 ) {
422 foreach ( $pager->mResult
as $row ) {
423 $feed->outItem( $this->feedItem( $row ) );
429 protected function feedTitle() {
430 $desc = $this->getDescription();
431 $code = $this->getConfig()->get( 'LanguageCode' );
432 $sitename = $this->getConfig()->get( 'Sitename' );
434 return "$sitename - $desc [$code]";
437 protected function feedItem( $row ) {
438 $title = Title
::makeTitle( intval( $row->rc_namespace
), $row->rc_title
);
440 $date = $row->rc_timestamp
;
441 $comments = $title->getTalkPage()->getFullURL();
444 $title->getPrefixedText(),
445 $this->feedItemDesc( $row ),
446 $title->getFullURL(),
448 $this->feedItemAuthor( $row ),
456 protected function feedItemAuthor( $row ) {
457 return isset( $row->rc_user_text
) ?
$row->rc_user_text
: '';
460 protected function feedItemDesc( $row ) {
461 $revision = Revision
::newFromId( $row->rev_id
);
463 //XXX: include content model/type in feed item?
464 return '<p>' . htmlspecialchars( $revision->getUserText() ) .
465 $this->msg( 'colon-separator' )->inContentLanguage()->escaped() .
466 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
467 "</p>\n<hr />\n<div>" .
468 nl2br( htmlspecialchars( $revision->getContent()->serialize() ) ) . "</div>";
474 protected function getGroupName() {
480 * @ingroup SpecialPage Pager
482 class NewPagesPager
extends ReverseChronologicalPager
{
491 function __construct( $form, FormOptions
$opts ) {
492 parent
::__construct( $form->getContext() );
493 $this->mForm
= $form;
497 function getQueryInfo() {
499 $conds['rc_new'] = 1;
501 $namespace = $this->opts
->getValue( 'namespace' );
502 $namespace = ( $namespace === 'all' ) ?
false : intval( $namespace );
504 $username = $this->opts
->getValue( 'username' );
505 $user = Title
::makeTitleSafe( NS_USER
, $username );
507 $rcIndexes = array();
509 if ( $namespace !== false ) {
510 if ( $this->opts
->getValue( 'invert' ) ) {
511 $conds[] = 'rc_namespace != ' . $this->mDb
->addQuotes( $namespace );
513 $conds['rc_namespace'] = $namespace;
518 $conds['rc_user_text'] = $user->getText();
519 $rcIndexes = 'rc_user_text';
520 } elseif ( User
::groupHasPermission( '*', 'createpage' ) &&
521 $this->opts
->getValue( 'hideliu' )
523 # If anons cannot make new pages, don't "exclude logged in users"!
524 $conds['rc_user'] = 0;
527 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
528 if ( $this->opts
->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
529 $conds['rc_patrolled'] = 0;
532 if ( $this->opts
->getValue( 'hidebots' ) ) {
533 $conds['rc_bot'] = 0;
536 if ( $this->opts
->getValue( 'hideredirs' ) ) {
537 $conds['page_is_redirect'] = 0;
540 // Allow changes to the New Pages query
541 $tables = array( 'recentchanges', 'page' );
543 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
544 'rc_comment', 'rc_timestamp', 'rc_patrolled', 'rc_id', 'rc_deleted',
545 'length' => 'page_len', 'rev_id' => 'page_latest', 'rc_this_oldid',
546 'page_namespace', 'page_title'
548 $join_conds = array( 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ) );
550 wfRunHooks( 'SpecialNewpagesConditions',
551 array( &$this, $this->opts
, &$conds, &$tables, &$fields, &$join_conds ) );
556 $options = array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) );
563 'options' => $options,
564 'join_conds' => $join_conds
567 // Modify query for tags
568 ChangeTags
::modifyDisplayQuery(
574 $this->opts
['tagfilter']
580 function getIndexField() {
581 return 'rc_timestamp';
584 function formatRow( $row ) {
585 return $this->mForm
->formatRow( $row );
588 function getStartBody() {
589 # Do a batch existence check on pages
590 $linkBatch = new LinkBatch();
591 foreach ( $this->mResult
as $row ) {
592 $linkBatch->add( NS_USER
, $row->rc_user_text
);
593 $linkBatch->add( NS_USER_TALK
, $row->rc_user_text
);
594 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
596 $linkBatch->execute();
601 function getEndBody() {