Merge "Special:PrefixIndex omits stripprefix=1 for "Next page" link"
[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 # Toggle watchlist content (all recent edits or just the latest)
182 if ( $opts['extended'] ) {
183 $limitWatchlist = $user->getIntOption( 'wllimit' );
184 $usePage = false;
185 } else {
186 # Top log Ids for a page are not stored
187 $nonRevisionTypes = array( RC_LOG );
188 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
189 if ( $nonRevisionTypes ) {
190 $conds[] = $dbr->makeList(
191 array(
192 'rc_this_oldid=page_latest',
193 'rc_type' => $nonRevisionTypes,
194 ),
195 LIST_OR
196 );
197 }
198 $limitWatchlist = 0;
199 $usePage = true;
200 }
201
202 $tables = array( 'recentchanges', 'watchlist' );
203 $fields = RecentChange::selectFields();
204 $join_conds = array(
205 'watchlist' => array(
206 'INNER JOIN',
207 array(
208 'wl_user' => $user->getId(),
209 'wl_namespace=rc_namespace',
210 'wl_title=rc_title'
211 ),
212 ),
213 );
214 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
215 if ( $wgShowUpdatedMarker ) {
216 $fields[] = 'wl_notificationtimestamp';
217 }
218 if ( $limitWatchlist ) {
219 $options['LIMIT'] = $limitWatchlist;
220 }
221
222 $rollbacker = $user->isAllowed( 'rollback' );
223 if ( $usePage || $rollbacker ) {
224 $tables[] = 'page';
225 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
226 if ( $rollbacker ) {
227 $fields[] = 'page_latest';
228 }
229 }
230
231 // Log entries with DELETED_ACTION must not show up unless the user has
232 // the necessary rights.
233 if ( !$user->isAllowed( 'deletedhistory' ) ) {
234 $bitmask = LogPage::DELETED_ACTION;
235 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
236 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
237 } else {
238 $bitmask = 0;
239 }
240 if ( $bitmask ) {
241 $conds[] = $dbr->makeList( array(
242 'rc_type != ' . RC_LOG,
243 $dbr->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
244 ), LIST_OR );
245 }
246
247
248 ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
249 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $opts ) );
250
251 return $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
252 }
253
254 /**
255 * Return a DatabaseBase object for reading
256 *
257 * @return DatabaseBase
258 */
259 protected function getDB() {
260 return wfGetDB( DB_SLAVE, 'watchlist' );
261 }
262
263 /**
264 * Send output to the OutputPage object, only called if not used feeds
265 *
266 * @param ResultWrapper $rows Database rows
267 * @param FormOptions $opts
268 */
269 public function webOutput( $rows, $opts ) {
270 global $wgShowUpdatedMarker, $wgRCShowWatchingUsers;
271
272 $dbr = $this->getDB();
273 $user = $this->getUser();
274
275 $dbr->dataSeek( $rows, 0 );
276
277 $list = ChangesList::newFromContext( $this->getContext() );
278 $list->setWatchlistDivs();
279
280 $s = $list->beginRecentChangesList();
281 $counter = 1;
282 foreach ( $rows as $obj ) {
283 # Make RC entry
284 $rc = RecentChange::newFromRow( $obj );
285 $rc->counter = $counter++;
286
287 if ( $wgShowUpdatedMarker ) {
288 $updated = $obj->wl_notificationtimestamp;
289 } else {
290 $updated = false;
291 }
292
293 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
294 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
295 'COUNT(*)',
296 array(
297 'wl_namespace' => $obj->rc_namespace,
298 'wl_title' => $obj->rc_title,
299 ),
300 __METHOD__ );
301 } else {
302 $rc->numberofWatchingusers = 0;
303 }
304
305 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
306 if ( $changeLine !== false ) {
307 $s .= $changeLine;
308 }
309 }
310 $s .= $list->endRecentChangesList();
311
312 // Print things out
313
314 $output = $this->getOutput();
315
316 $output->addSubtitle(
317 $this->msg( 'watchlistfor2', $user->getName() )
318 ->rawParams( SpecialEditWatchlist::buildTools( null ) )
319 );
320
321 // Output options box
322 $this->doHeader( $opts );
323
324 // Add feed links
325 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
326 if ( $wlToken ) {
327 $this->addFeedLinks( array(
328 'action' => 'feedwatchlist',
329 'allrev' => 1,
330 'wlowner' => $user->getName(),
331 'wltoken' => $wlToken,
332 ) );
333 }
334
335 # Show a message about slave lag, if applicable
336 $lag = wfGetLB()->safeGetLag( $dbr );
337 if ( $lag > 0 ) {
338 $output->showLagWarning( $lag );
339 }
340
341 if ( $rows->numRows() == 0 ) {
342 $output->wrapWikiMsg(
343 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
344 );
345 } else {
346 $output->addHTML( $s );
347 }
348 }
349
350 /**
351 * Return the text to be displayed above the changes
352 *
353 * @param FormOptions $opts
354 * @return string XHTML
355 */
356 public function doHeader( $opts ) {
357 $user = $this->getUser();
358
359 $this->setTopText( $opts );
360
361 $lang = $this->getLanguage();
362 $wlInfo = '';
363 if ( $opts['days'] > 0 ) {
364 $timestamp = wfTimestampNow();
365 $wlInfo = $this->msg( 'wlnote2' )->numParams( round( $opts['days'] * 24 ) )->params(
366 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
367 }
368
369 $nondefaults = $opts->getChangedValues();
370 $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults ) . "<br />\n";
371
372 # Spit out some control panel links
373 $filters = array(
374 'hideminor' => 'rcshowhideminor',
375 'hidebots' => 'rcshowhidebots',
376 'hideanons' => 'rcshowhideanons',
377 'hideliu' => 'rcshowhideliu',
378 'hidemyself' => 'rcshowhidemine',
379 'hidepatrolled' => 'rcshowhidepatr'
380 );
381 foreach ( $this->getCustomFilters() as $key => $params ) {
382 $filters[$key] = $params['msg'];
383 }
384 // Disable some if needed
385 if ( !$user->useNPPatrol() ) {
386 unset( $filters['hidepatrolled'] );
387 }
388
389 $links = array();
390 foreach ( $filters as $name => $msg ) {
391 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $opts[$name] );
392 }
393
394 $hiddenFields = $nondefaults;
395 unset( $hiddenFields['namespace'] );
396 unset( $hiddenFields['invert'] );
397 unset( $hiddenFields['associated'] );
398
399 # Create output
400 $form = '';
401
402 # Namespace filter and put the whole form together.
403 $form .= $wlInfo;
404 $form .= $cutofflinks;
405 $form .= $lang->pipeList( $links ) . "\n";
406 $form .= "<hr />\n<p>";
407 $form .= Html::namespaceSelector(
408 array(
409 'selected' => $opts['namespace'],
410 'all' => '',
411 'label' => $this->msg( 'namespace' )->text()
412 ), array(
413 'name' => 'namespace',
414 'id' => 'namespace',
415 'class' => 'namespaceselector',
416 )
417 ) . '&#160;';
418 $form .= Xml::checkLabel(
419 $this->msg( 'invert' )->text(),
420 'invert',
421 'nsinvert',
422 $opts['invert'],
423 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
424 ) . '&#160;';
425 $form .= Xml::checkLabel(
426 $this->msg( 'namespace_association' )->text(),
427 'associated',
428 'nsassociated',
429 $opts['associated'],
430 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
431 ) . '&#160;';
432 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
433 foreach ( $hiddenFields as $key => $value ) {
434 $form .= Html::hidden( $key, $value ) . "\n";
435 }
436 $form .= Xml::closeElement( 'fieldset' ) . "\n";
437 $form .= Xml::closeElement( 'form' ) . "\n";
438 $this->getOutput()->addHTML( $form );
439
440 $this->setBottomText( $opts );
441 }
442
443 function setTopText( FormOptions $opts ) {
444 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
445
446 $nondefaults = $opts->getChangedValues();
447 $form = "";
448 $user = $this->getUser();
449
450 $dbr = $this->getDB();
451 $numItems = $this->countItems( $dbr );
452
453 // Show watchlist header
454 $form .= "<p>";
455 if ( $numItems == 0 ) {
456 $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
457 } else {
458 $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
459 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
460 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
461 }
462 if ( $wgShowUpdatedMarker ) {
463 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
464 }
465 }
466 $form .= "</p>";
467
468 if ( $numItems > 0 && $wgShowUpdatedMarker ) {
469 $form .= Xml::openElement( 'form', array( 'method' => 'post',
470 'action' => $this->getPageTitle()->getLocalURL(),
471 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
472 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
473 Html::hidden( 'reset', 'all' ) . "\n";
474 foreach ( $nondefaults as $key => $value ) {
475 $form .= Html::hidden( $key, $value ) . "\n";
476 }
477 $form .= Xml::closeElement( 'form' ) . "\n";
478 }
479
480 $form .= Xml::openElement( 'form', array(
481 'method' => 'post',
482 'action' => $this->getPageTitle()->getLocalURL(),
483 'id' => 'mw-watchlist-form'
484 ) );
485 $form .= Xml::fieldset(
486 $this->msg( 'watchlist-options' )->text(),
487 false,
488 array( 'id' => 'mw-watchlist-options' )
489 );
490
491 $form .= SpecialRecentChanges::makeLegend( $this->getContext() );
492
493 $this->getOutput()->addHTML( $form );
494 }
495
496 protected function showHideLink( $options, $message, $name, $value ) {
497 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
498 $options[$name] = 1 - (int)$value;
499
500 return $this->msg( $message )->rawParams( Linker::linkKnown( $this->getPageTitle(), $label, array(), $options ) )->escaped();
501 }
502
503 protected function hoursLink( $h, $options = array() ) {
504 $options['days'] = ( $h / 24.0 );
505
506 return Linker::linkKnown(
507 $this->getPageTitle(),
508 $this->getLanguage()->formatNum( $h ),
509 array(),
510 $options
511 );
512 }
513
514 protected function daysLink( $d, $options = array() ) {
515 $options['days'] = $d;
516 $message = ( $d ? $this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
517
518 return Linker::linkKnown(
519 $this->getPageTitle(),
520 $message,
521 array(),
522 $options
523 );
524 }
525
526 /**
527 * Returns html
528 *
529 * @param int $days This gets overwritten, so is not used
530 * @param array $options Query parameters for URL
531 * @return string
532 */
533 protected function cutoffLinks( $days, $options = array() ) {
534 $hours = array( 1, 2, 6, 12 );
535 $days = array( 1, 3, 7 );
536 $i = 0;
537 foreach ( $hours as $h ) {
538 $hours[$i++] = $this->hoursLink( $h, $options );
539 }
540 $i = 0;
541 foreach ( $days as $d ) {
542 $days[$i++] = $this->daysLink( $d, $options );
543 }
544 return $this->msg( 'wlshowlast' )->rawParams(
545 $this->getLanguage()->pipeList( $hours ),
546 $this->getLanguage()->pipeList( $days ),
547 $this->daysLink( 0, $options ) )->parse();
548 }
549
550 /**
551 * Count the number of items on a user's watchlist
552 *
553 * @param DatabaseBase $dbr A database connection
554 * @return Integer
555 */
556 protected function countItems( $dbr ) {
557 # Fetch the raw count
558 $rows = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
559 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
560 $row = $dbr->fetchObject( $rows );
561 $count = $row->count;
562
563 return floor( $count / 2 );
564 }
565 }