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