Remove "related" searches
[mediawiki.git] / includes / specials / SpecialWatchlist.php
blob8f2f86b92a3753c70404a298cb23f21eb1a02114
1 <?php
2 /**
3 * Implements Special:Watchlist
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * A special page that lists last changes made to the wiki,
26 * limited to user-defined list of titles.
28 * @ingroup SpecialPage
30 class SpecialWatchlist extends ChangesListSpecialPage {
31 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
32 parent::__construct( $page, $restriction );
35 /**
36 * Main execution point
38 * @param string $subpage
40 function execute( $subpage ) {
41 // Anons don't get a watchlist
42 $this->requireLogin( 'watchlistanontext' );
44 $output = $this->getOutput();
45 $request = $this->getRequest();
47 $mode = SpecialEditWatchlist::getMode( $request, $subpage );
48 if ( $mode !== false ) {
49 if ( $mode === SpecialEditWatchlist::EDIT_RAW ) {
50 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'raw' );
51 } elseif ( $mode === SpecialEditWatchlist::EDIT_CLEAR ) {
52 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'clear' );
53 } else {
54 $title = SpecialPage::getTitleFor( 'EditWatchlist' );
57 $output->redirect( $title->getLocalURL() );
59 return;
62 $this->checkPermissions();
64 $user = $this->getUser();
65 $opts = $this->getOptions();
67 $config = $this->getConfig();
68 if ( ( $config->get( 'EnotifWatchlist' ) || $config->get( 'ShowUpdatedMarker' ) )
69 && $request->getVal( 'reset' )
70 && $request->wasPosted()
71 ) {
72 $user->clearAllNotifications();
73 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
75 return;
78 parent::execute( $subpage );
81 /**
82 * Return an array of subpages beginning with $search that this special page will accept.
84 * @param string $search Prefix to search for
85 * @param int $limit Maximum number of results to return
86 * @return string[] Matching subpages
88 public function prefixSearchSubpages( $search, $limit = 10 ) {
89 // See also SpecialEditWatchlist::prefixSearchSubpages
90 return self::prefixSearchArray(
91 $search,
92 $limit,
93 array(
94 'clear',
95 'edit',
96 'raw',
102 * Get a FormOptions object containing the default options
104 * @return FormOptions
106 public function getDefaultOptions() {
107 $opts = parent::getDefaultOptions();
108 $user = $this->getUser();
110 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions::FLOAT );
112 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
113 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
114 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
115 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
116 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
117 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
119 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
121 return $opts;
125 * Get custom show/hide filters
127 * @return array Map of filter URL param names to properties (msg/default)
129 protected function getCustomFilters() {
130 if ( $this->customFilters === null ) {
131 $this->customFilters = parent::getCustomFilters();
132 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters ), '1.23' );
135 return $this->customFilters;
139 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
141 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
142 * to the current ones.
144 * @param FormOptions $opts
145 * @return FormOptions
147 protected function fetchOptionsFromRequest( $opts ) {
148 static $compatibilityMap = array(
149 'hideMinor' => 'hideminor',
150 'hideBots' => 'hidebots',
151 'hideAnons' => 'hideanons',
152 'hideLiu' => 'hideliu',
153 'hidePatrolled' => 'hidepatrolled',
154 'hideOwn' => 'hidemyself',
157 $params = $this->getRequest()->getValues();
158 foreach ( $compatibilityMap as $from => $to ) {
159 if ( isset( $params[$from] ) ) {
160 $params[$to] = $params[$from];
161 unset( $params[$from] );
165 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
166 // methods defined on WebRequest and removing this dependency would cause some code duplication.
167 $request = new DerivativeRequest( $this->getRequest(), $params );
168 $opts->fetchValuesFromRequest( $request );
170 return $opts;
174 * Return an array of conditions depending of options set in $opts
176 * @param FormOptions $opts
177 * @return array
179 public function buildMainQueryConds( FormOptions $opts ) {
180 $dbr = $this->getDB();
181 $conds = parent::buildMainQueryConds( $opts );
183 // Calculate cutoff
184 if ( $opts['days'] > 0 ) {
185 $conds[] = 'rc_timestamp > ' .
186 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
189 return $conds;
193 * Process the query
195 * @param array $conds
196 * @param FormOptions $opts
197 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
199 public function doMainQuery( $conds, $opts ) {
200 $dbr = $this->getDB();
201 $user = $this->getUser();
203 # Toggle watchlist content (all recent edits or just the latest)
204 if ( $opts['extended'] ) {
205 $limitWatchlist = $user->getIntOption( 'wllimit' );
206 $usePage = false;
207 } else {
208 # Top log Ids for a page are not stored
209 $nonRevisionTypes = array( RC_LOG );
210 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
211 if ( $nonRevisionTypes ) {
212 $conds[] = $dbr->makeList(
213 array(
214 'rc_this_oldid=page_latest',
215 'rc_type' => $nonRevisionTypes,
217 LIST_OR
220 $limitWatchlist = 0;
221 $usePage = true;
224 $tables = array( 'recentchanges', 'watchlist' );
225 $fields = RecentChange::selectFields();
226 $query_options = array( 'ORDER BY' => 'rc_timestamp DESC' );
227 $join_conds = array(
228 'watchlist' => array(
229 'INNER JOIN',
230 array(
231 'wl_user' => $user->getId(),
232 'wl_namespace=rc_namespace',
233 'wl_title=rc_title'
238 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
239 $fields[] = 'wl_notificationtimestamp';
241 if ( $limitWatchlist ) {
242 $query_options['LIMIT'] = $limitWatchlist;
245 $rollbacker = $user->isAllowed( 'rollback' );
246 if ( $usePage || $rollbacker ) {
247 $tables[] = 'page';
248 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
249 if ( $rollbacker ) {
250 $fields[] = 'page_latest';
254 // Log entries with DELETED_ACTION must not show up unless the user has
255 // the necessary rights.
256 if ( !$user->isAllowed( 'deletedhistory' ) ) {
257 $bitmask = LogPage::DELETED_ACTION;
258 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
259 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
260 } else {
261 $bitmask = 0;
263 if ( $bitmask ) {
264 $conds[] = $dbr->makeList( array(
265 'rc_type != ' . RC_LOG,
266 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
267 ), LIST_OR );
270 ChangeTags::modifyDisplayQuery(
271 $tables,
272 $fields,
273 $conds,
274 $join_conds,
275 $query_options,
279 $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
281 return $dbr->select(
282 $tables,
283 $fields,
284 $conds,
285 __METHOD__,
286 $query_options,
287 $join_conds
291 protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts ) {
292 return parent::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts )
293 && wfRunHooks(
294 'SpecialWatchlistQuery',
295 array( &$conds, &$tables, &$join_conds, &$fields, $opts ),
296 '1.23'
301 * Return a DatabaseBase object for reading
303 * @return DatabaseBase
305 protected function getDB() {
306 return wfGetDB( DB_SLAVE, 'watchlist' );
310 * Output feed links.
312 public function outputFeedLinks() {
313 $user = $this->getUser();
314 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
315 if ( $wlToken ) {
316 $this->addFeedLinks( array(
317 'action' => 'feedwatchlist',
318 'allrev' => 1,
319 'wlowner' => $user->getName(),
320 'wltoken' => $wlToken,
321 ) );
326 * Build and output the actual changes list.
328 * @param ResultWrapper $rows Database rows
329 * @param FormOptions $opts
331 public function outputChangesList( $rows, $opts ) {
332 $dbr = $this->getDB();
333 $user = $this->getUser();
334 $output = $this->getOutput();
336 # Show a message about slave lag, if applicable
337 $lag = wfGetLB()->safeGetLag( $dbr );
338 if ( $lag > 0 ) {
339 $output->showLagWarning( $lag );
342 # If no rows to display, show message before try to render the list
343 if ( $rows->numRows() == 0 ) {
344 $output->wrapWikiMsg(
345 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
347 return;
350 $dbr->dataSeek( $rows, 0 );
352 $list = ChangesList::newFromContext( $this->getContext() );
353 $list->setWatchlistDivs();
354 $list->initChangesListRows( $rows );
355 $dbr->dataSeek( $rows, 0 );
357 $s = $list->beginRecentChangesList();
358 $counter = 1;
359 foreach ( $rows as $obj ) {
360 # Make RC entry
361 $rc = RecentChange::newFromRow( $obj );
362 $rc->counter = $counter++;
364 if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
365 $updated = $obj->wl_notificationtimestamp;
366 } else {
367 $updated = false;
370 if ( $this->getConfig()->get( 'RCShowWatchingUsers' ) && $user->getOption( 'shownumberswatching' ) ) {
371 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
372 'COUNT(*)',
373 array(
374 'wl_namespace' => $obj->rc_namespace,
375 'wl_title' => $obj->rc_title,
377 __METHOD__ );
378 } else {
379 $rc->numberofWatchingusers = 0;
382 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
383 if ( $changeLine !== false ) {
384 $s .= $changeLine;
387 $s .= $list->endRecentChangesList();
389 $output->addHTML( $s );
393 * Set the text to be displayed above the changes
395 * @param FormOptions $opts
396 * @param int $numRows Number of rows in the result to show after this header
398 public function doHeader( $opts, $numRows ) {
399 $user = $this->getUser();
401 $this->getOutput()->addSubtitle(
402 $this->msg( 'watchlistfor2', $user->getName() )
403 ->rawParams( SpecialEditWatchlist::buildTools( null ) )
406 $this->setTopText( $opts );
408 $lang = $this->getLanguage();
409 $wlInfo = '';
410 if ( $opts['days'] > 0 ) {
411 $timestamp = wfTimestampNow();
412 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $opts['days'] * 24 ) )->params(
413 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
414 )->parse() . "<br />\n";
417 $nondefaults = $opts->getChangedValues();
418 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
420 # Spit out some control panel links
421 $filters = array(
422 'hideminor' => 'rcshowhideminor',
423 'hidebots' => 'rcshowhidebots',
424 'hideanons' => 'rcshowhideanons',
425 'hideliu' => 'rcshowhideliu',
426 'hidemyself' => 'rcshowhidemine',
427 'hidepatrolled' => 'rcshowhidepatr'
429 foreach ( $this->getCustomFilters() as $key => $params ) {
430 $filters[$key] = $params['msg'];
432 // Disable some if needed
433 if ( !$user->useNPPatrol() ) {
434 unset( $filters['hidepatrolled'] );
437 $links = array();
438 foreach ( $filters as $name => $msg ) {
439 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
442 $hiddenFields = $nondefaults;
443 unset( $hiddenFields['namespace'] );
444 unset( $hiddenFields['invert'] );
445 unset( $hiddenFields['associated'] );
447 # Create output
448 $form = '';
450 # Namespace filter and put the whole form together.
451 $form .= $wlInfo;
452 $form .= $cutofflinks;
453 $form .= $lang->pipeList( $links ) . "\n";
454 $form .= "<hr />\n<p>";
455 $form .= Html::namespaceSelector(
456 array(
457 'selected' => $opts['namespace'],
458 'all' => '',
459 'label' => $this->msg( 'namespace' )->text()
460 ), array(
461 'name' => 'namespace',
462 'id' => 'namespace',
463 'class' => 'namespaceselector',
465 ) . '&#160;';
466 $form .= Xml::checkLabel(
467 $this->msg( 'invert' )->text(),
468 'invert',
469 'nsinvert',
470 $opts['invert'],
471 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
472 ) . '&#160;';
473 $form .= Xml::checkLabel(
474 $this->msg( 'namespace_association' )->text(),
475 'associated',
476 'nsassociated',
477 $opts['associated'],
478 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
479 ) . '&#160;';
480 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
481 foreach ( $hiddenFields as $key => $value ) {
482 $form .= Html::hidden( $key, $value ) . "\n";
484 $form .= Xml::closeElement( 'fieldset' ) . "\n";
485 $form .= Xml::closeElement( 'form' ) . "\n";
486 $this->getOutput()->addHTML( $form );
488 $this->setBottomText( $opts );
491 function setTopText( FormOptions $opts ) {
492 $nondefaults = $opts->getChangedValues();
493 $form = "";
494 $user = $this->getUser();
496 $dbr = $this->getDB();
497 $numItems = $this->countItems( $dbr );
498 $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
500 // Show watchlist header
501 $form .= "<p>";
502 if ( $numItems == 0 ) {
503 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
504 } else {
505 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
506 if ( $this->getConfig()->get( 'EnotifWatchlist' ) && $user->getOption( 'enotifwatchlistpages' ) ) {
507 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
509 if ( $showUpdatedMarker ) {
510 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
513 $form .= "</p>";
515 if ( $numItems > 0 && $showUpdatedMarker ) {
516 $form .= Xml::openElement( 'form', array( 'method' => 'post',
517 'action' => $this->getPageTitle()->getLocalURL(),
518 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
519 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
520 Html::hidden( 'reset', 'all' ) . "\n";
521 foreach ( $nondefaults as $key => $value ) {
522 $form .= Html::hidden( $key, $value ) . "\n";
524 $form .= Xml::closeElement( 'form' ) . "\n";
527 $form .= Xml::openElement( 'form', array(
528 'method' => 'post',
529 'action' => $this->getPageTitle()->getLocalURL(),
530 'id' => 'mw-watchlist-form'
531 ) );
532 $form .= Xml::fieldset(
533 $this->msg( 'watchlist-options' )->text(),
534 false,
535 array( 'id' => 'mw-watchlist-options' )
538 $form .= SpecialRecentChanges::makeLegend( $this->getContext() );
540 $this->getOutput()->addHTML( $form );
543 protected function showHideLink( $options, $message, $name, $value ) {
544 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
545 $options[$name] = 1 - (int)$value;
547 return $this->msg( $message )
548 ->rawParams( Linker::linkKnown( $this->getPageTitle(), $label, array(), $options ) )
549 ->escaped();
552 protected function hoursLink( $h, $options = array() ) {
553 $options['days'] = ( $h / 24.0 );
555 return Linker::linkKnown(
556 $this->getPageTitle(),
557 $this->getLanguage()->formatNum( $h ),
558 array(),
559 $options
563 protected function daysLink( $d, $options = array() ) {
564 $options['days'] = $d;
565 $message = $d ? $this->getLanguage()->formatNum( $d )
566 : $this->msg( 'watchlistall2' )->escaped();
568 return Linker::linkKnown(
569 $this->getPageTitle(),
570 $message,
571 array(),
572 $options
577 * Returns html
579 * @param int $days This gets overwritten, so is not used
580 * @param array $options Query parameters for URL
581 * @return string
583 protected function cutoffLinks( $days, $options = array() ) {
584 $hours = array( 1, 2, 6, 12 );
585 $days = array( 1, 3, 7 );
586 $i = 0;
587 foreach ( $hours as $h ) {
588 $hours[$i++] = $this->hoursLink( $h, $options );
590 $i = 0;
591 foreach ( $days as $d ) {
592 $days[$i++] = $this->daysLink( $d, $options );
595 return $this->msg( 'wlshowlast' )->rawParams(
596 $this->getLanguage()->pipeList( $hours ),
597 $this->getLanguage()->pipeList( $days ),
598 $this->daysLink( 0, $options ) )->parse();
602 * Count the number of items on a user's watchlist
604 * @param DatabaseBase $dbr A database connection
605 * @return int
607 protected function countItems( $dbr ) {
608 # Fetch the raw count
609 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
610 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
611 $row = $dbr->fetchObject( $rows );
612 $count = $row->count;
614 return floor( $count / 2 );