Removed call to OutputPage::setSyndicated( true ) just before calling OutputPage...
[lhc/web/wiklou.git] / includes / specials / SpecialNewpages.php
1 <?php
2 /**
3 * Implements Special:Newpages
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * A special page that list newly created pages
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialNewpages extends IncludableSpecialPage {
30
31 // Stored objects
32
33 /**
34 * @var FormOptions
35 */
36 protected $opts;
37 protected $customFilters;
38
39 // Some internal settings
40 protected $showNavigation = false;
41
42 public function __construct() {
43 parent::__construct( 'Newpages' );
44 }
45
46 protected function setup( $par ) {
47 global $wgEnableNewpagesUserFilter;
48
49 // Options
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', '' );
62
63 $this->customFilters = array();
64 wfRunHooks( 'SpecialNewPagesFilters', array( $this, &$this->customFilters ) );
65 foreach( $this->customFilters as $key => $params ) {
66 $opts->add( $key, $params['default'] );
67 }
68
69 // Set values
70 $opts->fetchValuesFromRequest( $this->getRequest() );
71 if ( $par ) $this->parseParams( $par );
72
73 // Validate
74 $opts->validateIntBounds( 'limit', 0, 5000 );
75 if( !$wgEnableNewpagesUserFilter ) {
76 $opts->setValue( 'username', '' );
77 }
78 }
79
80 protected function parseParams( $par ) {
81 global $wgLang;
82 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
83 foreach ( $bits as $bit ) {
84 if ( 'shownav' == $bit ) {
85 $this->showNavigation = true;
86 }
87 if ( 'hideliu' === $bit ) {
88 $this->opts->setValue( 'hideliu', true );
89 }
90 if ( 'hidepatrolled' == $bit ) {
91 $this->opts->setValue( 'hidepatrolled', true );
92 }
93 if ( 'hidebots' == $bit ) {
94 $this->opts->setValue( 'hidebots', true );
95 }
96 if ( 'showredirs' == $bit ) {
97 $this->opts->setValue( 'hideredirs', false );
98 }
99 if ( is_numeric( $bit ) ) {
100 $this->opts->setValue( 'limit', intval( $bit ) );
101 }
102
103 $m = array();
104 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
105 $this->opts->setValue( 'limit', intval( $m[1] ) );
106 }
107 // PG offsets not just digits!
108 if ( preg_match( '/^offset=([^=]+)$/', $bit, $m ) ) {
109 $this->opts->setValue( 'offset', intval( $m[1] ) );
110 }
111 if ( preg_match( '/^username=(.*)$/', $bit, $m ) ) {
112 $this->opts->setValue( 'username', $m[1] );
113 }
114 if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) {
115 $ns = $wgLang->getNsIndex( $m[1] );
116 if( $ns !== false ) {
117 $this->opts->setValue( 'namespace', $ns );
118 }
119 }
120 }
121 }
122
123 /**
124 * Show a form for filtering namespace and username
125 *
126 * @param $par String
127 * @return String
128 */
129 public function execute( $par ) {
130 $out = $this->getOutput();
131
132 $this->setHeaders();
133 $this->outputHeader();
134
135 $this->showNavigation = !$this->including(); // Maybe changed in setup
136 $this->setup( $par );
137
138 if( !$this->including() ) {
139 // Settings
140 $this->form();
141
142 $feedType = $this->opts->getValue( 'feed' );
143 if( $feedType ) {
144 return $this->feed( $feedType );
145 }
146
147 $out->setFeedAppendQuery( wfArrayToCGI( $this->opts->getAllValues() ) );
148 }
149
150 $pager = new NewPagesPager( $this, $this->opts );
151 $pager->mLimit = $this->opts->getValue( 'limit' );
152 $pager->mOffset = $this->opts->getValue( 'offset' );
153
154 if( $pager->getNumRows() ) {
155 $navigation = '';
156 if ( $this->showNavigation ) {
157 $navigation = $pager->getNavigationBar();
158 }
159 $out->addHTML( $navigation . $pager->getBody() . $navigation );
160 } else {
161 $out->addWikiMsg( 'specialpage-empty' );
162 }
163 }
164
165 protected function filterLinks() {
166 global $wgGroupPermissions, $wgLang;
167
168 // show/hide links
169 $showhide = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ) );
170
171 // Option value -> message mapping
172 $filters = array(
173 'hideliu' => 'rcshowhideliu',
174 'hidepatrolled' => 'rcshowhidepatr',
175 'hidebots' => 'rcshowhidebots',
176 'hideredirs' => 'whatlinkshere-hideredirs'
177 );
178 foreach ( $this->customFilters as $key => $params ) {
179 $filters[$key] = $params['msg'];
180 }
181
182 // Disable some if needed
183 # @todo FIXME: Throws E_NOTICEs if not set; and doesn't obey hooks etc.
184 if ( $wgGroupPermissions['*']['createpage'] !== true ) {
185 unset( $filters['hideliu'] );
186 }
187 if ( !$this->getUser()->useNPPatrol() ) {
188 unset( $filters['hidepatrolled'] );
189 }
190
191 $links = array();
192 $changed = $this->opts->getChangedValues();
193 unset( $changed['offset'] ); // Reset offset if query type changes
194
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
200 );
201 $links[$key] = wfMsgHtml( $msg, $link );
202 }
203
204 return $wgLang->pipeList( $links );
205 }
206
207 protected function form() {
208 global $wgEnableNewpagesUserFilter, $wgScript;
209
210 // Consume values
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
216 // Check username input validity
217 $ut = Title::makeTitleSafe( NS_USER, $username );
218 $userText = $ut ? $ut->getText() : '';
219
220 // Store query values in hidden fields so that form submission doesn't lose them
221 $hidden = array();
222 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
223 $hidden[] = Html::hidden( $key, $value );
224 }
225 $hidden = implode( "\n", $hidden );
226
227 $tagFilter = ChangeTags::buildTagFilterSelector( $tagFilterVal );
228 if ( $tagFilter ) {
229 list( $tagFilterLabel, $tagFilterSelector ) = $tagFilter;
230 }
231
232 $form = Xml::openElement( 'form', array( 'action' => $wgScript ) ) .
233 Html::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
234 Xml::fieldset( wfMsg( 'newpages' ) ) .
235 Xml::openElement( 'table', array( 'id' => 'mw-newpages-table' ) ) .
236 '<tr>
237 <td class="mw-label">' .
238 Xml::label( wfMsg( 'namespace' ), 'namespace' ) .
239 '</td>
240 <td class="mw-input">' .
241 Xml::namespaceSelector( $namespace, 'all' ) .
242 '</td>
243 </tr>' . ( $tagFilter ? (
244 '<tr>
245 <td class="mw-label">' .
246 $tagFilterLabel .
247 '</td>
248 <td class="mw-input">' .
249 $tagFilterSelector .
250 '</td>
251 </tr>' ) : '' ) .
252 ( $wgEnableNewpagesUserFilter ?
253 '<tr>
254 <td class="mw-label">' .
255 Xml::label( wfMsg( 'newpages-username' ), 'mw-np-username' ) .
256 '</td>
257 <td class="mw-input">' .
258 Xml::input( 'username', 30, $userText, array( 'id' => 'mw-np-username' ) ) .
259 '</td>
260 </tr>' : '' ) .
261 '<tr> <td></td>
262 <td class="mw-submit">' .
263 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
264 '</td>
265 </tr>' .
266 '<tr>
267 <td></td>
268 <td class="mw-input">' .
269 $this->filterLinks() .
270 '</td>
271 </tr>' .
272 Xml::closeElement( 'table' ) .
273 Xml::closeElement( 'fieldset' ) .
274 $hidden .
275 Xml::closeElement( 'form' );
276
277 $this->getOutput()->addHTML( $form );
278 }
279
280 /**
281 * Format a row, providing the timestamp, links to the page/history, size, user links, and a comment
282 *
283 * @param $result Result row
284 * @return String
285 */
286 public function formatRow( $result ) {
287 global $wgLang;
288
289 # Revision deletion works on revisions, so we should cast one
290 $row = array(
291 'comment' => $result->rc_comment,
292 'deleted' => $result->rc_deleted,
293 'user_text' => $result->rc_user_text,
294 'user' => $result->rc_user,
295 );
296 $rev = new Revision( $row );
297
298 $classes = array();
299
300 $dm = $wgLang->getDirMark();
301
302 $title = Title::makeTitleSafe( $result->rc_namespace, $result->rc_title );
303 $time = Html::element( 'span', array( 'class' => 'mw-newpages-time' ),
304 $wgLang->timeAndDate( $result->rc_timestamp, true )
305 );
306
307 $query = array( 'redirect' => 'no' );
308
309 if( $this->patrollable( $result ) ) {
310 $query['rcid'] = $result->rc_id;
311 }
312
313 $plink = Linker::linkKnown(
314 $title,
315 null,
316 array( 'class' => 'mw-newpages-pagename' ),
317 $query,
318 array( 'known' ) // Set explicitly to avoid the default of 'known','noclasses'. This breaks the colouration for stubs
319 );
320 $histLink = Linker::linkKnown(
321 $title,
322 wfMsgHtml( 'hist' ),
323 array(),
324 array( 'action' => 'history' )
325 );
326 $hist = Html::rawElement( 'span', array( 'class' => 'mw-newpages-history' ), wfMsg( 'parentheses', $histLink ) );
327
328 $length = Html::rawElement( 'span', array( 'class' => 'mw-newpages-length' ),
329 '[' . wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ), $wgLang->formatNum( $result->length ) ) .
330 ']'
331 );
332
333 $ulink = Linker::revUserTools( $rev );
334 $comment = Linker::revComment( $rev );
335
336 if ( $this->patrollable( $result ) ) {
337 $classes[] = 'not-patrolled';
338 }
339
340 # Add a class for zero byte pages
341 if ( $result->length == 0 ) {
342 $classes[] = 'mw-newpages-zero-byte-page';
343 }
344
345 # Tags, if any. check for including due to bug 23293
346 if ( !$this->including() ) {
347 list( $tagDisplay, $newClasses ) = ChangeTags::formatSummaryRow( $result->ts_tags, 'newpages' );
348 $classes = array_merge( $classes, $newClasses );
349 } else {
350 $tagDisplay = '';
351 }
352
353 $css = count( $classes ) ? ' class="' . implode( ' ', $classes ) . '"' : '';
354
355 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} {$dm}{$ulink} {$comment} {$tagDisplay}</li>\n";
356 }
357
358 /**
359 * Should a specific result row provide "patrollable" links?
360 *
361 * @param $result Result row
362 * @return Boolean
363 */
364 protected function patrollable( $result ) {
365 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled );
366 }
367
368 /**
369 * Output a subscription feed listing recent edits to this page.
370 *
371 * @param $type String
372 */
373 protected function feed( $type ) {
374 global $wgFeed, $wgFeedClasses, $wgFeedLimit;
375
376 if ( !$wgFeed ) {
377 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
378 return;
379 }
380
381 if( !isset( $wgFeedClasses[$type] ) ) {
382 $this->getOutput()->addWikiMsg( 'feed-invalid' );
383 return;
384 }
385
386 $feed = new $wgFeedClasses[$type](
387 $this->feedTitle(),
388 wfMsgExt( 'tagline', 'parsemag' ),
389 $this->getTitle()->getFullUrl()
390 );
391
392 $pager = new NewPagesPager( $this, $this->opts );
393 $limit = $this->opts->getValue( 'limit' );
394 $pager->mLimit = min( $limit, $wgFeedLimit );
395
396 $feed->outHeader();
397 if( $pager->getNumRows() > 0 ) {
398 foreach ( $pager->mResult as $row ) {
399 $feed->outItem( $this->feedItem( $row ) );
400 }
401 }
402 $feed->outFooter();
403 }
404
405 protected function feedTitle() {
406 global $wgLanguageCode, $wgSitename;
407 $desc = $this->getDescription();
408 return "$wgSitename - $desc [$wgLanguageCode]";
409 }
410
411 protected function feedItem( $row ) {
412 $title = Title::MakeTitle( intval( $row->rc_namespace ), $row->rc_title );
413 if( $title ) {
414 $date = $row->rc_timestamp;
415 $comments = $title->getTalkPage()->getFullURL();
416
417 return new FeedItem(
418 $title->getPrefixedText(),
419 $this->feedItemDesc( $row ),
420 $title->getFullURL(),
421 $date,
422 $this->feedItemAuthor( $row ),
423 $comments
424 );
425 } else {
426 return null;
427 }
428 }
429
430 protected function feedItemAuthor( $row ) {
431 return isset( $row->rc_user_text ) ? $row->rc_user_text : '';
432 }
433
434 protected function feedItemDesc( $row ) {
435 $revision = Revision::newFromId( $row->rev_id );
436 if( $revision ) {
437 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
438 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
439 "</p>\n<hr />\n<div>" .
440 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
441 }
442 return '';
443 }
444 }
445
446 /**
447 * @ingroup SpecialPage Pager
448 */
449 class NewPagesPager extends ReverseChronologicalPager {
450 // Stored opts
451 protected $opts;
452
453 /**
454 * @var HtmlForm
455 */
456 protected $mForm;
457
458 function __construct( $form, FormOptions $opts ) {
459 parent::__construct( $form->getContext() );
460 $this->mForm = $form;
461 $this->opts = $opts;
462 }
463
464 function getQueryInfo() {
465 global $wgEnableNewpagesUserFilter, $wgGroupPermissions;
466 $conds = array();
467 $conds['rc_new'] = 1;
468
469 $namespace = $this->opts->getValue( 'namespace' );
470 $namespace = ( $namespace === 'all' ) ? false : intval( $namespace );
471
472 $username = $this->opts->getValue( 'username' );
473 $user = Title::makeTitleSafe( NS_USER, $username );
474
475 if( $namespace !== false ) {
476 $conds['rc_namespace'] = $namespace;
477 $rcIndexes = array( 'new_name_timestamp' );
478 } else {
479 $rcIndexes = array( 'rc_timestamp' );
480 }
481
482 # $wgEnableNewpagesUserFilter - temp WMF hack
483 if( $wgEnableNewpagesUserFilter && $user ) {
484 $conds['rc_user_text'] = $user->getText();
485 $rcIndexes = 'rc_user_text';
486 # If anons cannot make new pages, don't "exclude logged in users"!
487 } elseif( $wgGroupPermissions['*']['createpage'] && $this->opts->getValue( 'hideliu' ) ) {
488 $conds['rc_user'] = 0;
489 }
490 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
491 if( $this->opts->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
492 $conds['rc_patrolled'] = 0;
493 }
494 if( $this->opts->getValue( 'hidebots' ) ) {
495 $conds['rc_bot'] = 0;
496 }
497
498 if ( $this->opts->getValue( 'hideredirs' ) ) {
499 $conds['page_is_redirect'] = 0;
500 }
501
502 // Allow changes to the New Pages query
503 $tables = array( 'recentchanges', 'page' );
504 $fields = array(
505 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
506 'rc_comment', 'rc_timestamp', 'rc_patrolled','rc_id', 'rc_deleted',
507 'page_len AS length', 'page_latest AS rev_id', 'ts_tags'
508 );
509 $join_conds = array( 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ) );
510
511 wfRunHooks( 'SpecialNewpagesConditions',
512 array( &$this, $this->opts, &$conds, &$tables, &$fields, &$join_conds ) );
513
514 $info = array(
515 'tables' => $tables,
516 'fields' => $fields,
517 'conds' => $conds,
518 'options' => array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) ),
519 'join_conds' => $join_conds
520 );
521
522 // Empty array for fields, it'll be set by us anyway.
523 $fields = array();
524
525 // Modify query for tags
526 ChangeTags::modifyDisplayQuery(
527 $info['tables'],
528 $fields,
529 $info['conds'],
530 $info['join_conds'],
531 $info['options'],
532 $this->opts['tagfilter']
533 );
534
535 return $info;
536 }
537
538 function getIndexField() {
539 return 'rc_timestamp';
540 }
541
542 function formatRow( $row ) {
543 return $this->mForm->formatRow( $row );
544 }
545
546 function getStartBody() {
547 # Do a batch existence check on pages
548 $linkBatch = new LinkBatch();
549 foreach ( $this->mResult as $row ) {
550 $linkBatch->add( NS_USER, $row->rc_user_text );
551 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
552 $linkBatch->add( $row->rc_namespace, $row->rc_title );
553 }
554 $linkBatch->execute();
555 return '<ul>';
556 }
557
558 function getEndBody() {
559 return '</ul>';
560 }
561 }