Add spacing, fix bracing
[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 Watchlist
22 */
23 class SpecialWatchlist extends SpecialPage {
24
25 /**
26 * Constructor
27 */
28 public function __construct(){
29 parent::__construct( 'Watchlist' );
30 }
31
32 /**
33 * Execute
34 * @param $par Parameter passed to the page
35 */
36 function execute( $par ) {
37 global $wgUser, $wgOut, $wgLang, $wgRequest;
38 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
39
40 // Add feed links
41 $wlToken = $wgUser->getOption( 'watchlisttoken' );
42 if (!$wlToken) {
43 $wlToken = sha1( mt_rand() . microtime( true ) );
44 $wgUser->setOption( 'watchlisttoken', $wlToken );
45 $wgUser->saveSettings();
46 }
47
48 global $wgFeedClasses;
49 $apiParams = array( 'action' => 'feedwatchlist', 'allrev' => 'allrev',
50 'wlowner' => $wgUser->getName(), 'wltoken' => $wlToken );
51 $feedTemplate = wfScript('api') . '?';
52
53 foreach( $wgFeedClasses as $format => $class ) {
54 $theseParams = $apiParams + array( 'feedformat' => $format );
55 $url = $feedTemplate . wfArrayToCGI( $theseParams );
56 $wgOut->addFeedLink( $format, $url );
57 }
58
59 $skin = $this->getSkin();
60 $wgOut->setRobotPolicy( 'noindex,nofollow' );
61
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' ),
68 array(),
69 array( 'returnto' => $this->getTitle()->getPrefixedText() )
70 );
71 $wgOut->addWikiMsgArray( 'watchlistanontext', array( $llink ), array( 'replaceafter' ) );
72 return;
73 }
74
75 $wgOut->setPageTitle( wfMsg( 'watchlist' ) );
76
77 $sub = wfMsgExt(
78 'watchlistfor2',
79 array( 'parseinline', 'replaceafter' ),
80 $wgUser->getName(),
81 SpecialEditWatchlist::buildTools( $skin )
82 );
83 $wgOut->setSubtitle( $sub );
84
85 if( ( $mode = SpecialEditWatchlist::getMode( $wgRequest, $par ) ) !== false ) {
86 # TODO: localise?
87 switch( $mode ){
88 case SpecialEditWatchlist::EDIT_CLEAR:
89 $mode = 'clear';
90 break;
91 case SpecialEditWatchlist::EDIT_RAW:
92 $mode = 'raw';
93 break;
94 default:
95 $mode = null;
96 }
97 $title = SpecialPage::getTitleFor( 'EditWatchlist', $mode );
98 $wgOut->redirect( $title->getLocalUrl() );
99 return;
100 }
101
102 $uid = $wgUser->getId();
103 if( ($wgEnotifWatchlist || $wgShowUpdatedMarker) && $wgRequest->getVal( 'reset' ) &&
104 $wgRequest->wasPosted() )
105 {
106 $wgUser->clearAllNotifications( $uid );
107 $wgOut->redirect( $this->getTitle()->getFullUrl() );
108 return;
109 }
110
111 $defaults = array(
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,
121 );
122
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' );
132
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'] );
141
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";
149 } else {
150 $nameSpaceClause = "rc_namespace = $nameSpace";
151 }
152 } else {
153 $nameSpace = '';
154 $nameSpaceClause = '';
155 }
156
157 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
158 $recentchanges = $dbr->tableName( 'recentchanges' );
159
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 );
165
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...
171 } else {
172 $days = $defaults['days']; # default cutoff for shortlisters
173 }
174 } else {
175 $days = floatval($days);
176 }
177
178 // Dump everything here
179 $nondefaults = array();
180
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 );
189
190 if( $nitems == 0 ) {
191 $wgOut->addWikiMsg( 'nowatchlist' );
192 return;
193 }
194
195 # Possible where conditions
196 $conds = array();
197
198 if( $days > 0 ) {
199 $conds[] = "rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
200 }
201
202 # If the watchlist is relatively short, it's simplest to zip
203 # down its entirety and then sort the results.
204
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.
207
208 # Up estimate of watched items by 15% to compensate for talk pages...
209
210 # Toggles
211 if( $hideOwn ) {
212 $conds[] = "rc_user != $uid";
213 }
214 if( $hideBots ) {
215 $conds[] = 'rc_bot = 0';
216 }
217 if( $hideMinor ) {
218 $conds[] = 'rc_minor = 0';
219 }
220 if( $hideLiu ) {
221 $conds[] = 'rc_user = 0';
222 }
223 if( $hideAnons ) {
224 $conds[] = 'rc_user != 0';
225 }
226 if ( $wgUser->useRCPatrol() && $hidePatrolled ) {
227 $conds[] = 'rc_patrolled != 1';
228 }
229 if( $nameSpaceClause ) {
230 $conds[] = $nameSpaceClause;
231 }
232
233 # Toggle watchlist content (all recent edits or just the latest)
234 if( $wgUser->getOption( 'extendwatchlist' ) ) {
235 $limitWatchlist = intval( $wgUser->getOption( 'wllimit' ) );
236 $usePage = false;
237 } else {
238 # Top log Ids for a page are not stored
239 $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG;
240 $limitWatchlist = 0;
241 $usePage = true;
242 }
243
244 # Show a message about slave lag, if applicable
245 $lag = $dbr->getLag();
246 if( $lag > 0 ) {
247 $wgOut->showLagWarning( $lag );
248 }
249
250 # Create output form
251 $form = Xml::fieldset( wfMsg( 'watchlist-options' ), false, array( 'id' => 'mw-watchlist-options' ) );
252
253 # Show watchlist header
254 $form .= wfMsgExt( 'watchlist-details', array( 'parseinline' ), $wgLang->formatNum( $nitems ) );
255
256 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
257 $form .= wfMsgExt( 'wlheader-enotif', 'parse' ) . "\n";
258 }
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' );
267 }
268 $form .= '<hr />';
269
270 $tables = array( 'recentchanges', 'watchlist' );
271 $fields = array( "{$recentchanges}.*" );
272 $join_conds = array(
273 'watchlist' => array('INNER JOIN',"wl_user='{$uid}' AND wl_namespace=rc_namespace AND wl_title=rc_title"),
274 );
275 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
276 if( $wgShowUpdatedMarker ) {
277 $fields[] = 'wl_notificationtimestamp';
278 }
279 if( $limitWatchlist ) {
280 $options['LIMIT'] = $limitWatchlist;
281 }
282
283 $rollbacker = $wgUser->isAllowed('rollback');
284 if ( $usePage || $rollbacker ) {
285 $tables[] = 'page';
286 $join_conds['page'] = array('LEFT JOIN','rc_cur_id=page_id');
287 if ($rollbacker) {
288 $fields[] = 'page_latest';
289 }
290 }
291
292 ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
293 wfRunHooks('SpecialWatchlistQuery', array(&$conds,&$tables,&$join_conds,&$fields) );
294
295 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
296 $numRows = $dbr->numRows( $res );
297
298 /* Start bottom header */
299
300 $wlInfo = '';
301 if( $days >= 1 ) {
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 )
308 ) . '<br />';
309 } elseif( $days > 0 ) {
310 $wlInfo = wfMsgExt( 'wlnote', 'parseinline',
311 $wgLang->formatNum( $numRows ),
312 $wgLang->formatNum( round($days*24) )
313 ) . '<br />';
314 }
315
316 $cutofflinks = "\n" . self::cutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n";
317
318 $thisTitle = SpecialPage::getTitleFor( 'Watchlist' );
319
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 );
326
327 if( $wgUser->useRCPatrol() ) {
328 $links[] = self::showHideLink( $nondefaults, 'rcshowhidepatr', 'hidePatrolled', $hidePatrolled );
329 }
330
331 # Namespace filter and put the whole form together.
332 $form .= $wlInfo;
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' ) . '&#160;';
338 $form .= Xml::namespaceSelector( $nameSpace, '' ) . '&#160;';
339 $form .= Xml::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $invert ) . '&#160;';
340 $form .= Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
341 $form .= Html::hidden( 'days', $days );
342 if( $hideMinor ) {
343 $form .= Html::hidden( 'hideMinor', 1 );
344 }
345 if( $hideBots ) {
346 $form .= Html::hidden( 'hideBots', 1 );
347 }
348 if( $hideAnons ) {
349 $form .= Html::hidden( 'hideAnons', 1 );
350 }
351 if( $hideLiu ) {
352 $form .= Html::hidden( 'hideLiu', 1 );
353 }
354 if( $hideOwn ) {
355 $form .= Html::hidden( 'hideOwn', 1 );
356 }
357 $form .= Xml::closeElement( 'form' );
358 $form .= Xml::closeElement( 'fieldset' );
359 $wgOut->addHTML( $form );
360
361 # If there's nothing to show, stop here
362 if( $numRows == 0 ) {
363 $wgOut->addWikiMsg( 'watchnochange' );
364 return;
365 }
366
367 /* End bottom header */
368
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 );
375 }
376 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
377
378 $linkBatch->add( $row->rc_namespace, $row->rc_title );
379 }
380 $linkBatch->execute();
381 $dbr->dataSeek( $res, 0 );
382
383 $list = ChangesList::newFromUser( $wgUser );
384 $list->setWatchlistDivs();
385
386 $s = $list->beginRecentChangesList();
387 $counter = 1;
388 foreach ( $res as $obj ) {
389 # Make RC entry
390 $rc = RecentChange::newFromRow( $obj );
391 $rc->counter = $counter++;
392
393 if ( $wgShowUpdatedMarker ) {
394 $updated = $obj->wl_notificationtimestamp;
395 } else {
396 $updated = false;
397 }
398
399 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
400 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
401 'COUNT(*)',
402 array(
403 'wl_namespace' => $obj->rc_namespace,
404 'wl_title' => $obj->rc_title,
405 ),
406 __METHOD__ );
407 } else {
408 $rc->numberofWatchingusers = 0;
409 }
410
411 $s .= $list->recentChangesLine( $rc, $updated, $counter );
412 }
413 $s .= $list->endRecentChangesList();
414
415 $wgOut->addHTML( $s );
416 }
417
418 public static function showHideLink( $options, $message, $name, $value ) {
419 global $wgUser;
420
421 $showLinktext = wfMsgHtml( 'show' );
422 $hideLinktext = wfMsgHtml( 'hide' );
423 $title = SpecialPage::getTitleFor( 'Watchlist' );
424 $skin = $wgUser->getSkin();
425
426 $label = $value ? $showLinktext : $hideLinktext;
427 $options[$name] = 1 - (int) $value;
428
429 return wfMsgHtml( $message, $skin->linkKnown( $title, $label, array(), $options ) );
430 }
431
432 public static function hoursLink( $h, $page, $options = array() ) {
433 global $wgUser, $wgLang, $wgContLang;
434
435 $sk = $wgUser->getSkin();
436 $title = Title::newFromText( $wgContLang->specialPage( $page ) );
437 $options['days'] = ($h / 24.0);
438
439 return $sk->linkKnown(
440 $title,
441 $wgLang->formatNum( $h ),
442 array(),
443 $options
444 );
445 }
446
447 public static function daysLink( $d, $page, $options = array() ) {
448 global $wgUser, $wgLang, $wgContLang;
449
450 $sk = $wgUser->getSkin();
451 $title = Title::newFromText( $wgContLang->specialPage( $page ) );
452 $options['days'] = $d;
453 $message = ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) );
454
455 return $sk->linkKnown(
456 $title,
457 $message,
458 array(),
459 $options
460 );
461 }
462
463 /**
464 * Returns html
465 */
466 protected static function cutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
467 global $wgLang;
468
469 $hours = array( 1, 2, 6, 12 );
470 $days = array( 1, 3, 7 );
471 $i = 0;
472 foreach( $hours as $h ) {
473 $hours[$i++] = self::hoursLink( $h, $page, $options );
474 }
475 $i = 0;
476 foreach( $days as $d ) {
477 $days[$i++] = self::daysLink( $d, $page, $options );
478 }
479 return wfMsgExt('wlshowlast',
480 array('parseinline', 'replaceafter'),
481 $wgLang->pipeList( $hours ),
482 $wgLang->pipeList( $days ),
483 self::daysLink( 0, $page, $options ) );
484 }
485
486 /**
487 * Count the number of items on a user's watchlist
488 *
489 * @param $user User object
490 * @param $talk Boolean: include talk pages
491 * @return Integer
492 */
493 protected static function countItems( &$user, $talk = true ) {
494 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
495
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;
501
502 # Halve to remove talk pages if needed
503 if( !$talk ) {
504 $count = floor( $count / 2 );
505 }
506
507 return( $count );
508 }
509 }