Merge "Fix the target URL of HTMLForm"
[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 SpecialPage {
24 protected $customFilters;
25
26 /**
27 * Constructor
28 */
29 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
30 parent::__construct( $page, $restriction );
31 }
32
33 /**
34 * Execute
35 * @param $par Parameter passed to the page
36 */
37 function execute( $par ) {
38 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
39
40 $user = $this->getUser();
41 $output = $this->getOutput();
42
43 # Anons don't get a watchlist
44 if ( $user->isAnon() ) {
45 $output->setPageTitle( $this->msg( 'watchnologin' ) );
46 $output->setRobotPolicy( 'noindex,nofollow' );
47 $llink = Linker::linkKnown(
48 SpecialPage::getTitleFor( 'Userlogin' ),
49 $this->msg( 'loginreqlink' )->escaped(),
50 array(),
51 array( 'returnto' => $this->getTitle()->getPrefixedText() )
52 );
53 $output->addHTML( $this->msg( 'watchlistanontext' )->rawParams( $llink )->parse() );
54 return;
55 }
56
57 // Check permissions
58 $this->checkPermissions();
59
60 // Add feed links
61 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
62 if ( $wlToken ) {
63 $this->addFeedLinks( array( 'action' => 'feedwatchlist', 'allrev' => 'allrev',
64 'wlowner' => $user->getName(), 'wltoken' => $wlToken ) );
65 }
66
67 $this->setHeaders();
68 $this->outputHeader();
69
70 $output->addSubtitle( $this->msg( 'watchlistfor2', $user->getName()
71 )->rawParams( SpecialEditWatchlist::buildTools( null ) ) );
72
73 $request = $this->getRequest();
74
75 $mode = SpecialEditWatchlist::getMode( $request, $par );
76 if ( $mode !== false ) {
77 # TODO: localise?
78 switch ( $mode ) {
79 case SpecialEditWatchlist::EDIT_CLEAR:
80 $mode = 'clear';
81 break;
82 case SpecialEditWatchlist::EDIT_RAW:
83 $mode = 'raw';
84 break;
85 default:
86 $mode = null;
87 }
88 $title = SpecialPage::getTitleFor( 'EditWatchlist', $mode );
89 $output->redirect( $title->getLocalURL() );
90 return;
91 }
92
93 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
94
95 $nitems = $this->countItems( $dbr );
96 if ( $nitems == 0 ) {
97 $output->addWikiMsg( 'nowatchlist' );
98 return;
99 }
100
101 // @todo use FormOptions!
102 $defaults = array(
103 /* float */ 'days' => floatval( $user->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
104 /* bool */ 'hideMinor' => (int)$user->getBoolOption( 'watchlisthideminor' ),
105 /* bool */ 'hideBots' => (int)$user->getBoolOption( 'watchlisthidebots' ),
106 /* bool */ 'hideAnons' => (int)$user->getBoolOption( 'watchlisthideanons' ),
107 /* bool */ 'hideLiu' => (int)$user->getBoolOption( 'watchlisthideliu' ),
108 /* bool */ 'hidePatrolled' => (int)$user->getBoolOption( 'watchlisthidepatrolled' ),
109 /* bool */ 'hideOwn' => (int)$user->getBoolOption( 'watchlisthideown' ),
110 /* bool */ 'extended' => (int)$user->getBoolOption( 'extendwatchlist' ),
111 /* ? */ 'namespace' => '', //means all
112 /* ? */ 'invert' => false,
113 /* bool */ 'associated' => false,
114 );
115 $this->customFilters = array();
116 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters ) );
117 foreach ( $this->customFilters as $key => $params ) {
118 $defaults[$key] = $params['default'];
119 }
120
121 # Extract variables from the request, falling back to user preferences or
122 # other default values if these don't exist
123 $values = array();
124 $values['days'] = $request->getVal( 'days', $defaults['days'] );
125 $values['hideMinor'] = (int)$request->getBool( 'hideMinor', $defaults['hideMinor'] );
126 $values['hideBots'] = (int)$request->getBool( 'hideBots', $defaults['hideBots'] );
127 $values['hideAnons'] = (int)$request->getBool( 'hideAnons', $defaults['hideAnons'] );
128 $values['hideLiu'] = (int)$request->getBool( 'hideLiu', $defaults['hideLiu'] );
129 $values['hideOwn'] = (int)$request->getBool( 'hideOwn', $defaults['hideOwn'] );
130 $values['hidePatrolled'] = (int)$request->getBool( 'hidePatrolled', $defaults['hidePatrolled'] );
131 $values['extended'] = (int)$request->getBool( 'extended', $defaults['extended'] );
132 foreach ( $this->customFilters as $key => $params ) {
133 $values[$key] = (int)$request->getBool( $key, $defaults[$key] );
134 }
135
136 # Get namespace value, if supplied, and prepare a WHERE fragment
137 $nameSpace = $request->getIntOrNull( 'namespace' );
138 $invert = $request->getBool( 'invert' );
139 $associated = $request->getBool( 'associated' );
140 if ( !is_null( $nameSpace ) ) {
141 $eq_op = $invert ? '!=' : '=';
142 $bool_op = $invert ? 'AND' : 'OR';
143 $nameSpace = intval( $nameSpace ); // paranioa
144 if ( !$associated ) {
145 $nameSpaceClause = "rc_namespace $eq_op $nameSpace";
146 } else {
147 $associatedNS = MWNamespace::getAssociated( $nameSpace );
148 $nameSpaceClause =
149 "rc_namespace $eq_op $nameSpace " .
150 $bool_op .
151 " rc_namespace $eq_op $associatedNS";
152 }
153 } else {
154 $nameSpace = '';
155 $nameSpaceClause = '';
156 }
157 $values['namespace'] = $nameSpace;
158 $values['invert'] = $invert;
159 $values['associated'] = $associated;
160
161 if ( is_null( $values['days'] ) || !is_numeric( $values['days'] ) ) {
162 $big = 1000; /* The magical big */
163 if ( $nitems > $big ) {
164 # Set default cutoff shorter
165 $values['days'] = $defaults['days'] = ( 12.0 / 24.0 ); # 12 hours...
166 } else {
167 $values['days'] = $defaults['days']; # default cutoff for shortlisters
168 }
169 } else {
170 $values['days'] = floatval( $values['days'] );
171 }
172
173 // Dump everything here
174 $nondefaults = array();
175 foreach ( $defaults as $name => $defValue ) {
176 wfAppendToArrayIfNotDefault( $name, $values[$name], $defaults, $nondefaults );
177 }
178
179 if ( ( $wgEnotifWatchlist || $wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
180 $request->wasPosted() )
181 {
182 $user->clearAllNotifications();
183 $output->redirect( $this->getTitle()->getFullURL( $nondefaults ) );
184 return;
185 }
186
187 # Possible where conditions
188 $conds = array();
189
190 if ( $values['days'] > 0 ) {
191 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $values['days'] * 86400 ) ) );
192 }
193
194 # If the watchlist is relatively short, it's simplest to zip
195 # down its entirety and then sort the results.
196
197 # If it's relatively long, it may be worth our while to zip
198 # through the time-sorted page list checking for watched items.
199
200 # Up estimate of watched items by 15% to compensate for talk pages...
201
202 # Toggles
203 if ( $values['hideOwn'] ) {
204 $conds[] = 'rc_user != ' . $user->getId();
205 }
206 if ( $values['hideBots'] ) {
207 $conds[] = 'rc_bot = 0';
208 }
209 if ( $values['hideMinor'] ) {
210 $conds[] = 'rc_minor = 0';
211 }
212 if ( $values['hideLiu'] ) {
213 $conds[] = 'rc_user = 0';
214 }
215 if ( $values['hideAnons'] ) {
216 $conds[] = 'rc_user != 0';
217 }
218 if ( $user->useRCPatrol() && $values['hidePatrolled'] ) {
219 $conds[] = 'rc_patrolled != 1';
220 }
221 if ( $nameSpaceClause ) {
222 $conds[] = $nameSpaceClause;
223 }
224
225 # Toggle watchlist content (all recent edits or just the latest)
226 if ( $values['extended'] ) {
227 $limitWatchlist = $user->getIntOption( 'wllimit' );
228 $usePage = false;
229 } else {
230 # Top log Ids for a page are not stored
231 $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG;
232 $limitWatchlist = 0;
233 $usePage = true;
234 }
235
236 # Show a message about slave lag, if applicable
237 $lag = wfGetLB()->safeGetLag( $dbr );
238 if ( $lag > 0 ) {
239 $output->showLagWarning( $lag );
240 }
241
242 # Create output form
243 $form = Xml::fieldset(
244 $this->msg( 'watchlist-options' )->text(),
245 false,
246 array( 'id' => 'mw-watchlist-options' )
247 );
248
249 # Show watchlist header
250 $form .= "<p>";
251 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse() . "\n";
252 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
253 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
254 }
255 if ( $wgShowUpdatedMarker ) {
256 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
257 }
258 $form .= "</p>";
259
260 if ( $wgShowUpdatedMarker ) {
261 $form .= Xml::openElement( 'form', array( 'method' => 'post',
262 'action' => $this->getTitle()->getLocalURL(),
263 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
264 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
265 Html::hidden( 'reset', 'all' ) . "\n";
266 foreach ( $nondefaults as $key => $value ) {
267 $form .= Html::hidden( $key, $value ) . "\n";
268 }
269 $form .= Xml::closeElement( 'form' ) . "\n";
270 }
271
272 $form .= "<hr />\n";
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, $values ) );
305
306 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
307 $numRows = $res->numRows();
308
309 /* Start bottom header */
310
311 $lang = $this->getLanguage();
312 $wlInfo = '';
313 if ( $values['days'] > 0 ) {
314 $timestamp = wfTimestampNow();
315 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $values['days'] * 24 ) )->params(
316 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
317 }
318
319 $cutofflinks = $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
320
321 # Spit out some control panel links
322 $filters = array(
323 'hideMinor' => 'rcshowhideminor',
324 'hideBots' => 'rcshowhidebots',
325 'hideAnons' => 'rcshowhideanons',
326 'hideLiu' => 'rcshowhideliu',
327 'hideOwn' => 'rcshowhidemine',
328 'hidePatrolled' => 'rcshowhidepatr'
329 );
330 foreach ( $this->customFilters as $key => $params ) {
331 $filters[$key] = $params['msg'];
332 }
333 // Disable some if needed
334 if ( !$user->useNPPatrol() ) {
335 unset( $filters['hidePatrolled'] );
336 }
337
338 $links = array();
339 foreach ( $filters as $name => $msg ) {
340 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
341 }
342
343 $hiddenFields = $nondefaults;
344 unset( $hiddenFields['namespace'] );
345 unset( $hiddenFields['invert'] );
346 unset( $hiddenFields['associated'] );
347
348 # Namespace filter and put the whole form together.
349 $form .= $wlInfo;
350 $form .= $cutofflinks;
351 $form .= $lang->pipeList( $links ) . "\n";
352 $form .= Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'id' => 'mw-watchlist-form-namespaceselector' ) ) . "\n";
353 $form .= "<hr />\n<p>";
354 $form .= Html::namespaceSelector(
355 array(
356 'selected' => $nameSpace,
357 'all' => '',
358 'label' => $this->msg( 'namespace' )->text()
359 ), array(
360 'name' => 'namespace',
361 'id' => 'namespace',
362 'class' => 'namespaceselector',
363 )
364 ) . '&#160;';
365 $form .= Xml::checkLabel(
366 $this->msg( 'invert' )->text(),
367 'invert',
368 'nsinvert',
369 $invert,
370 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
371 ) . '&#160;';
372 $form .= Xml::checkLabel(
373 $this->msg( 'namespace_association' )->text(),
374 'associated',
375 'associated',
376 $associated,
377 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
378 ) . '&#160;';
379 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
380 foreach ( $hiddenFields as $key => $value ) {
381 $form .= Html::hidden( $key, $value ) . "\n";
382 }
383 $form .= Xml::closeElement( 'form' ) . "\n";
384 $form .= Xml::closeElement( 'fieldset' ) . "\n";
385 $output->addHTML( $form );
386
387 # If there's nothing to show, stop here
388 if ( $numRows == 0 ) {
389 $output->wrapWikiMsg(
390 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
391 );
392 return;
393 }
394
395 /* End bottom header */
396
397 /* Do link batch query */
398 $linkBatch = new LinkBatch;
399 foreach ( $res as $row ) {
400 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
401 if ( $row->rc_user != 0 ) {
402 $linkBatch->add( NS_USER, $userNameUnderscored );
403 }
404 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
405
406 $linkBatch->add( $row->rc_namespace, $row->rc_title );
407 }
408 $linkBatch->execute();
409 $dbr->dataSeek( $res, 0 );
410
411 $list = ChangesList::newFromContext( $this->getContext() );
412 $list->setWatchlistDivs();
413
414 $s = $list->beginRecentChangesList();
415 $counter = 1;
416 foreach ( $res as $obj ) {
417 # Make RC entry
418 $rc = RecentChange::newFromRow( $obj );
419 $rc->counter = $counter++;
420
421 if ( $wgShowUpdatedMarker ) {
422 $updated = $obj->wl_notificationtimestamp;
423 } else {
424 $updated = false;
425 }
426
427 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
428 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
429 'COUNT(*)',
430 array(
431 'wl_namespace' => $obj->rc_namespace,
432 'wl_title' => $obj->rc_title,
433 ),
434 __METHOD__ );
435 } else {
436 $rc->numberofWatchingusers = 0;
437 }
438
439 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
440 if ( $changeLine !== false ) {
441 $s .= $changeLine;
442 }
443 }
444 $s .= $list->endRecentChangesList();
445
446 $output->addHTML( $s );
447 }
448
449 protected function showHideLink( $options, $message, $name, $value ) {
450 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
451 $options[$name] = 1 - (int) $value;
452
453 return $this->msg( $message )->rawParams( Linker::linkKnown( $this->getTitle(), $label, array(), $options ) )->escaped();
454 }
455
456 protected function hoursLink( $h, $options = array() ) {
457 $options['days'] = ( $h / 24.0 );
458
459 return Linker::linkKnown(
460 $this->getTitle(),
461 $this->getLanguage()->formatNum( $h ),
462 array(),
463 $options
464 );
465 }
466
467 protected function daysLink( $d, $options = array() ) {
468 $options['days'] = $d;
469 $message = ( $d ? $this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
470
471 return Linker::linkKnown(
472 $this->getTitle(),
473 $message,
474 array(),
475 $options
476 );
477 }
478
479 /**
480 * Returns html
481 *
482 * @param int $days This gets overwritten, so is not used
483 * @param array $options Query parameters for URL
484 * @return string
485 */
486 protected function cutoffLinks( $days, $options = array() ) {
487 $hours = array( 1, 2, 6, 12 );
488 $days = array( 1, 3, 7 );
489 $i = 0;
490 foreach ( $hours as $h ) {
491 $hours[$i++] = $this->hoursLink( $h, $options );
492 }
493 $i = 0;
494 foreach ( $days as $d ) {
495 $days[$i++] = $this->daysLink( $d, $options );
496 }
497 return $this->msg( 'wlshowlast' )->rawParams(
498 $this->getLanguage()->pipeList( $hours ),
499 $this->getLanguage()->pipeList( $days ),
500 $this->daysLink( 0, $options ) )->parse();
501 }
502
503 /**
504 * Count the number of items on a user's watchlist
505 *
506 * @param DatabaseBase $dbr A database connection
507 * @return Integer
508 */
509 protected function countItems( $dbr ) {
510 # Fetch the raw count
511 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
512 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
513 $row = $dbr->fetchObject( $res );
514 $count = $row->count;
515
516 return floor( $count / 2 );
517 }
518
519 protected function getGroupName() {
520 return 'changes';
521 }
522 }