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