*Replace crappy BeforeWatchlist hook
[lhc/web/wiklou.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' ), // TODO
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 list( $page, $watchlist, $recentchanges ) = $dbr->tableNamesN( 'page', 'watchlist', '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 } else {
163 # Top log Ids for a page are not stored
164 $andLatest = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG;
165 $limitWatchlist = 0;
166 }
167
168 # Show a message about slave lag, if applicable
169 if( ( $lag = $dbr->getLag() ) > 0 )
170 $wgOut->showLagWarning( $lag );
171
172 # Create output form
173 $form = Xml::fieldset( wfMsg( 'watchlist-options' ), false, array( 'id' => 'mw-watchlist-options' ) );
174
175 # Show watchlist header
176 $form .= wfMsgExt( 'watchlist-details', array( 'parseinline' ), $wgLang->formatNum( $nitems ) );
177
178 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
179 $form .= wfMsgExt( 'wlheader-enotif', 'parse' ) . "\n";
180 }
181 if( $wgShowUpdatedMarker ) {
182 $form .= Xml::openElement( 'form', array( 'method' => 'post',
183 'action' => $specialTitle->getLocalUrl(),
184 'id' => 'mw-watchlist-resetbutton' ) ) .
185 wfMsgExt( 'wlheader-showupdated', array( 'parseinline' ) ) . ' ' .
186 Xml::submitButton( wfMsg( 'enotif_reset' ), array( 'name' => 'dummy' ) ) .
187 Xml::hidden( 'reset', 'all' ) .
188 Xml::closeElement( 'form' );
189 }
190 $form .= '<hr />';
191
192 $tables = array( 'recentchanges', 'watchlist', 'page' );
193 $fields = array( "{$recentchanges}.*" );
194 $conds = array();
195 $join_conds = array(
196 'watchlist' => array('INNER JOIN',"wl_user='{$uid}' AND wl_namespace=rc_namespace AND wl_title=rc_title"),
197 'page' => array('LEFT JOIN','rc_cur_id=page_id')
198 );
199 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
200 if( $wgShowUpdatedMarker ) {
201 $fields[] = 'wl_notificationtimestamp';
202 }
203 if( $limitWatchlist ) {
204 $options['LIMIT'] = $limitWatchlist;
205 }
206 if( $andcutoff ) $conds[] = $andcutoff;
207 if( $andLatest ) $conds[] = $andLatest;
208 if( $andHideOwn ) $conds[] = $andHideOwn;
209 if( $andHideBots ) $conds[] = $andHideBots;
210 if( $andHideMinor ) $conds[] = $andHideMinor;
211 if( $andHideLiu ) $conds[] = $andHideLiu;
212 if( $andHideAnons ) $conds[] = $andHideAnons;
213 if( $andHidePatrolled ) $conds[] = $andHidePatrolled;
214 if( $nameSpaceClause ) $conds[] = $nameSpaceClause;
215
216 wfRunHooks('SpecialWatchlistQuery', array(&$conds,&$tables,&$join_conds,&$fields) );
217
218 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
219 $numRows = $dbr->numRows( $res );
220
221 /* Start bottom header */
222
223 $wlInfo = '';
224 if( $days >= 1 ) {
225 $wlInfo = wfMsgExt( 'rcnote', 'parseinline',
226 $wgLang->formatNum( $numRows ),
227 $wgLang->formatNum( $days ),
228 $wgLang->timeAndDate( wfTimestampNow(), true ),
229 $wgLang->date( wfTimestampNow(), true ),
230 $wgLang->time( wfTimestampNow(), true )
231 ) . '<br />';
232 } elseif( $days > 0 ) {
233 $wlInfo = wfMsgExt( 'wlnote', 'parseinline',
234 $wgLang->formatNum( $numRows ),
235 $wgLang->formatNum( round($days*24) )
236 ) . '<br />';
237 }
238
239 $cutofflinks = "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n";
240
241 # Spit out some control panel links
242 $thisTitle = SpecialPage::getTitleFor( 'Watchlist' );
243 $skin = $wgUser->getSkin();
244
245 $showLinktext = wfMsgHtml( 'show' );
246 $hideLinktext = wfMsgHtml( 'hide' );
247 # Hide/show minor edits
248 $label = $hideMinor ? $showLinktext : $hideLinktext;
249 $linkBits = wfArrayToCGI( array( 'hideMinor' => 1 - (int)$hideMinor ), $nondefaults );
250 $links[] = wfMsgHtml( 'rcshowhideminor', $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits ) );
251
252 # Hide/show bot edits
253 $label = $hideBots ? $showLinktext : $hideLinktext;
254 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
255 $links[] = wfMsgHtml( 'rcshowhidebots', $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits ) );
256
257 # Hide/show anonymous edits
258 $label = $hideAnons ? $showLinktext : $hideLinktext;
259 $linkBits = wfArrayToCGI( array( 'hideAnons' => 1 - (int)$hideAnons ), $nondefaults );
260 $links[] = wfMsgHtml( 'rcshowhideanons', $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits ) );
261
262 # Hide/show logged in edits
263 $label = $hideLiu ? $showLinktext : $hideLinktext;
264 $linkBits = wfArrayToCGI( array( 'hideLiu' => 1 - (int)$hideLiu ), $nondefaults );
265 $links[] = wfMsgHtml( 'rcshowhideliu', $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits ) );
266
267 # Hide/show own edits
268 $label = $hideOwn ? $showLinktext : $hideLinktext;
269 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
270 $links[] = wfMsgHtml( 'rcshowhidemine', $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits ) );
271
272 # Hide/show patrolled edits
273 if( $wgUser->useRCPatrol() ) {
274 $label = $hidePatrolled ? $showLinktext : $hideLinktext;
275 $linkBits = wfArrayToCGI( array( 'hidePatrolled' => 1 - (int)$hidePatrolled ), $nondefaults );
276 $links[] = wfMsgHtml( 'rcshowhidepatr', $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits ) );
277 }
278
279 # Namespace filter and put the whole form together.
280 $form .= $wlInfo;
281 $form .= $cutofflinks;
282 $form .= implode( ' | ', $links );
283 $form .= Xml::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl() ) );
284 $form .= '<hr /><p>';
285 $form .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&nbsp;';
286 $form .= Xml::namespaceSelector( $nameSpace, '' ) . '&nbsp;';
287 $form .= Xml::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $invert ) . '&nbsp;';
288 $form .= Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
289 $form .= Xml::hidden( 'days', $days );
290 if( $hideMinor )
291 $form .= Xml::hidden( 'hideMinor', 1 );
292 if( $hideBots )
293 $form .= Xml::hidden( 'hideBots', 1 );
294 if( $hideAnons )
295 $form .= Xml::hidden( 'hideAnons', 1 );
296 if( $hideLiu )
297 $form .= Xml::hidden( 'hideLiu', 1 );
298 if( $hideOwn )
299 $form .= Xml::hidden( 'hideOwn', 1 );
300 $form .= Xml::closeElement( 'form' );
301 $form .= Xml::closeElement( 'fieldset' );
302 $wgOut->addHTML( $form );
303
304 # If there's nothing to show, stop here
305 if( $numRows == 0 ) {
306 $wgOut->addWikiMsg( 'watchnochange' );
307 return;
308 }
309
310 /* End bottom header */
311
312 /* Do link batch query */
313 $linkBatch = new LinkBatch;
314 while ( $row = $dbr->fetchObject( $res ) ) {
315 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
316 if ( $row->rc_user != 0 ) {
317 $linkBatch->add( NS_USER, $userNameUnderscored );
318 }
319 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
320 }
321 $linkBatch->execute();
322 $dbr->dataSeek( $res, 0 );
323
324 $list = ChangesList::newFromUser( $wgUser );
325
326 $s = $list->beginRecentChangesList();
327 $counter = 1;
328 while ( $obj = $dbr->fetchObject( $res ) ) {
329 # Make RC entry
330 $rc = RecentChange::newFromRow( $obj );
331 $rc->counter = $counter++;
332
333 if ( $wgShowUpdatedMarker ) {
334 $updated = $obj->wl_notificationtimestamp;
335 } else {
336 $updated = false;
337 }
338
339 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
340 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
341 'COUNT(*)',
342 array(
343 'wl_namespace' => $obj->rc_namespace,
344 'wl_title' => $obj->rc_title,
345 ),
346 __METHOD__ );
347 } else {
348 $rc->numberofWatchingusers = 0;
349 }
350
351 $s .= $list->recentChangesLine( $rc, $updated );
352 }
353 $s .= $list->endRecentChangesList();
354
355 $dbr->freeResult( $res );
356 $wgOut->addHTML( $s );
357 }
358
359 function wlHoursLink( $h, $page, $options = array() ) {
360 global $wgUser, $wgLang, $wgContLang;
361 $sk = $wgUser->getSkin();
362 $s = $sk->makeKnownLink(
363 $wgContLang->specialPage( $page ),
364 $wgLang->formatNum( $h ),
365 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
366 return $s;
367 }
368
369 function wlDaysLink( $d, $page, $options = array() ) {
370 global $wgUser, $wgLang, $wgContLang;
371 $sk = $wgUser->getSkin();
372 $s = $sk->makeKnownLink(
373 $wgContLang->specialPage( $page ),
374 ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
375 wfArrayToCGI( array('days' => $d), $options ) );
376 return $s;
377 }
378
379 /**
380 * Returns html
381 */
382 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
383 $hours = array( 1, 2, 6, 12 );
384 $days = array( 1, 3, 7 );
385 $i = 0;
386 foreach( $hours as $h ) {
387 $hours[$i++] = wlHoursLink( $h, $page, $options );
388 }
389 $i = 0;
390 foreach( $days as $d ) {
391 $days[$i++] = wlDaysLink( $d, $page, $options );
392 }
393 return wfMsgExt('wlshowlast',
394 array('parseinline', 'replaceafter'),
395 implode(' | ', $hours),
396 implode(' | ', $days),
397 wlDaysLink( 0, $page, $options ) );
398 }
399
400 /**
401 * Count the number of items on a user's watchlist
402 *
403 * @param $talk Include talk pages
404 * @return integer
405 */
406 function wlCountItems( &$user, $talk = true ) {
407 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
408
409 # Fetch the raw count
410 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count',
411 array( 'wl_user' => $user->mId ), 'wlCountItems' );
412 $row = $dbr->fetchObject( $res );
413 $count = $row->count;
414 $dbr->freeResult( $res );
415
416 # Halve to remove talk pages if needed
417 if( !$talk )
418 $count = floor( $count / 2 );
419
420 return( $count );
421 }