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 $this->feed( $feedType );
147 $allValues = $this->opts
->getAllValues();
148 unset( $allValues['feed'] );
149 $out->setFeedAppendQuery( wfArrayToCgi( $allValues ) );
152 $pager = new NewPagesPager( $this, $this->opts
);
153 $pager->mLimit
= $this->opts
->getValue( 'limit' );
154 $pager->mOffset
= $this->opts
->getValue( 'offset' );
156 if( $pager->getNumRows() ) {
158 if ( $this->showNavigation
) {
159 $navigation = $pager->getNavigationBar();
161 $out->addHTML( $navigation . $pager->getBody() . $navigation );
163 $out->addWikiMsg( 'specialpage-empty' );
167 protected function filterLinks() {
169 $showhide = array( $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() );
171 // Option value -> message mapping
173 'hideliu' => 'rcshowhideliu',
174 'hidepatrolled' => 'rcshowhidepatr',
175 'hidebots' => 'rcshowhidebots',
176 'hideredirs' => 'whatlinkshere-hideredirs'
178 foreach ( $this->customFilters
as $key => $params ) {
179 $filters[$key] = $params['msg'];
182 // Disable some if needed
183 if ( !User
::groupHasPermission( '*', 'createpage' ) ) {
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] = $this->msg( $msg )->rawParams( $link )->escaped();
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( $this->msg( 'newpages' )->text() ) .
234 Xml
::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
236 <td class="mw-label">' .
237 Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
239 <td class="mw-input">' .
240 Html
::namespaceSelector(
242 'selected' => $namespace,
245 'name' => 'namespace',
247 'class' => 'namespaceselector',
251 </tr>' . ( $tagFilter ?
(
253 <td class="mw-label">' .
256 <td class="mw-input">' .
260 ( $wgEnableNewpagesUserFilter ?
262 <td class="mw-label">' .
263 Xml
::label( $this->msg( 'newpages-username' )->text(), 'mw-np-username' ) .
265 <td class="mw-input">' .
266 Xml
::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
270 <td class="mw-submit">' .
271 Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
276 <td class="mw-input">' .
277 $this->filterLinks() .
280 Xml
::closeElement( 'table' ) .
281 Xml
::closeElement( 'fieldset' ) .
283 Xml
::closeElement( 'form' );
285 $this->getOutput()->addHTML( $form );
289 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
291 * @param $result Result row
294 public function formatRow( $result ) {
295 $title = Title
::newFromRow( $result );
297 # Revision deletion works on revisions, so we should cast one
299 'comment' => $result->rc_comment
,
300 'deleted' => $result->rc_deleted
,
301 'user_text' => $result->rc_user_text
,
302 'user' => $result->rc_user
,
304 $rev = new Revision( $row );
305 $rev->setTitle( $title );
309 $lang = $this->getLanguage();
310 $dm = $lang->getDirMark();
312 $spanTime = Html
::element( 'span', array( 'class' => 'mw-newpages-time' ),
313 $lang->userTimeAndDate( $result->rc_timestamp
, $this->getUser() )
315 $time = Linker
::linkKnown(
319 array( 'oldid' => $result->rc_this_oldid
),
323 $query = array( 'redirect' => 'no' );
325 if( $this->patrollable( $result ) ) {
326 $query['rcid'] = $result->rc_id
;
329 // Linker::linkKnown() uses 'known' and 'noclasses' options. This breaks the colouration for stubs.
330 $plink = Linker
::link(
333 array( 'class' => 'mw-newpages-pagename' ),
337 $histLink = Linker
::linkKnown(
339 $this->msg( 'hist' )->escaped(),
341 array( 'action' => 'history' )
343 $hist = Html
::rawElement( 'span', array( 'class' => 'mw-newpages-history' ),
344 $this->msg( 'parentheses' )->rawParams( $histLink )->escaped() );
346 $length = Html
::element( 'span', array( 'class' => 'mw-newpages-length' ),
347 $this->msg( 'brackets' )->params( $this->msg( 'nbytes' )->numParams( $result->length
)->text() )
350 $ulink = Linker
::revUserTools( $rev );
351 $comment = Linker
::revComment( $rev );
353 if ( $this->patrollable( $result ) ) {
354 $classes[] = 'not-patrolled';
357 # Add a class for zero byte pages
358 if ( $result->length
== 0 ) {
359 $classes[] = 'mw-newpages-zero-byte-page';
363 if( isset( $result->ts_tags
) ) {
364 list( $tagDisplay, $newClasses ) = ChangeTags
::formatSummaryRow( $result->ts_tags
, 'newpages' );
365 $classes = array_merge( $classes, $newClasses );
370 $css = count( $classes ) ?
' class="' . implode( ' ', $classes ) . '"' : '';
372 # Display the old title if the namespace/title has been changed
374 $oldTitle = Title
::makeTitle( $result->rc_namespace
, $result->rc_title
);
375 if ( !$title->equals( $oldTitle ) ) {
376 $oldTitleText = $this->msg( 'rc-old-title' )->params( $oldTitle->getPrefixedText() )->escaped();
379 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} {$dm}{$ulink} {$comment} {$tagDisplay} {$oldTitleText}</li>\n";
383 * Should a specific result row provide "patrollable" links?
385 * @param $result Result row
388 protected function patrollable( $result ) {
389 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled
);
393 * Output a subscription feed listing recent edits to this page.
395 * @param $type String
397 protected function feed( $type ) {
398 global $wgFeed, $wgFeedClasses, $wgFeedLimit;
401 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
405 if( !isset( $wgFeedClasses[$type] ) ) {
406 $this->getOutput()->addWikiMsg( 'feed-invalid' );
410 $feed = new $wgFeedClasses[$type](
412 $this->msg( 'tagline' )->text(),
413 $this->getTitle()->getFullUrl()
416 $pager = new NewPagesPager( $this, $this->opts
);
417 $limit = $this->opts
->getValue( 'limit' );
418 $pager->mLimit
= min( $limit, $wgFeedLimit );
421 if( $pager->getNumRows() > 0 ) {
422 foreach ( $pager->mResult
as $row ) {
423 $feed->outItem( $this->feedItem( $row ) );
429 protected function feedTitle() {
430 global $wgLanguageCode, $wgSitename;
431 $desc = $this->getDescription();
432 return "$wgSitename - $desc [$wgLanguageCode]";
435 protected function feedItem( $row ) {
436 $title = Title
::makeTitle( intval( $row->rc_namespace
), $row->rc_title
);
438 $date = $row->rc_timestamp
;
439 $comments = $title->getTalkPage()->getFullURL();
442 $title->getPrefixedText(),
443 $this->feedItemDesc( $row ),
444 $title->getFullURL(),
446 $this->feedItemAuthor( $row ),
454 protected function feedItemAuthor( $row ) {
455 return isset( $row->rc_user_text
) ?
$row->rc_user_text
: '';
458 protected function feedItemDesc( $row ) {
459 $revision = Revision
::newFromId( $row->rev_id
);
461 //XXX: include content model/type in feed item?
462 return '<p>' . htmlspecialchars( $revision->getUserText() ) .
463 $this->msg( 'colon-separator' )->inContentLanguage()->escaped() .
464 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
465 "</p>\n<hr />\n<div>" .
466 nl2br( htmlspecialchars( $revision->getContent()->serialize() ) ) . "</div>";
473 * @ingroup SpecialPage Pager
475 class NewPagesPager
extends ReverseChronologicalPager
{
484 function __construct( $form, FormOptions
$opts ) {
485 parent
::__construct( $form->getContext() );
486 $this->mForm
= $form;
490 function getQueryInfo() {
491 global $wgEnableNewpagesUserFilter;
493 $conds['rc_new'] = 1;
495 $namespace = $this->opts
->getValue( 'namespace' );
496 $namespace = ( $namespace === 'all' ) ?
false : intval( $namespace );
498 $username = $this->opts
->getValue( 'username' );
499 $user = Title
::makeTitleSafe( NS_USER
, $username );
501 if( $namespace !== false ) {
502 $conds['rc_namespace'] = $namespace;
503 $rcIndexes = array( 'new_name_timestamp' );
505 $rcIndexes = array( 'rc_timestamp' );
508 # $wgEnableNewpagesUserFilter - temp WMF hack
509 if( $wgEnableNewpagesUserFilter && $user ) {
510 $conds['rc_user_text'] = $user->getText();
511 $rcIndexes = 'rc_user_text';
512 # If anons cannot make new pages, don't "exclude logged in users"!
513 } elseif( User
::groupHasPermission( '*', 'createpage' ) && $this->opts
->getValue( 'hideliu' ) ) {
514 $conds['rc_user'] = 0;
516 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
517 if( $this->opts
->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
518 $conds['rc_patrolled'] = 0;
520 if( $this->opts
->getValue( 'hidebots' ) ) {
521 $conds['rc_bot'] = 0;
524 if ( $this->opts
->getValue( 'hideredirs' ) ) {
525 $conds['page_is_redirect'] = 0;
528 // Allow changes to the New Pages query
529 $tables = array( 'recentchanges', 'page' );
531 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
532 'rc_comment', 'rc_timestamp', 'rc_patrolled','rc_id', 'rc_deleted',
533 'length' => 'page_len', 'rev_id' => 'page_latest', 'rc_this_oldid',
534 'page_namespace', 'page_title'
536 $join_conds = array( 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ) );
538 wfRunHooks( 'SpecialNewpagesConditions',
539 array( &$this, $this->opts
, &$conds, &$tables, &$fields, &$join_conds ) );
545 'options' => array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) ),
546 'join_conds' => $join_conds
549 // Modify query for tags
550 ChangeTags
::modifyDisplayQuery(
556 $this->opts
['tagfilter']
562 function getIndexField() {
563 return 'rc_timestamp';
566 function formatRow( $row ) {
567 return $this->mForm
->formatRow( $row );
570 function getStartBody() {
571 # Do a batch existence check on pages
572 $linkBatch = new LinkBatch();
573 foreach ( $this->mResult
as $row ) {
574 $linkBatch->add( NS_USER
, $row->rc_user_text
);
575 $linkBatch->add( NS_USER_TALK
, $row->rc_user_text
);
576 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
578 $linkBatch->execute();
582 function getEndBody() {