Merge "Forward port of https://www.mediawiki.org/wiki/Special:Code/MediaWiki/105964"
[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 if( ( $wgEnotifWatchlist || $wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
95 $request->wasPosted() )
96 {
97 $user->clearAllNotifications();
98 $output->redirect( $this->getTitle()->getFullUrl() );
99 return;
100 }
101
102 $nitems = $this->countItems();
103 if ( $nitems == 0 ) {
104 $output->addWikiMsg( 'nowatchlist' );
105 return;
106 }
107
108 // @TODO: use FormOptions!
109 $defaults = array(
110 /* float */ 'days' => floatval( $user->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
111 /* bool */ 'hideMinor' => (int)$user->getBoolOption( 'watchlisthideminor' ),
112 /* bool */ 'hideBots' => (int)$user->getBoolOption( 'watchlisthidebots' ),
113 /* bool */ 'hideAnons' => (int)$user->getBoolOption( 'watchlisthideanons' ),
114 /* bool */ 'hideLiu' => (int)$user->getBoolOption( 'watchlisthideliu' ),
115 /* bool */ 'hidePatrolled' => (int)$user->getBoolOption( 'watchlisthidepatrolled' ),
116 /* bool */ 'hideOwn' => (int)$user->getBoolOption( 'watchlisthideown' ),
117 /* ? */ 'namespace' => 'all',
118 /* ? */ 'invert' => false,
119 );
120 $this->customFilters = array();
121 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters ) );
122 foreach( $this->customFilters as $key => $params ) {
123 $defaults[$key] = $params['msg'];
124 }
125
126 # Extract variables from the request, falling back to user preferences or
127 # other default values if these don't exist
128 $prefs['days'] = floatval( $user->getOption( 'watchlistdays' ) );
129 $prefs['hideminor'] = $user->getBoolOption( 'watchlisthideminor' );
130 $prefs['hidebots'] = $user->getBoolOption( 'watchlisthidebots' );
131 $prefs['hideanons'] = $user->getBoolOption( 'watchlisthideanons' );
132 $prefs['hideliu'] = $user->getBoolOption( 'watchlisthideliu' );
133 $prefs['hideown' ] = $user->getBoolOption( 'watchlisthideown' );
134 $prefs['hidepatrolled' ] = $user->getBoolOption( 'watchlisthidepatrolled' );
135
136 # Get query variables
137 $values = array();
138 $values['days'] = $request->getVal( 'days', $prefs['days'] );
139 $values['hideMinor'] = (int)$request->getBool( 'hideMinor', $prefs['hideminor'] );
140 $values['hideBots'] = (int)$request->getBool( 'hideBots' , $prefs['hidebots'] );
141 $values['hideAnons'] = (int)$request->getBool( 'hideAnons', $prefs['hideanons'] );
142 $values['hideLiu'] = (int)$request->getBool( 'hideLiu' , $prefs['hideliu'] );
143 $values['hideOwn'] = (int)$request->getBool( 'hideOwn' , $prefs['hideown'] );
144 $values['hidePatrolled'] = (int)$request->getBool( 'hidePatrolled', $prefs['hidepatrolled'] );
145 foreach( $this->customFilters as $key => $params ) {
146 $values[$key] = (int)$request->getBool( $key );
147 }
148
149 # Get namespace value, if supplied, and prepare a WHERE fragment
150 $nameSpace = $request->getIntOrNull( 'namespace' );
151 $invert = $request->getIntOrNull( 'invert' );
152 if ( !is_null( $nameSpace ) ) {
153 $nameSpace = intval( $nameSpace ); // paranioa
154 if ( $invert ) {
155 $nameSpaceClause = "rc_namespace != $nameSpace";
156 } else {
157 $nameSpaceClause = "rc_namespace = $nameSpace";
158 }
159 } else {
160 $nameSpace = '';
161 $nameSpaceClause = '';
162 }
163 $values['namespace'] = $nameSpace;
164 $values['invert'] = $invert;
165
166 if( is_null( $values['days'] ) || !is_numeric( $values['days'] ) ) {
167 $big = 1000; /* The magical big */
168 if( $nitems > $big ) {
169 # Set default cutoff shorter
170 $values['days'] = $defaults['days'] = (12.0 / 24.0); # 12 hours...
171 } else {
172 $values['days'] = $defaults['days']; # default cutoff for shortlisters
173 }
174 } else {
175 $values['days'] = floatval( $values['days'] );
176 }
177
178 // Dump everything here
179 $nondefaults = array();
180 foreach ( $defaults as $name => $defValue ) {
181 wfAppendToArrayIfNotDefault( $name, $values[$name], $defaults, $nondefaults );
182 }
183
184 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
185
186 # Possible where conditions
187 $conds = array();
188
189 if( $values['days'] > 0 ) {
190 $conds[] = "rc_timestamp > '".$dbr->timestamp( time() - intval( $values['days'] * 86400 ) )."'";
191 }
192
193 # If the watchlist is relatively short, it's simplest to zip
194 # down its entirety and then sort the results.
195
196 # If it's relatively long, it may be worth our while to zip
197 # through the time-sorted page list checking for watched items.
198
199 # Up estimate of watched items by 15% to compensate for talk pages...
200
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( $user->getOption( 'extendwatchlist' ) ) {
227 $limitWatchlist = intval( $user->getOption( '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( $this->msg( 'watchlist-options' )->text(), false, array( 'id' => 'mw-watchlist-options' ) );
244
245 # Show watchlist header
246 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse();
247
248 if( $user->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
249 $form .= $this->msg( 'wlheader-enotif' )->parseAsBlock() . "\n";
250 }
251 if( $wgShowUpdatedMarker ) {
252 $form .= Xml::openElement( 'form', array( 'method' => 'post',
253 'action' => $this->getTitle()->getLocalUrl(),
254 'id' => 'mw-watchlist-resetbutton' ) ) .
255 $this->msg( 'wlheader-showupdated' )->parse() . ' ' .
256 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) .
257 Html::hidden( 'reset', 'all' ) .
258 Xml::closeElement( 'form' );
259 }
260 $form .= '<hr />';
261
262 $tables = array( 'recentchanges', 'watchlist' );
263 $fields = array( $dbr->tableName( 'recentchanges' ) . '.*' );
264 $join_conds = array(
265 'watchlist' => array(
266 'INNER JOIN',
267 array(
268 'wl_user' => $user->getId(),
269 'wl_namespace=rc_namespace',
270 'wl_title=rc_title'
271 ),
272 ),
273 );
274 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
275 if( $wgShowUpdatedMarker ) {
276 $fields[] = 'wl_notificationtimestamp';
277 }
278 if( $limitWatchlist ) {
279 $options['LIMIT'] = $limitWatchlist;
280 }
281
282 $rollbacker = $user->isAllowed('rollback');
283 if ( $usePage || $rollbacker ) {
284 $tables[] = 'page';
285 $join_conds['page'] = array('LEFT JOIN','rc_cur_id=page_id');
286 if ( $rollbacker ) {
287 $fields[] = 'page_latest';
288 }
289 }
290
291 ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
292 wfRunHooks('SpecialWatchlistQuery', array(&$conds,&$tables,&$join_conds,&$fields) );
293
294 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
295 $numRows = $res->numRows();
296
297 /* Start bottom header */
298
299 $lang = $this->getLanguage();
300 $wlInfo = '';
301 if( $values['days'] > 0 ) {
302 $timestamp = wfTimestampNow();
303 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $values['days'] * 24 ) )->params(
304 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . '<br />';
305 }
306
307 $cutofflinks = "\n" . $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
308
309 # Spit out some control panel links
310 $filters = array(
311 'hideMinor' => 'rcshowhideminor',
312 'hideBots' => 'rcshowhidebots',
313 'hideAnons' => 'rcshowhideanons',
314 'hideLiu' => 'rcshowhideliu',
315 'hideOwn' => 'rcshowhidemine',
316 'hidePatrolled' => 'rcshowhidepatr'
317 );
318 foreach ( $this->customFilters as $key => $params ) {
319 $filters[$key] = $params['msg'];
320 }
321 // Disable some if needed
322 if ( !$user->useNPPatrol() ) {
323 unset( $filters['hidePatrolled'] );
324 }
325
326 $links = array();
327 foreach( $filters as $name => $msg ) {
328 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
329 }
330
331 # Namespace filter and put the whole form together.
332 $form .= $wlInfo;
333 $form .= $cutofflinks;
334 $form .= $lang->pipeList( $links );
335 $form .= Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalUrl(), 'id' => 'mw-watchlist-form-namespaceselector' ) );
336 $form .= '<hr /><p>';
337 $form .= Html::namespaceSelector(
338 array(
339 'selected' => $nameSpace,
340 'all' => '',
341 'label' => $this->msg( 'namespace' )->text()
342 ), array(
343 'name' => 'namespace',
344 'id' => 'namespace',
345 'class' => 'namespaceselector',
346 )
347 ) . '&#160;';
348 $form .= Xml::checkLabel( $this->msg( 'invert' )->text(), 'invert', 'nsinvert', $invert ) . '&#160;';
349 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . '</p>';
350 $form .= Html::hidden( 'days', $values['days'] );
351 foreach ( $filters as $key => $msg ) {
352 if ( $values[$key] ) {
353 $form .= Html::hidden( $key, 1 );
354 }
355 }
356 $form .= Xml::closeElement( 'form' );
357 $form .= Xml::closeElement( 'fieldset' );
358 $output->addHTML( $form );
359
360 # If there's nothing to show, stop here
361 if( $numRows == 0 ) {
362 $output->addWikiMsg( 'watchnochange' );
363 return;
364 }
365
366 /* End bottom header */
367
368 /* Do link batch query */
369 $linkBatch = new LinkBatch;
370 foreach ( $res as $row ) {
371 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
372 if ( $row->rc_user != 0 ) {
373 $linkBatch->add( NS_USER, $userNameUnderscored );
374 }
375 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
376
377 $linkBatch->add( $row->rc_namespace, $row->rc_title );
378 }
379 $linkBatch->execute();
380 $dbr->dataSeek( $res, 0 );
381
382 $list = ChangesList::newFromContext( $this->getContext() );
383 $list->setWatchlistDivs();
384
385 $s = $list->beginRecentChangesList();
386 $counter = 1;
387 foreach ( $res as $obj ) {
388 # Make RC entry
389 $rc = RecentChange::newFromRow( $obj );
390 $rc->counter = $counter++;
391
392 if ( $wgShowUpdatedMarker ) {
393 $updated = $obj->wl_notificationtimestamp;
394 } else {
395 $updated = false;
396 }
397
398 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
399 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
400 'COUNT(*)',
401 array(
402 'wl_namespace' => $obj->rc_namespace,
403 'wl_title' => $obj->rc_title,
404 ),
405 __METHOD__ );
406 } else {
407 $rc->numberofWatchingusers = 0;
408 }
409
410 $s .= $list->recentChangesLine( $rc, $updated, $counter );
411 }
412 $s .= $list->endRecentChangesList();
413
414 $output->addHTML( $s );
415 }
416
417 protected function showHideLink( $options, $message, $name, $value ) {
418 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
419 $options[$name] = 1 - (int) $value;
420
421 return $this->msg( $message )->rawParams( Linker::linkKnown( $this->getTitle(), $label, array(), $options ) )->escaped();
422 }
423
424 protected function hoursLink( $h, $options = array() ) {
425 $options['days'] = ( $h / 24.0 );
426
427 return Linker::linkKnown(
428 $this->getTitle(),
429 $this->getLanguage()->formatNum( $h ),
430 array(),
431 $options
432 );
433 }
434
435 protected function daysLink( $d, $options = array() ) {
436 $options['days'] = $d;
437 $message = ( $d ? $this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
438
439 return Linker::linkKnown(
440 $this->getTitle(),
441 $message,
442 array(),
443 $options
444 );
445 }
446
447 /**
448 * Returns html
449 *
450 * @return string
451 */
452 protected function cutoffLinks( $days, $options = array() ) {
453 $hours = array( 1, 2, 6, 12 );
454 $days = array( 1, 3, 7 );
455 $i = 0;
456 foreach( $hours as $h ) {
457 $hours[$i++] = $this->hoursLink( $h, $options );
458 }
459 $i = 0;
460 foreach( $days as $d ) {
461 $days[$i++] = $this->daysLink( $d, $options );
462 }
463 return $this->msg( 'wlshowlast' )->rawParams(
464 $this->getLanguage()->pipeList( $hours ),
465 $this->getLanguage()->pipeList( $days ),
466 $this->daysLink( 0, $options ) )->parse();
467 }
468
469 /**
470 * Count the number of items on a user's watchlist
471 *
472 * @return Integer
473 */
474 protected function countItems() {
475 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
476
477 # Fetch the raw count
478 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count',
479 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
480 $row = $dbr->fetchObject( $res );
481 $count = $row->count;
482
483 return floor( $count / 2 );
484 }
485 }