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