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