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