Always load 'mediawiki.special.changeslist' on appropriate pages
[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
140 # Anons don't get a watchlist
141 $this->requireLogin( 'watchlistanontext' );
142
143 // Check permissions
144 $this->checkPermissions();
145
146 $request = $this->getRequest();
147 $opts = $this->getOptions();
148
149 $mode = SpecialEditWatchlist::getMode( $request, $par );
150 if ( $mode !== false ) {
151 if ( $mode === SpecialEditWatchlist::EDIT_RAW ) {
152 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'raw' );
153 } else {
154 $title = SpecialPage::getTitleFor( 'EditWatchlist' );
155 }
156
157 $output->redirect( $title->getLocalURL() );
158 return;
159 }
160
161 if ( ( $wgEnotifWatchlist || $wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
162 $request->wasPosted() )
163 {
164 $user->clearAllNotifications();
165 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
166 return;
167 }
168
169 $this->setHeaders();
170 $this->outputHeader();
171 $this->addModules();
172
173 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
174
175 $this->numItems = $this->countItems( $dbr ); // TODO kill me
176 if ( $this->numItems == 0 ) {
177 $output->addWikiMsg( 'nowatchlist' );
178 return;
179 }
180
181 // Fetch results, prepare a batch link existence check query
182 $conds = $this->buildMainQueryConds( $opts );
183 $rows = $this->doMainQuery( $conds, $opts );
184 $this->numRows = $rows->numRows(); // TODO kill me
185 if ( $rows === false ) {
186 $this->doHeader( $opts );
187
188 return;
189 }
190
191 $feedFormat = $this->getRequest()->getVal( 'feed' );
192 if ( !$feedFormat ) {
193 $batch = new LinkBatch;
194 foreach ( $rows as $row ) {
195 $batch->add( NS_USER, $row->rc_user_text );
196 $batch->add( NS_USER_TALK, $row->rc_user_text );
197 $batch->add( $row->rc_namespace, $row->rc_title );
198 }
199 $batch->execute();
200 }
201 if ( $feedFormat ) {
202 list( $changesFeed, $formatter ) = $this->getFeedObject( $feedFormat );
203 /** @var ChangesFeed $changesFeed */
204 $changesFeed->execute( $formatter, $rows, $lastmod, $opts );
205 } else {
206 $this->webOutput( $rows, $opts );
207 }
208
209 $rows->free();
210 }
211
212 /**
213 * Return an array of conditions depending of options set in $opts
214 *
215 * @param FormOptions $opts
216 * @return array
217 */
218 public function buildMainQueryConds( FormOptions $opts ) {
219 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
220 $conds = array();
221 $user = $this->getUser();
222
223 // Calculate cutoff
224 if ( $opts['days'] > 0 ) {
225 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
226 }
227
228 # Toggles
229 if ( $opts['hidemyself'] ) {
230 $conds[] = 'rc_user != ' . $user->getId();
231 }
232 if ( $opts['hidebots'] ) {
233 $conds[] = 'rc_bot = 0';
234 }
235 if ( $opts['hideminor'] ) {
236 $conds[] = 'rc_minor = 0';
237 }
238 if ( $opts['hideliu'] ) {
239 $conds[] = 'rc_user = 0';
240 }
241 if ( $opts['hideanons'] ) {
242 $conds[] = 'rc_user != 0';
243 }
244 if ( $user->useRCPatrol() && $opts['hidepatrolled'] ) {
245 $conds[] = 'rc_patrolled != 1';
246 }
247
248 # Namespace filtering
249 if ( $opts['namespace'] !== '' ) {
250 $selectedNS = $dbr->addQuotes( $opts['namespace'] );
251 $operator = $opts['invert'] ? '!=' : '=';
252 $boolean = $opts['invert'] ? 'AND' : 'OR';
253
254 # namespace association (bug 2429)
255 if ( !$opts['associated'] ) {
256 $condition = "rc_namespace $operator $selectedNS";
257 } else {
258 # Also add the associated namespace
259 $associatedNS = $dbr->addQuotes(
260 MWNamespace::getAssociated( $opts['namespace'] )
261 );
262 $condition = "(rc_namespace $operator $selectedNS "
263 . $boolean
264 . " rc_namespace $operator $associatedNS)";
265 }
266
267 $conds[] = $condition;
268 }
269
270 return $conds;
271 }
272
273 /**
274 * Process the query
275 *
276 * @param array $conds
277 * @param FormOptions $opts
278 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
279 */
280 public function doMainQuery( $conds, $opts ) {
281 global $wgShowUpdatedMarker;
282
283 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
284 $user = $this->getUser();
285 # Toggle watchlist content (all recent edits or just the latest)
286 if ( $opts['extended'] ) {
287 $limitWatchlist = $user->getIntOption( 'wllimit' );
288 $usePage = false;
289 } else {
290 # Top log Ids for a page are not stored
291 $nonRevisionTypes = array( RC_LOG );
292 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
293 if ( $nonRevisionTypes ) {
294 $conds[] = $dbr->makeList(
295 array(
296 'rc_this_oldid=page_latest',
297 'rc_type' => $nonRevisionTypes,
298 ),
299 LIST_OR
300 );
301 }
302 $limitWatchlist = 0;
303 $usePage = true;
304 }
305
306 $tables = array( 'recentchanges', 'watchlist' );
307 $fields = RecentChange::selectFields();
308 $join_conds = array(
309 'watchlist' => array(
310 'INNER JOIN',
311 array(
312 'wl_user' => $user->getId(),
313 'wl_namespace=rc_namespace',
314 'wl_title=rc_title'
315 ),
316 ),
317 );
318 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
319 if ( $wgShowUpdatedMarker ) {
320 $fields[] = 'wl_notificationtimestamp';
321 }
322 if ( $limitWatchlist ) {
323 $options['LIMIT'] = $limitWatchlist;
324 }
325
326 $rollbacker = $user->isAllowed( 'rollback' );
327 if ( $usePage || $rollbacker ) {
328 $tables[] = 'page';
329 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
330 if ( $rollbacker ) {
331 $fields[] = 'page_latest';
332 }
333 }
334
335 // Log entries with DELETED_ACTION must not show up unless the user has
336 // the necessary rights.
337 if ( !$user->isAllowed( 'deletedhistory' ) ) {
338 $bitmask = LogPage::DELETED_ACTION;
339 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
340 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
341 } else {
342 $bitmask = 0;
343 }
344 if ( $bitmask ) {
345 $conds[] = $dbr->makeList( array(
346 'rc_type != ' . RC_LOG,
347 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
348 ), LIST_OR );
349 }
350
351
352 ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
353 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $opts ) );
354
355 return $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
356 }
357
358 /**
359 * Send output to the OutputPage object, only called if not used feeds
360 *
361 * @param array $rows Database rows
362 * @param FormOptions $opts
363 */
364 public function webOutput( $rows, $opts ) {
365 global $wgShowUpdatedMarker, $wgRCShowWatchingUsers;
366
367 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
368 $user = $this->getUser();
369
370 $dbr->dataSeek( $rows, 0 );
371
372 $list = ChangesList::newFromContext( $this->getContext() );
373 $list->setWatchlistDivs();
374
375 $s = $list->beginRecentChangesList();
376 $counter = 1;
377 foreach ( $rows as $obj ) {
378 # Make RC entry
379 $rc = RecentChange::newFromRow( $obj );
380 $rc->counter = $counter++;
381
382 if ( $wgShowUpdatedMarker ) {
383 $updated = $obj->wl_notificationtimestamp;
384 } else {
385 $updated = false;
386 }
387
388 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
389 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
390 'COUNT(*)',
391 array(
392 'wl_namespace' => $obj->rc_namespace,
393 'wl_title' => $obj->rc_title,
394 ),
395 __METHOD__ );
396 } else {
397 $rc->numberofWatchingusers = 0;
398 }
399
400 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
401 if ( $changeLine !== false ) {
402 $s .= $changeLine;
403 }
404 }
405 $s .= $list->endRecentChangesList();
406
407 // Print things out
408
409 $output = $this->getOutput();
410
411 $output->addSubtitle(
412 $this->msg( 'watchlistfor2', $user->getName() )
413 ->rawParams( SpecialEditWatchlist::buildTools( null ) )
414 );
415
416 // Output options box
417 $this->doHeader( $opts );
418
419 // Add feed links
420 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
421 if ( $wlToken ) {
422 $this->addFeedLinks( array(
423 'action' => 'feedwatchlist',
424 'allrev' => 1,
425 'wlowner' => $user->getName(),
426 'wltoken' => $wlToken,
427 ) );
428 }
429
430 # Show a message about slave lag, if applicable
431 $lag = wfGetLB()->safeGetLag( $dbr );
432 if ( $lag > 0 ) {
433 $output->showLagWarning( $lag );
434 }
435
436 if ( $rows->numRows() == 0 ) {
437 $output->wrapWikiMsg(
438 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
439 );
440 } else {
441 $output->addHTML( $s );
442 }
443 }
444
445 function setTopText( FormOptions $opts ) {
446 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
447
448 $nondefaults = $opts->getChangedValues();
449 $form = "";
450
451 # Show watchlist header
452 $form .= "<p>";
453 $form .= $this->msg( 'watchlist-details' )->numParams( $this->numItems )->parse() . "\n";
454 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
455 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
456 }
457 if ( $wgShowUpdatedMarker ) {
458 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
459 }
460 $form .= "</p>";
461
462 if ( $wgShowUpdatedMarker ) {
463 $form .= Xml::openElement( 'form', array( 'method' => 'post',
464 'action' => $this->getPageTitle()->getLocalURL(),
465 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
466 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
467 Html::hidden( 'reset', 'all' ) . "\n";
468 foreach ( $nondefaults as $key => $value ) {
469 $form .= Html::hidden( $key, $value ) . "\n";
470 }
471 $form .= Xml::closeElement( 'form' ) . "\n";
472 }
473
474 $form .= Xml::openElement( 'form', array(
475 'method' => 'post',
476 'action' => $this->getPageTitle()->getLocalURL(),
477 'id' => 'mw-watchlist-form'
478 ) );
479 $form .= Xml::fieldset(
480 $this->msg( 'watchlist-options' )->text(),
481 false,
482 array( 'id' => 'mw-watchlist-options' )
483 );
484
485 $form .= SpecialRecentChanges::makeLegend( $this->getContext() );
486
487 $this->getOutput()->addHTML( $form );
488 }
489
490 /**
491 * Return the text to be displayed above the changes
492 *
493 * @param FormOptions $opts
494 * @return string XHTML
495 */
496 public function doHeader( $opts ) {
497 global $wgScript;
498
499 $user = $this->getUser();
500
501 $this->setTopText( $opts );
502
503 $lang = $this->getLanguage();
504 $wlInfo = '';
505 if ( $opts['days'] > 0 ) {
506 $timestamp = wfTimestampNow();
507 $wlInfo = $this->msg( 'wlnote' )->numParams( $this->numRows, round( $opts['days'] * 24 ) )->params(
508 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
509 }
510
511 $nondefaults = $opts->getChangedValues();
512 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
513
514 # Spit out some control panel links
515 $filters = array(
516 'hideminor' => 'rcshowhideminor',
517 'hidebots' => 'rcshowhidebots',
518 'hideanons' => 'rcshowhideanons',
519 'hideliu' => 'rcshowhideliu',
520 'hidemyself' => 'rcshowhidemine',
521 'hidepatrolled' => 'rcshowhidepatr'
522 );
523 foreach ( $this->getCustomFilters() as $key => $params ) {
524 $filters[$key] = $params['msg'];
525 }
526 // Disable some if needed
527 if ( !$user->useNPPatrol() ) {
528 unset( $filters['hidepatrolled'] );
529 }
530
531 $links = array();
532 foreach ( $filters as $name => $msg ) {
533 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
534 }
535
536 $hiddenFields = $nondefaults;
537 unset( $hiddenFields['namespace'] );
538 unset( $hiddenFields['invert'] );
539 unset( $hiddenFields['associated'] );
540
541 # Create output
542 $form = '';
543
544 # Namespace filter and put the whole form together.
545 $form .= $wlInfo;
546 $form .= $cutofflinks;
547 $form .= $lang->pipeList( $links ) . "\n";
548 $form .= "<hr />\n<p>";
549 $form .= Html::namespaceSelector(
550 array(
551 'selected' => $opts['namespace'],
552 'all' => '',
553 'label' => $this->msg( 'namespace' )->text()
554 ), array(
555 'name' => 'namespace',
556 'id' => 'namespace',
557 'class' => 'namespaceselector',
558 )
559 ) . '&#160;';
560 $form .= Xml::checkLabel(
561 $this->msg( 'invert' )->text(),
562 'invert',
563 'nsinvert',
564 $opts['invert'],
565 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
566 ) . '&#160;';
567 $form .= Xml::checkLabel(
568 $this->msg( 'namespace_association' )->text(),
569 'associated',
570 'nsassociated',
571 $opts['associated'],
572 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
573 ) . '&#160;';
574 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
575 foreach ( $hiddenFields as $key => $value ) {
576 $form .= Html::hidden( $key, $value ) . "\n";
577 }
578 $form .= Xml::closeElement( 'fieldset' ) . "\n";
579 $form .= Xml::closeElement( 'form' ) . "\n";
580 $this->getOutput()->addHTML( $form );
581
582 $this->setBottomText( $opts );
583 }
584
585 protected function showHideLink( $options, $message, $name, $value ) {
586 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
587 $options[$name] = 1 - (int)$value;
588
589 return $this->msg( $message )->rawParams( Linker::linkKnown( $this->getPageTitle(), $label, array(), $options ) )->escaped();
590 }
591
592 protected function hoursLink( $h, $options = array() ) {
593 $options['days'] = ( $h / 24.0 );
594
595 return Linker::linkKnown(
596 $this->getPageTitle(),
597 $this->getLanguage()->formatNum( $h ),
598 array(),
599 $options
600 );
601 }
602
603 protected function daysLink( $d, $options = array() ) {
604 $options['days'] = $d;
605 $message = ( $d ? $this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
606
607 return Linker::linkKnown(
608 $this->getPageTitle(),
609 $message,
610 array(),
611 $options
612 );
613 }
614
615 /**
616 * Returns html
617 *
618 * @param int $days This gets overwritten, so is not used
619 * @param array $options Query parameters for URL
620 * @return string
621 */
622 protected function cutoffLinks( $days, $options = array() ) {
623 $hours = array( 1, 2, 6, 12 );
624 $days = array( 1, 3, 7 );
625 $i = 0;
626 foreach ( $hours as $h ) {
627 $hours[$i++] = $this->hoursLink( $h, $options );
628 }
629 $i = 0;
630 foreach ( $days as $d ) {
631 $days[$i++] = $this->daysLink( $d, $options );
632 }
633 return $this->msg( 'wlshowlast' )->rawParams(
634 $this->getLanguage()->pipeList( $hours ),
635 $this->getLanguage()->pipeList( $days ),
636 $this->daysLink( 0, $options ) )->parse();
637 }
638
639 /**
640 * Count the number of items on a user's watchlist
641 *
642 * @param DatabaseBase $dbr A database connection
643 * @return Integer
644 */
645 protected function countItems( $dbr ) {
646 # Fetch the raw count
647 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
648 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
649 $row = $dbr->fetchObject( $rows );
650 $count = $row->count;
651
652 return floor( $count / 2 );
653 }
654 }