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