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