Merge "Enforce array type hinting in OutputPage.php"
[lhc/web/wiklou.git] / includes / specials / SpecialWatchlist.php
1 <?php
2 /**
3 * Implements Special:Watchlist
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 lists last changes made to the wiki,
26 * limited to user-defined list of titles.
27 *
28 * @ingroup SpecialPage
29 */
30 class SpecialWatchlist extends ChangesListSpecialPage {
31 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
32 parent::__construct( $page, $restriction );
33 }
34
35 /**
36 * Main execution point
37 *
38 * @param string $subpage
39 */
40 function execute( $subpage ) {
41 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
42
43 // Anons don't get a watchlist
44 $this->requireLogin( 'watchlistanontext' );
45
46 $output = $this->getOutput();
47 $request = $this->getRequest();
48
49 $mode = SpecialEditWatchlist::getMode( $request, $subpage );
50 if ( $mode !== false ) {
51 if ( $mode === SpecialEditWatchlist::EDIT_RAW ) {
52 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'raw' );
53 } elseif ( $mode === SpecialEditWatchlist::EDIT_CLEAR ) {
54 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'clear' );
55 } else {
56 $title = SpecialPage::getTitleFor( 'EditWatchlist' );
57 }
58
59 $output->redirect( $title->getLocalURL() );
60
61 return;
62 }
63
64 $this->checkPermissions();
65
66 $user = $this->getUser();
67 $opts = $this->getOptions();
68
69 if ( ( $wgEnotifWatchlist || $wgShowUpdatedMarker )
70 && $request->getVal( 'reset' )
71 && $request->wasPosted()
72 ) {
73 $user->clearAllNotifications();
74 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
75
76 return;
77 }
78
79 parent::execute( $subpage );
80 }
81
82 /**
83 * Return an array of subpages beginning with $search that this special page will accept.
84 *
85 * @param string $search Prefix to search for
86 * @param int $limit Maximum number of results to return
87 * @return string[] Matching subpages
88 */
89 public function prefixSearchSubpages( $search, $limit = 10 ) {
90 // See also SpecialEditWatchlist::prefixSearchSubpages
91 return self::prefixSearchArray(
92 $search,
93 $limit,
94 array(
95 'clear',
96 'edit',
97 'raw',
98 )
99 );
100 }
101
102 /**
103 * Get a FormOptions object containing the default options
104 *
105 * @return FormOptions
106 */
107 public function getDefaultOptions() {
108 $opts = parent::getDefaultOptions();
109 $user = $this->getUser();
110
111 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions::FLOAT );
112
113 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
114 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
115 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
116 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
117 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
118 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
119
120 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
121
122 return $opts;
123 }
124
125 /**
126 * Get custom show/hide filters
127 *
128 * @return array Map of filter URL param names to properties (msg/default)
129 */
130 protected function getCustomFilters() {
131 if ( $this->customFilters === null ) {
132 $this->customFilters = parent::getCustomFilters();
133 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters ), '1.23' );
134 }
135
136 return $this->customFilters;
137 }
138
139 /**
140 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
141 *
142 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
143 * to the current ones.
144 *
145 * @param FormOptions $opts
146 * @return FormOptions
147 */
148 protected function fetchOptionsFromRequest( $opts ) {
149 static $compatibilityMap = array(
150 'hideMinor' => 'hideminor',
151 'hideBots' => 'hidebots',
152 'hideAnons' => 'hideanons',
153 'hideLiu' => 'hideliu',
154 'hidePatrolled' => 'hidepatrolled',
155 'hideOwn' => 'hidemyself',
156 );
157
158 $params = $this->getRequest()->getValues();
159 foreach ( $compatibilityMap as $from => $to ) {
160 if ( isset( $params[$from] ) ) {
161 $params[$to] = $params[$from];
162 unset( $params[$from] );
163 }
164 }
165
166 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
167 // methods defined on WebRequest and removing this dependency would cause some code duplication.
168 $request = new DerivativeRequest( $this->getRequest(), $params );
169 $opts->fetchValuesFromRequest( $request );
170
171 return $opts;
172 }
173
174 /**
175 * Return an array of conditions depending of options set in $opts
176 *
177 * @param FormOptions $opts
178 * @return array
179 */
180 public function buildMainQueryConds( FormOptions $opts ) {
181 $dbr = $this->getDB();
182 $conds = parent::buildMainQueryConds( $opts );
183
184 // Calculate cutoff
185 if ( $opts['days'] > 0 ) {
186 $conds[] = 'rc_timestamp > ' .
187 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
188 }
189
190 return $conds;
191 }
192
193 /**
194 * Process the query
195 *
196 * @param array $conds
197 * @param FormOptions $opts
198 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
199 */
200 public function doMainQuery( $conds, $opts ) {
201 global $wgShowUpdatedMarker;
202
203 $dbr = $this->getDB();
204 $user = $this->getUser();
205
206 # Toggle watchlist content (all recent edits or just the latest)
207 if ( $opts['extended'] ) {
208 $limitWatchlist = $user->getIntOption( 'wllimit' );
209 $usePage = false;
210 } else {
211 # Top log Ids for a page are not stored
212 $nonRevisionTypes = array( RC_LOG );
213 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
214 if ( $nonRevisionTypes ) {
215 $conds[] = $dbr->makeList(
216 array(
217 'rc_this_oldid=page_latest',
218 'rc_type' => $nonRevisionTypes,
219 ),
220 LIST_OR
221 );
222 }
223 $limitWatchlist = 0;
224 $usePage = true;
225 }
226
227 $tables = array( 'recentchanges', 'watchlist' );
228 $fields = RecentChange::selectFields();
229 $query_options = array( 'ORDER BY' => 'rc_timestamp DESC' );
230 $join_conds = array(
231 'watchlist' => array(
232 'INNER JOIN',
233 array(
234 'wl_user' => $user->getId(),
235 'wl_namespace=rc_namespace',
236 'wl_title=rc_title'
237 ),
238 ),
239 );
240
241 if ( $wgShowUpdatedMarker ) {
242 $fields[] = 'wl_notificationtimestamp';
243 }
244 if ( $limitWatchlist ) {
245 $query_options['LIMIT'] = $limitWatchlist;
246 }
247
248 $rollbacker = $user->isAllowed( 'rollback' );
249 if ( $usePage || $rollbacker ) {
250 $tables[] = 'page';
251 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
252 if ( $rollbacker ) {
253 $fields[] = 'page_latest';
254 }
255 }
256
257 // Log entries with DELETED_ACTION must not show up unless the user has
258 // the necessary rights.
259 if ( !$user->isAllowed( 'deletedhistory' ) ) {
260 $bitmask = LogPage::DELETED_ACTION;
261 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
262 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
263 } else {
264 $bitmask = 0;
265 }
266 if ( $bitmask ) {
267 $conds[] = $dbr->makeList( array(
268 'rc_type != ' . RC_LOG,
269 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
270 ), LIST_OR );
271 }
272
273 ChangeTags::modifyDisplayQuery(
274 $tables,
275 $fields,
276 $conds,
277 $join_conds,
278 $query_options,
279 ''
280 );
281
282 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
283
284 return $dbr->select(
285 $tables,
286 $fields,
287 $conds,
288 __METHOD__,
289 $query_options,
290 $join_conds
291 );
292 }
293
294 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts ) {
295 return parent::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
296 && wfRunHooks(
297 'SpecialWatchlistQuery',
298 array( &$conds, &$tables, &$join_conds, &$fields, $opts ),
299 '1.23'
300 );
301 }
302
303 /**
304 * Return a DatabaseBase object for reading
305 *
306 * @return DatabaseBase
307 */
308 protected function getDB() {
309 return wfGetDB( DB_SLAVE, 'watchlist' );
310 }
311
312 /**
313 * Output feed links.
314 */
315 public function outputFeedLinks() {
316 $user = $this->getUser();
317 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
318 if ( $wlToken ) {
319 $this->addFeedLinks( array(
320 'action' => 'feedwatchlist',
321 'allrev' => 1,
322 'wlowner' => $user->getName(),
323 'wltoken' => $wlToken,
324 ) );
325 }
326 }
327
328 /**
329 * Build and output the actual changes list.
330 *
331 * @param ResultWrapper $rows Database rows
332 * @param FormOptions $opts
333 */
334 public function outputChangesList( $rows, $opts ) {
335 global $wgShowUpdatedMarker, $wgRCShowWatchingUsers;
336
337 $dbr = $this->getDB();
338 $user = $this->getUser();
339 $output = $this->getOutput();
340
341 # Show a message about slave lag, if applicable
342 $lag = wfGetLB()->safeGetLag( $dbr );
343 if ( $lag > 0 ) {
344 $output->showLagWarning( $lag );
345 }
346
347 # If no rows to display, show message before try to render the list
348 if ( $rows->numRows() == 0 ) {
349 $output->wrapWikiMsg(
350 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
351 );
352 return;
353 }
354
355 $dbr->dataSeek( $rows, 0 );
356
357 $list = ChangesList::newFromContext( $this->getContext() );
358 $list->setWatchlistDivs();
359 $list->initChangesListRows( $rows );
360 $dbr->dataSeek( $rows, 0 );
361
362 $s = $list->beginRecentChangesList();
363 $counter = 1;
364 foreach ( $rows as $obj ) {
365 # Make RC entry
366 $rc = RecentChange::newFromRow( $obj );
367 $rc->counter = $counter++;
368
369 if ( $wgShowUpdatedMarker ) {
370 $updated = $obj->wl_notificationtimestamp;
371 } else {
372 $updated = false;
373 }
374
375 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
376 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
377 'COUNT(*)',
378 array(
379 'wl_namespace' => $obj->rc_namespace,
380 'wl_title' => $obj->rc_title,
381 ),
382 __METHOD__ );
383 } else {
384 $rc->numberofWatchingusers = 0;
385 }
386
387 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
388 if ( $changeLine !== false ) {
389 $s .= $changeLine;
390 }
391 }
392 $s .= $list->endRecentChangesList();
393
394 $output->addHTML( $s );
395 }
396
397 /**
398 * Set the text to be displayed above the changes
399 *
400 * @param FormOptions $opts
401 * @param int $numRows Number of rows in the result to show after this header
402 */
403 public function doHeader( $opts, $numRows ) {
404 $user = $this->getUser();
405
406 $this->getOutput()->addSubtitle(
407 $this->msg( 'watchlistfor2', $user->getName() )
408 ->rawParams( SpecialEditWatchlist::buildTools( null ) )
409 );
410
411 $this->setTopText( $opts );
412
413 $lang = $this->getLanguage();
414 $wlInfo = '';
415 if ( $opts['days'] > 0 ) {
416 $timestamp = wfTimestampNow();
417 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $opts['days'] * 24 ) )->params(
418 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
419 )->parse() . "<br />\n";
420 }
421
422 $nondefaults = $opts->getChangedValues();
423 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
424
425 # Spit out some control panel links
426 $filters = array(
427 'hideminor' => 'rcshowhideminor',
428 'hidebots' => 'rcshowhidebots',
429 'hideanons' => 'rcshowhideanons',
430 'hideliu' => 'rcshowhideliu',
431 'hidemyself' => 'rcshowhidemine',
432 'hidepatrolled' => 'rcshowhidepatr'
433 );
434 foreach ( $this->getCustomFilters() as $key => $params ) {
435 $filters[$key] = $params['msg'];
436 }
437 // Disable some if needed
438 if ( !$user->useNPPatrol() ) {
439 unset( $filters['hidepatrolled'] );
440 }
441
442 $links = array();
443 foreach ( $filters as $name => $msg ) {
444 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
445 }
446
447 $hiddenFields = $nondefaults;
448 unset( $hiddenFields['namespace'] );
449 unset( $hiddenFields['invert'] );
450 unset( $hiddenFields['associated'] );
451
452 # Create output
453 $form = '';
454
455 # Namespace filter and put the whole form together.
456 $form .= $wlInfo;
457 $form .= $cutofflinks;
458 $form .= $lang->pipeList( $links ) . "\n";
459 $form .= "<hr />\n<p>";
460 $form .= Html::namespaceSelector(
461 array(
462 'selected' => $opts['namespace'],
463 'all' => '',
464 'label' => $this->msg( 'namespace' )->text()
465 ), array(
466 'name' => 'namespace',
467 'id' => 'namespace',
468 'class' => 'namespaceselector',
469 )
470 ) . '&#160;';
471 $form .= Xml::checkLabel(
472 $this->msg( 'invert' )->text(),
473 'invert',
474 'nsinvert',
475 $opts['invert'],
476 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
477 ) . '&#160;';
478 $form .= Xml::checkLabel(
479 $this->msg( 'namespace_association' )->text(),
480 'associated',
481 'nsassociated',
482 $opts['associated'],
483 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
484 ) . '&#160;';
485 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
486 foreach ( $hiddenFields as $key => $value ) {
487 $form .= Html::hidden( $key, $value ) . "\n";
488 }
489 $form .= Xml::closeElement( 'fieldset' ) . "\n";
490 $form .= Xml::closeElement( 'form' ) . "\n";
491 $this->getOutput()->addHTML( $form );
492
493 $this->setBottomText( $opts );
494 }
495
496 function setTopText( FormOptions $opts ) {
497 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
498
499 $nondefaults = $opts->getChangedValues();
500 $form = "";
501 $user = $this->getUser();
502
503 $dbr = $this->getDB();
504 $numItems = $this->countItems( $dbr );
505
506 // Show watchlist header
507 $form .= "<p>";
508 if ( $numItems == 0 ) {
509 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
510 } else {
511 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
512 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
513 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
514 }
515 if ( $wgShowUpdatedMarker ) {
516 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
517 }
518 }
519 $form .= "</p>";
520
521 if ( $numItems > 0 && $wgShowUpdatedMarker ) {
522 $form .= Xml::openElement( 'form', array( 'method' => 'post',
523 'action' => $this->getPageTitle()->getLocalURL(),
524 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
525 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
526 Html::hidden( 'reset', 'all' ) . "\n";
527 foreach ( $nondefaults as $key => $value ) {
528 $form .= Html::hidden( $key, $value ) . "\n";
529 }
530 $form .= Xml::closeElement( 'form' ) . "\n";
531 }
532
533 $form .= Xml::openElement( 'form', array(
534 'method' => 'post',
535 'action' => $this->getPageTitle()->getLocalURL(),
536 'id' => 'mw-watchlist-form'
537 ) );
538 $form .= Xml::fieldset(
539 $this->msg( 'watchlist-options' )->text(),
540 false,
541 array( 'id' => 'mw-watchlist-options' )
542 );
543
544 $form .= SpecialRecentChanges::makeLegend( $this->getContext() );
545
546 $this->getOutput()->addHTML( $form );
547 }
548
549 protected function showHideLink( $options, $message, $name, $value ) {
550 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
551 $options[$name] = 1 - (int)$value;
552
553 return $this->msg( $message )
554 ->rawParams( Linker::linkKnown( $this->getPageTitle(), $label, array(), $options ) )
555 ->escaped();
556 }
557
558 protected function hoursLink( $h, $options = array() ) {
559 $options['days'] = ( $h / 24.0 );
560
561 return Linker::linkKnown(
562 $this->getPageTitle(),
563 $this->getLanguage()->formatNum( $h ),
564 array(),
565 $options
566 );
567 }
568
569 protected function daysLink( $d, $options = array() ) {
570 $options['days'] = $d;
571 $message = $d ? $this->getLanguage()->formatNum( $d )
572 : $this->msg( 'watchlistall2' )->escaped();
573
574 return Linker::linkKnown(
575 $this->getPageTitle(),
576 $message,
577 array(),
578 $options
579 );
580 }
581
582 /**
583 * Returns html
584 *
585 * @param int $days This gets overwritten, so is not used
586 * @param array $options Query parameters for URL
587 * @return string
588 */
589 protected function cutoffLinks( $days, $options = array() ) {
590 $hours = array( 1, 2, 6, 12 );
591 $days = array( 1, 3, 7 );
592 $i = 0;
593 foreach ( $hours as $h ) {
594 $hours[$i++] = $this->hoursLink( $h, $options );
595 }
596 $i = 0;
597 foreach ( $days as $d ) {
598 $days[$i++] = $this->daysLink( $d, $options );
599 }
600
601 return $this->msg( 'wlshowlast' )->rawParams(
602 $this->getLanguage()->pipeList( $hours ),
603 $this->getLanguage()->pipeList( $days ),
604 $this->daysLink( 0, $options ) )->parse();
605 }
606
607 /**
608 * Count the number of items on a user's watchlist
609 *
610 * @param DatabaseBase $dbr A database connection
611 * @return int
612 */
613 protected function countItems( $dbr ) {
614 # Fetch the raw count
615 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
616 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
617 $row = $dbr->fetchObject( $rows );
618 $count = $row->count;
619
620 return floor( $count / 2 );
621 }
622 }