fc4842190c4c8e11d328e47d9445cc4bdd4e5b56
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
21 * @ingroup SpecialPage Watchlist
23 class SpecialWatchlist
extends ChangesListSpecialPage
{
27 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
28 parent
::__construct( $page, $restriction );
33 * @param $par Parameter passed to the page
35 function execute( $par ) {
36 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
38 $user = $this->getUser();
39 $output = $this->getOutput();
41 # Anons don't get a watchlist
42 $this->requireLogin( 'watchlistanontext' );
45 $this->checkPermissions();
47 $request = $this->getRequest();
48 $opts = $this->getOptions();
50 $mode = SpecialEditWatchlist
::getMode( $request, $par );
51 if ( $mode !== false ) {
52 if ( $mode === SpecialEditWatchlist
::EDIT_RAW
) {
53 $title = SpecialPage
::getTitleFor( 'EditWatchlist', 'raw' );
55 $title = SpecialPage
::getTitleFor( 'EditWatchlist' );
58 $output->redirect( $title->getLocalURL() );
62 if ( ( $wgEnotifWatchlist ||
$wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
63 $request->wasPosted() )
65 $user->clearAllNotifications();
66 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
71 $this->outputHeader();
74 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
76 $this->numItems
= $this->countItems( $dbr ); // TODO kill me
77 if ( $this->numItems
== 0 ) {
78 $output->addWikiMsg( 'nowatchlist' );
82 // Fetch results, prepare a batch link existence check query
83 $conds = $this->buildMainQueryConds( $opts );
84 $rows = $this->doMainQuery( $conds, $opts );
85 $this->numRows
= $rows->numRows(); // TODO kill me
86 if ( $rows === false ) {
87 $this->doHeader( $opts );
92 $feedFormat = $this->getRequest()->getVal( 'feed' );
94 $batch = new LinkBatch
;
95 foreach ( $rows as $row ) {
96 $batch->add( NS_USER
, $row->rc_user_text
);
97 $batch->add( NS_USER_TALK
, $row->rc_user_text
);
98 $batch->add( $row->rc_namespace
, $row->rc_title
);
103 list( $changesFeed, $formatter ) = $this->getFeedObject( $feedFormat );
104 /** @var ChangesFeed $changesFeed */
105 $changesFeed->execute( $formatter, $rows, $lastmod, $opts );
107 $this->webOutput( $rows, $opts );
114 * Get a FormOptions object containing the default options
116 * @return FormOptions
118 public function getDefaultOptions() {
119 $opts = parent
::getDefaultOptions();
120 $user = $this->getUser();
122 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions
::FLOAT );
124 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
125 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
126 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
127 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
128 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
129 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
131 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
137 * Get custom show/hide filters
139 * @return array Map of filter URL param names to properties (msg/default)
141 protected function getCustomFilters() {
142 if ( $this->customFilters
=== null ) {
143 $this->customFilters
= array();
144 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters
) );
147 return $this->customFilters
;
151 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
153 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
154 * to the current ones.
156 * @param FormOptions $parameters
157 * @return FormOptions
159 protected function fetchOptionsFromRequest( $opts ) {
160 static $compatibilityMap = array(
161 'hideMinor' => 'hideminor',
162 'hideBots' => 'hidebots',
163 'hideAnons' => 'hideanons',
164 'hideLiu' => 'hideliu',
165 'hidePatrolled' => 'hidepatrolled',
166 'hideOwn' => 'hidemyself',
169 $params = $this->getRequest()->getValues();
170 foreach ( $compatibilityMap as $from => $to ) {
171 if ( isset( $params[$from] ) ) {
172 $params[$to] = $params[$from];
173 unset( $params[$from] );
177 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
178 // methods defined on WebRequest and removing this dependency would cause some code duplication.
179 $request = new DerivativeRequest( $this->getRequest(), $params );
180 $opts->fetchValuesFromRequest( $request );
185 * Return an array of conditions depending of options set in $opts
187 * @param FormOptions $opts
190 public function buildMainQueryConds( FormOptions
$opts ) {
191 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
193 $user = $this->getUser();
196 if ( $opts['days'] > 0 ) {
197 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
201 if ( $opts['hidemyself'] ) {
202 $conds[] = 'rc_user != ' . $user->getId();
204 if ( $opts['hidebots'] ) {
205 $conds[] = 'rc_bot = 0';
207 if ( $opts['hideminor'] ) {
208 $conds[] = 'rc_minor = 0';
210 if ( $opts['hideliu'] ) {
211 $conds[] = 'rc_user = 0';
213 if ( $opts['hideanons'] ) {
214 $conds[] = 'rc_user != 0';
216 if ( $user->useRCPatrol() && $opts['hidepatrolled'] ) {
217 $conds[] = 'rc_patrolled != 1';
220 # Namespace filtering
221 if ( $opts['namespace'] !== '' ) {
222 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
223 $operator = $opts['invert'] ?
'!=' : '=';
224 $boolean = $opts['invert'] ?
'AND' : 'OR';
226 # namespace association (bug 2429)
227 if ( !$opts['associated'] ) {
228 $condition = "rc_namespace $operator $selectedNS";
230 # Also add the associated namespace
231 $associatedNS = $dbr->addQuotes(
232 MWNamespace
::getAssociated( $opts['namespace'] )
234 $condition = "(rc_namespace $operator $selectedNS "
236 . " rc_namespace $operator $associatedNS)";
239 $conds[] = $condition;
248 * @param array $conds
249 * @param FormOptions $opts
250 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
252 public function doMainQuery( $conds, $opts ) {
253 global $wgShowUpdatedMarker;
255 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
256 $user = $this->getUser();
257 # Toggle watchlist content (all recent edits or just the latest)
258 if ( $opts['extended'] ) {
259 $limitWatchlist = $user->getIntOption( 'wllimit' );
262 # Top log Ids for a page are not stored
263 $nonRevisionTypes = array( RC_LOG
);
264 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
265 if ( $nonRevisionTypes ) {
266 $conds[] = $dbr->makeList(
268 'rc_this_oldid=page_latest',
269 'rc_type' => $nonRevisionTypes,
278 $tables = array( 'recentchanges', 'watchlist' );
279 $fields = RecentChange
::selectFields();
281 'watchlist' => array(
284 'wl_user' => $user->getId(),
285 'wl_namespace=rc_namespace',
290 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
291 if ( $wgShowUpdatedMarker ) {
292 $fields[] = 'wl_notificationtimestamp';
294 if ( $limitWatchlist ) {
295 $options['LIMIT'] = $limitWatchlist;
298 $rollbacker = $user->isAllowed( 'rollback' );
299 if ( $usePage ||
$rollbacker ) {
301 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
303 $fields[] = 'page_latest';
307 // Log entries with DELETED_ACTION must not show up unless the user has
308 // the necessary rights.
309 if ( !$user->isAllowed( 'deletedhistory' ) ) {
310 $bitmask = LogPage
::DELETED_ACTION
;
311 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
312 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
317 $conds[] = $dbr->makeList( array(
318 'rc_type != ' . RC_LOG
,
319 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
324 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
325 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $opts ) );
327 return $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
331 * Send output to the OutputPage object, only called if not used feeds
333 * @param array $rows Database rows
334 * @param FormOptions $opts
336 public function webOutput( $rows, $opts ) {
337 global $wgShowUpdatedMarker, $wgRCShowWatchingUsers;
339 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
340 $user = $this->getUser();
342 $dbr->dataSeek( $rows, 0 );
344 $list = ChangesList
::newFromContext( $this->getContext() );
345 $list->setWatchlistDivs();
347 $s = $list->beginRecentChangesList();
349 foreach ( $rows as $obj ) {
351 $rc = RecentChange
::newFromRow( $obj );
352 $rc->counter
= $counter++
;
354 if ( $wgShowUpdatedMarker ) {
355 $updated = $obj->wl_notificationtimestamp
;
360 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
361 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
364 'wl_namespace' => $obj->rc_namespace
,
365 'wl_title' => $obj->rc_title
,
369 $rc->numberofWatchingusers
= 0;
372 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
373 if ( $changeLine !== false ) {
377 $s .= $list->endRecentChangesList();
381 $output = $this->getOutput();
383 $output->addSubtitle(
384 $this->msg( 'watchlistfor2', $user->getName() )
385 ->rawParams( SpecialEditWatchlist
::buildTools( null ) )
388 // Output options box
389 $this->doHeader( $opts );
392 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
394 $this->addFeedLinks( array(
395 'action' => 'feedwatchlist',
397 'wlowner' => $user->getName(),
398 'wltoken' => $wlToken,
402 # Show a message about slave lag, if applicable
403 $lag = wfGetLB()->safeGetLag( $dbr );
405 $output->showLagWarning( $lag );
408 if ( $rows->numRows() == 0 ) {
409 $output->wrapWikiMsg(
410 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
413 $output->addHTML( $s );
418 * Return the text to be displayed above the changes
420 * @param FormOptions $opts
421 * @return string XHTML
423 public function doHeader( $opts ) {
426 $user = $this->getUser();
428 $this->setTopText( $opts );
430 $lang = $this->getLanguage();
432 if ( $opts['days'] > 0 ) {
433 $timestamp = wfTimestampNow();
434 $wlInfo = $this->msg( 'wlnote' )->numParams( $this->numRows
, round( $opts['days'] * 24 ) )->params(
435 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
438 $nondefaults = $opts->getChangedValues();
439 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
441 # Spit out some control panel links
443 'hideminor' => 'rcshowhideminor',
444 'hidebots' => 'rcshowhidebots',
445 'hideanons' => 'rcshowhideanons',
446 'hideliu' => 'rcshowhideliu',
447 'hidemyself' => 'rcshowhidemine',
448 'hidepatrolled' => 'rcshowhidepatr'
450 foreach ( $this->getCustomFilters() as $key => $params ) {
451 $filters[$key] = $params['msg'];
453 // Disable some if needed
454 if ( !$user->useNPPatrol() ) {
455 unset( $filters['hidepatrolled'] );
459 foreach ( $filters as $name => $msg ) {
460 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
463 $hiddenFields = $nondefaults;
464 unset( $hiddenFields['namespace'] );
465 unset( $hiddenFields['invert'] );
466 unset( $hiddenFields['associated'] );
471 # Namespace filter and put the whole form together.
473 $form .= $cutofflinks;
474 $form .= $lang->pipeList( $links ) . "\n";
475 $form .= "<hr />\n<p>";
476 $form .= Html
::namespaceSelector(
478 'selected' => $opts['namespace'],
480 'label' => $this->msg( 'namespace' )->text()
482 'name' => 'namespace',
484 'class' => 'namespaceselector',
487 $form .= Xml
::checkLabel(
488 $this->msg( 'invert' )->text(),
492 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
494 $form .= Xml
::checkLabel(
495 $this->msg( 'namespace_association' )->text(),
499 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
501 $form .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
502 foreach ( $hiddenFields as $key => $value ) {
503 $form .= Html
::hidden( $key, $value ) . "\n";
505 $form .= Xml
::closeElement( 'fieldset' ) . "\n";
506 $form .= Xml
::closeElement( 'form' ) . "\n";
507 $this->getOutput()->addHTML( $form );
509 $this->setBottomText( $opts );
512 function setTopText( FormOptions
$opts ) {
513 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
515 $nondefaults = $opts->getChangedValues();
517 $user = $this->getUser();
519 # Show watchlist header
521 $form .= $this->msg( 'watchlist-details' )->numParams( $this->numItems
)->parse() . "\n";
522 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
523 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
525 if ( $wgShowUpdatedMarker ) {
526 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
530 if ( $wgShowUpdatedMarker ) {
531 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
532 'action' => $this->getPageTitle()->getLocalURL(),
533 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
534 Xml
::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
535 Html
::hidden( 'reset', 'all' ) . "\n";
536 foreach ( $nondefaults as $key => $value ) {
537 $form .= Html
::hidden( $key, $value ) . "\n";
539 $form .= Xml
::closeElement( 'form' ) . "\n";
542 $form .= Xml
::openElement( 'form', array(
544 'action' => $this->getPageTitle()->getLocalURL(),
545 'id' => 'mw-watchlist-form'
547 $form .= Xml
::fieldset(
548 $this->msg( 'watchlist-options' )->text(),
550 array( 'id' => 'mw-watchlist-options' )
553 $form .= SpecialRecentChanges
::makeLegend( $this->getContext() );
555 $this->getOutput()->addHTML( $form );
558 protected function showHideLink( $options, $message, $name, $value ) {
559 $label = $this->msg( $value ?
'show' : 'hide' )->escaped();
560 $options[$name] = 1 - (int)$value;
562 return $this->msg( $message )->rawParams( Linker
::linkKnown( $this->getPageTitle(), $label, array(), $options ) )->escaped();
565 protected function hoursLink( $h, $options = array() ) {
566 $options['days'] = ( $h / 24.0 );
568 return Linker
::linkKnown(
569 $this->getPageTitle(),
570 $this->getLanguage()->formatNum( $h ),
576 protected function daysLink( $d, $options = array() ) {
577 $options['days'] = $d;
578 $message = ( $d ?
$this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
580 return Linker
::linkKnown(
581 $this->getPageTitle(),
591 * @param int $days This gets overwritten, so is not used
592 * @param array $options Query parameters for URL
595 protected function cutoffLinks( $days, $options = array() ) {
596 $hours = array( 1, 2, 6, 12 );
597 $days = array( 1, 3, 7 );
599 foreach ( $hours as $h ) {
600 $hours[$i++
] = $this->hoursLink( $h, $options );
603 foreach ( $days as $d ) {
604 $days[$i++
] = $this->daysLink( $d, $options );
606 return $this->msg( 'wlshowlast' )->rawParams(
607 $this->getLanguage()->pipeList( $hours ),
608 $this->getLanguage()->pipeList( $days ),
609 $this->daysLink( 0, $options ) )->parse();
613 * Count the number of items on a user's watchlist
615 * @param DatabaseBase $dbr A database connection
618 protected function countItems( $dbr ) {
619 # Fetch the raw count
620 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
621 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__
);
622 $row = $dbr->fetchObject( $rows );
623 $count = $row->count
;
625 return floor( $count / 2 );