Merge "Http::getProxy() method to get proxy configuration"
[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 = [];
59 Hooks::run( 'SpecialNewPagesFilters', [ $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 = [];
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 = [ $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() ];
166
167 // Option value -> message mapping
168 $filters = [
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 = [];
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], [],
194 [ $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 = [];
219 foreach ( $this->opts->getUnconsumedValues() as $key => $value ) {
220 $hidden[] = Html::hidden( $key, $value );
221 }
222 $hidden = implode( "\n", $hidden );
223
224 $form = [
225 'namespace' => [
226 'type' => 'namespaceselect',
227 'name' => 'namespace',
228 'label-message' => 'namespace',
229 'default' => $namespace,
230 ],
231 'nsinvert' => [
232 'type' => 'check',
233 'name' => 'invert',
234 'label-message' => 'invert',
235 'default' => $nsinvert,
236 'tooltip' => 'invert',
237 ],
238 'tagFilter' => [
239 'type' => 'tagfilter',
240 'name' => 'tagfilter',
241 'label-raw' => $this->msg( 'tag-filter' )->parse(),
242 'default' => $tagFilterVal,
243 ],
244 'username' => [
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( 'newpages-submit' )->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 = [
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 = [];
303
304 $lang = $this->getLanguage();
305 $dm = $lang->getDirMark();
306
307 $spanTime = Html::element( 'span', [ 'class' => 'mw-newpages-time' ],
308 $lang->userTimeAndDate( $result->rc_timestamp, $this->getUser() )
309 );
310 $time = Linker::linkKnown(
311 $title,
312 $spanTime,
313 [],
314 [ 'oldid' => $result->rc_this_oldid ],
315 []
316 );
317
318 $query = $title->isRedirect() ? [ 'redirect' => 'no' ] : [];
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 [ 'class' => 'mw-newpages-pagename' ],
326 $query,
327 [ 'known' ]
328 );
329 $histLink = Linker::linkKnown(
330 $title,
331 $this->msg( 'hist' )->escaped(),
332 [],
333 [ 'action' => 'history' ]
334 );
335 $hist = Html::rawElement( 'span', [ 'class' => 'mw-newpages-history' ],
336 $this->msg( 'parentheses' )->rawParams( $histLink )->escaped() );
337
338 $length = Html::rawElement(
339 'span',
340 [ '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 $this->getContext()
364 );
365 $classes = array_merge( $classes, $newClasses );
366 } else {
367 $tagDisplay = '';
368 }
369
370 $css = count( $classes ) ? ' class="' . implode( ' ', $classes ) . '"' : '';
371
372 # Display the old title if the namespace/title has been changed
373 $oldTitleText = '';
374 $oldTitle = Title::makeTitle( $result->rc_namespace, $result->rc_title );
375
376 if ( !$title->equals( $oldTitle ) ) {
377 $oldTitleText = $oldTitle->getPrefixedText();
378 $oldTitleText = $this->msg( 'rc-old-title' )->params( $oldTitleText )->escaped();
379 }
380
381 return "<li{$css}>{$time} {$dm}{$plink} {$hist} {$dm}{$length} "
382 . "{$dm}{$ulink} {$comment} {$tagDisplay} {$oldTitleText}</li>\n";
383 }
384
385 /**
386 * Should a specific result row provide "patrollable" links?
387 *
388 * @param object $result Result row
389 * @return bool
390 */
391 protected function patrollable( $result ) {
392 return ( $this->getUser()->useNPPatrol() && !$result->rc_patrolled );
393 }
394
395 /**
396 * Output a subscription feed listing recent edits to this page.
397 *
398 * @param string $type
399 */
400 protected function feed( $type ) {
401 if ( !$this->getConfig()->get( 'Feed' ) ) {
402 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
403
404 return;
405 }
406
407 $feedClasses = $this->getConfig()->get( 'FeedClasses' );
408 if ( !isset( $feedClasses[$type] ) ) {
409 $this->getOutput()->addWikiMsg( 'feed-invalid' );
410
411 return;
412 }
413
414 $feed = new $feedClasses[$type](
415 $this->feedTitle(),
416 $this->msg( 'tagline' )->text(),
417 $this->getPageTitle()->getFullURL()
418 );
419
420 $pager = new NewPagesPager( $this, $this->opts );
421 $limit = $this->opts->getValue( 'limit' );
422 $pager->mLimit = min( $limit, $this->getConfig()->get( 'FeedLimit' ) );
423
424 $feed->outHeader();
425 if ( $pager->getNumRows() > 0 ) {
426 foreach ( $pager->mResult as $row ) {
427 $feed->outItem( $this->feedItem( $row ) );
428 }
429 }
430 $feed->outFooter();
431 }
432
433 protected function feedTitle() {
434 $desc = $this->getDescription();
435 $code = $this->getConfig()->get( 'LanguageCode' );
436 $sitename = $this->getConfig()->get( 'Sitename' );
437
438 return "$sitename - $desc [$code]";
439 }
440
441 protected function feedItem( $row ) {
442 $title = Title::makeTitle( intval( $row->rc_namespace ), $row->rc_title );
443 if ( $title ) {
444 $date = $row->rc_timestamp;
445 $comments = $title->getTalkPage()->getFullURL();
446
447 return new FeedItem(
448 $title->getPrefixedText(),
449 $this->feedItemDesc( $row ),
450 $title->getFullURL(),
451 $date,
452 $this->feedItemAuthor( $row ),
453 $comments
454 );
455 } else {
456 return null;
457 }
458 }
459
460 protected function feedItemAuthor( $row ) {
461 return isset( $row->rc_user_text ) ? $row->rc_user_text : '';
462 }
463
464 protected function feedItemDesc( $row ) {
465 $revision = Revision::newFromId( $row->rev_id );
466 if ( $revision ) {
467 // XXX: include content model/type in feed item?
468 return '<p>' . htmlspecialchars( $revision->getUserText() ) .
469 $this->msg( 'colon-separator' )->inContentLanguage()->escaped() .
470 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
471 "</p>\n<hr />\n<div>" .
472 nl2br( htmlspecialchars( $revision->getContent()->serialize() ) ) . "</div>";
473 }
474
475 return '';
476 }
477
478 protected function getGroupName() {
479 return 'changes';
480 }
481 }
482
483 /**
484 * @ingroup SpecialPage Pager
485 */
486 class NewPagesPager extends ReverseChronologicalPager {
487 // Stored opts
488 protected $opts;
489
490 /**
491 * @var HtmlForm
492 */
493 protected $mForm;
494
495 function __construct( $form, FormOptions $opts ) {
496 parent::__construct( $form->getContext() );
497 $this->mForm = $form;
498 $this->opts = $opts;
499 }
500
501 function getQueryInfo() {
502 $conds = [];
503 $conds['rc_new'] = 1;
504
505 $namespace = $this->opts->getValue( 'namespace' );
506 $namespace = ( $namespace === 'all' ) ? false : intval( $namespace );
507
508 $username = $this->opts->getValue( 'username' );
509 $user = Title::makeTitleSafe( NS_USER, $username );
510
511 $rcIndexes = [];
512
513 if ( $namespace !== false ) {
514 if ( $this->opts->getValue( 'invert' ) ) {
515 $conds[] = 'rc_namespace != ' . $this->mDb->addQuotes( $namespace );
516 } else {
517 $conds['rc_namespace'] = $namespace;
518 }
519 }
520
521 if ( $user ) {
522 $conds['rc_user_text'] = $user->getText();
523 $rcIndexes = 'rc_user_text';
524 } elseif ( User::groupHasPermission( '*', 'createpage' ) &&
525 $this->opts->getValue( 'hideliu' )
526 ) {
527 # If anons cannot make new pages, don't "exclude logged in users"!
528 $conds['rc_user'] = 0;
529 }
530
531 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
532 if ( $this->opts->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
533 $conds['rc_patrolled'] = 0;
534 }
535
536 if ( $this->opts->getValue( 'hidebots' ) ) {
537 $conds['rc_bot'] = 0;
538 }
539
540 if ( $this->opts->getValue( 'hideredirs' ) ) {
541 $conds['page_is_redirect'] = 0;
542 }
543
544 // Allow changes to the New Pages query
545 $tables = [ 'recentchanges', 'page' ];
546 $fields = [
547 'rc_namespace', 'rc_title', 'rc_cur_id', 'rc_user', 'rc_user_text',
548 'rc_comment', 'rc_timestamp', 'rc_patrolled', 'rc_id', 'rc_deleted',
549 'length' => 'page_len', 'rev_id' => 'page_latest', 'rc_this_oldid',
550 'page_namespace', 'page_title'
551 ];
552 $join_conds = [ 'page' => [ 'INNER JOIN', 'page_id=rc_cur_id' ] ];
553
554 Hooks::run( 'SpecialNewpagesConditions',
555 [ &$this, $this->opts, &$conds, &$tables, &$fields, &$join_conds ] );
556
557 $options = [];
558
559 if ( $rcIndexes ) {
560 $options = [ 'USE INDEX' => [ 'recentchanges' => $rcIndexes ] ];
561 }
562
563 $info = [
564 'tables' => $tables,
565 'fields' => $fields,
566 'conds' => $conds,
567 'options' => $options,
568 'join_conds' => $join_conds
569 ];
570
571 // Modify query for tags
572 ChangeTags::modifyDisplayQuery(
573 $info['tables'],
574 $info['fields'],
575 $info['conds'],
576 $info['join_conds'],
577 $info['options'],
578 $this->opts['tagfilter']
579 );
580
581 return $info;
582 }
583
584 function getIndexField() {
585 return 'rc_timestamp';
586 }
587
588 function formatRow( $row ) {
589 return $this->mForm->formatRow( $row );
590 }
591
592 function getStartBody() {
593 # Do a batch existence check on pages
594 $linkBatch = new LinkBatch();
595 foreach ( $this->mResult as $row ) {
596 $linkBatch->add( NS_USER, $row->rc_user_text );
597 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
598 $linkBatch->add( $row->page_namespace, $row->page_title );
599 }
600 $linkBatch->execute();
601
602 return '<ul>';
603 }
604
605 function getEndBody() {
606 return '</ul>';
607 }
608 }