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