Merge "API: Fix documentation for ApiBase::require*OneParameter"
[mediawiki.git] / includes / specials / SpecialWatchlist.php
blob490e81f00be33a6e43ed0e1b61fc82e16d39f7e7
1 <?php
2 /**
3 * Implements Special:Watchlist
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * A special page that lists last changes made to the wiki,
26 * limited to user-defined list of titles.
28 * @ingroup SpecialPage
30 class SpecialWatchlist extends ChangesListSpecialPage {
31 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
32 parent::__construct( $page, $restriction );
35 /**
36 * Main execution point
38 * @param string $subpage
40 function execute( $subpage ) {
41 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
43 // Anons don't get a watchlist
44 $this->requireLogin( 'watchlistanontext' );
46 $output = $this->getOutput();
47 $request = $this->getRequest();
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' );
59 $output->redirect( $title->getLocalURL() );
61 return;
64 $this->checkPermissions();
66 $user = $this->getUser();
67 $opts = $this->getOptions();
69 if ( ( $wgEnotifWatchlist || $wgShowUpdatedMarker )
70 && $request->getVal( 'reset' )
71 && $request->wasPosted()
72 ) {
73 $user->clearAllNotifications();
74 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
76 return;
79 parent::execute( $subpage );
82 /**
83 * Get a FormOptions object containing the default options
85 * @return FormOptions
87 public function getDefaultOptions() {
88 $opts = parent::getDefaultOptions();
89 $user = $this->getUser();
91 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions::FLOAT );
93 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
94 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
95 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
96 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
97 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
98 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
100 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
102 return $opts;
106 * Get custom show/hide filters
108 * @return array Map of filter URL param names to properties (msg/default)
110 protected function getCustomFilters() {
111 if ( $this->customFilters === null ) {
112 $this->customFilters = parent::getCustomFilters();
113 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters ), '1.23' );
116 return $this->customFilters;
120 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
122 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
123 * to the current ones.
125 * @param FormOptions $opts
126 * @return FormOptions
128 protected function fetchOptionsFromRequest( $opts ) {
129 static $compatibilityMap = array(
130 'hideMinor' => 'hideminor',
131 'hideBots' => 'hidebots',
132 'hideAnons' => 'hideanons',
133 'hideLiu' => 'hideliu',
134 'hidePatrolled' => 'hidepatrolled',
135 'hideOwn' => 'hidemyself',
138 $params = $this->getRequest()->getValues();
139 foreach ( $compatibilityMap as $from => $to ) {
140 if ( isset( $params[$from] ) ) {
141 $params[$to] = $params[$from];
142 unset( $params[$from] );
146 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
147 // methods defined on WebRequest and removing this dependency would cause some code duplication.
148 $request = new DerivativeRequest( $this->getRequest(), $params );
149 $opts->fetchValuesFromRequest( $request );
151 return $opts;
155 * Return an array of conditions depending of options set in $opts
157 * @param FormOptions $opts
158 * @return array
160 public function buildMainQueryConds( FormOptions $opts ) {
161 $dbr = $this->getDB();
162 $conds = parent::buildMainQueryConds( $opts );
164 // Calculate cutoff
165 if ( $opts['days'] > 0 ) {
166 $conds[] = 'rc_timestamp > ' .
167 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
170 return $conds;
174 * Process the query
176 * @param array $conds
177 * @param FormOptions $opts
178 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
180 public function doMainQuery( $conds, $opts ) {
181 global $wgShowUpdatedMarker;
183 $dbr = $this->getDB();
184 $user = $this->getUser();
186 # Toggle watchlist content (all recent edits or just the latest)
187 if ( $opts['extended'] ) {
188 $limitWatchlist = $user->getIntOption( 'wllimit' );
189 $usePage = false;
190 } else {
191 # Top log Ids for a page are not stored
192 $nonRevisionTypes = array( RC_LOG );
193 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
194 if ( $nonRevisionTypes ) {
195 $conds[] = $dbr->makeList(
196 array(
197 'rc_this_oldid=page_latest',
198 'rc_type' => $nonRevisionTypes,
200 LIST_OR
203 $limitWatchlist = 0;
204 $usePage = true;
207 $tables = array( 'recentchanges', 'watchlist' );
208 $fields = RecentChange::selectFields();
209 $query_options = array( 'ORDER BY' => 'rc_timestamp DESC' );
210 $join_conds = array(
211 'watchlist' => array(
212 'INNER JOIN',
213 array(
214 'wl_user' => $user->getId(),
215 'wl_namespace=rc_namespace',
216 'wl_title=rc_title'
221 if ( $wgShowUpdatedMarker ) {
222 $fields[] = 'wl_notificationtimestamp';
224 if ( $limitWatchlist ) {
225 $query_options['LIMIT'] = $limitWatchlist;
228 $rollbacker = $user->isAllowed( 'rollback' );
229 if ( $usePage || $rollbacker ) {
230 $tables[] = 'page';
231 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
232 if ( $rollbacker ) {
233 $fields[] = 'page_latest';
237 // Log entries with DELETED_ACTION must not show up unless the user has
238 // the necessary rights.
239 if ( !$user->isAllowed( 'deletedhistory' ) ) {
240 $bitmask = LogPage::DELETED_ACTION;
241 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
242 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
243 } else {
244 $bitmask = 0;
246 if ( $bitmask ) {
247 $conds[] = $dbr->makeList( array(
248 'rc_type != ' . RC_LOG,
249 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
250 ), LIST_OR );
253 ChangeTags::modifyDisplayQuery(
254 $tables,
255 $fields,
256 $conds,
257 $join_conds,
258 $query_options,
262 wfRunHooks( 'SpecialWatchlistQuery',
263 array( &$conds, &$tables, &$join_conds, &$fields, $opts ),
264 '1.23' );
266 return $dbr->select(
267 $tables,
268 $fields,
269 $conds,
270 __METHOD__,
271 $query_options,
272 $join_conds
277 * Return a DatabaseBase object for reading
279 * @return DatabaseBase
281 protected function getDB() {
282 return wfGetDB( DB_SLAVE, 'watchlist' );
286 * Output feed links.
288 public function outputFeedLinks() {
289 $user = $this->getUser();
290 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
291 if ( $wlToken ) {
292 $this->addFeedLinks( array(
293 'action' => 'feedwatchlist',
294 'allrev' => 1,
295 'wlowner' => $user->getName(),
296 'wltoken' => $wlToken,
297 ) );
302 * Build and output the actual changes list.
304 * @param ResultWrapper $rows Database rows
305 * @param FormOptions $opts
307 public function outputChangesList( $rows, $opts ) {
308 global $wgShowUpdatedMarker, $wgRCShowWatchingUsers;
310 $dbr = $this->getDB();
311 $user = $this->getUser();
312 $output = $this->getOutput();
314 # Show a message about slave lag, if applicable
315 $lag = wfGetLB()->safeGetLag( $dbr );
316 if ( $lag > 0 ) {
317 $output->showLagWarning( $lag );
320 $dbr->dataSeek( $rows, 0 );
322 $list = ChangesList::newFromContext( $this->getContext() );
323 $list->setWatchlistDivs();
324 $list->initChangesListRows( $rows );
325 $dbr->dataSeek( $rows, 0 );
327 $s = $list->beginRecentChangesList();
328 $counter = 1;
329 foreach ( $rows as $obj ) {
330 # Make RC entry
331 $rc = RecentChange::newFromRow( $obj );
332 $rc->counter = $counter++;
334 if ( $wgShowUpdatedMarker ) {
335 $updated = $obj->wl_notificationtimestamp;
336 } else {
337 $updated = false;
340 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
341 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
342 'COUNT(*)',
343 array(
344 'wl_namespace' => $obj->rc_namespace,
345 'wl_title' => $obj->rc_title,
347 __METHOD__ );
348 } else {
349 $rc->numberofWatchingusers = 0;
352 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
353 if ( $changeLine !== false ) {
354 $s .= $changeLine;
357 $s .= $list->endRecentChangesList();
359 if ( $rows->numRows() == 0 ) {
360 $output->wrapWikiMsg(
361 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
363 } else {
364 $output->addHTML( $s );
369 * Return the text to be displayed above the changes
371 * @param FormOptions $opts
372 * @return string XHTML
374 public function doHeader( $opts ) {
375 $user = $this->getUser();
377 $this->getOutput()->addSubtitle(
378 $this->msg( 'watchlistfor2', $user->getName() )
379 ->rawParams( SpecialEditWatchlist::buildTools( null ) )
382 $this->setTopText( $opts );
384 $lang = $this->getLanguage();
385 $wlInfo = '';
386 if ( $opts['days'] > 0 ) {
387 $timestamp = wfTimestampNow();
388 $wlInfo = $this->msg( 'wlnote2' )->numParams( round( $opts['days'] * 24 ) )->params(
389 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
390 )->parse() . "<br />\n";
393 $nondefaults = $opts->getChangedValues();
394 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
396 # Spit out some control panel links
397 $filters = array(
398 'hideminor' => 'rcshowhideminor',
399 'hidebots' => 'rcshowhidebots',
400 'hideanons' => 'rcshowhideanons',
401 'hideliu' => 'rcshowhideliu',
402 'hidemyself' => 'rcshowhidemine',
403 'hidepatrolled' => 'rcshowhidepatr'
405 foreach ( $this->getCustomFilters() as $key => $params ) {
406 $filters[$key] = $params['msg'];
408 // Disable some if needed
409 if ( !$user->useNPPatrol() ) {
410 unset( $filters['hidepatrolled'] );
413 $links = array();
414 foreach ( $filters as $name => $msg ) {
415 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
418 $hiddenFields = $nondefaults;
419 unset( $hiddenFields['namespace'] );
420 unset( $hiddenFields['invert'] );
421 unset( $hiddenFields['associated'] );
423 # Create output
424 $form = '';
426 # Namespace filter and put the whole form together.
427 $form .= $wlInfo;
428 $form .= $cutofflinks;
429 $form .= $lang->pipeList( $links ) . "\n";
430 $form .= "<hr />\n<p>";
431 $form .= Html::namespaceSelector(
432 array(
433 'selected' => $opts['namespace'],
434 'all' => '',
435 'label' => $this->msg( 'namespace' )->text()
436 ), array(
437 'name' => 'namespace',
438 'id' => 'namespace',
439 'class' => 'namespaceselector',
441 ) . '&#160;';
442 $form .= Xml::checkLabel(
443 $this->msg( 'invert' )->text(),
444 'invert',
445 'nsinvert',
446 $opts['invert'],
447 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
448 ) . '&#160;';
449 $form .= Xml::checkLabel(
450 $this->msg( 'namespace_association' )->text(),
451 'associated',
452 'nsassociated',
453 $opts['associated'],
454 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
455 ) . '&#160;';
456 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
457 foreach ( $hiddenFields as $key => $value ) {
458 $form .= Html::hidden( $key, $value ) . "\n";
460 $form .= Xml::closeElement( 'fieldset' ) . "\n";
461 $form .= Xml::closeElement( 'form' ) . "\n";
462 $this->getOutput()->addHTML( $form );
464 $this->setBottomText( $opts );
467 function setTopText( FormOptions $opts ) {
468 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
470 $nondefaults = $opts->getChangedValues();
471 $form = "";
472 $user = $this->getUser();
474 $dbr = $this->getDB();
475 $numItems = $this->countItems( $dbr );
477 // Show watchlist header
478 $form .= "<p>";
479 if ( $numItems == 0 ) {
480 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
481 } else {
482 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
483 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
484 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
486 if ( $wgShowUpdatedMarker ) {
487 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
490 $form .= "</p>";
492 if ( $numItems > 0 && $wgShowUpdatedMarker ) {
493 $form .= Xml::openElement( 'form', array( 'method' => 'post',
494 'action' => $this->getPageTitle()->getLocalURL(),
495 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
496 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
497 Html::hidden( 'reset', 'all' ) . "\n";
498 foreach ( $nondefaults as $key => $value ) {
499 $form .= Html::hidden( $key, $value ) . "\n";
501 $form .= Xml::closeElement( 'form' ) . "\n";
504 $form .= Xml::openElement( 'form', array(
505 'method' => 'post',
506 'action' => $this->getPageTitle()->getLocalURL(),
507 'id' => 'mw-watchlist-form'
508 ) );
509 $form .= Xml::fieldset(
510 $this->msg( 'watchlist-options' )->text(),
511 false,
512 array( 'id' => 'mw-watchlist-options' )
515 $form .= SpecialRecentChanges::makeLegend( $this->getContext() );
517 $this->getOutput()->addHTML( $form );
520 protected function showHideLink( $options, $message, $name, $value ) {
521 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
522 $options[$name] = 1 - (int)$value;
524 return $this->msg( $message )
525 ->rawParams( Linker::linkKnown( $this->getPageTitle(), $label, array(), $options ) )
526 ->escaped();
529 protected function hoursLink( $h, $options = array() ) {
530 $options['days'] = ( $h / 24.0 );
532 return Linker::linkKnown(
533 $this->getPageTitle(),
534 $this->getLanguage()->formatNum( $h ),
535 array(),
536 $options
540 protected function daysLink( $d, $options = array() ) {
541 $options['days'] = $d;
542 $message = $d ? $this->getLanguage()->formatNum( $d )
543 : $this->msg( 'watchlistall2' )->escaped();
545 return Linker::linkKnown(
546 $this->getPageTitle(),
547 $message,
548 array(),
549 $options
554 * Returns html
556 * @param int $days This gets overwritten, so is not used
557 * @param array $options Query parameters for URL
558 * @return string
560 protected function cutoffLinks( $days, $options = array() ) {
561 $hours = array( 1, 2, 6, 12 );
562 $days = array( 1, 3, 7 );
563 $i = 0;
564 foreach ( $hours as $h ) {
565 $hours[$i++] = $this->hoursLink( $h, $options );
567 $i = 0;
568 foreach ( $days as $d ) {
569 $days[$i++] = $this->daysLink( $d, $options );
572 return $this->msg( 'wlshowlast' )->rawParams(
573 $this->getLanguage()->pipeList( $hours ),
574 $this->getLanguage()->pipeList( $days ),
575 $this->daysLink( 0, $options ) )->parse();
579 * Count the number of items on a user's watchlist
581 * @param DatabaseBase $dbr A database connection
582 * @return int
584 protected function countItems( $dbr ) {
585 # Fetch the raw count
586 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
587 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
588 $row = $dbr->fetchObject( $rows );
589 $count = $row->count;
591 return floor( $count / 2 );