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
122 public function execute( $par ) {
123 $out = $this->getOutput();
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() {
205 $this->opts
->consumeValue( 'offset' ); // don't carry offset, DWIW
206 $namespace = $this->opts
->consumeValue( 'namespace' );
207 $username = $this->opts
->consumeValue( 'username' );
208 $tagFilterVal = $this->opts
->consumeValue( 'tagfilter' );
209 $nsinvert = $this->opts
->consumeValue( 'invert' );
211 // Check username input validity
212 $ut = Title
::makeTitleSafe( NS_USER
, $username );
213 $userText = $ut ?
$ut->getText() : '';
215 // Store query values in hidden fields so that form submission doesn't lose them
217 foreach ( $this->opts
->getUnconsumedValues() as $key => $value ) {
218 $hidden[] = Html
::hidden( $key, $value );
220 $hidden = implode( "\n", $hidden );
222 $tagFilter = ChangeTags
::buildTagFilterSelector( $tagFilterVal );
224 list( $tagFilterLabel, $tagFilterSelector ) = $tagFilter;
227 $form = Xml
::openElement( 'form', array( 'action' => $wgScript ) ) .
228 Html
::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
229 Xml
::fieldset( $this->msg( 'newpages' )->text() ) .
230 Xml
::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
232 <td class="mw-label">' .
233 Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
235 <td class="mw-input">' .
236 Html
::namespaceSelector(
238 'selected' => $namespace,
241 'name' => 'namespace',
243 'class' => 'namespaceselector',
247 $this->msg( 'invert' )->text(),
251 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
254 </tr>' . ( $tagFilter ?
(
256 <td class="mw-label">' .
259 <td class="mw-input">' .
264 <td class="mw-label">' .
265 Xml
::label( $this->msg( 'newpages-username' )->text(), 'mw-np-username' ) .
267 <td class="mw-input">' .
268 Xml
::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
272 <td class="mw-submit">' .
273 Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) .
278 <td class="mw-input">' .
279 $this->filterLinks() .
282 Xml
::closeElement( 'table' ) .
283 Xml
::closeElement( 'fieldset' ) .
285 Xml
::closeElement( 'form' );
287 $this->getOutput()->addHTML( $form );
291 * Format a row, providing the timestamp, links to the page/history,
292 * size, user links, and a comment
294 * @param object $result Result row
297 public function formatRow( $result ) {
298 $title = Title
::newFromRow( $result );
300 # Revision deletion works on revisions, so we should cast one
302 'comment' => $result->rc_comment
,
303 'deleted' => $result->rc_deleted
,
304 'user_text' => $result->rc_user_text
,
305 'user' => $result->rc_user
,
307 $rev = new Revision( $row );
308 $rev->setTitle( $title );
312 $lang = $this->getLanguage();
313 $dm = $lang->getDirMark();
315 $spanTime = Html
::element( 'span', array( 'class' => 'mw-newpages-time' ),
316 $lang->userTimeAndDate( $result->rc_timestamp
, $this->getUser() )
318 $time = Linker
::linkKnown(
322 array( 'oldid' => $result->rc_this_oldid
),
326 $query = array( 'redirect' => 'no' );
328 // Linker::linkKnown() uses 'known' and 'noclasses' options.
329 // 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(
348 array( 'class' => 'mw-newpages-length' ),
349 $this->msg( 'brackets' )->params( $this->msg( 'nbytes' )
350 ->numParams( $result->length
)->text()
354 $ulink = Linker
::revUserTools( $rev );
355 $comment = Linker
::revComment( $rev );
357 if ( $this->patrollable( $result ) ) {
358 $classes[] = 'not-patrolled';
361 # Add a class for zero byte pages
362 if ( $result->length
== 0 ) {
363 $classes[] = 'mw-newpages-zero-byte-page';
367 if ( isset( $result->ts_tags
) ) {
368 list( $tagDisplay, $newClasses ) = ChangeTags
::formatSummaryRow(
372 $classes = array_merge( $classes, $newClasses );
377 $css = count( $classes ) ?
' class="' . implode( ' ', $classes ) . '"' : '';
379 # Display the old title if the namespace/title has been changed
381 $oldTitle = Title
::makeTitle( $result->rc_namespace
, $result->rc_title
);
383 if ( !$title->equals( $oldTitle ) ) {
384 $oldTitleText = $oldTitle->getPrefixedText();
385 $oldTitleText = $this->msg( 'rc-old-title' )->params( $oldTitleText )->escaped();
388 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} "
389 . "{$dm}{$ulink} {$comment} {$tagDisplay} {$oldTitleText}</li>\n";
393 * Should a specific result row provide "patrollable" links?
395 * @param object $result Result row
398 protected function patrollable( $result ) {
399 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled
);
403 * Output a subscription feed listing recent edits to this page.
405 * @param string $type
407 protected function feed( $type ) {
408 global $wgFeed, $wgFeedClasses, $wgFeedLimit;
411 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
416 if ( !isset( $wgFeedClasses[$type] ) ) {
417 $this->getOutput()->addWikiMsg( 'feed-invalid' );
422 $feed = new $wgFeedClasses[$type](
424 $this->msg( 'tagline' )->text(),
425 $this->getPageTitle()->getFullURL()
428 $pager = new NewPagesPager( $this, $this->opts
);
429 $limit = $this->opts
->getValue( 'limit' );
430 $pager->mLimit
= min( $limit, $wgFeedLimit );
433 if ( $pager->getNumRows() > 0 ) {
434 foreach ( $pager->mResult
as $row ) {
435 $feed->outItem( $this->feedItem( $row ) );
441 protected function feedTitle() {
442 global $wgLanguageCode, $wgSitename;
443 $desc = $this->getDescription();
445 return "$wgSitename - $desc [$wgLanguageCode]";
448 protected function feedItem( $row ) {
449 $title = Title
::makeTitle( intval( $row->rc_namespace
), $row->rc_title
);
451 $date = $row->rc_timestamp
;
452 $comments = $title->getTalkPage()->getFullURL();
455 $title->getPrefixedText(),
456 $this->feedItemDesc( $row ),
457 $title->getFullURL(),
459 $this->feedItemAuthor( $row ),
467 protected function feedItemAuthor( $row ) {
468 return isset( $row->rc_user_text
) ?
$row->rc_user_text
: '';
471 protected function feedItemDesc( $row ) {
472 $revision = Revision
::newFromId( $row->rev_id
);
474 //XXX: include content model/type in feed item?
475 return '<p>' . htmlspecialchars( $revision->getUserText() ) .
476 $this->msg( 'colon-separator' )->inContentLanguage()->escaped() .
477 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
478 "</p>\n<hr />\n<div>" .
479 nl2br( htmlspecialchars( $revision->getContent()->serialize() ) ) . "</div>";
485 protected function getGroupName() {
491 * @ingroup SpecialPage Pager
493 class NewPagesPager
extends ReverseChronologicalPager
{
502 function __construct( $form, FormOptions
$opts ) {
503 parent
::__construct( $form->getContext() );
504 $this->mForm
= $form;
508 function getQueryInfo() {
510 $conds['rc_new'] = 1;
512 $namespace = $this->opts
->getValue( 'namespace' );
513 $namespace = ( $namespace === 'all' ) ?
false : intval( $namespace );
515 $username = $this->opts
->getValue( 'username' );
516 $user = Title
::makeTitleSafe( NS_USER
, $username );
518 $rcIndexes = array();
520 if ( $namespace !== false ) {
521 if ( $this->opts
->getValue( 'invert' ) ) {
522 $conds[] = 'rc_namespace != ' . $this->mDb
->addQuotes( $namespace );
524 $conds['rc_namespace'] = $namespace;
529 $conds['rc_user_text'] = $user->getText();
530 $rcIndexes = 'rc_user_text';
531 } elseif ( User
::groupHasPermission( '*', 'createpage' ) &&
532 $this->opts
->getValue( 'hideliu' )
534 # If anons cannot make new pages, don't "exclude logged in users"!
535 $conds['rc_user'] = 0;
538 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
539 if ( $this->opts
->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
540 $conds['rc_patrolled'] = 0;
543 if ( $this->opts
->getValue( 'hidebots' ) ) {
544 $conds['rc_bot'] = 0;
547 if ( $this->opts
->getValue( 'hideredirs' ) ) {
548 $conds['page_is_redirect'] = 0;
551 // Allow changes to the New Pages query
552 $tables = array( 'recentchanges', 'page' );
554 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
555 'rc_comment', 'rc_timestamp', 'rc_patrolled', 'rc_id', 'rc_deleted',
556 'length' => 'page_len', 'rev_id' => 'page_latest', 'rc_this_oldid',
557 'page_namespace', 'page_title'
559 $join_conds = array( 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ) );
561 wfRunHooks( 'SpecialNewpagesConditions',
562 array( &$this, $this->opts
, &$conds, &$tables, &$fields, &$join_conds ) );
567 $options = array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) );
574 'options' => $options,
575 'join_conds' => $join_conds
578 // Modify query for tags
579 ChangeTags
::modifyDisplayQuery(
585 $this->opts
['tagfilter']
591 function getIndexField() {
592 return 'rc_timestamp';
595 function formatRow( $row ) {
596 return $this->mForm
->formatRow( $row );
599 function getStartBody() {
600 # Do a batch existence check on pages
601 $linkBatch = new LinkBatch();
602 foreach ( $this->mResult
as $row ) {
603 $linkBatch->add( NS_USER
, $row->rc_user_text
);
604 $linkBatch->add( NS_USER_TALK
, $row->rc_user_text
);
605 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
607 $linkBatch->execute();
612 function getEndBody() {