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