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