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