* Modified some methods of SpecialWatchlist to be non-static so that they can use...
[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( wfMsg( 'watchnologin' ) );
59 $llink = Linker::linkKnown(
60 SpecialPage::getTitleFor( 'Userlogin' ),
61 wfMsgHtml( 'loginreqlink' ),
62 array(),
63 array( 'returnto' => $this->getTitle()->getPrefixedText() )
64 );
65 $output->addWikiMsgArray( 'watchlistanontext', array( $llink ), array( 'replaceafter' ) );
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 = $dbr->getLag();
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'] >= 1 ) {
302 $timestamp = wfTimestampNow();
303 $wlInfo = wfMsgExt( 'rcnote', 'parseinline',
304 $lang->formatNum( $numRows ),
305 $lang->formatNum( $values['days'] ),
306 $lang->timeAndDate( $timestamp, true ),
307 $lang->date( $timestamp, true ),
308 $lang->time( $timestamp, true )
309 ) . '<br />';
310 } elseif( $values['days'] > 0 ) {
311 $wlInfo = wfMsgExt( 'wlnote', 'parseinline',
312 $lang->formatNum( $numRows ),
313 $lang->formatNum( round( $values['days'] * 24 ) )
314 ) . '<br />';
315 }
316
317 $cutofflinks = "\n" . $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
318
319 # Spit out some control panel links
320 $filters = array(
321 'hideMinor' => 'rcshowhideminor',
322 'hideBots' => 'rcshowhidebots',
323 'hideAnons' => 'rcshowhideanons',
324 'hideLiu' => 'rcshowhideliu',
325 'hideOwn' => 'rcshowhidemine',
326 'hidePatrolled' => 'rcshowhidepatr'
327 );
328 foreach ( $this->customFilters as $key => $params ) {
329 $filters[$key] = $params['msg'];
330 }
331 // Disable some if needed
332 if ( !$user->useNPPatrol() ) {
333 unset( $filters['hidePatrolled'] );
334 }
335
336 $links = array();
337 foreach( $filters as $name => $msg ) {
338 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
339 }
340
341 # Namespace filter and put the whole form together.
342 $form .= $wlInfo;
343 $form .= $cutofflinks;
344 $form .= $lang->pipeList( $links );
345 $form .= Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalUrl(), 'id' => 'mw-watchlist-form-namespaceselector' ) );
346 $form .= '<hr /><p>';
347 $form .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&#160;';
348 $form .= Xml::namespaceSelector( $nameSpace, '' ) . '&#160;';
349 $form .= Xml::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $invert ) . '&#160;';
350 $form .= Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
351 $form .= Html::hidden( 'days', $values['days'] );
352 foreach ( $filters as $key => $msg ) {
353 if ( $values[$key] ) {
354 $form .= Html::hidden( $key, 1 );
355 }
356 }
357 $form .= Xml::closeElement( 'form' );
358 $form .= Xml::closeElement( 'fieldset' );
359 $output->addHTML( $form );
360
361 # If there's nothing to show, stop here
362 if( $numRows == 0 ) {
363 $output->addWikiMsg( 'watchnochange' );
364 return;
365 }
366
367 /* End bottom header */
368
369 /* Do link batch query */
370 $linkBatch = new LinkBatch;
371 foreach ( $res as $row ) {
372 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
373 if ( $row->rc_user != 0 ) {
374 $linkBatch->add( NS_USER, $userNameUnderscored );
375 }
376 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
377
378 $linkBatch->add( $row->rc_namespace, $row->rc_title );
379 }
380 $linkBatch->execute();
381 $dbr->dataSeek( $res, 0 );
382
383 $list = ChangesList::newFromUser( $this->getUser() );
384 $list->setWatchlistDivs();
385
386 $s = $list->beginRecentChangesList();
387 $counter = 1;
388 foreach ( $res as $obj ) {
389 # Make RC entry
390 $rc = RecentChange::newFromRow( $obj );
391 $rc->counter = $counter++;
392
393 if ( $wgShowUpdatedMarker ) {
394 $updated = $obj->wl_notificationtimestamp;
395 } else {
396 $updated = false;
397 }
398
399 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
400 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
401 'COUNT(*)',
402 array(
403 'wl_namespace' => $obj->rc_namespace,
404 'wl_title' => $obj->rc_title,
405 ),
406 __METHOD__ );
407 } else {
408 $rc->numberofWatchingusers = 0;
409 }
410
411 $s .= $list->recentChangesLine( $rc, $updated, $counter );
412 }
413 $s .= $list->endRecentChangesList();
414
415 $output->addHTML( $s );
416 }
417
418 protected function showHideLink( $options, $message, $name, $value ) {
419 $showLinktext = wfMsgHtml( 'show' );
420 $hideLinktext = wfMsgHtml( 'hide' );
421
422 $label = $value ? $showLinktext : $hideLinktext;
423 $options[$name] = 1 - (int) $value;
424
425 return wfMsgHtml( $message, Linker::linkKnown( $this->getTitle(), $label, array(), $options ) );
426 }
427
428 protected function hoursLink( $h, $options = array() ) {
429 $options['days'] = ( $h / 24.0 );
430
431 return Linker::linkKnown(
432 $this->getTitle(),
433 $this->getLang()->formatNum( $h ),
434 array(),
435 $options
436 );
437 }
438
439 protected function daysLink( $d, $options = array() ) {
440 $options['days'] = $d;
441 $message = ( $d ? $this->getLang()->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) );
442
443 return Linker::linkKnown(
444 $this->getTitle(),
445 $message,
446 array(),
447 $options
448 );
449 }
450
451 /**
452 * Returns html
453 *
454 * @return string
455 */
456 protected function cutoffLinks( $days, $options = array() ) {
457 $hours = array( 1, 2, 6, 12 );
458 $days = array( 1, 3, 7 );
459 $i = 0;
460 foreach( $hours as $h ) {
461 $hours[$i++] = $this->hoursLink( $h, $options );
462 }
463 $i = 0;
464 foreach( $days as $d ) {
465 $days[$i++] = $this->daysLink( $d, $options );
466 }
467 return wfMsgExt('wlshowlast',
468 array('parseinline', 'replaceafter'),
469 $this->getLang()->pipeList( $hours ),
470 $this->getLang()->pipeList( $days ),
471 $this->daysLink( 0, $options ) );
472 }
473
474 /**
475 * Count the number of items on a user's watchlist
476 *
477 * @return Integer
478 */
479 protected function countItems() {
480 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
481
482 # Fetch the raw count
483 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count',
484 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
485 $row = $dbr->fetchObject( $res );
486 $count = $row->count;
487
488 return floor( $count / 2 );
489 }
490 }