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() {
203 $this->opts
->consumeValue( 'offset' ); // don't carry offset, DWIW
204 $namespace = $this->opts
->consumeValue( 'namespace' );
205 $username = $this->opts
->consumeValue( 'username' );
206 $tagFilterVal = $this->opts
->consumeValue( 'tagfilter' );
207 $nsinvert = $this->opts
->consumeValue( 'invert' );
209 // Check username input validity
210 $ut = Title
::makeTitleSafe( NS_USER
, $username );
211 $userText = $ut ?
$ut->getText() : '';
213 // Store query values in hidden fields so that form submission doesn't lose them
215 foreach ( $this->opts
->getUnconsumedValues() as $key => $value ) {
216 $hidden[] = Html
::hidden( $key, $value );
218 $hidden = implode( "\n", $hidden );
220 $tagFilter = ChangeTags
::buildTagFilterSelector( $tagFilterVal );
222 list( $tagFilterLabel, $tagFilterSelector ) = $tagFilter;
225 $form = Xml
::openElement( 'form', array( 'action' => wfScript() ) ) .
226 Html
::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
227 Xml
::fieldset( $this->msg( 'newpages' )->text() ) .
228 Xml
::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
230 <td class="mw-label">' .
231 Xml
::label( $this->msg( 'namespace' )->text(), 'namespace' ) .
233 <td class="mw-input">' .
234 Html
::namespaceSelector(
236 'selected' => $namespace,
239 'name' => 'namespace',
241 'class' => 'namespaceselector',
245 $this->msg( 'invert' )->text(),
249 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
252 </tr>' . ( $tagFilter ?
(
254 <td class="mw-label">' .
257 <td class="mw-input">' .
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,
290 * size, user links, and a comment
292 * @param object $result Result row
295 public function formatRow( $result ) {
296 $title = Title
::newFromRow( $result );
298 # Revision deletion works on revisions, so we should cast one
300 'comment' => $result->rc_comment
,
301 'deleted' => $result->rc_deleted
,
302 'user_text' => $result->rc_user_text
,
303 'user' => $result->rc_user
,
305 $rev = new Revision( $row );
306 $rev->setTitle( $title );
310 $lang = $this->getLanguage();
311 $dm = $lang->getDirMark();
313 $spanTime = Html
::element( 'span', array( 'class' => 'mw-newpages-time' ),
314 $lang->userTimeAndDate( $result->rc_timestamp
, $this->getUser() )
316 $time = Linker
::linkKnown(
320 array( 'oldid' => $result->rc_this_oldid
),
324 $query = array( 'redirect' => 'no' );
326 // Linker::linkKnown() uses 'known' and 'noclasses' options.
327 // This breaks the colouration for stubs.
328 $plink = Linker
::link(
331 array( 'class' => 'mw-newpages-pagename' ),
335 $histLink = Linker
::linkKnown(
337 $this->msg( 'hist' )->escaped(),
339 array( 'action' => 'history' )
341 $hist = Html
::rawElement( 'span', array( 'class' => 'mw-newpages-history' ),
342 $this->msg( 'parentheses' )->rawParams( $histLink )->escaped() );
344 $length = Html
::element(
346 array( 'class' => 'mw-newpages-length' ),
347 $this->msg( 'brackets' )->params( $this->msg( 'nbytes' )
348 ->numParams( $result->length
)->text()
352 $ulink = Linker
::revUserTools( $rev );
353 $comment = Linker
::revComment( $rev );
355 if ( $this->patrollable( $result ) ) {
356 $classes[] = 'not-patrolled';
359 # Add a class for zero byte pages
360 if ( $result->length
== 0 ) {
361 $classes[] = 'mw-newpages-zero-byte-page';
365 if ( isset( $result->ts_tags
) ) {
366 list( $tagDisplay, $newClasses ) = ChangeTags
::formatSummaryRow(
370 $classes = array_merge( $classes, $newClasses );
375 $css = count( $classes ) ?
' class="' . implode( ' ', $classes ) . '"' : '';
377 # Display the old title if the namespace/title has been changed
379 $oldTitle = Title
::makeTitle( $result->rc_namespace
, $result->rc_title
);
381 if ( !$title->equals( $oldTitle ) ) {
382 $oldTitleText = $oldTitle->getPrefixedText();
383 $oldTitleText = $this->msg( 'rc-old-title' )->params( $oldTitleText )->escaped();
386 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} "
387 . "{$dm}{$ulink} {$comment} {$tagDisplay} {$oldTitleText}</li>\n";
391 * Should a specific result row provide "patrollable" links?
393 * @param object $result Result row
396 protected function patrollable( $result ) {
397 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled
);
401 * Output a subscription feed listing recent edits to this page.
403 * @param string $type
405 protected function feed( $type ) {
406 if ( !$this->getConfig()->get( 'Feed' ) ) {
407 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
412 $feedClasses = $this->getConfig()->get( 'FeedClasses' );
413 if ( !isset( $feedClasses[$type] ) ) {
414 $this->getOutput()->addWikiMsg( 'feed-invalid' );
419 $feed = new $feedClasses[$type](
421 $this->msg( 'tagline' )->text(),
422 $this->getPageTitle()->getFullURL()
425 $pager = new NewPagesPager( $this, $this->opts
);
426 $limit = $this->opts
->getValue( 'limit' );
427 $pager->mLimit
= min( $limit, $this->getConfig()->get( 'FeedLimit' ) );
430 if ( $pager->getNumRows() > 0 ) {
431 foreach ( $pager->mResult
as $row ) {
432 $feed->outItem( $this->feedItem( $row ) );
438 protected function feedTitle() {
439 $desc = $this->getDescription();
440 $code = $this->getConfig()->get( 'LanguageCode' );
441 $sitename = $this->getConfig()->get( 'Sitename' );
443 return "$sitename - $desc [$code]";
446 protected function feedItem( $row ) {
447 $title = Title
::makeTitle( intval( $row->rc_namespace
), $row->rc_title
);
449 $date = $row->rc_timestamp
;
450 $comments = $title->getTalkPage()->getFullURL();
453 $title->getPrefixedText(),
454 $this->feedItemDesc( $row ),
455 $title->getFullURL(),
457 $this->feedItemAuthor( $row ),
465 protected function feedItemAuthor( $row ) {
466 return isset( $row->rc_user_text
) ?
$row->rc_user_text
: '';
469 protected function feedItemDesc( $row ) {
470 $revision = Revision
::newFromId( $row->rev_id
);
472 //XXX: include content model/type in feed item?
473 return '<p>' . htmlspecialchars( $revision->getUserText() ) .
474 $this->msg( 'colon-separator' )->inContentLanguage()->escaped() .
475 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
476 "</p>\n<hr />\n<div>" .
477 nl2br( htmlspecialchars( $revision->getContent()->serialize() ) ) . "</div>";
483 protected function getGroupName() {
489 * @ingroup SpecialPage Pager
491 class NewPagesPager
extends ReverseChronologicalPager
{
500 function __construct( $form, FormOptions
$opts ) {
501 parent
::__construct( $form->getContext() );
502 $this->mForm
= $form;
506 function getQueryInfo() {
508 $conds['rc_new'] = 1;
510 $namespace = $this->opts
->getValue( 'namespace' );
511 $namespace = ( $namespace === 'all' ) ?
false : intval( $namespace );
513 $username = $this->opts
->getValue( 'username' );
514 $user = Title
::makeTitleSafe( NS_USER
, $username );
516 $rcIndexes = array();
518 if ( $namespace !== false ) {
519 if ( $this->opts
->getValue( 'invert' ) ) {
520 $conds[] = 'rc_namespace != ' . $this->mDb
->addQuotes( $namespace );
522 $conds['rc_namespace'] = $namespace;
527 $conds['rc_user_text'] = $user->getText();
528 $rcIndexes = 'rc_user_text';
529 } elseif ( User
::groupHasPermission( '*', 'createpage' ) &&
530 $this->opts
->getValue( 'hideliu' )
532 # If anons cannot make new pages, don't "exclude logged in users"!
533 $conds['rc_user'] = 0;
536 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
537 if ( $this->opts
->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
538 $conds['rc_patrolled'] = 0;
541 if ( $this->opts
->getValue( 'hidebots' ) ) {
542 $conds['rc_bot'] = 0;
545 if ( $this->opts
->getValue( 'hideredirs' ) ) {
546 $conds['page_is_redirect'] = 0;
549 // Allow changes to the New Pages query
550 $tables = array( 'recentchanges', 'page' );
552 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
553 'rc_comment', 'rc_timestamp', 'rc_patrolled', 'rc_id', 'rc_deleted',
554 'length' => 'page_len', 'rev_id' => 'page_latest', 'rc_this_oldid',
555 'page_namespace', 'page_title'
557 $join_conds = array( 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ) );
559 wfRunHooks( 'SpecialNewpagesConditions',
560 array( &$this, $this->opts
, &$conds, &$tables, &$fields, &$join_conds ) );
565 $options = array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) );
572 'options' => $options,
573 'join_conds' => $join_conds
576 // Modify query for tags
577 ChangeTags
::modifyDisplayQuery(
583 $this->opts
['tagfilter']
589 function getIndexField() {
590 return 'rc_timestamp';
593 function formatRow( $row ) {
594 return $this->mForm
->formatRow( $row );
597 function getStartBody() {
598 # Do a batch existence check on pages
599 $linkBatch = new LinkBatch();
600 foreach ( $this->mResult
as $row ) {
601 $linkBatch->add( NS_USER
, $row->rc_user_text
);
602 $linkBatch->add( NS_USER_TALK
, $row->rc_user_text
);
603 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
605 $linkBatch->execute();
610 function getEndBody() {