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