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