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