summaryrefslogtreecommitdiff
path: root/includes/specials/SpecialWatchlist.php
blob: c32af2ae39c6a0caee607809e4dad9111691e4bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php
/**
 * @file
 * @ingroup SpecialPage Watchlist
 */

/**
 * Constructor
 *
 * @param $par Parameter passed to the page
 */
function wfSpecialWatchlist( $par ) {
	global $wgUser, $wgOut, $wgLang, $wgRequest;
	global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
	
	// Add feed links
	$wlToken = $wgUser->getOption( 'watchlisttoken' );
	if (!$wlToken) {
		$wlToken = sha1( mt_rand() . microtime( true ) );
		$wgUser->setOption( 'watchlisttoken', $wlToken );
		$wgUser->saveSettings();
	}
	
	global $wgServer, $wgScriptPath, $wgFeedClasses;
	$apiParams = array( 'action' => 'feedwatchlist', 'allrev' => 'allrev',
						'wlowner' => $wgUser->getName(), 'wltoken' => $wlToken );
	$feedTemplate = wfScript('api').'?';
	
	foreach( $wgFeedClasses as $format => $class ) {
		$theseParams = $apiParams + array( 'feedformat' => $format );
		$url = $feedTemplate . wfArrayToCGI( $theseParams );
		$wgOut->addFeedLink( $format, $url );
	}

	$skin = $wgUser->getSkin();
	$specialTitle = SpecialPage::getTitleFor( 'Watchlist' );
	$wgOut->setRobotPolicy( 'noindex,nofollow' );

	# Anons don't get a watchlist
	if( $wgUser->isAnon() ) {
		$wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
		$llink = $skin->linkKnown(
			SpecialPage::getTitleFor( 'Userlogin' ), 
			wfMsgHtml( 'loginreqlink' ),
			array(),
			array( 'returnto' => $specialTitle->getPrefixedText() )
		);
		$wgOut->addHTML( wfMsgWikiHtml( 'watchlistanontext', $llink ) );
		return;
	}

	$wgOut->setPageTitle( wfMsg( 'watchlist' ) );

	$sub  = wfMsgExt( 'watchlistfor', 'parseinline', $wgUser->getName() );
	$sub .= '<br />' . WatchlistEditor::buildTools( $wgUser->getSkin() );
	$wgOut->setSubtitle( $sub );

	if( ( $mode = WatchlistEditor::getMode( $wgRequest, $par ) ) !== false ) {
		$editor = new WatchlistEditor();
		$editor->execute( $wgUser, $wgOut, $wgRequest, $mode );
		return;
	}

	$uid = $wgUser->getId();
	if( ($wgEnotifWatchlist || $wgShowUpdatedMarker) && $wgRequest->getVal( 'reset' ) && 
		$wgRequest->wasPosted() )
	{
		$wgUser->clearAllNotifications( $uid );
		$wgOut->redirect( $specialTitle->getFullUrl() );
		return;
	}

	$defaults = array(
	/* float */ 'days'      => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
	/* bool  */ 'hideMinor' => (int)$wgUser->getBoolOption( 'watchlisthideminor' ),
	/* bool  */ 'hideBots'  => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
	/* bool  */ 'hideAnons' => (int)$wgUser->getBoolOption( 'watchlisthideanons' ),
	/* bool  */ 'hideLiu'   => (int)$wgUser->getBoolOption( 'watchlisthideliu' ),
	/* bool  */ 'hidePatrolled' => (int)$wgUser->getBoolOption( 'watchlisthidepatrolled' ),
	/* bool  */ 'hideOwn'   => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
	/* ?     */ 'namespace' => 'all',
	/* ?     */ 'invert'    => false,
	);

	extract($defaults);

	# Extract variables from the request, falling back to user preferences or
	# other default values if these don't exist
	$prefs['days']      = floatval( $wgUser->getOption( 'watchlistdays' ) );
	$prefs['hideminor'] = $wgUser->getBoolOption( 'watchlisthideminor' );
	$prefs['hidebots']  = $wgUser->getBoolOption( 'watchlisthidebots' );
	$prefs['hideanons'] = $wgUser->getBoolOption( 'watchlisthideanon' );
	$prefs['hideliu']   = $wgUser->getBoolOption( 'watchlisthideliu' );
	$prefs['hideown' ]  = $wgUser->getBoolOption( 'watchlisthideown' );
	$prefs['hidepatrolled' ] = $wgUser->getBoolOption( 'watchlisthidepatrolled' );

	# Get query variables
	$days      = $wgRequest->getVal(  'days'     , $prefs['days'] );
	$hideMinor = $wgRequest->getBool( 'hideMinor', $prefs['hideminor'] );
	$hideBots  = $wgRequest->getBool( 'hideBots' , $prefs['hidebots'] );
	$hideAnons = $wgRequest->getBool( 'hideAnons', $prefs['hideanons'] );
	$hideLiu   = $wgRequest->getBool( 'hideLiu'  , $prefs['hideliu'] );
	$hideOwn   = $wgRequest->getBool( 'hideOwn'  , $prefs['hideown'] );
	$hidePatrolled   = $wgRequest->getBool( 'hidePatrolled'  , $prefs['hidepatrolled'] );

	# Get namespace value, if supplied, and prepare a WHERE fragment
	$nameSpace = $wgRequest->getIntOrNull( 'namespace' );
	$invert = $wgRequest->getIntOrNull( 'invert' );
	if( !is_null( $nameSpace ) ) {
		$nameSpace = intval( $nameSpace );
		if( $invert && $nameSpace !== 'all' )
			$nameSpaceClause = "rc_namespace != $nameSpace";
		else
			$nameSpaceClause = "rc_namespace = $nameSpace";
	} else {
		$nameSpace = '';
		$nameSpaceClause = '';
	}

	$dbr = wfGetDB( DB_SLAVE, 'watchlist' );
	$recentchanges = $dbr->tableName( 'recentchanges' );

	$watchlistCount = $dbr->selectField( 'watchlist', 'COUNT(*)',
		array( 'wl_user' => $uid ), __METHOD__ );
	// Adjust for page X, talk:page X, which are both stored separately,
	// but treated together
	$nitems = floor($watchlistCount / 2);

	if( is_null($days) || !is_numeric($days) ) {
		$big = 1000; /* The magical big */
		if($nitems > $big) {
			# Set default cutoff shorter
			$days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
		} else {
			$days = $defaults['days']; # default cutoff for shortlisters
		}
	} else {
		$days = floatval($days);
	}

	// Dump everything here
	$nondefaults = array();

	wfAppendToArrayIfNotDefault( 'days'     , $days          , $defaults, $nondefaults);
	wfAppendToArrayIfNotDefault( 'hideMinor', (int)$hideMinor, $defaults, $nondefaults );
	wfAppendToArrayIfNotDefault( 'hideBots' , (int)$hideBots , $defaults, $nondefaults);
	wfAppendToArrayIfNotDefault( 'hideAnons', (int)$hideAnons, $defaults, $nondefaults );
	wfAppendToArrayIfNotDefault( 'hideLiu'  , (int)$hideLiu  , $defaults, $nondefaults );
	wfAppendToArrayIfNotDefault( 'hideOwn'  , (int)$hideOwn  , $defaults, $nondefaults);
	wfAppendToArrayIfNotDefault( 'namespace', $nameSpace     , $defaults, $nondefaults);
	wfAppendToArrayIfNotDefault( 'hidePatrolled', (int)$hidePatrolled, $defaults, $nondefaults );

	if( $nitems == 0 ) {
		$wgOut->addWikiMsg( 'nowatchlist' );
		return;
	}

	if( $days <= 0 ) {
		$andcutoff = '';
	} else {
		$andcutoff = "rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
	}

	# If the watchlist is relatively short, it's simplest to zip
	# down its entirety and then sort the results.

	# If it's relatively long, it may be worth our while to zip
	# through the time-sorted page list checking for watched items.

	# Up estimate of watched items by 15% to compensate for talk pages...

	# Toggles
	$andHideOwn   = $hideOwn   ? "rc_user != $uid" : '';
	$andHideBots  = $hideBots  ? "rc_bot = 0" : '';
	$andHideMinor = $hideMinor ? "rc_minor = 0" : '';
	$andHideLiu   = $hideLiu   ? "rc_user = 0" : '';
	$andHideAnons = $hideAnons ? "rc_user != 0" : '';
	$andHidePatrolled = $wgUser->useRCPatrol() && $hidePatrolled ? "rc_patrolled != 1" : '';

	# Toggle watchlist content (all recent edits or just the latest)
	if( $wgUser->getOption( 'extendwatchlist' )) {
		$andLatest='';
 		$limitWatchlist = intval( $wgUser->getOption( 'wllimit' ) );
		$usePage = false;
	} else {
	# Top log Ids for a page are not stored
		$andLatest = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG;
		$limitWatchlist = 0;
		$usePage = true;
	}

	# Show a message about slave lag, if applicable
	if( ( $lag = $dbr->getLag() ) > 0 )
		$wgOut->showLagWarning( $lag );

	# Create output form
	$form  = Xml::fieldset( wfMsg( 'watchlist-options' ), false, array( 'id' => 'mw-watchlist-options' ) );

	# Show watchlist header
	$form .= wfMsgExt( 'watchlist-details', array( 'parseinline' ), $wgLang->formatNum( $nitems ) );

	if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
		$form .= wfMsgExt( 'wlheader-enotif', 'parse' ) . "\n";
	}
	if( $wgShowUpdatedMarker ) {
		$form .= Xml::openElement( 'form', array( 'method' => 'post',
					'action' => $specialTitle->getLocalUrl(),
					'id' => 'mw-watchlist-resetbutton' ) ) .
				wfMsgExt( 'wlheader-showupdated', array( 'parseinline' ) ) . ' ' .
				Xml::submitButton( wfMsg( 'enotif_reset' ), array( 'name' => 'dummy' ) ) .
				Xml::hidden( 'reset', 'all' ) .
				Xml::closeElement( 'form' );
	}
	$form .= '<hr />';
	
	$tables = array( 'recentchanges', 'watchlist' );
	$fields = array( "{$recentchanges}.*" );
	$conds = array();
	$join_conds = array(
		'watchlist' => array('INNER JOIN',"wl_user='{$uid}' AND wl_namespace=rc_namespace AND wl_title=rc_title"),
	);
	$options = array( 'ORDER BY' => 'rc_timestamp DESC' );
	if( $wgShowUpdatedMarker ) {
		$fields[] = 'wl_notificationtimestamp';
	}
	if( $limitWatchlist ) {
		$options['LIMIT'] = $limitWatchlist;
	}
	if( $andcutoff ) $conds[] = $andcutoff;
	if( $andLatest ) $conds[] = $andLatest;
	if( $andHideOwn ) $conds[] = $andHideOwn;
	if( $andHideBots ) $conds[] = $andHideBots;
	if( $andHideMinor ) $conds[] = $andHideMinor;
	if( $andHideLiu ) $conds[] = $andHideLiu;
	if( $andHideAnons ) $conds[] = $andHideAnons;
	if( $andHidePatrolled ) $conds[] = $andHidePatrolled;
	if( $nameSpaceClause ) $conds[] = $nameSpaceClause;

	$rollbacker = $wgUser->isAllowed('rollback');
	if ( $usePage || $rollbacker ) {
		$tables[] = 'page';
		$join_conds['page'] = array('LEFT JOIN','rc_cur_id=page_id');
		if ($rollbacker) 
			$fields[] = 'page_latest';
	}

	ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
	wfRunHooks('SpecialWatchlistQuery', array(&$conds,&$tables,&$join_conds,&$fields) );
	
	$res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
	$numRows = $dbr->numRows( $res );

	/* Start bottom header */

	$wlInfo = '';
	if( $days >= 1 ) {
		$wlInfo = wfMsgExt( 'rcnote', 'parseinline',
				$wgLang->formatNum( $numRows ),
				$wgLang->formatNum( $days ),
				$wgLang->timeAndDate( wfTimestampNow(), true ),
				$wgLang->date( wfTimestampNow(), true ),
				$wgLang->time( wfTimestampNow(), true )
			) . '<br />';
	} elseif( $days > 0 ) {
		$wlInfo = wfMsgExt( 'wlnote', 'parseinline',
				$wgLang->formatNum( $numRows ),
				$wgLang->formatNum( round($days*24) )
			) . '<br />';
	}

	$cutofflinks = "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n";

	$thisTitle = SpecialPage::getTitleFor( 'Watchlist' );

	# Spit out some control panel links
	$links[] = wlShowHideLink( $nondefaults, 'rcshowhideminor', 'hideMinor', $hideMinor );
	$links[] = wlShowHideLink( $nondefaults, 'rcshowhidebots', 'hideBots', $hideBots );
	$links[] = wlShowHideLink( $nondefaults, 'rcshowhideanons', 'hideAnons', $hideAnons );
	$links[] = wlShowHideLink( $nondefaults, 'rcshowhideliu', 'hideLiu', $hideLiu );
	$links[] = wlShowHideLink( $nondefaults, 'rcshowhidemine', 'hideOwn', $hideOwn );

	if( $wgUser->useRCPatrol() ) {
		$links[] = wlShowHideLink( $nondefaults, 'rcshowhidepatr', 'hidePatrolled', $hidePatrolled );
	}

	# Namespace filter and put the whole form together.
	$form .= $wlInfo;
	$form .= $cutofflinks;
	$form .= $wgLang->pipeList( $links );
	$form .= Xml::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl() ) );
	$form .= '<hr /><p>';
	$form .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&nbsp;';
	$form .= Xml::namespaceSelector( $nameSpace, '' ) . '&nbsp;';
	$form .= Xml::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $invert ) . '&nbsp;';
	$form .= Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
	$form .= Xml::hidden( 'days', $days );
	if( $hideMinor )
		$form .= Xml::hidden( 'hideMinor', 1 );
	if( $hideBots )
		$form .= Xml::hidden( 'hideBots', 1 );
	if( $hideAnons )
		$form .= Xml::hidden( 'hideAnons', 1 );
	if( $hideLiu )
		$form .= Xml::hidden( 'hideLiu', 1 );
	if( $hideOwn )
		$form .= Xml::hidden( 'hideOwn', 1 );
	$form .= Xml::closeElement( 'form' );
	$form .= Xml::closeElement( 'fieldset' );
	$wgOut->addHTML( $form );

	$wgOut->addHTML( ChangesList::flagLegend() );

	# If there's nothing to show, stop here
	if( $numRows == 0 ) {
		$wgOut->addWikiMsg( 'watchnochange' );
		return;
	}

	/* End bottom header */

	/* Do link batch query */
	$linkBatch = new LinkBatch;
	while ( $row = $dbr->fetchObject( $res ) ) {
		$userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
		if ( $row->rc_user != 0 ) {
			$linkBatch->add( NS_USER, $userNameUnderscored );
		}
		$linkBatch->add( NS_USER_TALK, $userNameUnderscored );

		$linkBatch->add( $row->rc_namespace, $row->rc_title );
	}
	$linkBatch->execute();
	$dbr->dataSeek( $res, 0 );

	$list = ChangesList::newFromUser( $wgUser );
	$list->setWatchlistDivs();
	
	$s = $list->beginRecentChangesList();
	$counter = 1;
	while ( $obj = $dbr->fetchObject( $res ) ) {
		# Make RC entry
		$rc = RecentChange::newFromRow( $obj );
		$rc->counter = $counter++;

		if ( $wgShowUpdatedMarker ) {
			$updated = $obj->wl_notificationtimestamp;
		} else {
			$updated = false;
		}

		if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
			$rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
				'COUNT(*)',
				array(
					'wl_namespace' => $obj->rc_namespace,
					'wl_title' => $obj->rc_title,
				),
				__METHOD__ );
		} else {
			$rc->numberofWatchingusers = 0;
		}

		$s .= $list->recentChangesLine( $rc, $updated, $counter );
	}
	$s .= $list->endRecentChangesList();

	$dbr->freeResult( $res );
	$wgOut->addHTML( $s );
}

