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