addWikiMsg
[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 # Toggle watchlist content (all recent edits or just the latest)
163 if( $wgUser->getOption( 'extendwatchlist' )) {
164 $andLatest='';
165 $limitWatchlist = 'LIMIT ' . intval( $wgUser->getOption( 'wllimit' ) );
166 } else {
167 # Top log Ids for a page are not stored
168 $andLatest = 'AND (rc_this_oldid=page_latest OR rc_type=' . RC_LOG . ') ';
169 $limitWatchlist = '';
170 }
171
172 # Show a message about slave lag, if applicable
173 if( ( $lag = $dbr->getLag() ) > 0 )
174 $wgOut->showLagWarning( $lag );
175
176 # Create output form
177 $form = Xml::fieldset( wfMsg( 'watchlist-options' ), false, array( 'id' => 'mw-watchlist-options' ) );
178
179 # Show watchlist header
180 $form .= wfMsgExt( 'watchlist-details', array( 'parseinline' ), $wgLang->formatNum( $nitems ) );
181
182 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
183 $form .= wfMsgExt( 'wlheader-enotif', 'parse' ) . "\n";
184 }
185 if ( $wgShowUpdatedMarker ) {
186 $form .= Xml::openElement( 'form', array( 'method' => 'post',
187 'action' => $specialTitle->getLocalUrl(),
188 'id' => 'mw-watchlist-resetbutton' ) ) .
189 wfMsgExt( 'wlheader-showupdated', array( 'parseinline' ) ) . ' ' .
190 Xml::submitButton( wfMsg( 'enotif_reset' ), array( 'name' => 'dummy' ) ) .
191 Xml::hidden( 'reset', 'all' ) .
192 Xml::closeElement( 'form' );
193 }
194 $form .= '<hr />';
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 .= $wlInfo;
273 $form .= $cutofflinks;
274 $form .= implode( ' | ', $links );
275 $form .= Xml::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl() ) );
276 $form .= '<p>';
277 $form .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&nbsp;';
278 $form .= Xml::namespaceSelector( $nameSpace, '' ) . '&nbsp;';
279 $form .= Xml::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $invert ) . '&nbsp;';
280 $form .= Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
281 $form .= Xml::hidden( 'days', $days );
282 if( $hideMinor )
283 $form .= Xml::hidden( 'hideMinor', 1 );
284 if( $hideBots )
285 $form .= Xml::hidden( 'hideBots', 1 );
286 if( $hideAnons )
287 $form .= Xml::hidden( 'hideAnons', 1 );
288 if( $hideLiu )
289 $form .= Xml::hidden( 'hideLiu', 1 );
290 if( $hideOwn )
291 $form .= Xml::hidden( 'hideOwn', 1 );
292 $form .= Xml::closeElement( 'form' );
293 $form .= Xml::closeElement( 'fieldset' );
294 $wgOut->addHtml( $form );
295
296 # If there's nothing to show, stop here
297 if( $numRows == 0 ) {
298 $wgOut->addWikiMsg( 'watchnochange' );
299 return;
300 }
301
302 /* End bottom header */
303
304 /* Do link batch query */
305 $linkBatch = new LinkBatch;
306 while ( $row = $dbr->fetchObject( $res ) ) {
307 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
308 if ( $row->rc_user != 0 ) {
309 $linkBatch->add( NS_USER, $userNameUnderscored );
310 }
311 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
312 }
313 $linkBatch->execute();
314 $dbr->dataSeek( $res, 0 );
315
316 $list = ChangesList::newFromUser( $wgUser );
317
318 $s = $list->beginRecentChangesList();
319 $counter = 1;
320 while ( $obj = $dbr->fetchObject( $res ) ) {
321 # Make RC entry
322 $rc = RecentChange::newFromRow( $obj );
323 $rc->counter = $counter++;
324
325 if ( $wgShowUpdatedMarker ) {
326 $updated = $obj->wl_notificationtimestamp;
327 } else {
328 $updated = false;
329 }
330
331 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
332 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
333 'COUNT(*)',
334 array(
335 'wl_namespace' => $obj->rc_namespace,
336 'wl_title' => $obj->rc_title,
337 ),
338 __METHOD__ );
339 } else {
340 $rc->numberofWatchingusers = 0;
341 }
342
343 $s .= $list->recentChangesLine( $rc, $updated );
344 }
345 $s .= $list->endRecentChangesList();
346
347 $dbr->freeResult( $res );
348 $wgOut->addHTML( $s );
349 }
350
351 function wlHoursLink( $h, $page, $options = array() ) {
352 global $wgUser, $wgLang, $wgContLang;
353 $sk = $wgUser->getSkin();
354 $s = $sk->makeKnownLink(
355 $wgContLang->specialPage( $page ),
356 $wgLang->formatNum( $h ),
357 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
358 return $s;
359 }
360
361 function wlDaysLink( $d, $page, $options = array() ) {
362 global $wgUser, $wgLang, $wgContLang;
363 $sk = $wgUser->getSkin();
364 $s = $sk->makeKnownLink(
365 $wgContLang->specialPage( $page ),
366 ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
367 wfArrayToCGI( array('days' => $d), $options ) );
368 return $s;
369 }
370
371 /**
372 * Returns html
373 */
374 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
375 $hours = array( 1, 2, 6, 12 );
376 $days = array( 1, 3, 7 );
377 $i = 0;
378 foreach( $hours as $h ) {
379 $hours[$i++] = wlHoursLink( $h, $page, $options );
380 }
381 $i = 0;
382 foreach( $days as $d ) {
383 $days[$i++] = wlDaysLink( $d, $page, $options );
384 }
385 return wfMsgExt('wlshowlast',
386 array('parseinline', 'replaceafter'),
387 implode(' | ', $hours),
388 implode(' | ', $days),
389 wlDaysLink( 0, $page, $options ) );
390 }
391
392 /**
393 * Count the number of items on a user's watchlist
394 *
395 * @param $talk Include talk pages
396 * @return integer
397 */
398 function wlCountItems( &$user, $talk = true ) {
399 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
400
401 # Fetch the raw count
402 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId ), 'wlCountItems' );
403 $row = $dbr->fetchObject( $res );
404 $count = $row->count;
405 $dbr->freeResult( $res );
406
407 # Halve to remove talk pages if needed
408 if( !$talk )
409 $count = floor( $count / 2 );
410
411 return( $count );
412 }