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