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