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