Merge "rcfeed: Use wfWikiID() instead of $wgDBname"
[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 SpecialRecentChanges {
24 protected $customFilters;
25
26 /**
27 * Constructor
28 */
29 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
30 parent::__construct( $page, $restriction );
31 }
32
33 public function isIncludable() {
34 return false;
35 }
36
37 /**
38 * Get a FormOptions object containing the default options
39 *
40 * @return FormOptions
41 */
42 public function getDefaultOptions() {
43 $opts = parent::getDefaultOptions();
44 $user = $this->getUser();
45
46 // Overwrite RC options with Watchlist options
47 // (calling #add() again is okay)
48 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions::FLOAT );
49 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
50 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
51 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
52 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
53 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
54 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
55
56 // Add new ones
57 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
58
59 return $opts;
60 }
61
62 /**
63 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
64 *
65 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
66 * to the current ones.
67 *
68 * @param FormOptions $parameters
69 * @return FormOptions
70 */
71 protected function fetchOptionsFromRequest( $opts ) {
72 static $compatibilityMap = array(
73 'hideMinor' => 'hideminor',
74 'hideBots' => 'hidebots',
75 'hideAnons' => 'hideanons',
76 'hideLiu' => 'hideliu',
77 'hidePatrolled' => 'hidepatrolled',
78 'hideOwn' => 'hidemyself',
79 );
80
81 $params = $this->getRequest()->getValues();
82 foreach ( $compatibilityMap as $from => $to ) {
83 if ( isset( $params[$from] ) ) {
84 $params[$to] = $params[$from];
85 unset( $params[$from] );
86 }
87 }
88
89 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
90 // methods defined on WebRequest and removing this dependency would cause some code duplication.
91 $request = new DerivativeRequest( $this->getRequest(), $params );
92 $opts->fetchValuesFromRequest( $request );
93 return $opts;
94 }
95
96 /**
97 * Get custom show/hide filters
98 *
99 * @return array Map of filter URL param names to properties (msg/default)
100 */
101 protected function getCustomFilters() {
102 if ( $this->customFilters === null ) {
103 $this->customFilters = array();
104 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters ) );
105 }
106
107 return $this->customFilters;
108 }
109
110 /**
111 * Process $par and put options found if $opts. Not used for Watchlist.
112 *
113 * @param string $par
114 * @param FormOptions $opts
115 */
116 public function parseParameters( $par, FormOptions $opts ) {
117 }
118
119 /**
120 * Get the current FormOptions for this request
121 */
122 public function getOptions() {
123 if ( $this->rcOptions === null ) {
124 $this->rcOptions = $this->setup( null );
125 }
126
127 return $this->rcOptions;
128 }
129
130 /**
131 * Execute
132 * @param $par Parameter passed to the page
133 */
134 function execute( $par ) {
135 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
136
137 $user = $this->getUser();
138 $output = $this->getOutput();
139 $output->addModuleStyles( 'mediawiki.special.changeslist' );
140 $output->addModules( 'mediawiki.special.changeslist.js' );
141
142 # Anons don't get a watchlist
143 $this->requireLogin( 'watchlistanontext' );
144
145 // Check permissions
146 $this->checkPermissions();
147
148 // Add feed links
149 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
150 if ( $wlToken ) {
151 $this->addFeedLinks( array(
152 'action' => 'feedwatchlist',
153 'allrev' => 1,
154 'wlowner' => $user->getName(),
155 'wltoken' => $wlToken,
156 ) );
157 }
158
159 $opts = $this->getOptions();
160 $this->setHeaders();
161 $this->outputHeader();
162
163 $output->addSubtitle(
164 $this->msg( 'watchlistfor2', $user->getName() )
165 ->rawParams( SpecialEditWatchlist::buildTools( null ) )
166 );
167
168 $request = $this->getRequest();
169
170 $mode = SpecialEditWatchlist::getMode( $request, $par );
171 if ( $mode !== false ) {
172 if ( $mode === SpecialEditWatchlist::EDIT_RAW ) {
173 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'raw' );
174 } else {
175 $title = SpecialPage::getTitleFor( 'EditWatchlist' );
176 }
177
178 $output->redirect( $title->getLocalURL() );
179 return;
180 }
181
182 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
183
184 $nitems = $this->countItems( $dbr );
185 if ( $nitems == 0 ) {
186 $output->addWikiMsg( 'nowatchlist' );
187 return;
188 }
189
190 # Get namespace value, if supplied, and prepare a WHERE fragment
191 $nameSpace = $opts['namespace'];
192 $invert = $opts['invert'];
193 $associated = $opts['associated'];
194
195 if ( $nameSpace !== '' ) {
196 $eq_op = $invert ? '!=' : '=';
197 $bool_op = $invert ? 'AND' : 'OR';
198 if ( !$associated ) {
199 $nameSpaceClause = "rc_namespace $eq_op $nameSpace";
200 } else {
201 $associatedNS = MWNamespace::getAssociated( $nameSpace );
202 $nameSpaceClause =
203 "rc_namespace $eq_op $nameSpace " .
204 $bool_op .
205 " rc_namespace $eq_op $associatedNS";
206 }
207 } else {
208 $nameSpaceClause = '';
209 }
210
211 // Dump everything here
212 $nondefaults = $opts->getChangedValues();
213
214 if ( ( $wgEnotifWatchlist || $wgShowUpdatedMarker ) && $request->getVal( 'reset' )
215 && $request->wasPosted()
216 ) {
217 $user->clearAllNotifications();
218 $output->redirect( $this->getPageTitle()->getFullURL( $nondefaults ) );
219 return;
220 }
221
222 # Possible where conditions
223 $conds = array();
224
225 if ( $opts['days'] > 0 ) {
226 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
227 }
228
229 # Toggles
230 if ( $opts['hidemyself'] ) {
231 $conds[] = 'rc_user != ' . $user->getId();
232 }
233 if ( $opts['hidebots'] ) {
234 $conds[] = 'rc_bot = 0';
235 }
236 if ( $opts['hideminor'] ) {
237 $conds[] = 'rc_minor = 0';
238 }
239 if ( $opts['hideliu'] ) {
240 $conds[] = 'rc_user = 0';
241 }
242 if ( $opts['hideanons'] ) {
243 $conds[] = 'rc_user != 0';
244 }
245 if ( $user->useRCPatrol() && $opts['hidepatrolled'] ) {
246 $conds[] = 'rc_patrolled != 1';
247 }
248 if ( $nameSpaceClause ) {
249 $conds[] = $nameSpaceClause;
250 }
251
252 # Toggle watchlist content (all recent edits or just the latest)
253 if ( $opts['extended'] ) {
254 $limitWatchlist = $user->getIntOption( 'wllimit' );
255 $usePage = false;
256 } else {
257 # Top log Ids for a page are not stored
258 $nonRevisionTypes = array( RC_LOG );
259 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
260 if ( $nonRevisionTypes ) {
261 $conds[] = $dbr->makeList(
262 array(
263 'rc_this_oldid=page_latest',
264 'rc_type' => $nonRevisionTypes,
265 ),
266 LIST_OR
267 );
268 }
269 $limitWatchlist = 0;
270 $usePage = true;
271 }
272
273 # Show a message about slave lag, if applicable
274 $lag = wfGetLB()->safeGetLag( $dbr );
275 if ( $lag > 0 ) {
276 $output->showLagWarning( $lag );
277 }
278
279 # Create output
280 $form = '';
281
282 # Show watchlist header
283 $form .= "<p>";
284 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse() . "\n";
285 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
286 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
287 }
288 if ( $wgShowUpdatedMarker ) {
289 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
290 }
291 $form .= "</p>";
292
293 if ( $wgShowUpdatedMarker ) {
294 $form .= Xml::openElement( 'form', array( 'method' => 'post',
295 'action' => $this->getPageTitle()->getLocalURL(),
296 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
297 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
298 Html::hidden( 'reset', 'all' ) . "\n";
299 foreach ( $nondefaults as $key => $value ) {
300 $form .= Html::hidden( $key, $value ) . "\n";
301 }
302 $form .= Xml::closeElement( 'form' ) . "\n";
303 }
304
305 $form .= Xml::openElement( 'form', array(
306 'method' => 'post',
307 'action' => $this->getPageTitle()->getLocalURL(),
308 'id' => 'mw-watchlist-form'
309 ) );
310 $form .= Xml::fieldset(
311 $this->msg( 'watchlist-options' )->text(),
312 false,
313 array( 'id' => 'mw-watchlist-options' )
314 );
315
316 $form .= SpecialRecentChanges::makeLegend( $this->getContext() );
317
318 $tables = array( 'recentchanges', 'watchlist' );
319 $fields = RecentChange::selectFields();
320 $join_conds = array(
321 'watchlist' => array(
322 'INNER JOIN',
323 array(
324 'wl_user' => $user->getId(),
325 'wl_namespace=rc_namespace',
326 'wl_title=rc_title'
327 ),
328 ),
329 );
330 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
331 if ( $wgShowUpdatedMarker ) {
332 $fields[] = 'wl_notificationtimestamp';
333 }
334 if ( $limitWatchlist ) {
335 $options['LIMIT'] = $limitWatchlist;
336 }
337
338 $rollbacker = $user->isAllowed( 'rollback' );
339 if ( $usePage || $rollbacker ) {
340 $tables[] = 'page';
341 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
342 if ( $rollbacker ) {
343 $fields[] = 'page_latest';
344 }
345 }
346
347 ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
348 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $opts ) );
349
350 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
351 $numRows = $res->numRows();
352
353 /* Start bottom header */
354
355 $lang = $this->getLanguage();
356 $wlInfo = '';
357 if ( $opts['days'] > 0 ) {
358 $timestamp = wfTimestampNow();
359 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $opts['days'] * 24 ) )->params(
360 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
361 }
362
363 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
364
365 # Spit out some control panel links
366 $filters = array(
367 'hideminor' => 'rcshowhideminor',
368 'hidebots' => 'rcshowhidebots',
369 'hideanons' => 'rcshowhideanons',
370 'hideliu' => 'rcshowhideliu',
371 'hidemyself' => 'rcshowhidemine',
372 'hidepatrolled' => 'rcshowhidepatr'
373 );
374 foreach ( $this->getCustomFilters() as $key => $params ) {
375 $filters[$key] = $params['msg'];
376 }
377 // Disable some if needed
378 if ( !$user->useNPPatrol() ) {
379 unset( $filters['hidepatrolled'] );
380 }
381
382 $links = array();
383 foreach ( $filters as $name => $msg ) {
384 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
385 }
386
387 $hiddenFields = $nondefaults;
388 unset( $hiddenFields['namespace'] );
389 unset( $hiddenFields['invert'] );
390 unset( $hiddenFields['associated'] );
391
392 # Namespace filter and put the whole form together.
393 $form .= $wlInfo;
394 $form .= $cutofflinks;
395 $form .= $lang->pipeList( $links ) . "\n";
396 $form .= "<hr />\n<p>";
397 $form .= Html::namespaceSelector(
398 array(
399 'selected' => $nameSpace,
400 'all' => '',
401 'label' => $this->msg( 'namespace' )->text()
402 ), array(
403 'name' => 'namespace',
404 'id' => 'namespace',
405 'class' => 'namespaceselector',
406 )
407 ) . '&#160;';
408 $form .= Xml::checkLabel(
409 $this->msg( 'invert' )->text(),
410 'invert',
411 'nsinvert',
412 $invert,
413 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
414 ) . '&#160;';
415 $form .= Xml::checkLabel(
416 $this->msg( 'namespace_association' )->text(),
417 'associated',
418 'associated',
419 $associated,
420 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
421 ) . '&#160;';
422 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
423 foreach ( $hiddenFields as $key => $value ) {
424 $form .= Html::hidden( $key, $value ) . "\n";
425 }
426 $form .= Xml::closeElement( 'fieldset' ) . "\n";
427 $form .= Xml::closeElement( 'form' ) . "\n";
428 $output->addHTML( $form );
429
430 # If there's nothing to show, stop here
431 if ( $numRows == 0 ) {
432 $output->wrapWikiMsg(
433 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
434 );
435 return;
436 }
437
438 /* End bottom header */
439
440 /* Do link batch query */
441 $linkBatch = new LinkBatch;
442 foreach ( $res as $row ) {
443 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
444 if ( $row->rc_user != 0 ) {
445 $linkBatch->add( NS_USER, $userNameUnderscored );
446 }
447 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
448
449 $linkBatch->add( $row->rc_namespace, $row->rc_title );
450 }
451 $linkBatch->execute();
452 $dbr->dataSeek( $res, 0 );
453
454 $list = ChangesList::newFromContext( $this->getContext() );
455 $list->setWatchlistDivs();
456
457 $s = $list->beginRecentChangesList();
458 $counter = 1;
459 foreach ( $res as $obj ) {
460 # Make RC entry
461 $rc = RecentChange::newFromRow( $obj );
462 $rc->counter = $counter++;
463
464 if ( $wgShowUpdatedMarker ) {
465 $updated = $obj->wl_notificationtimestamp;
466 } else {
467 $updated = false;
468 }
469
470 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
471 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
472 'COUNT(*)',
473 array(
474 'wl_namespace' => $obj->rc_namespace,
475 'wl_title' => $obj->rc_title,
476 ),
477 __METHOD__ );
478 } else {
479 $rc->numberofWatchingusers = 0;
480 }
481
482 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
483 if ( $changeLine !== false ) {
484 $s .= $changeLine;
485 }
486 }
487 $s .= $list->endRecentChangesList();
488
489 $output->addHTML( $s );
490 }
491
492 protected function showHideLink( $options, $message, $name, $value ) {
493 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
494 $options[$name] = 1 - (int)$value;
495
496 return $this->msg( $message )->rawParams( Linker::linkKnown( $this->getPageTitle(), $label, array(), $options ) )->escaped();
497 }
498
499 protected function hoursLink( $h, $options = array() ) {
500 $options['days'] = ( $h / 24.0 );
501
502 return Linker::linkKnown(
503 $this->getPageTitle(),
504 $this->getLanguage()->formatNum( $h ),
505 array(),
506 $options
507 );
508 }
509
510 protected function daysLink( $d, $options = array() ) {
511 $options['days'] = $d;
512 $message = ( $d ? $this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
513
514 return Linker::linkKnown(
515 $this->getPageTitle(),
516 $message,
517 array(),
518 $options
519 );
520 }
521
522 /**
523 * Returns html
524 *
525 * @param int $days This gets overwritten, so is not used
526 * @param array $options Query parameters for URL
527 * @return string
528 */
529 protected function cutoffLinks( $days, $options = array() ) {
530 $hours = array( 1, 2, 6, 12 );
531 $days = array( 1, 3, 7 );
532 $i = 0;
533 foreach ( $hours as $h ) {
534 $hours[$i++] = $this->hoursLink( $h, $options );
535 }
536 $i = 0;
537 foreach ( $days as $d ) {
538 $days[$i++] = $this->daysLink( $d, $options );
539 }
540 return $this->msg( 'wlshowlast' )->rawParams(
541 $this->getLanguage()->pipeList( $hours ),
542 $this->getLanguage()->pipeList( $days ),
543 $this->daysLink( 0, $options ) )->parse();
544 }
545
546 /**
547 * Count the number of items on a user's watchlist
548 *
549 * @param DatabaseBase $dbr A database connection
550 * @return Integer
551 */
552 protected function countItems( $dbr ) {
553 # Fetch the raw count
554 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
555 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
556 $row = $dbr->fetchObject( $res );
557 $count = $row->count;
558
559 return floor( $count / 2 );
560 }
561 }