c8d4aa637c39d6cea85c130caae3045cd557ba89
[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 * @var FormOptions
32 */
33 protected $opts;
34 protected $customFilters;
35
36 protected $showNavigation = false;
37
38 public function __construct() {
39 parent::__construct( 'Newpages' );
40 }
41
42 protected function setup( $par ) {
43 // Options
44 $opts = new FormOptions();
45 $this->opts = $opts; // bind
46 $opts->add( 'hideliu', false );
47 $opts->add( 'hidepatrolled', $this->getUser()->getBoolOption( 'newpageshidepatrolled' ) );
48 $opts->add( 'hidebots', false );
49 $opts->add( 'hideredirs', true );
50 $opts->add( 'limit', $this->getUser()->getIntOption( 'rclimit' ) );
51 $opts->add( 'offset', '' );
52 $opts->add( 'namespace', '0' );
53 $opts->add( 'username', '' );
54 $opts->add( 'feed', '' );
55 $opts->add( 'tagfilter', '' );
56 $opts->add( 'invert', false );
57
58 $this->customFilters = array();
59 Hooks::run( 'SpecialNewPagesFilters', array( $this, &$this->customFilters ) );
60 foreach ( $this->customFilters as $key => $params ) {
61 $opts->add( $key, $params['default'] );
62 }
63
64 // Set values
65 $opts->fetchValuesFromRequest( $this->getRequest() );
66 if ( $par ) {
67 $this->parseParams( $par );
68 }
69
70 // Validate
71 $opts->validateIntBounds( 'limit', 0, 5000 );
72 }
73
74 protected function parseParams( $par ) {
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 = $this->getLanguage()->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 string $par
120 */
121 public function execute( $par ) {
122 $out = $this->getOutput();
123
124 $this->setHeaders();
125 $this->outputHeader();
126
127 $this->showNavigation = !$this->including(); // Maybe changed in setup
128 $this->setup( $par );
129
130 $this->addHelpLink( 'Help:New pages' );
131
132 if ( !$this->including() ) {
133 // Settings
134 $this->form();
135
136 $feedType = $this->opts->getValue( 'feed' );
137 if ( $feedType ) {
138 $this->feed( $feedType );
139
140 return;
141 }
142
143 $allValues = $this->opts->getAllValues();
144 unset( $allValues['feed'] );
145 $out->setFeedAppendQuery( wfArrayToCgi( $allValues ) );
146 }
147
148 $pager = new NewPagesPager( $this, $this->opts );
149 $pager->mLimit = $this->opts->getValue( 'limit' );
150 $pager->mOffset = $this->opts->getValue( 'offset' );
151
152 if ( $pager->getNumRows() ) {
153 $navigation = '';
154 if ( $this->showNavigation ) {
155 $navigation = $pager->getNavigationBar();
156 }
157 $out->addHTML( $navigation . $pager->getBody() . $navigation );
158 } else {
159 $out->addWikiMsg( 'specialpage-empty' );
160 }
161 }
162
163 protected function filterLinks() {
164 // show/hide links
165 $showhide = array( $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() );
166
167 // Option value -> message mapping
168 $filters = array(
169 'hideliu' => 'rcshowhideliu',
170 'hidepatrolled' => 'rcshowhidepatr',
171 'hidebots' => 'rcshowhidebots',
172 'hideredirs' => 'whatlinkshere-hideredirs'
173 );
174 foreach ( $this->customFilters as $key => $params ) {
175 $filters[$key] = $params['msg'];
176 }
177
178 // Disable some if needed
179 if ( !User::groupHasPermission( '*', 'createpage' ) ) {
180 unset( $filters['hideliu'] );
181 }
182 if ( !$this->getUser()->useNPPatrol() ) {
183 unset( $filters['hidepatrolled'] );
184 }
185
186 $links = array();
187 $changed = $this->opts->getChangedValues();
188 unset( $changed['offset'] ); // Reset offset if query type changes
189
190 $self = $this->getPageTitle();
191 foreach ( $filters as $key => $msg ) {
192 $onoff = 1 - $this->opts->getValue( $key );
193 $link = Linker::link( $self, $showhide[$onoff], array(),
194 array( $key => $onoff ) + $changed
195 );
196 $links[$key] = $this->msg( $msg )->rawParams( $link )->escaped();
197 }
198
199 return $this->getLanguage()->pipeList( $links );
200 }
201
202 protected function form() {
203 $out = $this->getOutput();
204 $out->addModules( 'mediawiki.userSuggest' );
205
206 // Consume values
207 $this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW
208 $namespace = $this->opts->consumeValue( 'namespace' );
209 $username = $this->opts->consumeValue( 'username' );
210 $tagFilterVal = $this->opts->consumeValue( 'tagfilter' );
211 $nsinvert = $this->opts->consumeValue( 'invert' );
212
213 // Check username input validity
214 $ut = Title::makeTitleSafe( NS_USER, $username );
215 $userText = $ut ? $ut->getText() : '';
216
217 // Store query values in hidden fields so that form submission doesn't lose them
218 $hidden = array();
219 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
220 $hidden[] = Html::hidden( $key, $value );
221 }
222 $hidden = implode( "\n", $hidden );
223
224 $form = array(
225 'namespace' => array(
226 'type' => 'namespaceselect',
227 'name' => 'namespace',
228 'label-message' => 'namespace',
229 'default' => $namespace,
230 ),
231 'nsinvert' => array(
232 'type' => 'check',
233 'name' => 'invert',
234 'label-message' => 'invert',
235 'default' => $nsinvert,
236 'tooltip' => 'invert',
237 ),
238 'tagFilter' => array(
239 'type' => 'tagfilter',
240 'name' => 'tagfilter',
241 'label-raw' => $this->msg( 'tag-filter' )->parse(),
242 'default' => $tagFilterVal,
243 ),
244 'username' => array(
245 'type' => 'text',
246 'name' => 'username',
247 'label-message' => 'newpages-username',
248 'default' => $userText,
249 'id' => 'mw-np-username',
250 'size' => 30,
251 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
252 ),
253 );
254
255 $htmlForm = new HTMLForm( $form, $this->getContext() );
256
257 $htmlForm->setSubmitText( $this->msg( 'allpagessubmit' )->text() );
258 $htmlForm->setSubmitProgressive();
259 // The form should be visible on each request (inclusive requests with submitted forms), so
260 // return always false here.
261 $htmlForm->setSubmitCallback(
262 function () {
263 return false;
264 }
265 );
266 $htmlForm->setMethod( 'get' );
267
268 $out->addHtml( Xml::fieldset( $this->msg( 'newpages' )->text() ) );
269
270 $htmlForm->show();
271
272 $out->addHtml(
273 Html::rawElement(
274 'div',
275 null,
276 $this->filterLinks()
277 ) .
278 Xml::closeElement( 'fieldset' )
279 );
280 }
281
282 /**
283 * Format a row, providing the timestamp, links to the page/history,
284 * size, user links, and a comment
285 *
286 * @param object $result Result row
287 * @return string
288 */
289 public function formatRow( $result ) {
290 $title = Title::newFromRow( $result );
291
292 # Revision deletion works on revisions, so we should cast one
293 $row = array(
294 'comment' => $result->rc_comment,
295 'deleted' => $result->rc_deleted,
296 'user_text' => $result->rc_user_text,
297 'user' => $result->rc_user,
298 );
299 $rev = new Revision( $row );
300 $rev->setTitle( $title );
301
302 $classes = array();
303
304 $lang = $this->getLanguage();
305 $dm = $lang->getDirMark();
306
307 $spanTime = Html::element( 'span', array( 'class' => 'mw-newpages-time' ),
308 $lang->userTimeAndDate( $result->rc_timestamp, $this->getUser() )
309 );
310 $time = Linker::linkKnown(
311 $title,
312 $spanTime,
313 array(),
314 array( 'oldid' => $result->rc_this_oldid ),
315 array()
316 );
317
318 $query = $title->isRedirect() ? array( 'redirect' => 'no' ) : array();
319
320 // Linker::linkKnown() uses 'known' and 'noclasses' options.
321 // This breaks the colouration for stubs.
322 $plink = Linker::link(
323 $title,
324 null,
325 array( 'class' => 'mw-newpages-pagename' ),
326 $query,
327 array( 'known' )
328 );
329 $histLink = Linker::linkKnown(
330 $title,
331 $this->msg( 'hist' )->escaped(),
332 array(),
333 array( 'action' => 'history' )
334 );
335 $hist = Html::rawElement( 'span', array( 'class' => 'mw-newpages-history' ),
336 $this->msg( 'parentheses' )->rawParams( $histLink )->escaped() );
337
338 $length = Html::rawElement(
339 'span',
340 array( 'class' => 'mw-newpages-length' ),
341 $this->msg( 'brackets' )->rawParams(
342 $this->msg( 'nbytes' )->numParams( $result->length )->escaped()
343 )->escaped()
344 );
345
346 $ulink = Linker::revUserTools( $rev );
347 $comment = Linker::revComment( $rev );
348
349 if ( $this->patrollable( $result ) ) {
350 $classes[] = 'not-patrolled';
351 }
352
353 # Add a class for zero byte pages
354 if ( $result->length == 0 ) {
355 $classes[] = 'mw-newpages-zero-byte-page';
356 }
357
358 # Tags, if any.
359 if ( isset( $result->ts_tags ) ) {
360 list( $tagDisplay, $newClasses ) = ChangeTags::formatSummaryRow(
361 $result->ts_tags,
362 'newpages'
363 );
364 $classes = array_merge( $classes, $newClasses );
365 } else {
366 $tagDisplay = '';
367 }
368
369 $css = count( $classes ) ? ' class="' . implode( ' ', $classes ) . '"' : '';
370
371 # Display the old title if the namespace/title has been changed
372 $oldTitleText = '';
373 $oldTitle = Title::makeTitle( $result->rc_namespace, $result->rc_title );
374
375 if ( !$title->equals( $oldTitle ) ) {
376 $oldTitleText = $oldTitle->getPrefixedText();
377 $oldTitleText = $this->msg( 'rc-old-title' )->params( $oldTitleText )->escaped();
378 }
379
380 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} "
381 . "{$dm}{$ulink} {$comment} {$tagDisplay} {$oldTitleText}</li>\n";
382 }
383
384 /**
385 * Should a specific result row provide "patrollable" links?
386 *
387 * @param object $result Result row
388 * @return bool
389 */
390 protected function patrollable( $result ) {
391 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled );
392 }
393
394 /**
395 * Output a subscription feed listing recent edits to this page.
396 *
397 * @param string $type
398 */
399 protected function feed( $type ) {
400 if ( !$this->getConfig()->get( 'Feed' ) ) {
401 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
402
403 return;
404 }
405
406 $feedClasses = $this->getConfig()->get( 'FeedClasses' );
407 if ( !isset( $feedClasses[$type] ) ) {
408 $this->getOutput()->addWikiMsg( 'feed-invalid' );
409
410 return;
411 }
412
413 $feed = new $feedClasses[$type](
414 $this->feedTitle(),
415 $this->msg( 'tagline' )->text(),
416 $this->getPageTitle()->getFullURL()
417 );
418
419 $pager = new NewPagesPager( $this, $this->opts );
420 $limit = $this->opts->getValue( 'limit' );
421 $pager->mLimit = min( $limit, $this->getConfig()->get( 'FeedLimit' ) );
422
423 $feed->outHeader();
424 if ( $pager->getNumRows() > 0 ) {
425 foreach ( $pager->mResult as $row ) {
426 $feed->outItem( $this->feedItem( $row ) );
427 }
428 }
429 $feed->outFooter();
430 }
431
432 protected function feedTitle() {
433 $desc = $this->getDescription();
434 $code = $this->getConfig()->get( 'LanguageCode' );
435 $sitename = $this->getConfig()->get( 'Sitename' );
436
437 return "$sitename - $desc [$code]";
438 }
439
440 protected function feedItem( $row ) {
441 $title = Title::makeTitle( intval( $row->rc_namespace ), $row->rc_title );
442 if ( $title ) {
443 $date = $row->rc_timestamp;
444 $comments = $title->getTalkPage()->getFullURL();
445
446 return new FeedItem(
447 $title->getPrefixedText(),
448 $this->feedItemDesc( $row ),
449 $title->getFullURL(),
450 $date,
451 $this->feedItemAuthor( $row ),
452 $comments
453 );
454 } else {
455 return null;
456 }
457 }
458
459 protected function feedItemAuthor( $row ) {
460 return isset( $row->rc_user_text ) ? $row->rc_user_text : '';
461 }
462
463 protected function feedItemDesc( $row ) {
464 $revision = Revision::newFromId( $row->rev_id );
465 if ( $revision ) {
466 //XXX: include content model/type in feed item?
467 return '<p>' . htmlspecialchars( $revision->getUserText() ) .
468 $this->msg( 'colon-separator' )->inContentLanguage()->escaped() .
469 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
470 "</p>\n<hr />\n<div>" .
471 nl2br( htmlspecialchars( $revision->getContent()->serialize() ) ) . "</div>";
472 }
473
474 return '';
475 }
476
477 protected function getGroupName() {
478 return 'changes';
479 }
480 }
481
482 /**
483 * @ingroup SpecialPage Pager
484 */
485 class NewPagesPager extends ReverseChronologicalPager {
486 // Stored opts
487 protected $opts;
488
489 /**
490 * @var HtmlForm
491 */
492 protected $mForm;
493
494 function __construct( $form, FormOptions $opts ) {
495 parent::__construct( $form->getContext() );
496 $this->mForm = $form;
497 $this->opts = $opts;
498 }
499
500 function getQueryInfo() {
501 $conds = array();
502 $conds['rc_new'] = 1;
503
504 $namespace = $this->opts->getValue( 'namespace' );
505 $namespace = ( $namespace === 'all' ) ? false : intval( $namespace );
506
507 $username = $this->opts->getValue( 'username' );
508 $user = Title::makeTitleSafe( NS_USER, $username );
509
510 $rcIndexes = array();
511
512 if ( $namespace !== false ) {
513 if ( $this->opts->getValue( 'invert' ) ) {
514 $conds[] = 'rc_namespace != ' . $this->mDb->addQuotes( $namespace );
515 } else {
516 $conds['rc_namespace'] = $namespace;
517 }
518 }
519
520 if ( $user ) {
521 $conds['rc_user_text'] = $user->getText();
522 $rcIndexes = 'rc_user_text';
523 } elseif ( User::groupHasPermission( '*', 'createpage' ) &&
524 $this->opts->getValue( 'hideliu' )
525 ) {
526 # If anons cannot make new pages, don't "exclude logged in users"!
527 $conds['rc_user'] = 0;
528 }
529
530 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
531 if ( $this->opts->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
532 $conds['rc_patrolled'] = 0;
533 }
534
535 if ( $this->opts->getValue( 'hidebots' ) ) {
536 $conds['rc_bot'] = 0;
537 }
538
539 if ( $this->opts->getValue( 'hideredirs' ) ) {
540 $conds['page_is_redirect'] = 0;
541 }
542
543 // Allow changes to the New Pages query
544 $tables = array( 'recentchanges', 'page' );
545 $fields = array(
546 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
547 'rc_comment', 'rc_timestamp', 'rc_patrolled', 'rc_id', 'rc_deleted',
548 'length' => 'page_len', 'rev_id' => 'page_latest', 'rc_this_oldid',
549 'page_namespace', 'page_title'
550 );
551 $join_conds = array( 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ) );
552
553 Hooks::run( 'SpecialNewpagesConditions',
554 array( &$this, $this->opts, &$conds, &$tables, &$fields, &$join_conds ) );
555
556 $options = array();
557
558 if ( $rcIndexes ) {
559 $options = array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) );
560 }
561
562 $info = array(
563 'tables' => $tables,
564 'fields' => $fields,
565 'conds' => $conds,
566 'options' => $options,
567 'join_conds' => $join_conds
568 );
569
570 // Modify query for tags
571 ChangeTags::modifyDisplayQuery(
572 $info['tables'],
573 $info['fields'],
574 $info['conds'],
575 $info['join_conds'],
576 $info['options'],
577 $this->opts['tagfilter']
578 );
579
580 return $info;
581 }
582
583 function getIndexField() {
584 return 'rc_timestamp';
585 }
586
587 function formatRow( $row ) {
588 return $this->mForm->formatRow( $row );
589 }
590
591 function getStartBody() {
592 # Do a batch existence check on pages
593 $linkBatch = new LinkBatch();
594 foreach ( $this->mResult as $row ) {
595 $linkBatch->add( NS_USER, $row->rc_user_text );
596 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
597 $linkBatch->add( $row->page_namespace, $row->page_title );
598 }
599 $linkBatch->execute();
600
601 return '<ul>';
602 }
603
604 function getEndBody() {
605 return '</ul>';
606 }
607 }