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