Merge "(bug 37755) Set robot meta tags for 'view source' pages"
[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 $nitems = $this->countItems();
95 if ( $nitems == 0 ) {
96 $output->addWikiMsg( 'nowatchlist' );
97 return;
98 }
99
100 // @TODO: use FormOptions!
101 $defaults = array(
102 /* float */ 'days' => floatval( $user->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
103 /* bool */ 'hideMinor' => (int)$user->getBoolOption( 'watchlisthideminor' ),
104 /* bool */ 'hideBots' => (int)$user->getBoolOption( 'watchlisthidebots' ),
105 /* bool */ 'hideAnons' => (int)$user->getBoolOption( 'watchlisthideanons' ),
106 /* bool */ 'hideLiu' => (int)$user->getBoolOption( 'watchlisthideliu' ),
107 /* bool */ 'hidePatrolled' => (int)$user->getBoolOption( 'watchlisthidepatrolled' ),
108 /* bool */ 'hideOwn' => (int)$user->getBoolOption( 'watchlisthideown' ),
109 /* ? */ 'namespace' => 'all',
110 /* ? */ 'invert' => false,
111 /* bool */ 'associated' => false,
112 );
113 $this->customFilters = array();
114 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters ) );
115 foreach( $this->customFilters as $key => $params ) {
116 $defaults[$key] = $params['msg'];
117 }
118
119 # Extract variables from the request, falling back to user preferences or
120 # other default values if these don't exist
121 $prefs['days'] = floatval( $user->getOption( 'watchlistdays' ) );
122 $prefs['hideminor'] = $user->getBoolOption( 'watchlisthideminor' );
123 $prefs['hidebots'] = $user->getBoolOption( 'watchlisthidebots' );
124 $prefs['hideanons'] = $user->getBoolOption( 'watchlisthideanons' );
125 $prefs['hideliu'] = $user->getBoolOption( 'watchlisthideliu' );
126 $prefs['hideown' ] = $user->getBoolOption( 'watchlisthideown' );
127 $prefs['hidepatrolled' ] = $user->getBoolOption( 'watchlisthidepatrolled' );
128
129 # Get query variables
130 $values = array();
131 $values['days'] = $request->getVal( 'days', $prefs['days'] );
132 $values['hideMinor'] = (int)$request->getBool( 'hideMinor', $prefs['hideminor'] );
133 $values['hideBots'] = (int)$request->getBool( 'hideBots' , $prefs['hidebots'] );
134 $values['hideAnons'] = (int)$request->getBool( 'hideAnons', $prefs['hideanons'] );
135 $values['hideLiu'] = (int)$request->getBool( 'hideLiu' , $prefs['hideliu'] );
136 $values['hideOwn'] = (int)$request->getBool( 'hideOwn' , $prefs['hideown'] );
137 $values['hidePatrolled'] = (int)$request->getBool( 'hidePatrolled', $prefs['hidepatrolled'] );
138 foreach( $this->customFilters as $key => $params ) {
139 $values[$key] = (int)$request->getBool( $key );
140 }
141
142 # Get namespace value, if supplied, and prepare a WHERE fragment
143 $nameSpace = $request->getIntOrNull( 'namespace' );
144 $invert = $request->getBool( 'invert' );
145 $associated = $request->getBool( 'associated' );
146 if ( !is_null( $nameSpace ) ) {
147 $eq_op = $invert ? '!=' : '=';
148 $bool_op = $invert ? 'AND' : 'OR';
149 $nameSpace = intval( $nameSpace ); // paranioa
150 if ( !$associated ) {
151 $nameSpaceClause = "rc_namespace $eq_op $nameSpace";
152 } else {
153 $associatedNS = MWNamespace::getAssociated( $nameSpace );
154 $nameSpaceClause =
155 "rc_namespace $eq_op $nameSpace " .
156 $bool_op .
157 " rc_namespace $eq_op $associatedNS";
158 }
159 } else {
160 $nameSpace = '';
161 $nameSpaceClause = '';
162 }
163 $values['namespace'] = $nameSpace;
164 $values['invert'] = $invert;
165 $values['associated'] = $associated;
166
167 if( is_null( $values['days'] ) || !is_numeric( $values['days'] ) ) {
168 $big = 1000; /* The magical big */
169 if( $nitems > $big ) {
170 # Set default cutoff shorter
171 $values['days'] = $defaults['days'] = (12.0 / 24.0); # 12 hours...
172 } else {
173 $values['days'] = $defaults['days']; # default cutoff for shortlisters
174 }
175 } else {
176 $values['days'] = floatval( $values['days'] );
177 }
178
179 // Dump everything here
180 $nondefaults = array();
181 foreach ( $defaults as $name => $defValue ) {
182 wfAppendToArrayIfNotDefault( $name, $values[$name], $defaults, $nondefaults );
183 }
184
185 if( ( $wgEnotifWatchlist || $wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
186 $request->wasPosted() )
187 {
188 $user->clearAllNotifications();
189 $output->redirect( $this->getTitle()->getFullUrl( $nondefaults ) );
190 return;
191 }
192
193 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
194
195 # Possible where conditions
196 $conds = array();
197
198 if( $values['days'] > 0 ) {
199 $conds[] = "rc_timestamp > '".$dbr->timestamp( time() - intval( $values['days'] * 86400 ) )."'";
200 }
201
202 # If the watchlist is relatively short, it's simplest to zip
203 # down its entirety and then sort the results.
204
205 # If it's relatively long, it may be worth our while to zip
206 # through the time-sorted page list checking for watched items.
207
208 # Up estimate of watched items by 15% to compensate for talk pages...
209
210
211 # Toggles
212 if( $values['hideOwn'] ) {
213 $conds[] = 'rc_user != ' . $user->getId();
214 }
215 if( $values['hideBots'] ) {
216 $conds[] = 'rc_bot = 0';
217 }
218 if( $values['hideMinor'] ) {
219 $conds[] = 'rc_minor = 0';
220 }
221 if( $values['hideLiu'] ) {
222 $conds[] = 'rc_user = 0';
223 }
224 if( $values['hideAnons'] ) {
225 $conds[] = 'rc_user != 0';
226 }
227 if ( $user->useRCPatrol() && $values['hidePatrolled'] ) {
228 $conds[] = 'rc_patrolled != 1';
229 }
230 if ( $nameSpaceClause ) {
231 $conds[] = $nameSpaceClause;
232 }
233
234 # Toggle watchlist content (all recent edits or just the latest)
235 if( $user->getOption( 'extendwatchlist' ) ) {
236 $limitWatchlist = intval( $user->getOption( 'wllimit' ) );
237 $usePage = false;
238 } else {
239 # Top log Ids for a page are not stored
240 $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG;
241 $limitWatchlist = 0;
242 $usePage = true;
243 }
244
245 # Show a message about slave lag, if applicable
246 $lag = wfGetLB()->safeGetLag( $dbr );
247 if( $lag > 0 ) {
248 $output->showLagWarning( $lag );
249 }
250
251 # Create output form
252 $form = Xml::fieldset( $this->msg( 'watchlist-options' )->text(), false, array( 'id' => 'mw-watchlist-options' ) );
253
254 # Show watchlist header
255 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse();
256
257 if( $user->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
258 $form .= $this->msg( 'wlheader-enotif' )->parseAsBlock() . "\n";
259 }
260 if( $wgShowUpdatedMarker ) {
261 $form .= Xml::openElement( 'form', array( 'method' => 'post',
262 'action' => $this->getTitle()->getLocalUrl(),
263 'id' => 'mw-watchlist-resetbutton' ) ) .
264 $this->msg( 'wlheader-showupdated' )->parse() . ' ' .
265 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) .
266 Html::hidden( 'reset', 'all' );
267 foreach ( $nondefaults as $key => $value ) {
268 $form .= Html::hidden( $key, $value );
269 }
270 $form .= Xml::closeElement( 'form' );
271 }
272 $form .= '<hr />';
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) );
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 />';
317 }
318
319 $cutofflinks = "\n" . $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 # Namespace filter and put the whole form together.
344 $form .= $wlInfo;
345 $form .= $cutofflinks;
346 $form .= $lang->pipeList( $links );
347 $form .= Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalUrl(), 'id' => 'mw-watchlist-form-namespaceselector' ) );
348 $form .= '<hr /><p>';
349 $form .= Html::namespaceSelector(
350 array(
351 'selected' => $nameSpace,
352 'all' => '',
353 'label' => $this->msg( 'namespace' )->text()
354 ), array(
355 'name' => 'namespace',
356 'id' => 'namespace',
357 'class' => 'namespaceselector',
358 )
359 ) . '&#160;';
360 $form .= Xml::checkLabel(
361 $this->msg( 'invert' )->text(),
362 'invert',
363 'nsinvert',
364 $invert,
365 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
366 ) . '&#160;';
367 $form .= Xml::checkLabel(
368 $this->msg( 'namespace_association' )->text(),
369 'associated',
370 'associated',
371 $associated,
372 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
373 ) . '&#160;';
374 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . '</p>';
375 $form .= Html::hidden( 'days', $values['days'] );
376 foreach ( $filters as $key => $msg ) {
377 if ( $values[$key] ) {
378 $form .= Html::hidden( $key, 1 );
379 }
380 }
381 $form .= Xml::closeElement( 'form' );
382 $form .= Xml::closeElement( 'fieldset' );
383 $output->addHTML( $form );
384
385 # If there's nothing to show, stop here
386 if( $numRows == 0 ) {
387 $output->addWikiMsg( 'watchnochange' );
388 return;
389 }
390
391 /* End bottom header */
392
393 /* Do link batch query */
394 $linkBatch = new LinkBatch;
395 foreach ( $res as $row ) {
396 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
397 if ( $row->rc_user != 0 ) {
398 $linkBatch->add( NS_USER, $userNameUnderscored );
399 }
400 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
401
402 $linkBatch->add( $row->rc_namespace, $row->rc_title );
403 }
404 $linkBatch->execute();
405 $dbr->dataSeek( $res, 0 );
406
407 $list = ChangesList::newFromContext( $this->getContext() );
408 $list->setWatchlistDivs();
409
410 $s = $list->beginRecentChangesList();
411 $counter = 1;
412 foreach ( $res as $obj ) {
413 # Make RC entry
414 $rc = RecentChange::newFromRow( $obj );
415 $rc->counter = $counter++;
416
417 if ( $wgShowUpdatedMarker ) {
418 $updated = $obj->wl_notificationtimestamp;
419 } else {
420 $updated = false;
421 }
422
423 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
424 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
425 'COUNT(*)',
426 array(
427 'wl_namespace' => $obj->rc_namespace,
428 'wl_title' => $obj->rc_title,
429 ),
430 __METHOD__ );
431 } else {
432 $rc->numberofWatchingusers = 0;
433 }
434
435 $s .= $list->recentChangesLine( $rc, $updated, $counter );
436 }
437 $s .= $list->endRecentChangesList();
438
439 $output->addHTML( $s );
440 }
441
442 protected function showHideLink( $options, $message, $name, $value ) {
443 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
444 $options[$name] = 1 - (int) $value;
445
446 return $this->msg( $message )->rawParams( Linker::linkKnown( $this->getTitle(), $label, array(), $options ) )->escaped();
447 }
448
449 protected function hoursLink( $h, $options = array() ) {
450 $options['days'] = ( $h / 24.0 );
451
452 return Linker::linkKnown(
453 $this->getTitle(),
454 $this->getLanguage()->formatNum( $h ),
455 array(),
456 $options
457 );
458 }
459
460 protected function daysLink( $d, $options = array() ) {
461 $options['days'] = $d;
462 $message = ( $d ? $this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
463
464 return Linker::linkKnown(
465 $this->getTitle(),
466 $message,
467 array(),
468 $options
469 );
470 }
471
472 /**
473 * Returns html
474 *
475 * @return string
476 */
477 protected function cutoffLinks( $days, $options = array() ) {
478 $hours = array( 1, 2, 6, 12 );
479 $days = array( 1, 3, 7 );
480 $i = 0;
481 foreach( $hours as $h ) {
482 $hours[$i++] = $this->hoursLink( $h, $options );
483 }
484 $i = 0;
485 foreach( $days as $d ) {
486 $days[$i++] = $this->daysLink( $d, $options );
487 }
488 return $this->msg( 'wlshowlast' )->rawParams(
489 $this->getLanguage()->pipeList( $hours ),
490 $this->getLanguage()->pipeList( $days ),
491 $this->daysLink( 0, $options ) )->parse();
492 }
493
494 /**
495 * Count the number of items on a user's watchlist
496 *
497 * @return Integer
498 */
499 protected function countItems() {
500 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
501
502 # Fetch the raw count
503 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
504 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
505 $row = $dbr->fetchObject( $res );
506 $count = $row->count;
507
508 return floor( $count / 2 );
509 }
510 }