* Ensure that rate-limiting is applied to rollbacks.
[lhc/web/wiklou.git] / includes / SpecialWatchlist.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 *
9 */
10 require_once( dirname(__FILE__) . '/SpecialRecentchanges.php' );
11
12 /**
13 * Constructor
14 *
15 * @param $par Parameter passed to the page
16 */
17 function wfSpecialWatchlist( $par ) {
18 global $wgUser, $wgOut, $wgLang, $wgRequest;
19 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
20 global $wgEnotifWatchlist;
21 $fname = 'wfSpecialWatchlist';
22
23 $skin = $wgUser->getSkin();
24 $specialTitle = SpecialPage::getTitleFor( 'Watchlist' );
25 $wgOut->setRobotPolicy( 'noindex,nofollow' );
26
27 # Anons don't get a watchlist
28 if( $wgUser->isAnon() ) {
29 $wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
30 $llink = $skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Userlogin' ), wfMsgHtml( 'loginreqlink' ), 'returnto=' . $specialTitle->getPrefixedUrl() );
31 $wgOut->addHtml( wfMsgWikiHtml( 'watchlistanontext', $llink ) );
32 return;
33 }
34
35 $wgOut->setPageTitle( wfMsg( 'watchlist' ) );
36
37 $sub = wfMsgExt( 'watchlistfor', 'parseinline', $wgUser->getName() );
38 $sub .= '<br />' . WatchlistEditor::buildTools( $wgUser->getSkin() );
39 $wgOut->setSubtitle( $sub );
40
41 if( ( $mode = WatchlistEditor::getMode( $wgRequest, $par ) ) !== false ) {
42 $editor = new WatchlistEditor();
43 $editor->execute( $wgUser, $wgOut, $wgRequest, $mode );
44 return;
45 }
46
47 $uid = $wgUser->getId();
48 if( $wgEnotifWatchlist && $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
49 $wgUser->clearAllNotifications( $uid );
50 $wgOut->redirect( $specialTitle->getFullUrl() );
51 return;
52 }
53
54 $defaults = array(
55 /* float */ 'days' => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
56 /* bool */ 'hideOwn' => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
57 /* bool */ 'hideBots' => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
58 /* bool */ 'hideMinor' => (int)$wgUser->getBoolOption( 'watchlisthideminor' ),
59 /* ? */ 'namespace' => 'all',
60 );
61
62 extract($defaults);
63
64 # Extract variables from the request, falling back to user preferences or
65 # other default values if these don't exist
66 $prefs['days' ] = floatval( $wgUser->getOption( 'watchlistdays' ) );
67 $prefs['hideown' ] = $wgUser->getBoolOption( 'watchlisthideown' );
68 $prefs['hidebots'] = $wgUser->getBoolOption( 'watchlisthidebots' );
69 $prefs['hideminor'] = $wgUser->getBoolOption( 'watchlisthideminor' );
70
71 # Get query variables
72 $days = $wgRequest->getVal( 'days', $prefs['days'] );
73 $hideOwn = $wgRequest->getBool( 'hideOwn', $prefs['hideown'] );
74 $hideBots = $wgRequest->getBool( 'hideBots', $prefs['hidebots'] );
75 $hideMinor = $wgRequest->getBool( 'hideMinor', $prefs['hideminor'] );
76
77 # Get namespace value, if supplied, and prepare a WHERE fragment
78 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
79 if( !is_null( $nameSpace ) ) {
80 $nameSpace = intval( $nameSpace );
81 $nameSpaceClause = " AND rc_namespace = $nameSpace";
82 } else {
83 $nameSpace = '';
84 $nameSpaceClause = '';
85 }
86
87 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
88 list( $page, $watchlist, $recentchanges ) = $dbr->tableNamesN( 'page', 'watchlist', 'recentchanges' );
89
90 $sql = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_user=$uid";
91 $res = $dbr->query( $sql, $fname );
92 $s = $dbr->fetchObject( $res );
93
94 # Patch *** A1 *** (see A2 below)
95 # adjust for page X, talk:page X, which are both stored separately, but treated together
96 $nitems = floor($s->n / 2);
97 # $nitems = $s->n;
98
99 if( is_null($days) || !is_numeric($days) ) {
100 $big = 1000; /* The magical big */
101 if($nitems > $big) {
102 # Set default cutoff shorter
103 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
104 } else {
105 $days = $defaults['days']; # default cutoff for shortlisters
106 }
107 } else {
108 $days = floatval($days);
109 }
110
111 // Dump everything here
112 $nondefaults = array();
113
114 wfAppendToArrayIfNotDefault('days' , $days , $defaults, $nondefaults);
115 wfAppendToArrayIfNotDefault('hideOwn' , (int)$hideOwn , $defaults, $nondefaults);
116 wfAppendToArrayIfNotDefault('hideBots' , (int)$hideBots, $defaults, $nondefaults);
117 wfAppendToArrayIfNotDefault( 'hideMinor', (int)$hideMinor, $defaults, $nondefaults );
118 wfAppendToArrayIfNotDefault('namespace', $nameSpace , $defaults, $nondefaults);
119
120 $hookSql = "";
121 if( ! wfRunHooks('BeforeWatchlist', array($nondefaults, $wgUser, &$hookSql)) ) {
122 return;
123 }
124
125 if($nitems == 0) {
126 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
127 return;
128 }
129
130 if ( $days <= 0 ) {
131 $andcutoff = '';
132 } else {
133 $andcutoff = "AND rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
134 /*
135 $sql = "SELECT COUNT(*) AS n FROM $page, $revision WHERE rev_timestamp>'$cutoff' AND page_id=rev_page";
136 $res = $dbr->query( $sql, $fname );
137 $s = $dbr->fetchObject( $res );
138 $npages = $s->n;
139 */
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 ? "AND (rc_user <> $uid)" : '';
152 $andHideBots = $hideBots ? "AND (rc_bot = 0)" : '';
153 $andHideMinor = $hideMinor ? 'AND rc_minor = 0' : '';
154
155 # Show watchlist header
156 $header = '';
157 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
158 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
159 }
160 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
161 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
162 }
163
164 # Toggle watchlist content (all recent edits or just the latest)
165 if( $wgUser->getOption( 'extendwatchlist' )) {
166 $andLatest='';
167 $limitWatchlist = 'LIMIT ' . intval( $wgUser->getOption( 'wllimit' ) );
168 } else {
169 $andLatest= 'AND rc_this_oldid=page_latest';
170 $limitWatchlist = '';
171 }
172
173 $header .= wfMsgExt( 'watchlist-details', array( 'parsemag' ), $wgLang->formatNum( $nitems ) );
174 $wgOut->addWikiText( $header );
175
176 # Show a message about slave lag, if applicable
177 if( ( $lag = $dbr->getLag() ) > 0 )
178 $wgOut->showLagWarning( $lag );
179
180 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
181 $wgOut->addHTML( '<form action="' .
182 $specialTitle->escapeLocalUrl() .
183 '" method="post"><input type="submit" name="dummy" value="' .
184 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
185 '" /><input type="hidden" name="reset" value="all" /></form>' .
186 "\n\n" );
187 }
188 if ( $wgShowUpdatedMarker ) {
189 $wltsfield=", ${watchlist}.wl_notificationtimestamp ";
190 }
191 $sql = "SELECT ${recentchanges}.* ${wltsfield}
192 FROM $watchlist,$recentchanges,$page
193 WHERE wl_user=$uid
194 AND wl_namespace=rc_namespace
195 AND wl_title=rc_title
196 AND rc_cur_id=page_id
197 $andcutoff
198 $andLatest
199 $andHideOwn
200 $andHideBots
201 $andHideMinor
202 $nameSpaceClause
203 $hookSql
204 ORDER BY rc_timestamp DESC
205 $limitWatchlist";
206
207 $res = $dbr->query( $sql, $fname );
208 $numRows = $dbr->numRows( $res );
209
210 /* Start bottom header */
211 $wgOut->addHTML( "<hr />\n" );
212
213 if($days >= 1) {
214 $wgOut->addWikiText( wfMsgExt( 'rcnote', array( 'parseinline' ), $wgLang->formatNum( $numRows ),
215 $wgLang->formatNum( $days ), $wgLang->timeAndDate( wfTimestampNow(), true ) ) . '<br />' , false );
216 } elseif($days > 0) {
217 $wgOut->addWikiText( wfMsgExt( 'wlnote', array( 'parseinline' ), $wgLang->formatNum( $numRows ),
218 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
219 }
220
221 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
222
223 # Spit out some control panel links
224 $thisTitle = SpecialPage::getTitleFor( 'Watchlist' );
225 $skin = $wgUser->getSkin();
226
227 # Hide/show bot edits
228 $label = $hideBots ? wfMsgHtml( 'watchlist-show-bots' ) : wfMsgHtml( 'watchlist-hide-bots' );
229 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
230 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
231
232 # Hide/show own edits
233 $label = $hideOwn ? wfMsgHtml( 'watchlist-show-own' ) : wfMsgHtml( 'watchlist-hide-own' );
234 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
235 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
236
237 # Hide/show minor edits
238 $label = $hideMinor ? wfMsgHtml( 'watchlist-show-minor' ) : wfMsgHtml( 'watchlist-hide-minor' );
239 $linkBits = wfArrayToCGI( array( 'hideMinor' => 1 - (int)$hideMinor ), $nondefaults );
240 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
241
242 $wgOut->addHTML( implode( ' | ', $links ) );
243
244 # Form for namespace filtering
245 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl() ) );
246 $form .= '<p>';
247 $form .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&nbsp;';
248 $form .= Xml::namespaceSelector( $nameSpace, '' ) . '&nbsp;';
249 $form .= Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
250 $form .= Xml::hidden( 'days', $days );
251 if( $hideOwn )
252 $form .= Xml::hidden( 'hideOwn', 1 );
253 if( $hideBots )
254 $form .= Xml::hidden( 'hideBots', 1 );
255 if( $hideMinor )
256 $form .= Xml::hidden( 'hideMinor', 1 );
257 $form .= Xml::closeElement( 'form' );
258 $wgOut->addHtml( $form );
259
260 # If there's nothing to show, stop here
261 if( $numRows == 0 ) {
262 $wgOut->addWikiText( wfMsgNoTrans( 'watchnochange' ) );
263 return;
264 }
265
266 /* End bottom header */
267
268 /* Do link batch query */
269 $linkBatch = new LinkBatch;
270 while ( $row = $dbr->fetchObject( $res ) ) {
271 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
272 if ( $row->rc_user != 0 ) {
273 $linkBatch->add( NS_USER, $userNameUnderscored );
274 }
275 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
276 }
277 $linkBatch->execute();
278 $dbr->dataSeek( $res, 0 );
279
280 $list = ChangesList::newFromUser( $wgUser );
281
282 $s = $list->beginRecentChangesList();
283 $counter = 1;
284 while ( $obj = $dbr->fetchObject( $res ) ) {
285 # Make RC entry
286 $rc = RecentChange::newFromRow( $obj );
287 $rc->counter = $counter++;
288
289 if ( $wgShowUpdatedMarker ) {
290 $updated = $obj->wl_notificationtimestamp;
291 } else {
292 // Same visual appearance as MW 1.4
293 $updated = true;
294 }
295
296 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
297 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" .$dbr->strencode($obj->page_title). "' AND wl_namespace='{$obj->page_namespace}'" ;
298 $res3 = $dbr->query( $sql3, $fname );
299 $x = $dbr->fetchObject( $res3 );
300 $rc->numberofWatchingusers = $x->n;
301 } else {
302 $rc->numberofWatchingusers = 0;
303 }
304
305 $s .= $list->recentChangesLine( $rc, $updated );
306 }
307 $s .= $list->endRecentChangesList();
308
309 $dbr->freeResult( $res );
310 $wgOut->addHTML( $s );
311
312 }
313
314 function wlHoursLink( $h, $page, $options = array() ) {
315 global $wgUser, $wgLang, $wgContLang;
316 $sk = $wgUser->getSkin();
317 $s = $sk->makeKnownLink(
318 $wgContLang->specialPage( $page ),
319 $wgLang->formatNum( $h ),
320 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
321 return $s;
322 }
323
324 function wlDaysLink( $d, $page, $options = array() ) {
325 global $wgUser, $wgLang, $wgContLang;
326 $sk = $wgUser->getSkin();
327 $s = $sk->makeKnownLink(
328 $wgContLang->specialPage( $page ),
329 ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
330 wfArrayToCGI( array('days' => $d), $options ) );
331 return $s;
332 }
333
334 /**
335 * Returns html
336 */
337 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
338 $hours = array( 1, 2, 6, 12 );
339 $days = array( 1, 3, 7 );
340 $i = 0;
341 foreach( $hours as $h ) {
342 $hours[$i++] = wlHoursLink( $h, $page, $options );
343 }
344 $i = 0;
345 foreach( $days as $d ) {
346 $days[$i++] = wlDaysLink( $d, $page, $options );
347 }
348 return wfMsgExt('wlshowlast',
349 array('parseinline', 'replaceafter'),
350 implode(' | ', $hours),
351 implode(' | ', $days),
352 wlDaysLink( 0, $page, $options ) );
353 }
354
355 /**
356 * Count the number of items on a user's watchlist
357 *
358 * @param $talk Include talk pages
359 * @return integer
360 */
361 function wlCountItems( &$user, $talk = true ) {
362 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
363
364 # Fetch the raw count
365 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId ), 'wlCountItems' );
366 $row = $dbr->fetchObject( $res );
367 $count = $row->count;
368 $dbr->freeResult( $res );
369
370 # Halve to remove talk pages if needed
371 if( !$talk )
372 $count = floor( $count / 2 );
373
374 return( $count );
375 }