function wlShowHideLink( $options, $message, $name, $value ) {
	global $wgUser;

	$showLinktext = wfMsgHtml( 'show' );
	$hideLinktext = wfMsgHtml( 'hide' );
	$title = SpecialPage::getTitleFor( 'Watchlist' );
	$skin = $wgUser->getSkin();

	$label = $value ? $showLinktext : $hideLinktext;
	$options[$name] = 1 - (int) $value;

	return wfMsgHtml( $message, $skin->linkKnown( $title, $label, array(), $options ) );
}


function wlHoursLink( $h, $page, $options = array() ) {
	global $wgUser, $wgLang, $wgContLang;

	$sk = $wgUser->getSkin();
	$title = Title::newFromText( $wgContLang->specialPage( $page ) );
	$options['days'] = ($h / 24.0);

	$s = $sk->linkKnown(
		$title,
		$wgLang->formatNum( $h ),
		array(),
		$options
	);

	return $s;
}

function wlDaysLink( $d, $page, $options = array() ) {
	global $wgUser, $wgLang, $wgContLang;

	$sk = $wgUser->getSkin();
	$title = Title::newFromText( $wgContLang->specialPage( $page ) );
	$options['days'] = $d;
	$message = ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) );

	$s = $sk->linkKnown(
		$title,
		$message,
		array(),
		$options
	);

	return $s;
}

