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