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