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