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