Fix access to invalid symbol in Special:Userrights
[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 $wlInfo = '';
426 if ( $opts['days'] > 0 ) {
427 $timestamp = wfTimestampNow();
428 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $opts['days'] * 24 ) )->params(
429 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
430 )->parse() . "<br />\n";
431 }
432
433 $nondefaults = $opts->getChangedValues();
434 $cutofflinks = $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts );
435
436 # Spit out some control panel links
437 $filters = array(
438 'hideminor' => 'wlshowhideminor',
439 'hidebots' => 'wlshowhidebots',
440 'hideanons' => 'wlshowhideanons',
441 'hideliu' => 'wlshowhideliu',
442 'hidemyself' => 'wlshowhidemine',
443 'hidepatrolled' => 'wlshowhidepatr'
444 );
445
446 if ( $this->getConfig()->get( 'RCWatchCategoryMembership' ) ) {
447 $filters['hidecategorization'] = 'wlshowhidecategorization';
448 }
449
450 foreach ( $this->getCustomFilters() as $key => $params ) {
451 $filters[$key] = $params['msg'];
452 }
453 // Disable some if needed
454 if ( !$user->useRCPatrol() ) {
455 unset( $filters['hidepatrolled'] );
456 }
457
458 $links = array();
459 foreach ( $filters as $name => $msg ) {
460 $links[] = $this->showHideCheck( $nondefaults, $msg, $name, $opts[$name] );
461 }
462
463 $hiddenFields = $nondefaults;
464 $hiddenFields['action'] = 'submit';
465 unset( $hiddenFields['namespace'] );
466 unset( $hiddenFields['invert'] );
467 unset( $hiddenFields['associated'] );
468 unset( $hiddenFields['days'] );
469 foreach ( $filters as $key => $value ) {
470 unset( $hiddenFields[$key] );
471 }
472
473 # Create output
474 $form = '';
475
476 # Namespace filter and put the whole form together.
477 $form .= $wlInfo;
478 $form .= $cutofflinks;
479 $form .= $this->msg( 'watchlist-hide' ) .
480 $this->msg( 'colon-separator' )->escaped() .
481 implode( ' ', $links );
482 $form .= "\n<br />\n";
483 $form .= Html::namespaceSelector(
484 array(
485 'selected' => $opts['namespace'],
486 'all' => '',
487 'label' => $this->msg( 'namespace' )->text()
488 ), array(
489 'name' => 'namespace',
490 'id' => 'namespace',
491 'class' => 'namespaceselector',
492 )
493 ) . "\n";
494 $form .= '<span class="mw-input-with-label">' . Xml::checkLabel(
495 $this->msg( 'invert' )->text(),
496 'invert',
497 'nsinvert',
498 $opts['invert'],
499 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
500 ) . "</span>\n";
501 $form .= '<span class="mw-input-with-label">' . Xml::checkLabel(
502 $this->msg( 'namespace_association' )->text(),
503 'associated',
504 'nsassociated',
505 $opts['associated'],
506 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
507 ) . "</span>\n";
508 $form .= Xml::submitButton( $this->msg( 'watchlist-submit' )->text() ) . "\n";
509 foreach ( $hiddenFields as $key => $value ) {
510 $form .= Html::hidden( $key, $value ) . "\n";
511 }
512 $form .= Xml::closeElement( 'fieldset' ) . "\n";
513 $form .= Xml::closeElement( 'form' ) . "\n";
514 $this->getOutput()->addHTML( $form );
515
516 $this->setBottomText( $opts );
517 }
518
519 function cutoffselector( $options ) {
520 // Cast everything to strings immediately, so that we know all of the values have the same
521 // precision, and can be compared with '==='. 2/24 has a few more decimal places than its
522 // default string representation, for example, and would confuse comparisons.
523
524 // Misleadingly, the 'days' option supports hours too.
525 $days = array_map( 'strval', array( 1/24, 2/24, 6/24, 12/24, 1, 3, 7 ) );
526
527 $userWatchlistOption = (string)$this->getUser()->getOption( 'watchlistdays' );
528 // add the user preference, if it isn't available already
529 if ( !in_array( $userWatchlistOption, $days ) && $userWatchlistOption !== '0' ) {
530 $days[] = $userWatchlistOption;
531 }
532
533 $selected = (string)$options['days'];
534 // add the currently selected value, if it isn't available already
535 if ( !in_array( $selected, $days ) && $selected !== '0' ) {
536 $days[] = $selected;
537 }
538
539 $select = new XmlSelect( 'days', 'days', $selected );
540
541 asort( $days );
542 foreach ( $days as $value ) {
543 if ( $value < 1 ) {
544 $name = $this->msg( 'hours' )->numParams( $value * 24 )->text();
545 } else {
546 $name = $this->msg( 'days' )->numParams( $value )->text();
547 }
548 $select->addOption( $name, $value );
549 }
550
551 // 'all' option
552 $name = $this->msg( 'watchlistall2' )->text();
553 $value = '0';
554 $select->addOption( $name, $value );
555
556 return $select->getHTML() . "\n<br />\n";
557 }
558
559 function setTopText( FormOptions $opts ) {
560 $nondefaults = $opts->getChangedValues();
561 $form = "";
562 $user = $this->getUser();
563
564 $dbr = $this->getDB();
565 $numItems = $this->countItems( $dbr );
566 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
567
568 // Show watchlist header
569 $form .= "<p>";
570 if ( $numItems == 0 ) {
571 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
572 } else {
573 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
574 if ( $this->getConfig()->get( 'EnotifWatchlist' )
575 && $user->getOption( 'enotifwatchlistpages' )
576 ) {
577 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
578 }
579 if ( $showUpdatedMarker ) {
580 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
581 }
582 }
583 $form .= "</p>";
584
585 if ( $numItems > 0 && $showUpdatedMarker ) {
586 $form .= Xml::openElement( 'form', array( 'method' => 'post',
587 'action' => $this->getPageTitle()->getLocalURL(),
588 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
589 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
590 Html::hidden( 'reset', 'all' ) . "\n";
591 foreach ( $nondefaults as $key => $value ) {
592 $form .= Html::hidden( $key, $value ) . "\n";
593 }
594 $form .= Xml::closeElement( 'form' ) . "\n";
595 }
596
597 $form .= Xml::openElement( 'form', array(
598 'method' => 'get',
599 'action' => $this->getPageTitle()->getLocalURL(),
600 'id' => 'mw-watchlist-form'
601 ) );
602 $form .= Xml::fieldset(
603 $this->msg( 'watchlist-options' )->text(),
604 false,
605 array( 'id' => 'mw-watchlist-options' )
606 );
607
608 $form .= SpecialRecentChanges::makeLegend( $this->getContext() );
609
610 $this->getOutput()->addHTML( $form );
611 }
612
613 protected function showHideCheck( $options, $message, $name, $value ) {
614 $options[$name] = 1 - (int)$value;
615
616 return '<span class="mw-input-with-label">' . Xml::checkLabel(
617 $this->msg( $message, '' )->text(),
618 $name,
619 $name,
620 (int)$value
621 ) . '</span>';
622 }
623
624 /**
625 * Count the number of items on a user's watchlist
626 *
627 * @param IDatabase $dbr A database connection
628 * @return int
629 */
630 protected function countItems( $dbr ) {
631 # Fetch the raw count
632 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
633 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
634 $row = $dbr->fetchObject( $rows );
635 $count = $row->count;
636
637 return floor( $count / 2 );
638 }
639 }