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
{
36 protected $customFilters;
38 // Some internal settings
39 protected $showNavigation = false;
41 public function __construct() {
42 parent
::__construct( 'Newpages' );
45 protected function setup( $par ) {
46 global $wgEnableNewpagesUserFilter;
49 $opts = new FormOptions();
50 $this->opts
= $opts; // bind
51 $opts->add( 'hideliu', false );
52 $opts->add( 'hidepatrolled', $this->getUser()->getBoolOption( 'newpageshidepatrolled' ) );
53 $opts->add( 'hidebots', false );
54 $opts->add( 'hideredirs', true );
55 $opts->add( 'limit', $this->getUser()->getIntOption( 'rclimit' ) );
56 $opts->add( 'offset', '' );
57 $opts->add( 'namespace', '0' );
58 $opts->add( 'username', '' );
59 $opts->add( 'feed', '' );
60 $opts->add( 'tagfilter', '' );
61 $opts->add( 'invert', false );
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() );
72 $this->parseParams( $par );
76 $opts->validateIntBounds( 'limit', 0, 5000 );
77 if ( !$wgEnableNewpagesUserFilter ) {
78 $opts->setValue( 'username', '' );
82 protected function parseParams( $par ) {
83 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
84 foreach ( $bits as $bit ) {
85 if ( 'shownav' == $bit ) {
86 $this->showNavigation
= true;
88 if ( 'hideliu' === $bit ) {
89 $this->opts
->setValue( 'hideliu', true );
91 if ( 'hidepatrolled' == $bit ) {
92 $this->opts
->setValue( 'hidepatrolled', true );
94 if ( 'hidebots' == $bit ) {
95 $this->opts
->setValue( 'hidebots', true );
97 if ( 'showredirs' == $bit ) {
98 $this->opts
->setValue( 'hideredirs', false );
100 if ( is_numeric( $bit ) ) {
101 $this->opts
->setValue( 'limit', intval( $bit ) );
105 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
106 $this->opts
->setValue( 'limit', intval( $m[1] ) );
108 // PG offsets not just digits!
109 if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) ) {
110 $this->opts
->setValue( 'offset', intval( $m[1] ) );
112 if ( preg_match( '/^username=(.*)$/', $bit, $m ) ) {
113 $this->opts
->setValue( 'username', $m[1] );
115 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
116 $ns = $this->getLanguage()->getNsIndex( $m[1] );
117 if ( $ns !== false ) {
118 $this->opts
->setValue( 'namespace', $ns );
125 * Show a form for filtering namespace and username
130 public function execute( $par ) {
131 $out = $this->getOutput();
134 $this->outputHeader();
136 $this->showNavigation
= !$this->including(); // Maybe changed in setup
137 $this->setup( $par );
139 if ( !$this->including() ) {
143 $feedType = $this->opts
->getValue( 'feed' );
145 $this->feed( $feedType );
150 $allValues = $this->opts
->getAllValues();
151 unset( $allValues['feed'] );
152 $out->setFeedAppendQuery( wfArrayToCgi( $allValues ) );
155 $pager = new NewPagesPager( $this, $this->opts
);
156 $pager->mLimit
= $this->opts
->getValue( 'limit' );
157 $pager->mOffset
= $this->opts
->getValue( 'offset' );
159 if ( $pager->getNumRows() ) {
161 if ( $this->showNavigation
) {
162 $navigation = $pager->getNavigationBar();
164 $out->addHTML( $navigation . $pager->getBody() . $navigation );
166 $out->addWikiMsg( 'specialpage-empty' );
170 protected function filterLinks() {
172 $showhide = array( $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() );
174 // Option value -> message mapping
176 'hideliu' => 'rcshowhideliu',
177 'hidepatrolled' => 'rcshowhidepatr',
178 'hidebots' => 'rcshowhidebots',
179 'hideredirs' => 'whatlinkshere-hideredirs'
181 foreach ( $this->customFilters
as $key => $params ) {
182 $filters[$key] = $params['msg'];
185 // Disable some if needed
186 if ( !User
::groupHasPermission( '*', 'createpage' ) ) {
187 unset( $filters['hideliu'] );
189 if ( !$this->getUser()->useNPPatrol() ) {
190 unset( $filters['hidepatrolled'] );
194 $changed = $this->opts
->getChangedValues();
195 unset( $changed['offset'] ); // Reset offset if query type changes
197 $self = $this->getTitle();
198 foreach ( $filters as $key => $msg ) {
199 $onoff = 1 - $this->opts
->getValue( $key );
200 $link = Linker
::link( $self, $showhide[$onoff], array(),
201 array( $key => $onoff ) +
$changed
203 $links[$key] = $this->msg( $msg )->rawParams( $link )->escaped();
206 return $this->getLanguage()->pipeList( $links );
209 protected function form() {
210 global $wgEnableNewpagesUserFilter, $wgScript;
213 $this->opts
->consumeValue( 'offset' ); // don't carry offset, DWIW
214 $namespace = $this->opts
->consumeValue( 'namespace' );
215 $username = $this->opts
->consumeValue( 'username' );
216 $tagFilterVal = $this->opts
->consumeValue( 'tagfilter' );
217 $nsinvert = $this->opts
->consumeValue( 'invert' );
219 // Check username input validity
220 $ut = Title
::makeTitleSafe( NS_USER
, $username );
221 $userText = $ut ?
$ut->getText() : '';
223 // Store query values in hidden fields so that form submission doesn't lose them
225 foreach ( $this->opts
->getUnconsumedValues() as $key => $value ) {
226 $hidden[] = Html
::hidden( $key, $value );
228 $hidden = implode( "\n", $hidden );
230 $tagFilter = ChangeTags
::buildTagFilterSelector( $tagFilterVal );
232 list( $tagFilterLabel, $tagFilterSelector ) = $tagFilter;
235 $form = Xml
::openElement( 'form', array( 'action' => $wgScript ) ) .
236 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
237 Xml
::fieldset( $this->msg( 'newpages' )->text() ) .
238 Xml
::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
240 <td class="mw-label">' .
241 Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
243 <td class="mw-input">' .
244 Html
::namespaceSelector(
246 'selected' => $namespace,
249 'name' => 'namespace',
251 'class' => 'namespaceselector',
255 $this->msg( 'invert' )->text(),
259 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
262 </tr>' . ( $tagFilter ?
(
264 <td class="mw-label">' .
267 <td class="mw-input">' .
271 ( $wgEnableNewpagesUserFilter ?
273 <td class="mw-label">' .
274 Xml
::label( $this->msg( 'newpages-username' )->text(), 'mw-np-username' ) .
276 <td class="mw-input">' .
277 Xml
::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
281 <td class="mw-submit">' .
282 Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
287 <td class="mw-input">' .
288 $this->filterLinks() .
291 Xml
::closeElement( 'table' ) .
292 Xml
::closeElement( 'fieldset' ) .
294 Xml
::closeElement( 'form' );
296 $this->getOutput()->addHTML( $form );
300 * Format a row, providing the timestamp, links to the page/history,
301 * size, user links, and a comment
303 * @param object $result Result row
306 public function formatRow( $result ) {
307 $title = Title
::newFromRow( $result );
309 # Revision deletion works on revisions, so we should cast one
311 'comment' => $result->rc_comment
,
312 'deleted' => $result->rc_deleted
,
313 'user_text' => $result->rc_user_text
,
314 'user' => $result->rc_user
,
316 $rev = new Revision( $row );
317 $rev->setTitle( $title );
321 $lang = $this->getLanguage();
322 $dm = $lang->getDirMark();
324 $spanTime = Html
::element( 'span', array( 'class' => 'mw-newpages-time' ),
325 $lang->userTimeAndDate( $result->rc_timestamp
, $this->getUser() )
327 $time = Linker
::linkKnown(
331 array( 'oldid' => $result->rc_this_oldid
),
335 $query = array( 'redirect' => 'no' );
337 // Linker::linkKnown() uses 'known' and 'noclasses' options.
338 // 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(
357 array( 'class' => 'mw-newpages-length' ),
358 $this->msg( 'brackets' )->params( $this->msg( 'nbytes' )
359 ->numParams( $result->length
)->text()
363 $ulink = Linker
::revUserTools( $rev );
364 $comment = Linker
::revComment( $rev );
366 if ( $this->patrollable( $result ) ) {
367 $classes[] = 'not-patrolled';
370 # Add a class for zero byte pages
371 if ( $result->length
== 0 ) {
372 $classes[] = 'mw-newpages-zero-byte-page';
376 if ( isset( $result->ts_tags
) ) {
377 list( $tagDisplay, $newClasses ) = ChangeTags
::formatSummaryRow(
381 $classes = array_merge( $classes, $newClasses );
386 $css = count( $classes ) ?
' class="' . implode( ' ', $classes ) . '"' : '';
388 # Display the old title if the namespace/title has been changed
390 $oldTitle = Title
::makeTitle( $result->rc_namespace
, $result->rc_title
);
392 if ( !$title->equals( $oldTitle ) ) {
393 $oldTitleText = $oldTitle->getPrefixedText();
394 $oldTitleText = $this->msg( 'rc-old-title' )->params( $oldTitleText )->escaped();
397 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} {$dm}{$ulink} {$comment} {$tagDisplay} {$oldTitleText}</li>\n";
401 * Should a specific result row provide "patrollable" links?
403 * @param object $result Result row
406 protected function patrollable( $result ) {
407 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled
);
411 * Output a subscription feed listing recent edits to this page.
413 * @param $type String
415 protected function feed( $type ) {
416 global $wgFeed, $wgFeedClasses, $wgFeedLimit;
419 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
424 if ( !isset( $wgFeedClasses[$type] ) ) {
425 $this->getOutput()->addWikiMsg( 'feed-invalid' );
430 $feed = new $wgFeedClasses[$type](
432 $this->msg( 'tagline' )->text(),
433 $this->getTitle()->getFullURL()
436 $pager = new NewPagesPager( $this, $this->opts
);
437 $limit = $this->opts
->getValue( 'limit' );
438 $pager->mLimit
= min( $limit, $wgFeedLimit );
441 if ( $pager->getNumRows() > 0 ) {
442 foreach ( $pager->mResult
as $row ) {
443 $feed->outItem( $this->feedItem( $row ) );
449 protected function feedTitle() {
450 global $wgLanguageCode, $wgSitename;
451 $desc = $this->getDescription();
453 return "$wgSitename - $desc [$wgLanguageCode]";
456 protected function feedItem( $row ) {
457 $title = Title
::makeTitle( intval( $row->rc_namespace
), $row->rc_title
);
459 $date = $row->rc_timestamp
;
460 $comments = $title->getTalkPage()->getFullURL();
463 $title->getPrefixedText(),
464 $this->feedItemDesc( $row ),
465 $title->getFullURL(),
467 $this->feedItemAuthor( $row ),
475 protected function feedItemAuthor( $row ) {
476 return isset( $row->rc_user_text
) ?
$row->rc_user_text
: '';
479 protected function feedItemDesc( $row ) {
480 $revision = Revision
::newFromId( $row->rev_id
);
482 //XXX: include content model/type in feed item?
483 return '<p>' . htmlspecialchars( $revision->getUserText() ) .
484 $this->msg( 'colon-separator' )->inContentLanguage()->escaped() .
485 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
486 "</p>\n<hr />\n<div>" .
487 nl2br( htmlspecialchars( $revision->getContent()->serialize() ) ) . "</div>";
493 protected function getGroupName() {
499 * @ingroup SpecialPage Pager
501 class NewPagesPager
extends ReverseChronologicalPager
{
510 function __construct( $form, FormOptions
$opts ) {
511 parent
::__construct( $form->getContext() );
512 $this->mForm
= $form;
516 function getQueryInfo() {
517 global $wgEnableNewpagesUserFilter;
519 $conds['rc_new'] = 1;
521 $namespace = $this->opts
->getValue( 'namespace' );
522 $namespace = ( $namespace === 'all' ) ?
false : intval( $namespace );
524 $username = $this->opts
->getValue( 'username' );
525 $user = Title
::makeTitleSafe( NS_USER
, $username );
527 if ( $namespace !== false ) {
528 if ( $this->opts
->getValue( 'invert' ) ) {
529 $conds[] = 'rc_namespace != ' . $this->mDb
->addQuotes( $namespace );
531 $conds['rc_namespace'] = $namespace;
533 $rcIndexes = array( 'new_name_timestamp' );
535 $rcIndexes = array( 'rc_timestamp' );
538 # $wgEnableNewpagesUserFilter - temp WMF hack
539 if ( $wgEnableNewpagesUserFilter && $user ) {
540 $conds['rc_user_text'] = $user->getText();
541 $rcIndexes = 'rc_user_text';
542 } elseif ( User
::groupHasPermission( '*', 'createpage' ) &&
543 $this->opts
->getValue( 'hideliu' )
545 # If anons cannot make new pages, don't "exclude logged in users"!
546 $conds['rc_user'] = 0;
549 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
550 if ( $this->opts
->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
551 $conds['rc_patrolled'] = 0;
554 if ( $this->opts
->getValue( 'hidebots' ) ) {
555 $conds['rc_bot'] = 0;
558 if ( $this->opts
->getValue( 'hideredirs' ) ) {
559 $conds['page_is_redirect'] = 0;
562 // Allow changes to the New Pages query
563 $tables = array( 'recentchanges', 'page' );
565 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
566 'rc_comment', 'rc_timestamp', 'rc_patrolled', 'rc_id', 'rc_deleted',
567 'length' => 'page_len', 'rev_id' => 'page_latest', 'rc_this_oldid',
568 'page_namespace', 'page_title'
570 $join_conds = array( 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ) );
572 wfRunHooks( 'SpecialNewpagesConditions',
573 array( &$this, $this->opts
, &$conds, &$tables, &$fields, &$join_conds ) );
579 'options' => array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) ),
580 'join_conds' => $join_conds
583 // Modify query for tags
584 ChangeTags
::modifyDisplayQuery(
590 $this->opts
['tagfilter']
596 function getIndexField() {
597 return 'rc_timestamp';
600 function formatRow( $row ) {
601 return $this->mForm
->formatRow( $row );
604 function getStartBody() {
605 # Do a batch existence check on pages
606 $linkBatch = new LinkBatch();
607 foreach ( $this->mResult
as $row ) {
608 $linkBatch->add( NS_USER
, $row->rc_user_text
);
609 $linkBatch->add( NS_USER_TALK
, $row->rc_user_text
);
610 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
612 $linkBatch->execute();
617 function getEndBody() {