/**
 * Returns html
 */
function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
	global $wgLang;

	$hours = array( 1, 2, 6, 12 );
	$days = array( 1, 3, 7 );
	$i = 0;
	foreach( $hours as $h ) {
		$hours[$i++] = wlHoursLink( $h, $page, $options );
	}
	$i = 0;
	foreach( $days as $d ) {
		$days[$i++] = wlDaysLink( $d, $page, $options );
	}
	return wfMsgExt('wlshowlast',
		array('parseinline', 'replaceafter'),
		$wgLang->pipeList( $hours ),
		$wgLang->pipeList( $days ),
		wlDaysLink( 0, $page, $options ) );
}

/**
 * Count the number of items on a user's watchlist
 *
 * @param $talk Include talk pages
 * @return integer
 */
function wlCountItems( &$user, $talk = true ) {
	$dbr = wfGetDB( DB_SLAVE, 'watchlist' );

	# Fetch the raw count
	$res = $dbr->select( 'watchlist', 'COUNT(*) AS count', 
		array( 'wl_user' => $user->mId ), 'wlCountItems' );
	$row = $dbr->fetchObject( $res );
	$count = $row->count;
	$dbr->freeResult( $res );

	# Halve to remove talk pages if needed
	if( !$talk )
		$count = floor( $count / 2 );

	return( $count );
}