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