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