Actually format search error Status objects nicely
[mediawiki.git] / includes / specials / SpecialWatchlist.php
blob2aec8dfec2e9516226caba342f0d916506c3352f
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 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
43 // Anons don't get a watchlist
44 $this->requireLogin( 'watchlistanontext' );
46 $output = $this->getOutput();
47 $request = $this->getRequest();
49 $mode = SpecialEditWatchlist::getMode( $request, $subpage );
50 if ( $mode !== false ) {
51 if ( $mode === SpecialEditWatchlist::EDIT_RAW ) {
52 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'raw' );
53 } elseif ( $mode === SpecialEditWatchlist::EDIT_CLEAR ) {
54 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'clear' );
55 } else {
56 $title = SpecialPage::getTitleFor( 'EditWatchlist' );
59 $output->redirect( $title->getLocalURL() );
61 return;
64 $this->checkPermissions();
66 $user = $this->getUser();
67 $opts = $this->getOptions();
69 if ( ( $wgEnotifWatchlist || $wgShowUpdatedMarker )
70 && $request->getVal( 'reset' )
71 && $request->wasPosted()
72 ) {
73 $user->clearAllNotifications();
74 $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
76 return;
79 parent::execute( $subpage );
82 /**
83 * Return an array of subpages beginning with $search that this special page will accept.
85 * @param string $search Prefix to search for
86 * @param integer $limit Maximum number of results to return
87 * @return string[] Matching subpages
89 public function prefixSearchSubpages( $search, $limit = 10 ) {
90 // See also SpecialEditWatchlist::prefixSearchSubpages
91 $subpages = array( 'clear', 'edit', 'raw' );
92 $escaped = preg_quote( $search );
93 return array_slice( preg_grep( "/^$escaped/i", $subpages ), 0, $limit );
96 /**
97 * Get a FormOptions object containing the default options
99 * @return FormOptions
101 public function getDefaultOptions() {
102 $opts = parent::getDefaultOptions();
103 $user = $this->getUser();
105 $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions::FLOAT );
107 $opts->add( 'hideminor', $user->getBoolOption( 'watchlisthideminor' ) );
108 $opts->add( 'hidebots', $user->getBoolOption( 'watchlisthidebots' ) );
109 $opts->add( 'hideanons', $user->getBoolOption( 'watchlisthideanons' ) );
110 $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' ) );
111 $opts->add( 'hidepatrolled', $user->getBoolOption( 'watchlisthidepatrolled' ) );
112 $opts->add( 'hidemyself', $user->getBoolOption( 'watchlisthideown' ) );
114 $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
116 return $opts;
120 * Get custom show/hide filters
122 * @return array Map of filter URL param names to properties (msg/default)
124 protected function getCustomFilters() {
125 if ( $this->customFilters === null ) {
126 $this->customFilters = parent::getCustomFilters();
127 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters ), '1.23' );
130 return $this->customFilters;
134 * Fetch values for a FormOptions object from the WebRequest associated with this instance.
136 * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
137 * to the current ones.
139 * @param FormOptions $opts
140 * @return FormOptions
142 protected function fetchOptionsFromRequest( $opts ) {
143 static $compatibilityMap = array(
144 'hideMinor' => 'hideminor',
145 'hideBots' => 'hidebots',
146 'hideAnons' => 'hideanons',
147 'hideLiu' => 'hideliu',
148 'hidePatrolled' => 'hidepatrolled',
149 'hideOwn' => 'hidemyself',
152 $params = $this->getRequest()->getValues();
153 foreach ( $compatibilityMap as $from => $to ) {
154 if ( isset( $params[$from] ) ) {
155 $params[$to] = $params[$from];
156 unset( $params[$from] );
160 // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
161 // methods defined on WebRequest and removing this dependency would cause some code duplication.
162 $request = new DerivativeRequest( $this->getRequest(), $params );
163 $opts->fetchValuesFromRequest( $request );
165 return $opts;
169 * Return an array of conditions depending of options set in $opts
171 * @param FormOptions $opts
172 * @return array
174 public function buildMainQueryConds( FormOptions $opts ) {
175 $dbr = $this->getDB();
176 $conds = parent::buildMainQueryConds( $opts );
178 // Calculate cutoff
179 if ( $opts['days'] > 0 ) {
180 $conds[] = 'rc_timestamp > ' .
181 $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
184 return $conds;
188 * Process the query
190 * @param array $conds
191 * @param FormOptions $opts
192 * @return bool|ResultWrapper Result or false (for Recentchangeslinked only)
194 public function doMainQuery( $conds, $opts ) {
195 global $wgShowUpdatedMarker;
197 $dbr = $this->getDB();
198 $user = $this->getUser();
200 # Toggle watchlist content (all recent edits or just the latest)
201 if ( $opts['extended'] ) {
202 $limitWatchlist = $user->getIntOption( 'wllimit' );
203 $usePage = false;
204 } else {
205 # Top log Ids for a page are not stored
206 $nonRevisionTypes = array( RC_LOG );
207 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
208 if ( $nonRevisionTypes ) {
209 $conds[] = $dbr->makeList(
210 array(
211 'rc_this_oldid=page_latest',
212 'rc_type' => $nonRevisionTypes,
214 LIST_OR
217 $limitWatchlist = 0;
218 $usePage = true;
221 $tables = array( 'recentchanges', 'watchlist' );
222 $fields = RecentChange::selectFields();
223 $query_options = array( 'ORDER BY' => 'rc_timestamp DESC' );
224 $join_conds = array(
225 'watchlist' => array(
226 'INNER JOIN',
227 array(
228 'wl_user' => $user->getId(),
229 'wl_namespace=rc_namespace',
230 'wl_title=rc_title'
235 if ( $wgShowUpdatedMarker ) {
236 $fields[] = 'wl_notificationtimestamp';
238 if ( $limitWatchlist ) {
239 $query_options['LIMIT'] = $limitWatchlist;
242 $rollbacker = $user->isAllowed( 'rollback' );
243 if ( $usePage || $rollbacker ) {
244 $tables[] = 'page';
245 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
246 if ( $rollbacker ) {
247 $fields[] = 'page_latest';
251 // Log entries with DELETED_ACTION must not show up unless the user has
252 // the necessary rights.
253 if ( !$user->isAllowed( 'deletedhistory' ) ) {
254 $bitmask = LogPage::DELETED_ACTION;
255 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
256 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
257 } else {
258 $bitmask = 0;
260 if ( $bitmask ) {
261 $conds[] = $dbr->makeList( array(
262 'rc_type != ' . RC_LOG,
263 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
264 ), LIST_OR );
267 ChangeTags::modifyDisplayQuery(
268 $tables,
269 $fields,
270 $conds,
271 $join_conds,
272 $query_options,
276 wfRunHooks( 'SpecialWatchlistQuery',
277 array( &$conds, &$tables, &$join_conds, &$fields, $opts ),
278 '1.23' );
280 return $dbr->select(
281 $tables,
282 $fields,
283 $conds,
284 __METHOD__,
285 $query_options,
286 $join_conds
291 * Return a DatabaseBase object for reading
293 * @return DatabaseBase
295 protected function getDB() {
296 return wfGetDB( DB_SLAVE, 'watchlist' );
300 * Output feed links.
302 public function outputFeedLinks() {
303 $user = $this->getUser();
304 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
305 if ( $wlToken ) {
306 $this->addFeedLinks( array(
307 'action' => 'feedwatchlist',
308 'allrev' => 1,
309 'wlowner' => $user->getName(),
310 'wltoken' => $wlToken,
311 ) );
316 * Build and output the actual changes list.
318 * @param ResultWrapper $rows Database rows
319 * @param FormOptions $opts
321 public function outputChangesList( $rows, $opts ) {
322 global $wgShowUpdatedMarker, $wgRCShowWatchingUsers;
324 $dbr = $this->getDB();
325 $user = $this->getUser();
326 $output = $this->getOutput();
328 # Show a message about slave lag, if applicable
329 $lag = wfGetLB()->safeGetLag( $dbr );
330 if ( $lag > 0 ) {
331 $output->showLagWarning( $lag );
334 $dbr->dataSeek( $rows, 0 );
336 $list = ChangesList::newFromContext( $this->getContext() );
337 $list->setWatchlistDivs();
338 $list->initChangesListRows( $rows );
339 $dbr->dataSeek( $rows, 0 );
341 $s = $list->beginRecentChangesList();
342 $counter = 1;
343 foreach ( $rows as $obj ) {
344 # Make RC entry
345 $rc = RecentChange::newFromRow( $obj );
346 $rc->counter = $counter++;
348 if ( $wgShowUpdatedMarker ) {
349 $updated = $obj->wl_notificationtimestamp;
350 } else {
351 $updated = false;
354 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
355 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
356 'COUNT(*)',
357 array(
358 'wl_namespace' => $obj->rc_namespace,
359 'wl_title' => $obj->rc_title,
361 __METHOD__ );
362 } else {
363 $rc->numberofWatchingusers = 0;
366 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
367 if ( $changeLine !== false ) {
368 $s .= $changeLine;
371 $s .= $list->endRecentChangesList();
373 if ( $rows->numRows() == 0 ) {
374 $output->wrapWikiMsg(
375 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
377 } else {
378 $output->addHTML( $s );
383 * Return the text to be displayed above the changes
385 * @param FormOptions $opts
386 * @return string XHTML
388 public function doHeader( $opts ) {
389 $user = $this->getUser();
391 $this->getOutput()->addSubtitle(
392 $this->msg( 'watchlistfor2', $user->getName() )
393 ->rawParams( SpecialEditWatchlist::buildTools( null ) )
396 $this->setTopText( $opts );
398 $lang = $this->getLanguage();
399 $wlInfo = '';
400 if ( $opts['days'] > 0 ) {
401 $timestamp = wfTimestampNow();
402 $wlInfo = $this->msg( 'wlnote2' )->numParams( round( $opts['days'] * 24 ) )->params(
403 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
404 )->parse() . "<br />\n";
407 $nondefaults = $opts->getChangedValues();
408 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
410 # Spit out some control panel links
411 $filters = array(
412 'hideminor' => 'rcshowhideminor',
413 'hidebots' => 'rcshowhidebots',
414 'hideanons' => 'rcshowhideanons',
415 'hideliu' => 'rcshowhideliu',
416 'hidemyself' => 'rcshowhidemine',
417 'hidepatrolled' => 'rcshowhidepatr'
419 foreach ( $this->getCustomFilters() as $key => $params ) {
420 $filters[$key] = $params['msg'];
422 // Disable some if needed
423 if ( !$user->useNPPatrol() ) {
424 unset( $filters['hidepatrolled'] );
427 $links = array();
428 foreach ( $filters as $name => $msg ) {
429 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
432 $hiddenFields = $nondefaults;
433 unset( $hiddenFields['namespace'] );
434 unset( $hiddenFields['invert'] );
435 unset( $hiddenFields['associated'] );
437 # Create output
438 $form = '';
440 # Namespace filter and put the whole form together.
441 $form .= $wlInfo;
442 $form .= $cutofflinks;
443 $form .= $lang->pipeList( $links ) . "\n";
444 $form .= "<hr />\n<p>";
445 $form .= Html::namespaceSelector(
446 array(
447 'selected' => $opts['namespace'],
448 'all' => '',
449 'label' => $this->msg( 'namespace' )->text()
450 ), array(
451 'name' => 'namespace',
452 'id' => 'namespace',
453 'class' => 'namespaceselector',
455 ) . '&#160;';
456 $form .= Xml::checkLabel(
457 $this->msg( 'invert' )->text(),
458 'invert',
459 'nsinvert',
460 $opts['invert'],
461 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
462 ) . '&#160;';
463 $form .= Xml::checkLabel(
464 $this->msg( 'namespace_association' )->text(),
465 'associated',
466 'nsassociated',
467 $opts['associated'],
468 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
469 ) . '&#160;';
470 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
471 foreach ( $hiddenFields as $key => $value ) {
472 $form .= Html::hidden( $key, $value ) . "\n";
474 $form .= Xml::closeElement( 'fieldset' ) . "\n";
475 $form .= Xml::closeElement( 'form' ) . "\n";
476 $this->getOutput()->addHTML( $form );
478 $this->setBottomText( $opts );
481 function setTopText( FormOptions $opts ) {
482 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
484 $nondefaults = $opts->getChangedValues();
485 $form = "";
486 $user = $this->getUser();
488 $dbr = $this->getDB();
489 $numItems = $this->countItems( $dbr );
491 // Show watchlist header
492 $form .= "<p>";
493 if ( $numItems == 0 ) {
494 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
495 } else {
496 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
497 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
498 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
500 if ( $wgShowUpdatedMarker ) {
501 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
504 $form .= "</p>";
506 if ( $numItems > 0 && $wgShowUpdatedMarker ) {
507 $form .= Xml::openElement( 'form', array( 'method' => 'post',
508 'action' => $this->getPageTitle()->getLocalURL(),
509 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
510 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
511 Html::hidden( 'reset', 'all' ) . "\n";
512 foreach ( $nondefaults as $key => $value ) {
513 $form .= Html::hidden( $key, $value ) . "\n";
515 $form .= Xml::closeElement( 'form' ) . "\n";
518 $form .= Xml::openElement( 'form', array(
519 'method' => 'post',
520 'action' => $this->getPageTitle()->getLocalURL(),
521 'id' => 'mw-watchlist-form'
522 ) );
523 $form .= Xml::fieldset(
524 $this->msg( 'watchlist-options' )->text(),
525 false,
526 array( 'id' => 'mw-watchlist-options' )
529 $form .= SpecialRecentChanges::makeLegend( $this->getContext() );
531 $this->getOutput()->addHTML( $form );
534 protected function showHideLink( $options, $message, $name, $value ) {
535 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
536 $options[$name] = 1 - (int)$value;
538 return $this->msg( $message )
539 ->rawParams( Linker::linkKnown( $this->getPageTitle(), $label, array(), $options ) )
540 ->escaped();
543 protected function hoursLink( $h, $options = array() ) {
544 $options['days'] = ( $h / 24.0 );
546 return Linker::linkKnown(
547 $this->getPageTitle(),
548 $this->getLanguage()->formatNum( $h ),
549 array(),
550 $options
554 protected function daysLink( $d, $options = array() ) {
555 $options['days'] = $d;
556 $message = $d ? $this->getLanguage()->formatNum( $d )
557 : $this->msg( 'watchlistall2' )->escaped();
559 return Linker::linkKnown(
560 $this->getPageTitle(),
561 $message,
562 array(),
563 $options
568 * Returns html
570 * @param int $days This gets overwritten, so is not used
571 * @param array $options Query parameters for URL
572 * @return string
574 protected function cutoffLinks( $days, $options = array() ) {
575 $hours = array( 1, 2, 6, 12 );
576 $days = array( 1, 3, 7 );
577 $i = 0;
578 foreach ( $hours as $h ) {
579 $hours[$i++] = $this->hoursLink( $h, $options );
581 $i = 0;
582 foreach ( $days as $d ) {
583 $days[$i++] = $this->daysLink( $d, $options );
586 return $this->msg( 'wlshowlast' )->rawParams(
587 $this->getLanguage()->pipeList( $hours ),
588 $this->getLanguage()->pipeList( $days ),
589 $this->daysLink( 0, $options ) )->parse();
593 * Count the number of items on a user's watchlist
595 * @param DatabaseBase $dbr A database connection
596 * @return int
598 protected function countItems( $dbr ) {
599 # Fetch the raw count
600 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
601 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
602 $row = $dbr->fetchObject( $rows );
603 $count = $row->count;
605 return floor( $count / 2 );