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