Per Nikerabbit, follow-up to r77972: use a string instead of boolean for readability
[mediawiki.git] / includes / specials / SpecialWatchlist.php
blobb64af984311cd3de9b667573454ee802fa62647d
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 Watchlist
24 /**
25 * Constructor
27 * @param $par Parameter passed to the page
29 function wfSpecialWatchlist( $par ) {
30 global $wgUser, $wgOut, $wgLang, $wgRequest;
31 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
33 // Add feed links
34 $wlToken = $wgUser->getOption( 'watchlisttoken' );
35 if (!$wlToken) {
36 $wlToken = sha1( mt_rand() . microtime( true ) );
37 $wgUser->setOption( 'watchlisttoken', $wlToken );
38 $wgUser->saveSettings();
41 global $wgFeedClasses;
42 $apiParams = array( 'action' => 'feedwatchlist', 'allrev' => 'allrev',
43 'wlowner' => $wgUser->getName(), 'wltoken' => $wlToken );
44 $feedTemplate = wfScript('api').'?';
46 foreach( $wgFeedClasses as $format => $class ) {
47 $theseParams = $apiParams + array( 'feedformat' => $format );
48 $url = $feedTemplate . wfArrayToCGI( $theseParams );
49 $wgOut->addFeedLink( $format, $url );
52 $skin = $wgUser->getSkin();
53 $specialTitle = SpecialPage::getTitleFor( 'Watchlist' );
54 $wgOut->setRobotPolicy( 'noindex,nofollow' );
56 # Anons don't get a watchlist
57 if( $wgUser->isAnon() ) {
58 $wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
59 $llink = $skin->linkKnown(
60 SpecialPage::getTitleFor( 'Userlogin' ),
61 wfMsgHtml( 'loginreqlink' ),
62 array(),
63 array( 'returnto' => $specialTitle->getPrefixedText() )
65 $wgOut->addHTML( wfMsgWikiHtml( 'watchlistanontext', $llink ) );
66 return;
69 $wgOut->setPageTitle( wfMsg( 'watchlist' ) );
71 $sub = wfMsgExt( 'watchlistfor2', array( 'parseinline', 'replaceafter' ), $wgUser->getName(), WatchlistEditor::buildTools( $wgUser->getSkin() ) );
72 $wgOut->setSubtitle( $sub );
74 if( ( $mode = WatchlistEditor::getMode( $wgRequest, $par ) ) !== false ) {
75 $editor = new WatchlistEditor();
76 $editor->execute( $wgUser, $wgOut, $wgRequest, $mode );
77 return;
80 $uid = $wgUser->getId();
81 if( ($wgEnotifWatchlist || $wgShowUpdatedMarker) && $wgRequest->getVal( 'reset' ) &&
82 $wgRequest->wasPosted() )
84 $wgUser->clearAllNotifications( $uid );
85 $wgOut->redirect( $specialTitle->getFullUrl() );
86 return;
89 $defaults = array(
90 /* float */ 'days' => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
91 /* bool */ 'hideMinor' => (int)$wgUser->getBoolOption( 'watchlisthideminor' ),
92 /* bool */ 'hideBots' => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
93 /* bool */ 'hideAnons' => (int)$wgUser->getBoolOption( 'watchlisthideanons' ),
94 /* bool */ 'hideLiu' => (int)$wgUser->getBoolOption( 'watchlisthideliu' ),
95 /* bool */ 'hidePatrolled' => (int)$wgUser->getBoolOption( 'watchlisthidepatrolled' ),
96 /* bool */ 'hideOwn' => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
97 /* ? */ 'namespace' => 'all',
98 /* ? */ 'invert' => false,
101 extract($defaults);
103 # Extract variables from the request, falling back to user preferences or
104 # other default values if these don't exist
105 $prefs['days'] = floatval( $wgUser->getOption( 'watchlistdays' ) );
106 $prefs['hideminor'] = $wgUser->getBoolOption( 'watchlisthideminor' );
107 $prefs['hidebots'] = $wgUser->getBoolOption( 'watchlisthidebots' );
108 $prefs['hideanons'] = $wgUser->getBoolOption( 'watchlisthideanon' );
109 $prefs['hideliu'] = $wgUser->getBoolOption( 'watchlisthideliu' );
110 $prefs['hideown' ] = $wgUser->getBoolOption( 'watchlisthideown' );
111 $prefs['hidepatrolled' ] = $wgUser->getBoolOption( 'watchlisthidepatrolled' );
113 # Get query variables
114 $days = $wgRequest->getVal( 'days' , $prefs['days'] );
115 $hideMinor = $wgRequest->getBool( 'hideMinor', $prefs['hideminor'] );
116 $hideBots = $wgRequest->getBool( 'hideBots' , $prefs['hidebots'] );
117 $hideAnons = $wgRequest->getBool( 'hideAnons', $prefs['hideanons'] );
118 $hideLiu = $wgRequest->getBool( 'hideLiu' , $prefs['hideliu'] );
119 $hideOwn = $wgRequest->getBool( 'hideOwn' , $prefs['hideown'] );
120 $hidePatrolled = $wgRequest->getBool( 'hidePatrolled' , $prefs['hidepatrolled'] );
122 # Get namespace value, if supplied, and prepare a WHERE fragment
123 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
124 $invert = $wgRequest->getIntOrNull( 'invert' );
125 if( !is_null( $nameSpace ) ) {
126 $nameSpace = intval( $nameSpace );
127 if( $invert && $nameSpace !== 'all' )
128 $nameSpaceClause = "rc_namespace != $nameSpace";
129 else
130 $nameSpaceClause = "rc_namespace = $nameSpace";
131 } else {
132 $nameSpace = '';
133 $nameSpaceClause = '';
136 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
137 $recentchanges = $dbr->tableName( 'recentchanges' );
139 $watchlistCount = $dbr->selectField( 'watchlist', 'COUNT(*)',
140 array( 'wl_user' => $uid ), __METHOD__ );
141 // Adjust for page X, talk:page X, which are both stored separately,
142 // but treated together
143 $nitems = floor($watchlistCount / 2);
145 if( is_null($days) || !is_numeric($days) ) {
146 $big = 1000; /* The magical big */
147 if($nitems > $big) {
148 # Set default cutoff shorter
149 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
150 } else {
151 $days = $defaults['days']; # default cutoff for shortlisters
153 } else {
154 $days = floatval($days);
157 // Dump everything here
158 $nondefaults = array();
160 wfAppendToArrayIfNotDefault( 'days' , $days , $defaults, $nondefaults);
161 wfAppendToArrayIfNotDefault( 'hideMinor', (int)$hideMinor, $defaults, $nondefaults );
162 wfAppendToArrayIfNotDefault( 'hideBots' , (int)$hideBots , $defaults, $nondefaults);
163 wfAppendToArrayIfNotDefault( 'hideAnons', (int)$hideAnons, $defaults, $nondefaults );
164 wfAppendToArrayIfNotDefault( 'hideLiu' , (int)$hideLiu , $defaults, $nondefaults );
165 wfAppendToArrayIfNotDefault( 'hideOwn' , (int)$hideOwn , $defaults, $nondefaults);
166 wfAppendToArrayIfNotDefault( 'namespace', $nameSpace , $defaults, $nondefaults);
167 wfAppendToArrayIfNotDefault( 'hidePatrolled', (int)$hidePatrolled, $defaults, $nondefaults );
169 if( $nitems == 0 ) {
170 $wgOut->addWikiMsg( 'nowatchlist' );
171 return;
174 # Possible where conditions
175 $conds = array();
177 if( $days > 0 ) {
178 $conds[] = "rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
181 # If the watchlist is relatively short, it's simplest to zip
182 # down its entirety and then sort the results.
184 # If it's relatively long, it may be worth our while to zip
185 # through the time-sorted page list checking for watched items.
187 # Up estimate of watched items by 15% to compensate for talk pages...
189 # Toggles
190 if( $hideOwn ) {
191 $conds[] = "rc_user != $uid";
193 if( $hideBots ) {
194 $conds[] = 'rc_bot = 0';
196 if( $hideMinor ) {
197 $conds[] = 'rc_minor = 0';
199 if( $hideLiu ) {
200 $conds[] = 'rc_user = 0';
202 if( $hideAnons ) {
203 $conds[] = 'rc_user != 0';
205 if ( $wgUser->useRCPatrol() && $hidePatrolled ) {
206 $conds[] = 'rc_patrolled != 1';
208 if( $nameSpaceClause ) {
209 $conds[] = $nameSpaceClause;
212 # Toggle watchlist content (all recent edits or just the latest)
213 if( $wgUser->getOption( 'extendwatchlist' )) {
214 $limitWatchlist = intval( $wgUser->getOption( 'wllimit' ) );
215 $usePage = false;
216 } else {
217 # Top log Ids for a page are not stored
218 $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG;
219 $limitWatchlist = 0;
220 $usePage = true;
223 # Show a message about slave lag, if applicable
224 if( ( $lag = $dbr->getLag() ) > 0 )
225 $wgOut->showLagWarning( $lag );
227 # Create output form
228 $form = Xml::fieldset( wfMsg( 'watchlist-options' ), false, array( 'id' => 'mw-watchlist-options' ) );
230 # Show watchlist header
231 $form .= wfMsgExt( 'watchlist-details', array( 'parseinline' ), $wgLang->formatNum( $nitems ) );
233 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
234 $form .= wfMsgExt( 'wlheader-enotif', 'parse' ) . "\n";
236 if( $wgShowUpdatedMarker ) {
237 $form .= Xml::openElement( 'form', array( 'method' => 'post',
238 'action' => $specialTitle->getLocalUrl(),
239 'id' => 'mw-watchlist-resetbutton' ) ) .
240 wfMsgExt( 'wlheader-showupdated', array( 'parseinline' ) ) . ' ' .
241 Xml::submitButton( wfMsg( 'enotif_reset' ), array( 'name' => 'dummy' ) ) .
242 Html::hidden( 'reset', 'all' ) .
243 Xml::closeElement( 'form' );
245 $form .= '<hr />';
247 $tables = array( 'recentchanges', 'watchlist' );
248 $fields = array( "{$recentchanges}.*" );
249 $join_conds = array(
250 'watchlist' => array('INNER JOIN',"wl_user='{$uid}' AND wl_namespace=rc_namespace AND wl_title=rc_title"),
252 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
253 if( $wgShowUpdatedMarker ) {
254 $fields[] = 'wl_notificationtimestamp';
256 if( $limitWatchlist ) {
257 $options['LIMIT'] = $limitWatchlist;
260 $rollbacker = $wgUser->isAllowed('rollback');
261 if ( $usePage || $rollbacker ) {
262 $tables[] = 'page';
263 $join_conds['page'] = array('LEFT JOIN','rc_cur_id=page_id');
264 if ($rollbacker)
265 $fields[] = 'page_latest';
268 ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
269 wfRunHooks('SpecialWatchlistQuery', array(&$conds,&$tables,&$join_conds,&$fields) );
271 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
272 $numRows = $dbr->numRows( $res );
274 /* Start bottom header */
276 $wlInfo = '';
277 if( $days >= 1 ) {
278 $wlInfo = wfMsgExt( 'rcnote', 'parseinline',
279 $wgLang->formatNum( $numRows ),
280 $wgLang->formatNum( $days ),
281 $wgLang->timeAndDate( wfTimestampNow(), true ),
282 $wgLang->date( wfTimestampNow(), true ),
283 $wgLang->time( wfTimestampNow(), true )
284 ) . '<br />';
285 } elseif( $days > 0 ) {
286 $wlInfo = wfMsgExt( 'wlnote', 'parseinline',
287 $wgLang->formatNum( $numRows ),
288 $wgLang->formatNum( round($days*24) )
289 ) . '<br />';
292 $cutofflinks = "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n";
294 $thisTitle = SpecialPage::getTitleFor( 'Watchlist' );
296 # Spit out some control panel links
297 $links[] = wlShowHideLink( $nondefaults, 'rcshowhideminor', 'hideMinor', $hideMinor );
298 $links[] = wlShowHideLink( $nondefaults, 'rcshowhidebots', 'hideBots', $hideBots );
299 $links[] = wlShowHideLink( $nondefaults, 'rcshowhideanons', 'hideAnons', $hideAnons );
300 $links[] = wlShowHideLink( $nondefaults, 'rcshowhideliu', 'hideLiu', $hideLiu );
301 $links[] = wlShowHideLink( $nondefaults, 'rcshowhidemine', 'hideOwn', $hideOwn );
303 if( $wgUser->useRCPatrol() ) {
304 $links[] = wlShowHideLink( $nondefaults, 'rcshowhidepatr', 'hidePatrolled', $hidePatrolled );
307 # Namespace filter and put the whole form together.
308 $form .= $wlInfo;
309 $form .= $cutofflinks;
310 $form .= $wgLang->pipeList( $links );
311 $form .= Xml::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl(), 'id' => 'mw-watchlist-form-namespaceselector' ) );
312 $form .= '<hr /><p>';
313 $form .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&#160;';
314 $form .= Xml::namespaceSelector( $nameSpace, '' ) . '&#160;';
315 $form .= Xml::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $invert ) . '&#160;';
316 $form .= Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
317 $form .= Html::hidden( 'days', $days );
318 if( $hideMinor )
319 $form .= Html::hidden( 'hideMinor', 1 );
320 if( $hideBots )
321 $form .= Html::hidden( 'hideBots', 1 );
322 if( $hideAnons )
323 $form .= Html::hidden( 'hideAnons', 1 );
324 if( $hideLiu )
325 $form .= Html::hidden( 'hideLiu', 1 );
326 if( $hideOwn )
327 $form .= Html::hidden( 'hideOwn', 1 );
328 $form .= Xml::closeElement( 'form' );
329 $form .= Xml::closeElement( 'fieldset' );
330 $wgOut->addHTML( $form );
332 # If there's nothing to show, stop here
333 if( $numRows == 0 ) {
334 $wgOut->addWikiMsg( 'watchnochange' );
335 return;
338 /* End bottom header */
340 /* Do link batch query */
341 $linkBatch = new LinkBatch;
342 foreach ( $res as $row ) {
343 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
344 if ( $row->rc_user != 0 ) {
345 $linkBatch->add( NS_USER, $userNameUnderscored );
347 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
349 $linkBatch->add( $row->rc_namespace, $row->rc_title );
351 $linkBatch->execute();
352 $dbr->dataSeek( $res, 0 );
354 $list = ChangesList::newFromUser( $wgUser );
355 $list->setWatchlistDivs();
357 $s = $list->beginRecentChangesList();
358 $counter = 1;
359 foreach ( $res as $obj ) {
360 # Make RC entry
361 $rc = RecentChange::newFromRow( $obj );
362 $rc->counter = $counter++;
364 if ( $wgShowUpdatedMarker ) {
365 $updated = $obj->wl_notificationtimestamp;
366 } else {
367 $updated = false;
370 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
371 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
372 'COUNT(*)',
373 array(
374 'wl_namespace' => $obj->rc_namespace,
375 'wl_title' => $obj->rc_title,
377 __METHOD__ );
378 } else {
379 $rc->numberofWatchingusers = 0;
382 $s .= $list->recentChangesLine( $rc, $updated, $counter );
384 $s .= $list->endRecentChangesList();
386 $wgOut->addHTML( $s );
389 function wlShowHideLink( $options, $message, $name, $value ) {
390 global $wgUser;
392 $showLinktext = wfMsgHtml( 'show' );
393 $hideLinktext = wfMsgHtml( 'hide' );
394 $title = SpecialPage::getTitleFor( 'Watchlist' );
395 $skin = $wgUser->getSkin();
397 $label = $value ? $showLinktext : $hideLinktext;
398 $options[$name] = 1 - (int) $value;
400 return wfMsgHtml( $message, $skin->linkKnown( $title, $label, array(), $options ) );
404 function wlHoursLink( $h, $page, $options = array() ) {
405 global $wgUser, $wgLang, $wgContLang;
407 $sk = $wgUser->getSkin();
408 $title = Title::newFromText( $wgContLang->specialPage( $page ) );
409 $options['days'] = ($h / 24.0);
411 $s = $sk->linkKnown(
412 $title,
413 $wgLang->formatNum( $h ),
414 array(),
415 $options
418 return $s;
421 function wlDaysLink( $d, $page, $options = array() ) {
422 global $wgUser, $wgLang, $wgContLang;
424 $sk = $wgUser->getSkin();
425 $title = Title::newFromText( $wgContLang->specialPage( $page ) );
426 $options['days'] = $d;
427 $message = ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) );
429 $s = $sk->linkKnown(
430 $title,
431 $message,
432 array(),
433 $options
436 return $s;
440 * Returns html
442 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
443 global $wgLang;
445 $hours = array( 1, 2, 6, 12 );
446 $days = array( 1, 3, 7 );
447 $i = 0;
448 foreach( $hours as $h ) {
449 $hours[$i++] = wlHoursLink( $h, $page, $options );
451 $i = 0;
452 foreach( $days as $d ) {
453 $days[$i++] = wlDaysLink( $d, $page, $options );
455 return wfMsgExt('wlshowlast',
456 array('parseinline', 'replaceafter'),
457 $wgLang->pipeList( $hours ),
458 $wgLang->pipeList( $days ),
459 wlDaysLink( 0, $page, $options ) );
463 * Count the number of items on a user's watchlist
465 * @param $user User object
466 * @param $talk Boolean: include talk pages
467 * @return Integer
469 function wlCountItems( &$user, $talk = true ) {
470 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
472 # Fetch the raw count
473 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count',
474 array( 'wl_user' => $user->mId ), 'wlCountItems' );
475 $row = $dbr->fetchObject( $res );
476 $count = $row->count;
478 # Halve to remove talk pages if needed
479 if( !$talk )
480 $count = floor( $count / 2 );
482 return( $count );