Merge "Add two hooks to allow for extensions to expose log_search values in the UI"
[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 wfRunHooks( '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 // Consume values
203 $this->opts->consumeValue( 'offset' ); // don't carry offset, DWIW
204 $namespace = $this->opts->consumeValue( 'namespace' );
205 $username = $this->opts->consumeValue( 'username' );
206 $tagFilterVal = $this->opts->consumeValue( 'tagfilter' );
207 $nsinvert = $this->opts->consumeValue( 'invert' );
208
209 // Check username input validity
210 $ut = Title::makeTitleSafe( NS_USER, $username );
211 $userText = $ut ? $ut->getText() : '';
212
213 // Store query values in hidden fields so that form submission doesn't lose them
214 $hidden = array();
215 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
216 $hidden[] = Html::hidden( $key, $value );
217 }
218 $hidden = implode( "\n", $hidden );
219
220 $form = array(
221 'namespace' => array(
222 'type' => 'namespaceselect',
223 'name' => 'namespace',
224 'label-message' => 'namespace',
225 'default' => $namespace,
226 ),
227 'nsinvert' => array(
228 'type' => 'check',
229 'name' => 'nsinvert',
230 'label-message' => 'invert',
231 'default' => $nsinvert,
232 'tooltip' => $this->msg( 'tooltip-invert' )->text(),
233 ),
234 'tagFilter' => array(
235 'type' => 'tagfilter',
236 'name' => 'tagfilter',
237 'label-raw' => wfMessage( 'tag-filter' )->parse(),
238 'default' => $tagFilterVal,
239 ),
240 'username' => array(
241 'type' => 'text',
242 'name' => 'username',
243 'label-message' => 'newpages-username',
244 'default' => $userText,
245 'id' => 'mw-np-username',
246 'size' => 30,
247 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
248 ),
249 );
250
251 $htmlForm = new HTMLForm( $form, $this->getContext() );
252
253 $htmlForm->setSubmitText( $this->msg( 'allpagessubmit' )->text() );
254 $htmlForm->setSubmitProgressive();
255 // The form should be visible on each request (inclusive requests with submitted forms), so
256 // return always false here.
257 $htmlForm->setSubmitCallback(
258 function () {
259 return false;
260 }
261 );
262 $htmlForm->setMethod( 'get' );
263
264 $out->addHtml( Xml::fieldset( $this->msg( 'newpages' )->text() ) );
265
266 $htmlForm->show();
267
268 $out->addHtml(
269 Html::rawElement(
270 'div',
271 null,
272 $this->filterLinks()
273 ) .
274 Xml::closeElement( 'fieldset' )
275 );
276 }
277
278 /**
279 * Format a row, providing the timestamp, links to the page/history,
280 * size, user links, and a comment
281 *
282 * @param object $result Result row
283 * @return string
284 */
285 public function formatRow( $result ) {
286 $title = Title::newFromRow( $result );
287
288 # Revision deletion works on revisions, so we should cast one
289 $row = array(
290 'comment' => $result->rc_comment,
291 'deleted' => $result->rc_deleted,
292 'user_text' => $result->rc_user_text,
293 'user' => $result->rc_user,
294 );
295 $rev = new Revision( $row );
296 $rev->setTitle( $title );
297
298 $classes = array();
299
300 $lang = $this->getLanguage();
301 $dm = $lang->getDirMark();
302
303 $spanTime = Html::element( 'span', array( 'class' => 'mw-newpages-time' ),
304 $lang->userTimeAndDate( $result->rc_timestamp, $this->getUser() )
305 );
306 $time = Linker::linkKnown(
307 $title,
308 $spanTime,
309 array(),
310 array( 'oldid' => $result->rc_this_oldid ),
311 array()
312 );
313
314 $query = array( 'redirect' => 'no' );
315
316 // Linker::linkKnown() uses 'known' and 'noclasses' options.
317 // This breaks the colouration for stubs.
318 $plink = Linker::link(
319 $title,
320 null,
321 array( 'class' => 'mw-newpages-pagename' ),
322 $query,
323 array( 'known' )
324 );
325 $histLink = Linker::linkKnown(
326 $title,
327 $this->msg( 'hist' )->escaped(),
328 array(),
329 array( 'action' => 'history' )
330 );
331 $hist = Html::rawElement( 'span', array( 'class' => 'mw-newpages-history' ),
332 $this->msg( 'parentheses' )->rawParams( $histLink )->escaped() );
333
334 $length = Html::element(
335 'span',
336 array( 'class' => 'mw-newpages-length' ),
337 $this->msg( 'brackets' )->params( $this->msg( 'nbytes' )
338 ->numParams( $result->length )->text()
339 )
340 );
341
342 $ulink = Linker::revUserTools( $rev );
343 $comment = Linker::revComment( $rev );
344
345 if ( $this->patrollable( $result ) ) {
346 $classes[] = 'not-patrolled';
347 }
348
349 # Add a class for zero byte pages
350 if ( $result->length == 0 ) {
351 $classes[] = 'mw-newpages-zero-byte-page';
352 }
353
354 # Tags, if any.
355 if ( isset( $result->ts_tags ) ) {
356 list( $tagDisplay, $newClasses ) = ChangeTags::formatSummaryRow(
357 $result->ts_tags,
358 'newpages'
359 );
360 $classes = array_merge( $classes, $newClasses );
361 } else {
362 $tagDisplay = '';
363 }
364
365 $css = count( $classes ) ? ' class="' . implode( ' ', $classes ) . '"' : '';
366
367 # Display the old title if the namespace/title has been changed
368 $oldTitleText = '';
369 $oldTitle = Title::makeTitle( $result->rc_namespace, $result->rc_title );
370
371 if ( !$title->equals( $oldTitle ) ) {
372 $oldTitleText = $oldTitle->getPrefixedText();
373 $oldTitleText = $this->msg( 'rc-old-title' )->params( $oldTitleText )->escaped();
374 }
375
376 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} "
377 . "{$dm}{$ulink} {$comment} {$tagDisplay} {$oldTitleText}</li>\n";
378 }
379
380 /**
381 * Should a specific result row provide "patrollable" links?
382 *
383 * @param object $result Result row
384 * @return bool
385 */
386 protected function patrollable( $result ) {
387 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled );
388 }
389
390 /**
391 * Output a subscription feed listing recent edits to this page.
392 *
393 * @param string $type
394 */
395 protected function feed( $type ) {
396 if ( !$this->getConfig()->get( 'Feed' ) ) {
397 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
398
399 return;
400 }
401
402 $feedClasses = $this->getConfig()->get( 'FeedClasses' );
403 if ( !isset( $feedClasses[$type] ) ) {
404 $this->getOutput()->addWikiMsg( 'feed-invalid' );
405
406 return;
407 }
408
409 $feed = new $feedClasses[$type](
410 $this->feedTitle(),
411 $this->msg( 'tagline' )->text(),
412 $this->getPageTitle()->getFullURL()
413 );
414
415 $pager = new NewPagesPager( $this, $this->opts );
416 $limit = $this->opts->getValue( 'limit' );
417 $pager->mLimit = min( $limit, $this->getConfig()->get( 'FeedLimit' ) );
418
419 $feed->outHeader();
420 if ( $pager->getNumRows() > 0 ) {
421 foreach ( $pager->mResult as $row ) {
422 $feed->outItem( $this->feedItem( $row ) );
423 }
424 }
425 $feed->outFooter();
426 }
427
428 protected function feedTitle() {
429 $desc = $this->getDescription();
430 $code = $this->getConfig()->get( 'LanguageCode' );
431 $sitename = $this->getConfig()->get( 'Sitename' );
432
433 return "$sitename - $desc [$code]";
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
470 return '';
471 }
472
473 protected function getGroupName() {
474 return 'changes';
475 }
476 }
477
478 /**
479 * @ingroup SpecialPage Pager
480 */
481 class NewPagesPager extends ReverseChronologicalPager {
482 // Stored opts
483 protected $opts;
484
485 /**
486 * @var HtmlForm
487 */
488 protected $mForm;
489
490 function __construct( $form, FormOptions $opts ) {
491 parent::__construct( $form->getContext() );
492 $this->mForm = $form;
493 $this->opts = $opts;
494 }
495
496 function getQueryInfo() {
497 $conds = array();
498 $conds['rc_new'] = 1;
499
500 $namespace = $this->opts->getValue( 'namespace' );
501 $namespace = ( $namespace === 'all' ) ? false : intval( $namespace );
502
503 $username = $this->opts->getValue( 'username' );
504 $user = Title::makeTitleSafe( NS_USER, $username );
505
506 $rcIndexes = array();
507
508 if ( $namespace !== false ) {
509 if ( $this->opts->getValue( 'invert' ) ) {
510 $conds[] = 'rc_namespace != ' . $this->mDb->addQuotes( $namespace );
511 } else {
512 $conds['rc_namespace'] = $namespace;
513 }
514 }
515
516 if ( $user ) {
517 $conds['rc_user_text'] = $user->getText();
518 $rcIndexes = 'rc_user_text';
519 } elseif ( User::groupHasPermission( '*', 'createpage' ) &&
520 $this->opts->getValue( 'hideliu' )
521 ) {
522 # If anons cannot make new pages, don't "exclude logged in users"!
523 $conds['rc_user'] = 0;
524 }
525
526 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
527 if ( $this->opts->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
528 $conds['rc_patrolled'] = 0;
529 }
530
531 if ( $this->opts->getValue( 'hidebots' ) ) {
532 $conds['rc_bot'] = 0;
533 }
534
535 if ( $this->opts->getValue( 'hideredirs' ) ) {
536 $conds['page_is_redirect'] = 0;
537 }
538
539 // Allow changes to the New Pages query
540 $tables = array( 'recentchanges', 'page' );
541 $fields = array(
542 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
543 'rc_comment', 'rc_timestamp', 'rc_patrolled', 'rc_id', 'rc_deleted',
544 'length' => 'page_len', 'rev_id' => 'page_latest', 'rc_this_oldid',
545 'page_namespace', 'page_title'
546 );
547 $join_conds = array( 'page' => array( 'INNER JOIN', 'page_id=rc_cur_id' ) );
548
549 wfRunHooks( 'SpecialNewpagesConditions',
550 array( &$this, $this->opts, &$conds, &$tables, &$fields, &$join_conds ) );
551
552 $options = array();
553
554 if ( $rcIndexes ) {
555 $options = array( 'USE INDEX' => array( 'recentchanges' => $rcIndexes ) );
556 }
557
558 $info = array(
559 'tables' => $tables,
560 'fields' => $fields,
561 'conds' => $conds,
562 'options' => $options,
563 'join_conds' => $join_conds
564 );
565
566 // Modify query for tags
567 ChangeTags::modifyDisplayQuery(
568 $info['tables'],
569 $info['fields'],
570 $info['conds'],
571 $info['join_conds'],
572 $info['options'],
573 $this->opts['tagfilter']
574 );
575
576 return $info;
577 }
578
579 function getIndexField() {
580 return 'rc_timestamp';
581 }
582
583 function formatRow( $row ) {
584 return $this->mForm->formatRow( $row );
585 }
586
587 function getStartBody() {
588 # Do a batch existence check on pages
589 $linkBatch = new LinkBatch();
590 foreach ( $this->mResult as $row ) {
591 $linkBatch->add( NS_USER, $row->rc_user_text );
592 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
593 $linkBatch->add( $row->rc_namespace, $row->rc_title );
594 }
595 $linkBatch->execute();
596
597 return '<ul>';
598 }
599
600 function getEndBody() {
601 return '</ul>';
602 }
603 }