Consistent casing for addHTML()
[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 $fname = 'wfSpecialWatchlist';
17
18 $skin = $wgUser->getSkin();
19 $specialTitle = SpecialPage::getTitleFor( 'Watchlist' );
20 $wgOut->setRobotPolicy( 'noindex,nofollow' );
21
22 # Anons don't get a watchlist
23 if( $wgUser->isAnon() ) {
24 $wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
25 $llink = $skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Userlogin' ),
26 wfMsgHtml( 'loginreqlink' ), 'returnto=' . $specialTitle->getPrefixedUrl() );
27 $wgOut->addHTML( wfMsgWikiHtml( 'watchlistanontext', $llink ) );
28 return;
29 }
30
31 $wgOut->setPageTitle( wfMsg( 'watchlist' ) );
32
33 $sub = wfMsgExt( 'watchlistfor', 'parseinline', $wgUser->getName() );
34 $sub .= '<br />' . WatchlistEditor::buildTools( $wgUser->getSkin() );
35 $wgOut->setSubtitle( $sub );
36
37 if( ( $mode = WatchlistEditor::getMode( $wgRequest, $par ) ) !== false ) {
38 $editor = new WatchlistEditor();
39 $editor->execute( $wgUser, $wgOut, $wgRequest, $mode );
40 return;
41 }
42
43 $uid = $wgUser->getId();
44 if( ($wgEnotifWatchlist || $wgShowUpdatedMarker) && $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
45 $wgUser->clearAllNotifications( $uid );
46 $wgOut->redirect( $specialTitle->getFullUrl() );
47 return;
48 }
49
50 $defaults = array(
51 /* float */ 'days' => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
52 /* bool */ 'hideMinor' => (int)$wgUser->getBoolOption( 'watchlisthideminor' ),
53 /* bool */ 'hideBots' => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
54 /* bool */ 'hideAnons' => (int)$wgUser->getBoolOption( 'watchlisthideanons' ),
55 /* bool */ 'hideLiu' => (int)$wgUser->getBoolOption( 'watchlisthideliu' ),
56 /* bool */ 'hidePatrolled' => (int)$wgUser->getBoolOption( 'watchlisthidepatrolled' ), // TODO
57 /* bool */ 'hideOwn' => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
58 /* ? */ 'namespace' => 'all',
59 /* ? */ 'invert' => false,
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['hideminor'] = $wgUser->getBoolOption( 'watchlisthideminor' );
68 $prefs['hidebots'] = $wgUser->getBoolOption( 'watchlisthidebots' );
69 $prefs['hideanons'] = $wgUser->getBoolOption( 'watchlisthideanon' );
70 $prefs['hideliu'] = $wgUser->getBoolOption( 'watchlisthideliu' );
71 $prefs['hideown' ] = $wgUser->getBoolOption( 'watchlisthideown' );
72 $prefs['hidepatrolled' ] = $wgUser->getBoolOption( 'watchlisthidepatrolled' );
73
74 # Get query variables
75 $days = $wgRequest->getVal( 'days' , $prefs['days'] );
76 $hideMinor = $wgRequest->getBool( 'hideMinor', $prefs['hideminor'] );
77 $hideBots = $wgRequest->getBool( 'hideBots' , $prefs['hidebots'] );
78 $hideAnons = $wgRequest->getBool( 'hideAnons', $prefs['hideanons'] );
79 $hideLiu = $wgRequest->getBool( 'hideLiu' , $prefs['hideliu'] );
80 $hideOwn = $wgRequest->getBool( 'hideOwn' , $prefs['hideown'] );
81 $hidePatrolled = $wgRequest->getBool( 'hidePatrolled' , $prefs['hidepatrolled'] );
82
83 # Get namespace value, if supplied, and prepare a WHERE fragment
84 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
85 $invert = $wgRequest->getIntOrNull( 'invert' );
86 if( !is_null( $nameSpace ) ) {
87 $nameSpace = intval( $nameSpace );
88 if( $invert && $nameSpace !== 'all' )
89 $nameSpaceClause = " AND rc_namespace != $nameSpace";
90 else
91 $nameSpaceClause = " AND rc_namespace = $nameSpace";
92 } else {
93 $nameSpace = '';
94 $nameSpaceClause = '';
95 }
96
97 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
98 list( $page, $watchlist, $recentchanges ) = $dbr->tableNamesN( 'page', 'watchlist', 'recentchanges' );
99
100 $watchlistCount = $dbr->selectField( 'watchlist', 'COUNT(*)',
101 array( 'wl_user' => $uid ), __METHOD__ );
102 // Adjust for page X, talk:page X, which are both stored separately,
103 // but treated together
104 $nitems = floor($watchlistCount / 2);
105
106 if( is_null($days) || !is_numeric($days) ) {
107 $big = 1000; /* The magical big */
108 if($nitems > $big) {
109 # Set default cutoff shorter
110 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
111 } else {
112 $days = $defaults['days']; # default cutoff for shortlisters
113 }
114 } else {
115 $days = floatval($days);
116 }
117
118 // Dump everything here
119 $nondefaults = array();
120
121 wfAppendToArrayIfNotDefault( 'days' , $days , $defaults, $nondefaults);
122 wfAppendToArrayIfNotDefault( 'hideMinor', (int)$hideMinor, $defaults, $nondefaults );
123 wfAppendToArrayIfNotDefault( 'hideBots' , (int)$hideBots , $defaults, $nondefaults);
124 wfAppendToArrayIfNotDefault( 'hideAnons', (int)$hideAnons, $defaults, $nondefaults );
125 wfAppendToArrayIfNotDefault( 'hideLiu' , (int)$hideLiu , $defaults, $nondefaults );
126 wfAppendToArrayIfNotDefault( 'hideOwn' , (int)$hideOwn , $defaults, $nondefaults);
127 wfAppendToArrayIfNotDefault( 'namespace', $nameSpace , $defaults, $nondefaults);
128 wfAppendToArrayIfNotDefault( 'hidePatrolled', (int)$hidePatrolled, $defaults, $nondefaults );
129
130 $hookSql = "";
131 if( ! wfRunHooks('BeforeWatchlist', array($nondefaults, $wgUser, &$hookSql)) ) {
132 return;
133 }
134
135 if($nitems == 0) {
136 $wgOut->addWikiMsg( 'nowatchlist' );
137 return;
138 }
139
140 if ( $days <= 0 ) {
141 $andcutoff = '';
142 } else {
143 $andcutoff = "AND rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
144 /*
145 $sql = "SELECT COUNT(*) AS n FROM $page, $revision WHERE rev_timestamp>'$cutoff' AND page_id=rev_page";
146 $res = $dbr->query( $sql, $fname );
147 $s = $dbr->fetchObject( $res );
148 $npages = $s->n;
149 */
150 }
151
152 # If the watchlist is relatively short, it's simplest to zip
153 # down its entirety and then sort the results.
154
155 # If it's relatively long, it may be worth our while to zip
156 # through the time-sorted page list checking for watched items.
157
158 # Up estimate of watched items by 15% to compensate for talk pages...
159
160 # Toggles
161 $andHideOwn = $hideOwn ? "AND (rc_user <> $uid)" : '';
162 $andHideBots = $hideBots ? "AND (rc_bot = 0)" : '';
163 $andHideMinor = $hideMinor ? "AND (rc_minor = 0)" : '';
164 $andHideLiu = $hideLiu ? "AND (rc_user = 0)" : '';
165 $andHideAnons = $hideAnons ? "AND (rc_user != 0)" : '';
166 $andHidePatrolled = $wgUser->useRCPatrol() && $hidePatrolled ? "AND (rc_patrolled != 1)" : '';
167
168 # Toggle watchlist content (all recent edits or just the latest)
169 if( $wgUser->getOption( 'extendwatchlist' )) {
170 $andLatest='';
171 $limitWatchlist = 'LIMIT ' . intval( $wgUser->getOption( 'wllimit' ) );
172 } else {
173 # Top log Ids for a page are not stored
174 $andLatest = 'AND (rc_this_oldid=page_latest OR rc_type=' . RC_LOG . ') ';
175 $limitWatchlist = '';
176 }
177
178 # Show a message about slave lag, if applicable
179 if( ( $lag = $dbr->getLag() ) > 0 )
180 $wgOut->showLagWarning( $lag );
181
182 # Create output form
183 $form = Xml::fieldset( wfMsg( 'watchlist-options' ), false, array( 'id' => 'mw-watchlist-options' ) );
184
185 # Show watchlist header
186 $form .= wfMsgExt( 'watchlist-details', array( 'parseinline' ), $wgLang->formatNum( $nitems ) );
187
188 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
189 $form .= wfMsgExt( 'wlheader-enotif', 'parse' ) . "\n";
190 }
191 if ( $wgShowUpdatedMarker ) {
192 $form .= Xml::openElement( 'form', array( 'method' => 'post',
193 'action' => $specialTitle->getLocalUrl(),
194 'id' => 'mw-watchlist-resetbutton' ) ) .
195 wfMsgExt( 'wlheader-showupdated', array( 'parseinline' ) ) . ' ' .
196 Xml::submitButton( wfMsg( 'enotif_reset' ), array( 'name' => 'dummy' ) ) .
197 Xml::hidden( 'reset', 'all' ) .
198 Xml::closeElement( 'form' );
199 }
200 $form .= '<hr />';
201
202 if ( $wgShowUpdatedMarker ) {
203 $wltsfield = ", ${watchlist}.wl_notificationtimestamp ";
204 } else {
205 $wltsfield = '';
206 }
207 $sql = "SELECT ${recentchanges}.* ${wltsfield}
208 FROM $watchlist,$recentchanges
209 LEFT JOIN $page ON rc_cur_id=page_id
210 WHERE wl_user=$uid
211 AND wl_namespace=rc_namespace
212 AND wl_title=rc_title
213 $andcutoff
214 $andLatest
215 $andHideOwn
216 $andHideBots
217 $andHideMinor
218 $andHideLiu
219 $andHideAnons
220 $andHidePatrolled
221 $nameSpaceClause
222 $hookSql
223 ORDER BY rc_timestamp DESC
224 $limitWatchlist";
225
226 $res = $dbr->query( $sql, $fname );
227 $numRows = $dbr->numRows( $res );
228
229 /* Start bottom header */
230
231 $wlInfo = '';
232 if( $days >= 1 ) {
233 $wlInfo = wfMsgExt( 'rcnote', 'parseinline',
234 $wgLang->formatNum( $numRows ),
235 $wgLang->formatNum( $days ),
236 $wgLang->timeAndDate( wfTimestampNow(), true ),
237 $wgLang->date( wfTimestampNow(), true ),
238 $wgLang->time( wfTimestampNow(), true )
239 ) . '<br />';
240 } elseif( $days > 0 ) {
241 $wlInfo = wfMsgExt( 'wlnote', 'parseinline',
242 $wgLang->formatNum( $numRows ),
243 $wgLang->formatNum( round($days*24) )
244 ) . '<br />';
245 }
246
247 $cutofflinks = "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n";
248
249 # Spit out some control panel links
250 $thisTitle = SpecialPage::getTitleFor( 'Watchlist' );
251 $skin = $wgUser->getSkin();
252
253 $showLinktext = wfMsgHtml( 'show' );
254 $hideLinktext = wfMsgHtml( 'hide' );
255 # Hide/show minor edits
256 $label = $hideMinor ? $showLinktext : $hideLinktext;
257 $linkBits = wfArrayToCGI( array( 'hideMinor' => 1 - (int)$hideMinor ), $nondefaults );
258 $links[] = wfMsgHtml( 'rcshowhideminor', $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits ) );
259
260 # Hide/show bot edits
261 $label = $hideBots ? $showLinktext : $hideLinktext;
262 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
263 $links[] = wfMsgHtml( 'rcshowhidebots', $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits ) );
264
265 # Hide/show anonymous edits
266 $label = $hideAnons ? $showLinktext : $hideLinktext;
267 $linkBits = wfArrayToCGI( array( 'hideAnons' => 1 - (int)$hideAnons ), $nondefaults );
268 $links[] = wfMsgHtml( 'rcshowhideanons', $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits ) );
269
270 # Hide/show logged in edits
271 $label = $hideLiu ? $showLinktext : $hideLinktext;
272 $linkBits = wfArrayToCGI( array( 'hideLiu' => 1 - (int)$hideLiu ), $nondefaults );
273 $links[] = wfMsgHtml( 'rcshowhideliu', $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits ) );
274
275 # Hide/show own edits
276 $label = $hideOwn ? $showLinktext : $hideLinktext;
277 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
278 $links[] = wfMsgHtml( 'rcshowhidemine', $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits ) );
279
280 # Hide/show patrolled edits
281 if( $wgUser->useRCPatrol() ) {
282 $label = $hidePatrolled ? $showLinktext : $hideLinktext;
283 $linkBits = wfArrayToCGI( array( 'hidePatrolled' => 1 - (int)$hidePatrolled ), $nondefaults );
284 $links[] = wfMsgHtml( 'rcshowhidepatr', $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits ) );
285 }
286
287 # Namespace filter and put the whole form together.
288 $form .= $wlInfo;
289 $form .= $cutofflinks;
290 $form .= implode( ' | ', $links );
291 $form .= Xml::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl() ) );
292 $form .= '<p>';
293 $form .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&nbsp;';
294 $form .= Xml::namespaceSelector( $nameSpace, '' ) . '&nbsp;';
295 $form .= Xml::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $invert ) . '&nbsp;';
296 $form .= Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
297 $form .= Xml::hidden( 'days', $days );
298 if( $hideMinor )
299 $form .= Xml::hidden( 'hideMinor', 1 );
300 if( $hideBots )
301 $form .= Xml::hidden( 'hideBots', 1 );
302 if( $hideAnons )
303 $form .= Xml::hidden( 'hideAnons', 1 );
304 if( $hideLiu )
305 $form .= Xml::hidden( 'hideLiu', 1 );
306 if( $hideOwn )
307 $form .= Xml::hidden( 'hideOwn', 1 );
308 $form .= Xml::closeElement( 'form' );
309 $form .= Xml::closeElement( 'fieldset' );
310 $wgOut->addHTML( $form );
311
312 # If there's nothing to show, stop here
313 if( $numRows == 0 ) {
314 $wgOut->addWikiMsg( 'watchnochange' );
315 return;
316 }
317
318 /* End bottom header */
319
320 /* Do link batch query */
321 $linkBatch = new LinkBatch;
322 while ( $row = $dbr->fetchObject( $res ) ) {
323 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
324 if ( $row->rc_user != 0 ) {
325 $linkBatch->add( NS_USER, $userNameUnderscored );
326 }
327 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
328 }
329 $linkBatch->execute();
330 $dbr->dataSeek( $res, 0 );
331
332 $list = ChangesList::newFromUser( $wgUser );
333
334 $s = $list->beginRecentChangesList();
335 $counter = 1;
336 while ( $obj = $dbr->fetchObject( $res ) ) {
337 # Make RC entry
338 $rc = RecentChange::newFromRow( $obj );
339 $rc->counter = $counter++;
340
341 if ( $wgShowUpdatedMarker ) {
342 $updated = $obj->wl_notificationtimestamp;
343 } else {
344 $updated = false;
345 }
346
347 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
348 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
349 'COUNT(*)',
350 array(
351 'wl_namespace' => $obj->rc_namespace,
352 'wl_title' => $obj->rc_title,
353 ),
354 __METHOD__ );
355 } else {
356 $rc->numberofWatchingusers = 0;
357 }
358
359 $s .= $list->recentChangesLine( $rc, $updated );
360 }
361 $s .= $list->endRecentChangesList();
362
363 $dbr->freeResult( $res );
364 $wgOut->addHTML( $s );
365 }
366
367 function wlHoursLink( $h, $page, $options = array() ) {
368 global $wgUser, $wgLang, $wgContLang;
369 $sk = $wgUser->getSkin();
370 $s = $sk->makeKnownLink(
371 $wgContLang->specialPage( $page ),
372 $wgLang->formatNum( $h ),
373 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
374 return $s;
375 }
376
377 function wlDaysLink( $d, $page, $options = array() ) {
378 global $wgUser, $wgLang, $wgContLang;
379 $sk = $wgUser->getSkin();
380 $s = $sk->makeKnownLink(
381 $wgContLang->specialPage( $page ),
382 ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
383 wfArrayToCGI( array('days' => $d), $options ) );
384 return $s;
385 }
386
387 /**
388 * Returns html
389 */
390 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
391 $hours = array( 1, 2, 6, 12 );
392 $days = array( 1, 3, 7 );
393 $i = 0;
394 foreach( $hours as $h ) {
395 $hours[$i++] = wlHoursLink( $h, $page, $options );
396 }
397 $i = 0;
398 foreach( $days as $d ) {
399 $days[$i++] = wlDaysLink( $d, $page, $options );
400 }
401 return wfMsgExt('wlshowlast',
402 array('parseinline', 'replaceafter'),
403 implode(' | ', $hours),
404 implode(' | ', $days),
405 wlDaysLink( 0, $page, $options ) );
406 }
407
408 /**
409 * Count the number of items on a user's watchlist
410 *
411 * @param $talk Include talk pages
412 * @return integer
413 */
414 function wlCountItems( &$user, $talk = true ) {
415 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
416
417 # Fetch the raw count
418 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId ), 'wlCountItems' );
419 $row = $dbr->fetchObject( $res );
420 $count = $row->count;
421 $dbr->freeResult( $res );
422
423 # Halve to remove talk pages if needed
424 if( !$talk )
425 $count = floor( $count / 2 );
426
427 return( $count );
428 }