6ebe9a8074d06109b8ab1500d1a543ad6381783e
[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
22 */
23
24 /**
25 * A special page that lists last changes made to the wiki,
26 * limited to user-defined list of titles.
27 *
28 * @ingroup SpecialPage
29 */
30 class SpecialWatchlist extends ChangesListSpecialPage {
31 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
32 parent::__construct( $page, $restriction );
33 }
34
35 /**
36 * Main execution point
37 *
38 * @param string $subpage
39 */
40 function execute( $subpage ) {
41 // Anons don't get a watchlist
42 $this->requireLogin( 'watchlistanontext' );
43
44 $output = $this->getOutput();
45 $request = $this->getRequest();
46 $this->addHelpLink( 'Help:Watching pages' );
47 $output->addModules( array(
48 'mediawiki.special.changeslist.visitedstatus',
49 ) );
50
51 $mode = SpecialEditWatchlist::getMode( $request, $subpage );
52 if ( $mode !== false ) {
53 if ( $mode === SpecialEditWatchlist::EDIT_RAW ) {
54 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'raw' );
55 } elseif ( $mode === SpecialEditWatchlist::EDIT_CLEAR ) {
56 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'clear' );
57 } else {
58 $title = SpecialPage::getTitleFor( 'EditWatchlist' );
59 }
60
61 $output->redirect( $title->getLocalURL() );
62
63 return;
64 }
65
66 $this->checkPermissions();
67
68 $user = $this->getUser();
69 $opts = $this->getOptions();
70
71 $config = $this->getConfig();
72 if ( ( $config->get( 'EnotifWatchlist' ) || $config->get( 'ShowUpdatedMarker' ) )
73 && $request->getVal( 'reset' )
74 && $request->wasPosted()
75 ) {
76 $user->clearAllNotifications();
77 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
78
79 return;
80 }
81
82 parent::execute( $subpage );
83 }
84
85 /**
86 * Return an array of subpages that this special page will accept.
87 *
88 * @see also SpecialEditWatchlist::getSubpagesForPrefixSearch
89 * @return string[] subpages
90 */
91 public function getSubpagesForPrefixSearch() {
92 return array(
93 'clear',
94 'edit',
95 'raw',
96 );
97 }
98
99 /**
100 * Get a FormOptions object containing the default options
101 *
102 * @return FormOptions
103 */
104 public function getDefaultOptions() {
105 $opts = parent::getDefaultOptions();
106 $user = $this->getUser();
107
108 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions::FLOAT );
109 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
110 if ( $this->getRequest()->getVal( 'action' ) == 'submit' ) {
111 // The user has submitted the form, so we dont need the default values
112 return $opts;
113 }
114
115 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
116 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
117 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
118 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
119 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
120 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
121
122 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
123 $opts->add( 'hidecategorization', $user->getBoolOption( 'watchlisthidecategorization' ) );
124 }
125
126 return $opts;
127 }
128
129 /**
130 * Get custom show/hide filters
131 *
132 * @return array Map of filter URL param names to properties (msg/default)
133 */
134 protected function getCustomFilters() {
135 if ( $this->customFilters === null ) {
136 $this->customFilters = parent::getCustomFilters();
137 Hooks::run( 'SpecialWatchlistFilters', array( $this, &$this->customFilters ), '1.23' );
138 }
139
140 return $this->customFilters;
141 }
142
143 /**
144 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
145 *
146 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
147 * to the current ones.
148 *
149 * @param FormOptions $opts
150 * @return FormOptions
151 */
152 protected function fetchOptionsFromRequest( $opts ) {
153 static $compatibilityMap = array(
154 'hideMinor' => 'hideminor',
155 'hideBots' => 'hidebots',
156 'hideAnons' => 'hideanons',
157 'hideLiu' => 'hideliu',
158 'hidePatrolled' => 'hidepatrolled',
159 'hideOwn' => 'hidemyself',
160 );
161
162 $params = $this->getRequest()->getValues();
163 foreach ( $compatibilityMap as $from => $to ) {
164 if ( isset( $params[$from] ) ) {
165 $params[$to] = $params[$from];
166 unset( $params[$from] );
167 }
168 }
169
170 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
171 // methods defined on WebRequest and removing this dependency would cause some code duplication.
172 $request = new DerivativeRequest( $this->getRequest(), $params );
173 $opts->fetchValuesFromRequest( $request );
174
175 return $opts;
176 }
177
178 /**
179 * Return an array of conditions depending of options set in $opts
180 *
181 * @param FormOptions $opts
182 * @return array
183 */
184 public function buildMainQueryConds( FormOptions $opts ) {
185 $dbr = $this->getDB();
186 $conds = parent::buildMainQueryConds( $opts );
187
188 // Calculate cutoff
189 if ( $opts['days'] > 0 ) {
190 $conds[] = 'rc_timestamp > ' .
191 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
192 }
193
194 return $conds;
195 }
196
197 /**
198 * Process the query
199 *
200 * @param array $conds
201 * @param FormOptions $opts
202 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
203 */
204 public function doMainQuery( $conds, $opts ) {
205 $dbr = $this->getDB();
206 $user = $this->getUser();
207
208 # Toggle watchlist content (all recent edits or just the latest)
209 if ( $opts['extended'] ) {
210 $limitWatchlist = $user->getIntOption( 'wllimit' );
211 $usePage = false;
212 } else {
213 # Top log Ids for a page are not stored
214 $nonRevisionTypes = array( RC_LOG );
215 Hooks::run( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
216 if ( $nonRevisionTypes ) {
217 $conds[] = $dbr->makeList(
218 array(
219 'rc_this_oldid=page_latest',
220 'rc_type' => $nonRevisionTypes,
221 ),
222 LIST_OR
223 );
224 }
225 $limitWatchlist = 0;
226 $usePage = true;
227 }
228
229 $tables = array( 'recentchanges', 'watchlist' );
230 $fields = RecentChange::selectFields();
231 $query_options = array( 'ORDER BY' => 'rc_timestamp DESC' );
232 $join_conds = array(
233 'watchlist' => array(
234 'INNER JOIN',
235 array(
236 'wl_user' => $user->getId(),
237 'wl_namespace=rc_namespace',
238 'wl_title=rc_title'
239 ),
240 ),
241 );
242
243 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
244 $fields[] = 'wl_notificationtimestamp';
245 }
246 if ( $limitWatchlist ) {
247 $query_options['LIMIT'] = $limitWatchlist;
248 }
249
250 $rollbacker = $user->isAllowed( 'rollback' );
251 if ( $usePage || $rollbacker ) {
252 $tables[] = 'page';
253 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
254 if ( $rollbacker ) {
255 $fields[] = 'page_latest';
256 }
257 }
258
259 // Log entries with DELETED_ACTION must not show up unless the user has
260 // the necessary rights.
261 if ( !$user->isAllowed( 'deletedhistory' ) ) {
262 $bitmask = LogPage::DELETED_ACTION;
263 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
264 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
265 } else {
266 $bitmask = 0;
267 }
268 if ( $bitmask ) {
269 $conds[] = $dbr->makeList( array(
270 'rc_type != ' . RC_LOG,
271 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
272 ), LIST_OR );
273 }
274
275 ChangeTags::modifyDisplayQuery(
276 $tables,
277 $fields,
278 $conds,
279 $join_conds,
280 $query_options,
281 ''
282 );
283
284 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
285
286 return $dbr->select(
287 $tables,
288 $fields,
289 $conds,
290 __METHOD__,
291 $query_options,
292 $join_conds
293 );
294 }
295
296 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options,
297 &$join_conds, $opts
298 ) {
299 return parent::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
300 && Hooks::run(
301 'SpecialWatchlistQuery',
302 array( &$conds, &$tables, &$join_conds, &$fields, $opts ),
303 '1.23'
304 );
305 }
306
307 /**
308 * Return a IDatabase object for reading
309 *
310 * @return IDatabase
311 */
312 protected function getDB() {
313 return wfGetDB( DB_SLAVE, 'watchlist' );
314 }
315
316 /**
317 * Output feed links.
318 */
319 public function outputFeedLinks() {
320 $user = $this->getUser();
321 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
322 if ( $wlToken ) {
323 $this->addFeedLinks( array(
324 'action' => 'feedwatchlist',
325 'allrev' => 1,
326 'wlowner' => $user->getName(),
327 'wltoken' => $wlToken,
328 ) );
329 }
330 }
331
332 /**
333 * Build and output the actual changes list.
334 *
335 * @param ResultWrapper $rows Database rows
336 * @param FormOptions $opts
337 */
338 public function outputChangesList( $rows, $opts ) {
339 $dbr = $this->getDB();
340 $user = $this->getUser();
341 $output = $this->getOutput();
342
343 # Show a message about slave lag, if applicable
344 $lag = wfGetLB()->safeGetLag( $dbr );
345 if ( $lag > 0 ) {
346 $output->showLagWarning( $lag );
347 }
348
349 # If no rows to display, show message before try to render the list
350 if ( $rows->numRows() == 0 ) {
351 $output->wrapWikiMsg(
352 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
353 );
354 return;
355 }
356
357 $dbr->dataSeek( $rows, 0 );
358
359 $list = ChangesList::newFromContext( $this->getContext() );
360 $list->setWatchlistDivs();
361 $list->initChangesListRows( $rows );
362 $dbr->dataSeek( $rows, 0 );
363
364 $s = $list->beginRecentChangesList();
365 $counter = 1;
366 foreach ( $rows as $obj ) {
367 # Make RC entry
368 $rc = RecentChange::newFromRow( $obj );
369 $rc->counter = $counter++;
370
371 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
372 $updated = $obj->wl_notificationtimestamp;
373 } else {
374 $updated = false;
375 }
376
377 if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
378 && $user->getOption( 'shownumberswatching' )
379 ) {
380 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
381 'COUNT(*)',
382 array(
383 'wl_namespace' => $obj->rc_namespace,
384 'wl_title' => $obj->rc_title,
385 ),
386 __METHOD__ );
387 } else {
388 $rc->numberofWatchingusers = 0;
389 }
390
391 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
392 if ( $changeLine !== false ) {
393 $s .= $changeLine;
394 }
395 }
396 $s .= $list->endRecentChangesList();
397
398 $output->addHTML( $s );
399 }
400
401 /**
402 * Set the text to be displayed above the changes
403 *
404 * @param FormOptions $opts
405 * @param int $numRows Number of rows in the result to show after this header
406 */
407 public function doHeader( $opts, $numRows ) {
408 $user = $this->getUser();
409 $out = $this->getOutput();
410
411 // if the user wishes, that the watchlist is reloaded, whenever a filter changes,
412 // add the module for that
413 if ( $user->getBoolOption( 'watchlistreloadautomatically' ) ) {
414 $out->addModules( array( 'mediawiki.special.watchlist' ) );
415 }
416
417 $out->addSubtitle(
418 $this->msg( 'watchlistfor2', $user->getName() )
419 ->rawParams( SpecialEditWatchlist::buildTools( null ) )
420 );
421
422 $this->setTopText( $opts );
423
424 $lang = $this->getLanguage();
425 if ( $opts['days'] > 0 ) {
426 $days = $opts['days'];
427 } else {
428 $days = $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 );
429 }
430 $timestamp = wfTimestampNow();
431 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $days * 24 ) )->params(
432 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
433 )->parse() . "<br />\n";
434
435 $nondefaults = $opts->getChangedValues();
436 $cutofflinks = $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts );
437
438 # Spit out some control panel links
439 $filters = array(
440 'hideminor' => 'wlshowhideminor',
441 'hidebots' => 'wlshowhidebots',
442 'hideanons' => 'wlshowhideanons',
443 'hideliu' => 'wlshowhideliu',
444 'hidemyself' => 'wlshowhidemine',
445 'hidepatrolled' => 'wlshowhidepatr'
446 );
447
448 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
449 $filters['hidecategorization'] = 'wlshowhidecategorization';
450 }
451
452 foreach ( $this->getCustomFilters() as $key => $params ) {
453 $filters[$key] = $params['msg'];
454 }
455 // Disable some if needed
456 if ( !$user->useRCPatrol() ) {
457 unset( $filters['hidepatrolled'] );
458 }
459
460 $links = array();
461 foreach ( $filters as $name => $msg ) {
462 $links[] = $this->showHideCheck( $nondefaults, $msg, $name, $opts[$name] );
463 }
464
465 $hiddenFields = $nondefaults;
466 $hiddenFields['action'] = 'submit';
467 unset( $hiddenFields['namespace'] );
468 unset( $hiddenFields['invert'] );
469 unset( $hiddenFields['associated'] );
470 unset( $hiddenFields['days'] );
471 foreach ( $filters as $key => $value ) {
472 unset( $hiddenFields[$key] );
473 }
474
475 # Create output
476 $form = '';
477
478 # Namespace filter and put the whole form together.
479 $form .= $wlInfo;
480 $form .= $cutofflinks;
481 $form .= $this->msg( 'watchlist-hide' ) .
482 $this->msg( 'colon-separator' )->escaped() .
483 implode( ' ', $links );
484 $form .= "\n<br />\n";
485 $form .= Html::namespaceSelector(
486 array(
487 'selected' => $opts['namespace'],
488 'all' => '',
489 'label' => $this->msg( 'namespace' )->text()
490 ), array(
491 'name' => 'namespace',
492 'id' => 'namespace',
493 'class' => 'namespaceselector',
494 )
495 ) . "\n";
496 $form .= '<span class="mw-input-with-label">' . Xml::checkLabel(
497 $this->msg( 'invert' )->text(),
498 'invert',
499 'nsinvert',
500 $opts['invert'],
501 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
502 ) . "</span>\n";
503 $form .= '<span class="mw-input-with-label">' . Xml::checkLabel(
504 $this->msg( 'namespace_association' )->text(),
505 'associated',
506 'nsassociated',
507 $opts['associated'],
508 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
509 ) . "</span>\n";
510 $form .= Xml::submitButton( $this->msg( 'watchlist-submit' )->text() ) . "\n";
511 foreach ( $hiddenFields as $key => $value ) {
512 $form .= Html::hidden( $key, $value ) . "\n";
513 }
514 $form .= Xml::closeElement( 'fieldset' ) . "\n";
515 $form .= Xml::closeElement( 'form' ) . "\n";
516 $this->getOutput()->addHTML( $form );
517
518 $this->setBottomText( $opts );
519 }
520
521 function cutoffselector( $options ) {
522 // Cast everything to strings immediately, so that we know all of the values have the same
523 // precision, and can be compared with '==='. 2/24 has a few more decimal places than its
524 // default string representation, for example, and would confuse comparisons.
525
526 // Misleadingly, the 'days' option supports hours too.
527 $days = array_map( 'strval', array( 1/24, 2/24, 6/24, 12/24, 1, 3, 7 ) );
528
529 $userWatchlistOption = (string)$this->getUser()->getOption( 'watchlistdays' );
530 // add the user preference, if it isn't available already
531 if ( !in_array( $userWatchlistOption, $days ) && $userWatchlistOption !== '0' ) {
532 $days[] = $userWatchlistOption;
533 }
534
535 $maxDays = (string)( $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 ) );
536 // add the maximum possible value, if it isn't available already
537 if ( !in_array( $maxDays, $days ) ) {
538 $days[] = $maxDays;
539 }
540
541 $selected = (string)$options['days'];
542 if ( $selected <= 0 ) {
543 $selected = $maxDays;
544 }
545
546 // add the currently selected value, if it isn't available already
547 if ( !in_array( $selected, $days ) ) {
548 $days[] = $selected;
549 }
550
551 $select = new XmlSelect( 'days', 'days', $selected );
552
553 asort( $days );
554 foreach ( $days as $value ) {
555 if ( $value < 1 ) {
556 $name = $this->msg( 'hours' )->numParams( $value * 24 )->text();
557 } else {
558 $name = $this->msg( 'days' )->numParams( $value )->text();
559 }
560 $select->addOption( $name, $value );
561 }
562
563 return $select->getHTML() . "\n<br />\n";
564 }
565
566 function setTopText( FormOptions $opts ) {
567 $nondefaults = $opts->getChangedValues();
568 $form = "";
569 $user = $this->getUser();
570
571 $dbr = $this->getDB();
572 $numItems = $this->countItems( $dbr );
573 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
574
575 // Show watchlist header
576 $form .= "<p>";
577 if ( $numItems == 0 ) {
578 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
579 } else {
580 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
581 if ( $this->getConfig()->get( 'EnotifWatchlist' )
582 && $user->getOption( 'enotifwatchlistpages' )
583 ) {
584 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
585 }
586 if ( $showUpdatedMarker ) {
587 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
588 }
589 }
590 $form .= "</p>";
591
592 if ( $numItems > 0 && $showUpdatedMarker ) {
593 $form .= Xml::openElement( 'form', array( 'method' => 'post',
594 'action' => $this->getPageTitle()->getLocalURL(),
595 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
596 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
597 Html::hidden( 'reset', 'all' ) . "\n";
598 foreach ( $nondefaults as $key => $value ) {
599 $form .= Html::hidden( $key, $value ) . "\n";
600 }
601 $form .= Xml::closeElement( 'form' ) . "\n";
602 }
603
604 $form .= Xml::openElement( 'form', array(
605 'method' => 'get',
606 'action' => $this->getPageTitle()->getLocalURL(),
607 'id' => 'mw-watchlist-form'
608 ) );
609 $form .= Xml::fieldset(
610 $this->msg( 'watchlist-options' )->text(),
611 false,
612 array( 'id' => 'mw-watchlist-options' )
613 );
614
615 $form .= $this->makeLegend();
616
617 $this->getOutput()->addHTML( $form );
618 }
619
620 protected function showHideCheck( $options, $message, $name, $value ) {
621 $options[$name] = 1 - (int)$value;
622
623 return '<span class="mw-input-with-label">' . Xml::checkLabel(
624 $this->msg( $message, '' )->text(),
625 $name,
626 $name,
627 (int)$value
628 ) . '</span>';
629 }
630
631 /**
632 * Count the number of items on a user's watchlist
633 *
634 * @param IDatabase $dbr A database connection
635 * @return int
636 */
637 protected function countItems( $dbr ) {
638 # Fetch the raw count
639 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
640 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
641 $row = $dbr->fetchObject( $rows );
642 $count = $row->count;
643
644 return floor( $count / 2 );
645 }
646 }