Break svn blame on every single line of SpecialWatchlist.php by converting it to...
[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 = $wgUser->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( $wgUser->getSkin() )
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 } else {
152 $nameSpace = '';
153 $nameSpaceClause = '';
154 }
155
156 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
157 $recentchanges = $dbr->tableName( 'recentchanges' );
158
159 $watchlistCount = $dbr->selectField( 'watchlist', 'COUNT(*)',
160 array( 'wl_user' => $uid ), __METHOD__ );
161 // Adjust for page X, talk:page X, which are both stored separately,
162 // but treated together
163 $nitems = floor($watchlistCount / 2);
164
165 if( is_null($days) || !is_numeric($days) ) {
166 $big = 1000; /* The magical big */
167 if($nitems > $big) {
168 # Set default cutoff shorter
169 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
170 } else {
171 $days = $defaults['days']; # default cutoff for shortlisters
172 }
173 } else {
174 $days = floatval($days);
175 }
176
177 // Dump everything here
178 $nondefaults = array();
179
180 wfAppendToArrayIfNotDefault( 'days' , $days , $defaults, $nondefaults);
181 wfAppendToArrayIfNotDefault( 'hideMinor', (int)$hideMinor, $defaults, $nondefaults );
182 wfAppendToArrayIfNotDefault( 'hideBots' , (int)$hideBots , $defaults, $nondefaults);
183 wfAppendToArrayIfNotDefault( 'hideAnons', (int)$hideAnons, $defaults, $nondefaults );
184 wfAppendToArrayIfNotDefault( 'hideLiu' , (int)$hideLiu , $defaults, $nondefaults );
185 wfAppendToArrayIfNotDefault( 'hideOwn' , (int)$hideOwn , $defaults, $nondefaults);
186 wfAppendToArrayIfNotDefault( 'namespace', $nameSpace , $defaults, $nondefaults);
187 wfAppendToArrayIfNotDefault( 'hidePatrolled', (int)$hidePatrolled, $defaults, $nondefaults );
188
189 if( $nitems == 0 ) {
190 $wgOut->addWikiMsg( 'nowatchlist' );
191 return;
192 }
193
194 # Possible where conditions
195 $conds = array();
196
197 if( $days > 0 ) {
198 $conds[] = "rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
199 }
200
201 # If the watchlist is relatively short, it's simplest to zip
202 # down its entirety and then sort the results.
203
204 # If it's relatively long, it may be worth our while to zip
205 # through the time-sorted page list checking for watched items.
206
207 # Up estimate of watched items by 15% to compensate for talk pages...
208
209 # Toggles
210 if( $hideOwn ) {
211 $conds[] = "rc_user != $uid";
212 }
213 if( $hideBots ) {
214 $conds[] = 'rc_bot = 0';
215 }
216 if( $hideMinor ) {
217 $conds[] = 'rc_minor = 0';
218 }
219 if( $hideLiu ) {
220 $conds[] = 'rc_user = 0';
221 }
222 if( $hideAnons ) {
223 $conds[] = 'rc_user != 0';
224 }
225 if ( $wgUser->useRCPatrol() && $hidePatrolled ) {
226 $conds[] = 'rc_patrolled != 1';
227 }
228 if( $nameSpaceClause ) {
229 $conds[] = $nameSpaceClause;
230 }
231
232 # Toggle watchlist content (all recent edits or just the latest)
233 if( $wgUser->getOption( 'extendwatchlist' )) {
234 $limitWatchlist = intval( $wgUser->getOption( 'wllimit' ) );
235 $usePage = false;
236 } else {
237 # Top log Ids for a page are not stored
238 $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG;
239 $limitWatchlist = 0;
240 $usePage = true;
241 }
242
243 # Show a message about slave lag, if applicable
244 if( ( $lag = $dbr->getLag() ) > 0 )
245 $wgOut->showLagWarning( $lag );
246
247 # Create output form
248 $form = Xml::fieldset( wfMsg( 'watchlist-options' ), false, array( 'id' => 'mw-watchlist-options' ) );
249
250 # Show watchlist header
251 $form .= wfMsgExt( 'watchlist-details', array( 'parseinline' ), $wgLang->formatNum( $nitems ) );
252
253 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
254 $form .= wfMsgExt( 'wlheader-enotif', 'parse' ) . "\n";
255 }
256 if( $wgShowUpdatedMarker ) {
257 $form .= Xml::openElement( 'form', array( 'method' => 'post',
258 'action' => $this->getTitle()->getLocalUrl(),
259 'id' => 'mw-watchlist-resetbutton' ) ) .
260 wfMsgExt( 'wlheader-showupdated', array( 'parseinline' ) ) . ' ' .
261 Xml::submitButton( wfMsg( 'enotif_reset' ), array( 'name' => 'dummy' ) ) .
262 Html::hidden( 'reset', 'all' ) .
263 Xml::closeElement( 'form' );
264 }
265 $form .= '<hr />';
266
267 $tables = array( 'recentchanges', 'watchlist' );
268 $fields = array( "{$recentchanges}.*" );
269 $join_conds = array(
270 'watchlist' => array('INNER JOIN',"wl_user='{$uid}' AND wl_namespace=rc_namespace AND wl_title=rc_title"),
271 );
272 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
273 if( $wgShowUpdatedMarker ) {
274 $fields[] = 'wl_notificationtimestamp';
275 }
276 if( $limitWatchlist ) {
277 $options['LIMIT'] = $limitWatchlist;
278 }
279
280 $rollbacker = $wgUser->isAllowed('rollback');
281 if ( $usePage || $rollbacker ) {
282 $tables[] = 'page';
283 $join_conds['page'] = array('LEFT JOIN','rc_cur_id=page_id');
284 if ($rollbacker)
285 $fields[] = 'page_latest';
286 }
287
288 ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
289 wfRunHooks('SpecialWatchlistQuery', array(&$conds,&$tables,&$join_conds,&$fields) );
290
291 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
292 $numRows = $dbr->numRows( $res );
293
294 /* Start bottom header */
295
296 $wlInfo = '';
297 if( $days >= 1 ) {
298 $wlInfo = wfMsgExt( 'rcnote', 'parseinline',
299 $wgLang->formatNum( $numRows ),
300 $wgLang->formatNum( $days ),
301 $wgLang->timeAndDate( wfTimestampNow(), true ),
302 $wgLang->date( wfTimestampNow(), true ),
303 $wgLang->time( wfTimestampNow(), true )
304 ) . '<br />';
305 } elseif( $days > 0 ) {
306 $wlInfo = wfMsgExt( 'wlnote', 'parseinline',
307 $wgLang->formatNum( $numRows ),
308 $wgLang->formatNum( round($days*24) )
309 ) . '<br />';
310 }
311
312 $cutofflinks = "\n" . self::cutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n";
313
314 $thisTitle = SpecialPage::getTitleFor( 'Watchlist' );
315
316 # Spit out some control panel links
317 $links[] = self::showHideLink( $nondefaults, 'rcshowhideminor', 'hideMinor', $hideMinor );
318 $links[] = self::showHideLink( $nondefaults, 'rcshowhidebots', 'hideBots', $hideBots );
319 $links[] = self::showHideLink( $nondefaults, 'rcshowhideanons', 'hideAnons', $hideAnons );
320 $links[] = self::showHideLink( $nondefaults, 'rcshowhideliu', 'hideLiu', $hideLiu );
321 $links[] = self::showHideLink( $nondefaults, 'rcshowhidemine', 'hideOwn', $hideOwn );
322
323 if( $wgUser->useRCPatrol() ) {
324 $links[] = self::showHideLink( $nondefaults, 'rcshowhidepatr', 'hidePatrolled', $hidePatrolled );
325 }
326
327 # Namespace filter and put the whole form together.
328 $form .= $wlInfo;
329 $form .= $cutofflinks;
330 $form .= $wgLang->pipeList( $links );
331 $form .= Xml::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl(), 'id' => 'mw-watchlist-form-namespaceselector' ) );
332 $form .= '<hr /><p>';
333 $form .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&#160;';
334 $form .= Xml::namespaceSelector( $nameSpace, '' ) . '&#160;';
335 $form .= Xml::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $invert ) . '&#160;';
336 $form .= Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
337 $form .= Html::hidden( 'days', $days );
338 if( $hideMinor )
339 $form .= Html::hidden( 'hideMinor', 1 );
340 if( $hideBots )
341 $form .= Html::hidden( 'hideBots', 1 );
342 if( $hideAnons )
343 $form .= Html::hidden( 'hideAnons', 1 );
344 if( $hideLiu )
345 $form .= Html::hidden( 'hideLiu', 1 );
346 if( $hideOwn )
347 $form .= Html::hidden( 'hideOwn', 1 );
348 $form .= Xml::closeElement( 'form' );
349 $form .= Xml::closeElement( 'fieldset' );
350 $wgOut->addHTML( $form );
351
352 # If there's nothing to show, stop here
353 if( $numRows == 0 ) {
354 $wgOut->addWikiMsg( 'watchnochange' );
355 return;
356 }
357
358 /* End bottom header */
359
360 /* Do link batch query */
361 $linkBatch = new LinkBatch;
362 foreach ( $res as $row ) {
363 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
364 if ( $row->rc_user != 0 ) {
365 $linkBatch->add( NS_USER, $userNameUnderscored );
366 }
367 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
368
369 $linkBatch->add( $row->rc_namespace, $row->rc_title );
370 }
371 $linkBatch->execute();
372 $dbr->dataSeek( $res, 0 );
373
374 $list = ChangesList::newFromUser( $wgUser );
375 $list->setWatchlistDivs();
376
377 $s = $list->beginRecentChangesList();
378 $counter = 1;
379 foreach ( $res as $obj ) {
380 # Make RC entry
381 $rc = RecentChange::newFromRow( $obj );
382 $rc->counter = $counter++;
383
384 if ( $wgShowUpdatedMarker ) {
385 $updated = $obj->wl_notificationtimestamp;
386 } else {
387 $updated = false;
388 }
389
390 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
391 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
392 'COUNT(*)',
393 array(
394 'wl_namespace' => $obj->rc_namespace,
395 'wl_title' => $obj->rc_title,
396 ),
397 __METHOD__ );
398 } else {
399 $rc->numberofWatchingusers = 0;
400 }
401
402 $s .= $list->recentChangesLine( $rc, $updated, $counter );
403 }
404 $s .= $list->endRecentChangesList();
405
406 $wgOut->addHTML( $s );
407 }
408
409 public static function showHideLink( $options, $message, $name, $value ) {
410 global $wgUser;
411
412 $showLinktext = wfMsgHtml( 'show' );
413 $hideLinktext = wfMsgHtml( 'hide' );
414 $title = SpecialPage::getTitleFor( 'Watchlist' );
415 $skin = $wgUser->getSkin();
416
417 $label = $value ? $showLinktext : $hideLinktext;
418 $options[$name] = 1 - (int) $value;
419
420 return wfMsgHtml( $message, $skin->linkKnown( $title, $label, array(), $options ) );
421 }
422
423
424 public static function hoursLink( $h, $page, $options = array() ) {
425 global $wgUser, $wgLang, $wgContLang;
426
427 $sk = $wgUser->getSkin();
428 $title = Title::newFromText( $wgContLang->specialPage( $page ) );
429 $options['days'] = ($h / 24.0);
430
431 $s = $sk->linkKnown(
432 $title,
433 $wgLang->formatNum( $h ),
434 array(),
435 $options
436 );
437
438 return $s;
439 }
440
441 public static function daysLink( $d, $page, $options = array() ) {
442 global $wgUser, $wgLang, $wgContLang;
443
444 $sk = $wgUser->getSkin();
445 $title = Title::newFromText( $wgContLang->specialPage( $page ) );
446 $options['days'] = $d;
447 $message = ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) );
448
449 $s = $sk->linkKnown(
450 $title,
451 $message,
452 array(),
453 $options
454 );
455
456 return $s;
457 }
458
459 /**
460 * Returns html
461 */
462 protected static function cutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
463 global $wgLang;
464
465 $hours = array( 1, 2, 6, 12 );
466 $days = array( 1, 3, 7 );
467 $i = 0;
468 foreach( $hours as $h ) {
469 $hours[$i++] = self::hoursLink( $h, $page, $options );
470 }
471 $i = 0;
472 foreach( $days as $d ) {
473 $days[$i++] = self::daysLink( $d, $page, $options );
474 }
475 return wfMsgExt('wlshowlast',
476 array('parseinline', 'replaceafter'),
477 $wgLang->pipeList( $hours ),
478 $wgLang->pipeList( $days ),
479 self::daysLink( 0, $page, $options ) );
480 }
481
482 /**
483 * Count the number of items on a user's watchlist
484 *
485 * @param $user User object
486 * @param $talk Boolean: include talk pages
487 * @return Integer
488 */
489 protected static function countItems( &$user, $talk = true ) {
490 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
491
492 # Fetch the raw count
493 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count',
494 array( 'wl_user' => $user->mId ), __METHOD__ );
495 $row = $dbr->fetchObject( $res );
496 $count = $row->count;
497
498 # Halve to remove talk pages if needed
499 if( !$talk )
500 $count = floor( $count / 2 );
501
502 return( $count );
503 }
504 }