* Small cleanups
[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' ),
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 if ( $usePage ) {
218 $tables[] = 'page';
219 $join_conds['page'] = array('LEFT JOIN','rc_cur_id=page_id');
220 }
221
222 ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, '' );
223 wfRunHooks('SpecialWatchlistQuery', array(&$conds,&$tables,&$join_conds,&$fields) );
224
225 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
226 $numRows = $dbr->numRows( $res );
227
228 /* Start bottom header */
229
230 $wlInfo = '';
231 if( $days >= 1 ) {
232 $wlInfo = wfMsgExt( 'rcnote', 'parseinline',
233 $wgLang->formatNum( $numRows ),
234 $wgLang->formatNum( $days ),
235 $wgLang->timeAndDate( wfTimestampNow(), true ),
236 $wgLang->date( wfTimestampNow(), true ),
237 $wgLang->time( wfTimestampNow(), true )
238 ) . '<br />';
239 } elseif( $days > 0 ) {
240 $wlInfo = wfMsgExt( 'wlnote', 'parseinline',
241 $wgLang->formatNum( $numRows ),
242 $wgLang->formatNum( round($days*24) )
243 ) . '<br />';
244 }
245
246 $cutofflinks = "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n";
247
248 # Spit out some control panel links
249 $thisTitle = SpecialPage::getTitleFor( 'Watchlist' );
250 $skin = $wgUser->getSkin();
251
252 $showLinktext = wfMsgHtml( 'show' );
253 $hideLinktext = wfMsgHtml( 'hide' );
254 # Hide/show minor edits
255 $label = $hideMinor ? $showLinktext : $hideLinktext;
256 $linkBits = wfArrayToCGI( array( 'hideMinor' => 1 - (int)$hideMinor ), $nondefaults );
257 $links[] = wfMsgHtml( 'rcshowhideminor', $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits ) );
258
259 # Hide/show bot edits
260 $label = $hideBots ? $showLinktext : $hideLinktext;
261 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
262 $links[] = wfMsgHtml( 'rcshowhidebots', $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits ) );
263
264 # Hide/show anonymous edits
265 $label = $hideAnons ? $showLinktext : $hideLinktext;
266 $linkBits = wfArrayToCGI( array( 'hideAnons' => 1 - (int)$hideAnons ), $nondefaults );
267 $links[] = wfMsgHtml( 'rcshowhideanons', $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits ) );
268
269 # Hide/show logged in edits
270 $label = $hideLiu ? $showLinktext : $hideLinktext;
271 $linkBits = wfArrayToCGI( array( 'hideLiu' => 1 - (int)$hideLiu ), $nondefaults );
272 $links[] = wfMsgHtml( 'rcshowhideliu', $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits ) );
273
274 # Hide/show own edits
275 $label = $hideOwn ? $showLinktext : $hideLinktext;
276 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
277 $links[] = wfMsgHtml( 'rcshowhidemine', $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits ) );
278
279 # Hide/show patrolled edits
280 if( $wgUser->useRCPatrol() ) {
281 $label = $hidePatrolled ? $showLinktext : $hideLinktext;
282 $linkBits = wfArrayToCGI( array( 'hidePatrolled' => 1 - (int)$hidePatrolled ), $nondefaults );
283 $links[] = wfMsgHtml( 'rcshowhidepatr', $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits ) );
284 }
285
286 # Namespace filter and put the whole form together.
287 $form .= $wlInfo;
288 $form .= $cutofflinks;
289 $form .= $wgLang->pipeList( $links );
290 $form .= Xml::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl() ) );
291 $form .= '<hr /><p>';
292 $form .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&nbsp;';
293 $form .= Xml::namespaceSelector( $nameSpace, '' ) . '&nbsp;';
294 $form .= Xml::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $invert ) . '&nbsp;';
295 $form .= Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
296 $form .= Xml::hidden( 'days', $days );
297 if( $hideMinor )
298 $form .= Xml::hidden( 'hideMinor', 1 );
299 if( $hideBots )
300 $form .= Xml::hidden( 'hideBots', 1 );
301 if( $hideAnons )
302 $form .= Xml::hidden( 'hideAnons', 1 );
303 if( $hideLiu )
304 $form .= Xml::hidden( 'hideLiu', 1 );
305 if( $hideOwn )
306 $form .= Xml::hidden( 'hideOwn', 1 );
307 $form .= Xml::closeElement( 'form' );
308 $form .= Xml::closeElement( 'fieldset' );
309 $wgOut->addHTML( $form );
310
311 # If there's nothing to show, stop here
312 if( $numRows == 0 ) {
313 $wgOut->addWikiMsg( 'watchnochange' );
314 return;
315 }
316
317 /* End bottom header */
318
319 /* Do link batch query */
320 $linkBatch = new LinkBatch;
321 while ( $row = $dbr->fetchObject( $res ) ) {
322 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
323 if ( $row->rc_user != 0 ) {
324 $linkBatch->add( NS_USER, $userNameUnderscored );
325 }
326 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
327
328 $linkBatch->add( $row->rc_namespace, $row->rc_title );
329 }
330 $linkBatch->execute();
331 $dbr->dataSeek( $res, 0 );
332
333 $list = ChangesList::newFromUser( $wgUser );
334
335 $s = $list->beginRecentChangesList();
336 $counter = 1;
337 while ( $obj = $dbr->fetchObject( $res ) ) {
338 # Make RC entry
339 $rc = RecentChange::newFromRow( $obj );
340 $rc->counter = $counter++;
341
342 if ( $wgShowUpdatedMarker ) {
343 $updated = $obj->wl_notificationtimestamp;
344 } else {
345 $updated = false;
346 }
347
348 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
349 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
350 'COUNT(*)',
351 array(
352 'wl_namespace' => $obj->rc_namespace,
353 'wl_title' => $obj->rc_title,
354 ),
355 __METHOD__ );
356 } else {
357 $rc->numberofWatchingusers = 0;
358 }
359
360 $s .= $list->recentChangesLine( $rc, $updated, $counter );
361 }
362 $s .= $list->endRecentChangesList();
363
364 $dbr->freeResult( $res );
365 $wgOut->addHTML( $s );
366 }
367
368 function wlHoursLink( $h, $page, $options = array() ) {
369 global $wgUser, $wgLang, $wgContLang;
370 $sk = $wgUser->getSkin();
371 $s = $sk->makeKnownLink(
372 $wgContLang->specialPage( $page ),
373 $wgLang->formatNum( $h ),
374 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
375 return $s;
376 }
377
378 function wlDaysLink( $d, $page, $options = array() ) {
379 global $wgUser, $wgLang, $wgContLang;
380 $sk = $wgUser->getSkin();
381 $s = $sk->makeKnownLink(
382 $wgContLang->specialPage( $page ),
383 ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
384 wfArrayToCGI( array('days' => $d), $options ) );
385 return $s;
386 }
387
388 /**
389 * Returns html
390 */
391 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
392 global $wgLang;
393
394 $hours = array( 1, 2, 6, 12 );
395 $days = array( 1, 3, 7 );
396 $i = 0;
397 foreach( $hours as $h ) {
398 $hours[$i++] = wlHoursLink( $h, $page, $options );
399 }
400 $i = 0;
401 foreach( $days as $d ) {
402 $days[$i++] = wlDaysLink( $d, $page, $options );
403 }
404 return wfMsgExt('wlshowlast',
405 array('parseinline', 'replaceafter'),
406 $wgLang->pipeList( $hours ),
407 $wgLang->pipeList( $days ),
408 wlDaysLink( 0, $page, $options ) );
409 }
410
411 /**
412 * Count the number of items on a user's watchlist
413 *
414 * @param $talk Include talk pages
415 * @return integer
416 */
417 function wlCountItems( &$user, $talk = true ) {
418 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
419
420 # Fetch the raw count
421 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count',
422 array( 'wl_user' => $user->mId ), 'wlCountItems' );
423 $row = $dbr->fetchObject( $res );
424 $count = $row->count;
425 $dbr->freeResult( $res );
426
427 # Halve to remove talk pages if needed
428 if( !$talk )
429 $count = floor( $count / 2 );
430
431 return( $count );
432 }