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