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', $this->getUser()->getIntOption( 'rclimit' ) );
57 $opts->add( 'offset', '' );
58 $opts->add( 'namespace', '0' );
59 $opts->add( 'username', '' );
60 $opts->add( 'feed', '' );
61 $opts->add( 'tagfilter', '' );
62 $opts->add( 'invert', false );
64 $this->customFilters
= array();
65 wfRunHooks( 'SpecialNewPagesFilters', array( $this, &$this->customFilters
) );
66 foreach( $this->customFilters
as $key => $params ) {
67 $opts->add( $key, $params['default'] );
71 $opts->fetchValuesFromRequest( $this->getRequest() );
72 if ( $par ) $this->parseParams( $par );
75 $opts->validateIntBounds( 'limit', 0, 5000 );
76 if( !$wgEnableNewpagesUserFilter ) {
77 $opts->setValue( 'username', '' );
81 protected function parseParams( $par ) {
82 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
83 foreach ( $bits as $bit ) {
84 if ( 'shownav' == $bit ) {
85 $this->showNavigation
= true;
87 if ( 'hideliu' === $bit ) {
88 $this->opts
->setValue( 'hideliu', true );
90 if ( 'hidepatrolled' == $bit ) {
91 $this->opts
->setValue( 'hidepatrolled', true );
93 if ( 'hidebots' == $bit ) {
94 $this->opts
->setValue( 'hidebots', true );
96 if ( 'showredirs' == $bit ) {
97 $this->opts
->setValue( 'hideredirs', false );
99 if ( is_numeric( $bit ) ) {
100 $this->opts
->setValue( 'limit', intval( $bit ) );
104 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
105 $this->opts
->setValue( 'limit', intval( $m[1] ) );
107 // PG offsets not just digits!
108 if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) ) {
109 $this->opts
->setValue( 'offset', intval( $m[1] ) );
111 if ( preg_match( '/^username=(.*)$/', $bit, $m ) ) {
112 $this->opts
->setValue( 'username', $m[1] );
114 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
115 $ns = $this->getLanguage()->getNsIndex( $m[1] );
116 if( $ns !== false ) {
117 $this->opts
->setValue( 'namespace', $ns );
124 * Show a form for filtering namespace and username
129 public function execute( $par ) {
130 $out = $this->getOutput();
133 $this->outputHeader();
135 $this->showNavigation
= !$this->including(); // Maybe changed in setup
136 $this->setup( $par );
138 if( !$this->including() ) {
142 $feedType = $this->opts
->getValue( 'feed' );
144 $this->feed( $feedType );
148 $allValues = $this->opts
->getAllValues();
149 unset( $allValues['feed'] );
150 $out->setFeedAppendQuery( wfArrayToCgi( $allValues ) );
153 $pager = new NewPagesPager( $this, $this->opts
);
154 $pager->mLimit
= $this->opts
->getValue( 'limit' );
155 $pager->mOffset
= $this->opts
->getValue( 'offset' );
157 if( $pager->getNumRows() ) {
159 if ( $this->showNavigation
) {
160 $navigation = $pager->getNavigationBar();
162 $out->addHTML( $navigation . $pager->getBody() . $navigation );
164 $out->addWikiMsg( 'specialpage-empty' );
168 protected function filterLinks() {
170 $showhide = array( $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() );
172 // Option value -> message mapping
174 'hideliu' => 'rcshowhideliu',
175 'hidepatrolled' => 'rcshowhidepatr',
176 'hidebots' => 'rcshowhidebots',
177 'hideredirs' => 'whatlinkshere-hideredirs'
179 foreach ( $this->customFilters
as $key => $params ) {
180 $filters[$key] = $params['msg'];
183 // Disable some if needed
184 if ( !User
::groupHasPermission( '*', 'createpage' ) ) {
185 unset( $filters['hideliu'] );
187 if ( !$this->getUser()->useNPPatrol() ) {
188 unset( $filters['hidepatrolled'] );
192 $changed = $this->opts
->getChangedValues();
193 unset( $changed['offset'] ); // Reset offset if query type changes
195 $self = $this->getTitle();
196 foreach ( $filters as $key => $msg ) {
197 $onoff = 1 - $this->opts
->getValue( $key );
198 $link = Linker
::link( $self, $showhide[$onoff], array(),
199 array( $key => $onoff ) +
$changed
201 $links[$key] = $this->msg( $msg )->rawParams( $link )->escaped();
204 return $this->getLanguage()->pipeList( $links );
207 protected function form() {
208 global $wgEnableNewpagesUserFilter, $wgScript;
211 $this->opts
->consumeValue( 'offset' ); // don't carry offset, DWIW
212 $namespace = $this->opts
->consumeValue( 'namespace' );
213 $username = $this->opts
->consumeValue( 'username' );
214 $tagFilterVal = $this->opts
->consumeValue( 'tagfilter' );
215 $nsinvert = $this->opts
->consumeValue( 'invert' );
217 // Check username input validity
218 $ut = Title
::makeTitleSafe( NS_USER
, $username );
219 $userText = $ut ?
$ut->getText() : '';
221 // Store query values in hidden fields so that form submission doesn't lose them
223 foreach ( $this->opts
->getUnconsumedValues() as $key => $value ) {
224 $hidden[] = Html
::hidden( $key, $value );
226 $hidden = implode( "\n", $hidden );
228 $tagFilter = ChangeTags
::buildTagFilterSelector( $tagFilterVal );
230 list( $tagFilterLabel, $tagFilterSelector ) = $tagFilter;
233 $form = Xml
::openElement( 'form', array( 'action' => $wgScript ) ) .
234 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
235 Xml
::fieldset( $this->msg( 'newpages' )->text() ) .
236 Xml
::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
238 <td class="mw-label">' .
239 Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
241 <td class="mw-input">' .
242 Html
::namespaceSelector(
244 'selected' => $namespace,
247 'name' => 'namespace',
249 'class' => 'namespaceselector',
253 $this->msg( 'invert' )->text(),
257 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
260 </tr>' . ( $tagFilter ?
(
262 <td class="mw-label">' .
265 <td class="mw-input">' .
269 ( $wgEnableNewpagesUserFilter ?
271 <td class="mw-label">' .
272 Xml
::label( $this->msg( 'newpages-username' )->text(), 'mw-np-username' ) .
274 <td class="mw-input">' .
275 Xml
::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
279 <td class="mw-submit">' .
280 Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
285 <td class="mw-input">' .
286 $this->filterLinks() .
289 Xml
::closeElement( 'table' ) .
290 Xml
::closeElement( 'fieldset' ) .
292 Xml
::closeElement( 'form' );
294 $this->getOutput()->addHTML( $form );
298 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
300 * @param $result Result row
303 public function formatRow( $result ) {
304 $title = Title
::newFromRow( $result );
306 # Revision deletion works on revisions, so we should cast one
308 'comment' => $result->rc_comment
,
309 'deleted' => $result->rc_deleted
,
310 'user_text' => $result->rc_user_text
,
311 'user' => $result->rc_user
,
313 $rev = new Revision( $row );
314 $rev->setTitle( $title );
318 $lang = $this->getLanguage();
319 $dm = $lang->getDirMark();
321 $spanTime = Html
::element( 'span', array( 'class' => 'mw-newpages-time' ),
322 $lang->userTimeAndDate( $result->rc_timestamp
, $this->getUser() )
324 $time = Linker
::linkKnown(
328 array( 'oldid' => $result->rc_this_oldid
),
332 $query = array( 'redirect' => 'no' );
334 if( $this->patrollable( $result ) ) {
335 $query['rcid'] = $result->rc_id
;
338 // Linker::linkKnown() uses 'known' and 'noclasses' options. This breaks the colouration for stubs.
339 $plink = Linker
::link(
342 array( 'class' => 'mw-newpages-pagename' ),
346 $histLink = Linker
::linkKnown(
348 $this->msg( 'hist' )->escaped(),
350 array( 'action' => 'history' )
352 $hist = Html
::rawElement( 'span', array( 'class' => 'mw-newpages-history' ),
353 $this->msg( 'parentheses' )->rawParams( $histLink )->escaped() );
355 $length = Html
::element( 'span', array( 'class' => 'mw-newpages-length' ),
356 $this->msg( 'brackets' )->params( $this->msg( 'nbytes' )->numParams( $result->length
)->text() )
359 $ulink = Linker
::revUserTools( $rev );
360 $comment = Linker
::revComment( $rev );
362 if ( $this->patrollable( $result ) ) {
363 $classes[] = 'not-patrolled';
366 # Add a class for zero byte pages
367 if ( $result->length
== 0 ) {
368 $classes[] = 'mw-newpages-zero-byte-page';
372 if( isset( $result->ts_tags
) ) {
373 list( $tagDisplay, $newClasses ) = ChangeTags
::formatSummaryRow( $result->ts_tags
, 'newpages' );
374 $classes = array_merge( $classes, $newClasses );
379 $css = count( $classes ) ?
' class="' . implode( ' ', $classes ) . '"' : '';
381 # Display the old title if the namespace/title has been changed
383 $oldTitle = Title
::makeTitle( $result->rc_namespace
, $result->rc_title
);
384 if ( !$title->equals( $oldTitle ) ) {
385 $oldTitleText = $this->msg( 'rc-old-title' )->params( $oldTitle->getPrefixedText() )->escaped();
388 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} {$dm}{$ulink} {$comment} {$tagDisplay} {$oldTitleText}</li>\n";
392 * Should a specific result row provide "patrollable" links?
394 * @param $result Result row
397 protected function patrollable( $result ) {
398 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled
);
402 * Output a subscription feed listing recent edits to this page.
404 * @param $type String
406 protected function feed( $type ) {
407 global $wgFeed, $wgFeedClasses, $wgFeedLimit;
410 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
414 if( !isset( $wgFeedClasses[$type] ) ) {
415 $this->getOutput()->addWikiMsg( 'feed-invalid' );
419 $feed = new $wgFeedClasses[$type](
421 $this->msg( 'tagline' )->text(),
422 $this->getTitle()->getFullURL()
425 $pager = new NewPagesPager( $this, $this->opts
);
426 $limit = $this->opts
->getValue( 'limit' );
427 $pager->mLimit
= min( $limit, $wgFeedLimit );
430 if( $pager->getNumRows() > 0 ) {
431 foreach ( $pager->mResult
as $row ) {
432 $feed->outItem( $this->feedItem( $row ) );
438 protected function feedTitle() {
439 global $wgLanguageCode, $wgSitename;
440 $desc = $this->getDescription();
441 return "$wgSitename - $desc [$wgLanguageCode]";
444 protected function feedItem( $row ) {
445 $title = Title
::makeTitle( intval( $row->rc_namespace
), $row->rc_title
);
447 $date = $row->rc_timestamp
;
448 $comments = $title->getTalkPage()->getFullURL();
451 $title->getPrefixedText(),
452 $this->feedItemDesc( $row ),
453 $title->getFullURL(),
455 $this->feedItemAuthor( $row ),
463 protected function feedItemAuthor( $row ) {
464 return isset( $row->rc_user_text
) ?
$row->rc_user_text
: '';
467 protected function feedItemDesc( $row ) {
468 $revision = Revision
::newFromId( $row->rev_id
);
470 //XXX: include content model/type in feed item?
471 return '<p>' . htmlspecialchars( $revision->getUserText() ) .
472 $this->msg( 'colon-separator' )->inContentLanguage()->escaped() .
473 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
474 "</p>\n<hr />\n<div>" .
475 nl2br( htmlspecialchars( $revision->getContent()->serialize() ) ) . "</div>";
480 protected function getGroupName() {
486 * @ingroup SpecialPage Pager
488 class NewPagesPager
extends ReverseChronologicalPager
{
497 function __construct( $form, FormOptions
$opts ) {
498 parent
::__construct( $form->getContext() );
499 $this->mForm
= $form;
503 function getQueryInfo() {
504 global $wgEnableNewpagesUserFilter;
506 $conds['rc_new'] = 1;
508 $namespace = $this->opts
->getValue( 'namespace' );
509 $namespace = ( $namespace === 'all' ) ?
false : intval( $namespace );
511 $username = $this->opts
->getValue( 'username' );
512 $user = Title
::makeTitleSafe( NS_USER
, $username );
514 if( $namespace !== false ) {
515 if ( $this->opts
->getValue( 'invert' ) ) {
516 $conds[] = 'rc_namespace != ' . $this->mDb
->addQuotes( $namespace );
518 $conds['rc_namespace'] = $namespace;
520 $rcIndexes = array( 'new_name_timestamp' );
522 $rcIndexes = array( 'rc_timestamp' );
525 # $wgEnableNewpagesUserFilter - temp WMF hack
526 if( $wgEnableNewpagesUserFilter && $user ) {
527 $conds['rc_user_text'] = $user->getText();
528 $rcIndexes = 'rc_user_text';
529 # If anons cannot make new pages, don't "exclude logged in users"!
530 } elseif( User
::groupHasPermission( '*', 'createpage' ) && $this->opts
->getValue( 'hideliu' ) ) {
531 $conds['rc_user'] = 0;
533 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
534 if( $this->opts
->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
535 $conds['rc_patrolled'] = 0;
537 if( $this->opts
->getValue( 'hidebots' ) ) {
538 $conds['rc_bot'] = 0;
541 if ( $this->opts
->getValue( 'hideredirs' ) ) {
542 $conds['page_is_redirect'] = 0;
545 // Allow changes to the New Pages query
546 $tables = array( 'recentchanges', 'page' );
548 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
549 'rc_comment', 'rc_timestamp', 'rc_patrolled','rc_id', 'rc_deleted',
550 'length' => 'page_len', 'rev_id' => 'page_latest', 'rc_this_oldid',
551 'page_namespace', 'page_title'
553 $join_conds = array( 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ) );
555 wfRunHooks( 'SpecialNewpagesConditions',
556 array( &$this, $this->opts
, &$conds, &$tables, &$fields, &$join_conds ) );
562 'options' => array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) ),
563 'join_conds' => $join_conds
566 // Modify query for tags
567 ChangeTags
::modifyDisplayQuery(
573 $this->opts
['tagfilter']
579 function getIndexField() {
580 return 'rc_timestamp';
583 function formatRow( $row ) {
584 return $this->mForm
->formatRow( $row );
587 function getStartBody() {
588 # Do a batch existence check on pages
589 $linkBatch = new LinkBatch();
590 foreach ( $this->mResult
as $row ) {
591 $linkBatch->add( NS_USER
, $row->rc_user_text
);
592 $linkBatch->add( NS_USER_TALK
, $row->rc_user_text
);
593 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
595 $linkBatch->execute();
599 function getEndBody() {