b2ade1c63222fd1aef17b2857788b4a45b6b65ce
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 SpecialPage
{
28 public function __construct(){
29 parent
::__construct( 'Watchlist' );
34 * @param $par Parameter passed to the page
36 function execute( $par ) {
37 global $wgUser, $wgOut, $wgLang, $wgRequest;
38 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
41 $wlToken = $wgUser->getOption( 'watchlisttoken' );
43 $wlToken = sha1( mt_rand() . microtime( true ) );
44 $wgUser->setOption( 'watchlisttoken', $wlToken );
45 $wgUser->saveSettings();
48 global $wgFeedClasses;
49 $apiParams = array( 'action' => 'feedwatchlist', 'allrev' => 'allrev',
50 'wlowner' => $wgUser->getName(), 'wltoken' => $wlToken );
51 $feedTemplate = wfScript('api') . '?';
53 foreach( $wgFeedClasses as $format => $class ) {
54 $theseParams = $apiParams +
array( 'feedformat' => $format );
55 $url = $feedTemplate . wfArrayToCGI( $theseParams );
56 $wgOut->addFeedLink( $format, $url );
59 $skin = $this->getSkin();
60 $wgOut->setRobotPolicy( 'noindex,nofollow' );
62 # Anons don't get a watchlist
63 if( $wgUser->isAnon() ) {
64 $wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
65 $llink = $skin->linkKnown(
66 SpecialPage
::getTitleFor( 'Userlogin' ),
67 wfMsgHtml( 'loginreqlink' ),
69 array( 'returnto' => $this->getTitle()->getPrefixedText() )
71 $wgOut->addWikiMsgArray( 'watchlistanontext', array( $llink ), array( 'replaceafter' ) );
75 $wgOut->setPageTitle( wfMsg( 'watchlist' ) );
79 array( 'parseinline', 'replaceafter' ),
81 SpecialEditWatchlist
::buildTools( $skin )
83 $wgOut->setSubtitle( $sub );
85 if( ( $mode = SpecialEditWatchlist
::getMode( $wgRequest, $par ) ) !== false ) {
88 case SpecialEditWatchlist
::EDIT_CLEAR
:
91 case SpecialEditWatchlist
::EDIT_RAW
:
97 $title = SpecialPage
::getTitleFor( 'EditWatchlist', $mode );
98 $wgOut->redirect( $title->getLocalUrl() );
102 $uid = $wgUser->getId();
103 if( ($wgEnotifWatchlist ||
$wgShowUpdatedMarker) && $wgRequest->getVal( 'reset' ) &&
104 $wgRequest->wasPosted() )
106 $wgUser->clearAllNotifications( $uid );
107 $wgOut->redirect( $this->getTitle()->getFullUrl() );
112 /* float */ 'days' => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
113 /* bool */ 'hideMinor' => (int)$wgUser->getBoolOption( 'watchlisthideminor' ),
114 /* bool */ 'hideBots' => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
115 /* bool */ 'hideAnons' => (int)$wgUser->getBoolOption( 'watchlisthideanons' ),
116 /* bool */ 'hideLiu' => (int)$wgUser->getBoolOption( 'watchlisthideliu' ),
117 /* bool */ 'hidePatrolled' => (int)$wgUser->getBoolOption( 'watchlisthidepatrolled' ),
118 /* bool */ 'hideOwn' => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
119 /* ? */ 'namespace' => 'all',
120 /* ? */ 'invert' => false,
123 # Extract variables from the request, falling back to user preferences or
124 # other default values if these don't exist
125 $prefs['days'] = floatval( $wgUser->getOption( 'watchlistdays' ) );
126 $prefs['hideminor'] = $wgUser->getBoolOption( 'watchlisthideminor' );
127 $prefs['hidebots'] = $wgUser->getBoolOption( 'watchlisthidebots' );
128 $prefs['hideanons'] = $wgUser->getBoolOption( 'watchlisthideanons' );
129 $prefs['hideliu'] = $wgUser->getBoolOption( 'watchlisthideliu' );
130 $prefs['hideown' ] = $wgUser->getBoolOption( 'watchlisthideown' );
131 $prefs['hidepatrolled' ] = $wgUser->getBoolOption( 'watchlisthidepatrolled' );
133 # Get query variables
134 $days = $wgRequest->getVal( 'days' , $prefs['days'] );
135 $hideMinor = $wgRequest->getBool( 'hideMinor', $prefs['hideminor'] );
136 $hideBots = $wgRequest->getBool( 'hideBots' , $prefs['hidebots'] );
137 $hideAnons = $wgRequest->getBool( 'hideAnons', $prefs['hideanons'] );
138 $hideLiu = $wgRequest->getBool( 'hideLiu' , $prefs['hideliu'] );
139 $hideOwn = $wgRequest->getBool( 'hideOwn' , $prefs['hideown'] );
140 $hidePatrolled = $wgRequest->getBool( 'hidePatrolled' , $prefs['hidepatrolled'] );
142 # Get namespace value, if supplied, and prepare a WHERE fragment
143 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
144 $invert = $wgRequest->getIntOrNull( 'invert' );
145 if( !is_null( $nameSpace ) ) {
146 $nameSpace = intval( $nameSpace );
147 if( $invert && $nameSpace !== 'all' ) {
148 $nameSpaceClause = "rc_namespace != $nameSpace";
150 $nameSpaceClause = "rc_namespace = $nameSpace";
154 $nameSpaceClause = '';
157 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
158 $recentchanges = $dbr->tableName( 'recentchanges' );
160 $watchlistCount = $dbr->selectField( 'watchlist', 'COUNT(*)',
161 array( 'wl_user' => $uid ), __METHOD__
);
162 // Adjust for page X, talk:page X, which are both stored separately,
163 // but treated together
164 $nitems = floor( $watchlistCount / 2 );
166 if( is_null( $days ) ||
!is_numeric( $days ) ) {
167 $big = 1000; /* The magical big */
168 if( $nitems > $big ) {
169 # Set default cutoff shorter
170 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
172 $days = $defaults['days']; # default cutoff for shortlisters
175 $days = floatval($days);
178 // Dump everything here
179 $nondefaults = array();
181 wfAppendToArrayIfNotDefault( 'days' , $days , $defaults, $nondefaults);
182 wfAppendToArrayIfNotDefault( 'hideMinor', (int)$hideMinor, $defaults, $nondefaults );
183 wfAppendToArrayIfNotDefault( 'hideBots' , (int)$hideBots , $defaults, $nondefaults);
184 wfAppendToArrayIfNotDefault( 'hideAnons', (int)$hideAnons, $defaults, $nondefaults );
185 wfAppendToArrayIfNotDefault( 'hideLiu' , (int)$hideLiu , $defaults, $nondefaults );
186 wfAppendToArrayIfNotDefault( 'hideOwn' , (int)$hideOwn , $defaults, $nondefaults);
187 wfAppendToArrayIfNotDefault( 'namespace', $nameSpace , $defaults, $nondefaults);
188 wfAppendToArrayIfNotDefault( 'hidePatrolled', (int)$hidePatrolled, $defaults, $nondefaults );
191 $wgOut->addWikiMsg( 'nowatchlist' );
195 # Possible where conditions
199 $conds[] = "rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
202 # If the watchlist is relatively short, it's simplest to zip
203 # down its entirety and then sort the results.
205 # If it's relatively long, it may be worth our while to zip
206 # through the time-sorted page list checking for watched items.
208 # Up estimate of watched items by 15% to compensate for talk pages...
212 $conds[] = "rc_user != $uid";
215 $conds[] = 'rc_bot = 0';
218 $conds[] = 'rc_minor = 0';
221 $conds[] = 'rc_user = 0';
224 $conds[] = 'rc_user != 0';
226 if ( $wgUser->useRCPatrol() && $hidePatrolled ) {
227 $conds[] = 'rc_patrolled != 1';
229 if( $nameSpaceClause ) {
230 $conds[] = $nameSpaceClause;
233 # Toggle watchlist content (all recent edits or just the latest)
234 if( $wgUser->getOption( 'extendwatchlist' ) ) {
235 $limitWatchlist = intval( $wgUser->getOption( 'wllimit' ) );
238 # Top log Ids for a page are not stored
239 $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG
;
244 # Show a message about slave lag, if applicable
245 $lag = $dbr->getLag();
247 $wgOut->showLagWarning( $lag );
251 $form = Xml
::fieldset( wfMsg( 'watchlist-options' ), false, array( 'id' => 'mw-watchlist-options' ) );
253 # Show watchlist header
254 $form .= wfMsgExt( 'watchlist-details', array( 'parseinline' ), $wgLang->formatNum( $nitems ) );
256 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
257 $form .= wfMsgExt( 'wlheader-enotif', 'parse' ) . "\n";
259 if( $wgShowUpdatedMarker ) {
260 $form .= Xml
::openElement( 'form', array( 'method' => 'post',
261 'action' => $this->getTitle()->getLocalUrl(),
262 'id' => 'mw-watchlist-resetbutton' ) ) .
263 wfMsgExt( 'wlheader-showupdated', array( 'parseinline' ) ) . ' ' .
264 Xml
::submitButton( wfMsg( 'enotif_reset' ), array( 'name' => 'dummy' ) ) .
265 Html
::hidden( 'reset', 'all' ) .
266 Xml
::closeElement( 'form' );
270 $tables = array( 'recentchanges', 'watchlist' );
271 $fields = array( "{$recentchanges}.*" );
273 'watchlist' => array('INNER JOIN',"wl_user='{$uid}' AND wl_namespace=rc_namespace AND wl_title=rc_title"),
275 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
276 if( $wgShowUpdatedMarker ) {
277 $fields[] = 'wl_notificationtimestamp';
279 if( $limitWatchlist ) {
280 $options['LIMIT'] = $limitWatchlist;
283 $rollbacker = $wgUser->isAllowed('rollback');
284 if ( $usePage ||
$rollbacker ) {
286 $join_conds['page'] = array('LEFT JOIN','rc_cur_id=page_id');
288 $fields[] = 'page_latest';
292 ChangeTags
::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
293 wfRunHooks('SpecialWatchlistQuery', array(&$conds,&$tables,&$join_conds,&$fields) );
295 $res = $dbr->select( $tables, $fields, $conds, __METHOD__
, $options, $join_conds );
296 $numRows = $dbr->numRows( $res );
298 /* Start bottom header */
302 $wlInfo = wfMsgExt( 'rcnote', 'parseinline',
303 $wgLang->formatNum( $numRows ),
304 $wgLang->formatNum( $days ),
305 $wgLang->timeAndDate( wfTimestampNow(), true ),
306 $wgLang->date( wfTimestampNow(), true ),
307 $wgLang->time( wfTimestampNow(), true )
309 } elseif( $days > 0 ) {
310 $wlInfo = wfMsgExt( 'wlnote', 'parseinline',
311 $wgLang->formatNum( $numRows ),
312 $wgLang->formatNum( round($days*24) )
316 $cutofflinks = "\n" . self
::cutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n";
318 $thisTitle = SpecialPage
::getTitleFor( 'Watchlist' );
320 # Spit out some control panel links
321 $links[] = self
::showHideLink( $nondefaults, 'rcshowhideminor', 'hideMinor', $hideMinor );
322 $links[] = self
::showHideLink( $nondefaults, 'rcshowhidebots', 'hideBots', $hideBots );
323 $links[] = self
::showHideLink( $nondefaults, 'rcshowhideanons', 'hideAnons', $hideAnons );
324 $links[] = self
::showHideLink( $nondefaults, 'rcshowhideliu', 'hideLiu', $hideLiu );
325 $links[] = self
::showHideLink( $nondefaults, 'rcshowhidemine', 'hideOwn', $hideOwn );
327 if( $wgUser->useRCPatrol() ) {
328 $links[] = self
::showHideLink( $nondefaults, 'rcshowhidepatr', 'hidePatrolled', $hidePatrolled );
331 # Namespace filter and put the whole form together.
333 $form .= $cutofflinks;
334 $form .= $wgLang->pipeList( $links );
335 $form .= Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl(), 'id' => 'mw-watchlist-form-namespaceselector' ) );
336 $form .= '<hr /><p>';
337 $form .= Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ';
338 $form .= Xml
::namespaceSelector( $nameSpace, '' ) . ' ';
339 $form .= Xml
::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $invert ) . ' ';
340 $form .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
341 $form .= Html
::hidden( 'days', $days );
343 $form .= Html
::hidden( 'hideMinor', 1 );
346 $form .= Html
::hidden( 'hideBots', 1 );
349 $form .= Html
::hidden( 'hideAnons', 1 );
352 $form .= Html
::hidden( 'hideLiu', 1 );
355 $form .= Html
::hidden( 'hideOwn', 1 );
357 $form .= Xml
::closeElement( 'form' );
358 $form .= Xml
::closeElement( 'fieldset' );
359 $wgOut->addHTML( $form );
361 # If there's nothing to show, stop here
362 if( $numRows == 0 ) {
363 $wgOut->addWikiMsg( 'watchnochange' );
367 /* End bottom header */
369 /* Do link batch query */
370 $linkBatch = new LinkBatch
;
371 foreach ( $res as $row ) {
372 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text
);
373 if ( $row->rc_user
!= 0 ) {
374 $linkBatch->add( NS_USER
, $userNameUnderscored );
376 $linkBatch->add( NS_USER_TALK
, $userNameUnderscored );
378 $linkBatch->add( $row->rc_namespace
, $row->rc_title
);
380 $linkBatch->execute();
381 $dbr->dataSeek( $res, 0 );
383 $list = ChangesList
::newFromUser( $wgUser );
384 $list->setWatchlistDivs();
386 $s = $list->beginRecentChangesList();
388 foreach ( $res as $obj ) {
390 $rc = RecentChange
::newFromRow( $obj );
391 $rc->counter
= $counter++
;
393 if ( $wgShowUpdatedMarker ) {
394 $updated = $obj->wl_notificationtimestamp
;
399 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
400 $rc->numberofWatchingusers
= $dbr->selectField( 'watchlist',
403 'wl_namespace' => $obj->rc_namespace
,
404 'wl_title' => $obj->rc_title
,
408 $rc->numberofWatchingusers
= 0;
411 $s .= $list->recentChangesLine( $rc, $updated, $counter );
413 $s .= $list->endRecentChangesList();
415 $wgOut->addHTML( $s );
418 public static function showHideLink( $options, $message, $name, $value ) {
421 $showLinktext = wfMsgHtml( 'show' );
422 $hideLinktext = wfMsgHtml( 'hide' );
423 $title = SpecialPage
::getTitleFor( 'Watchlist' );
424 $skin = $wgUser->getSkin();
426 $label = $value ?
$showLinktext : $hideLinktext;
427 $options[$name] = 1 - (int) $value;
429 return wfMsgHtml( $message, $skin->linkKnown( $title, $label, array(), $options ) );
432 public static function hoursLink( $h, $page, $options = array() ) {
433 global $wgUser, $wgLang, $wgContLang;
435 $sk = $wgUser->getSkin();
436 $title = Title
::newFromText( $wgContLang->specialPage( $page ) );
437 $options['days'] = ($h / 24.0);
439 return $sk->linkKnown(
441 $wgLang->formatNum( $h ),
447 public static function daysLink( $d, $page, $options = array() ) {
448 global $wgUser, $wgLang, $wgContLang;
450 $sk = $wgUser->getSkin();
451 $title = Title
::newFromText( $wgContLang->specialPage( $page ) );
452 $options['days'] = $d;
453 $message = ($d ?
$wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) );
455 return $sk->linkKnown(
466 protected static function cutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
469 $hours = array( 1, 2, 6, 12 );
470 $days = array( 1, 3, 7 );
472 foreach( $hours as $h ) {
473 $hours[$i++
] = self
::hoursLink( $h, $page, $options );
476 foreach( $days as $d ) {
477 $days[$i++
] = self
::daysLink( $d, $page, $options );
479 return wfMsgExt('wlshowlast',
480 array('parseinline', 'replaceafter'),
481 $wgLang->pipeList( $hours ),
482 $wgLang->pipeList( $days ),
483 self
::daysLink( 0, $page, $options ) );
487 * Count the number of items on a user's watchlist
489 * @param $user User object
490 * @param $talk Boolean: include talk pages
493 protected static function countItems( &$user, $talk = true ) {
494 $dbr = wfGetDB( DB_SLAVE
, 'watchlist' );
496 # Fetch the raw count
497 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count',
498 array( 'wl_user' => $user->mId
), __METHOD__
);
499 $row = $dbr->fetchObject( $res );
500 $count = $row->count
;
502 # Halve to remove talk pages if needed
504 $count = floor( $count / 2 );