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