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