fix some spacing
[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 # Toggles
204 if( $values['hideOwn'] ) {
205 $conds[] = 'rc_user != ' . $user->getId();
206 }
207 if( $values['hideBots'] ) {
208 $conds[] = 'rc_bot = 0';
209 }
210 if( $values['hideMinor'] ) {
211 $conds[] = 'rc_minor = 0';
212 }
213 if( $values['hideLiu'] ) {
214 $conds[] = 'rc_user = 0';
215 }
216 if( $values['hideAnons'] ) {
217 $conds[] = 'rc_user != 0';
218 }
219 if ( $user->useRCPatrol() && $values['hidePatrolled'] ) {
220 $conds[] = 'rc_patrolled != 1';
221 }
222 if ( $nameSpaceClause ) {
223 $conds[] = $nameSpaceClause;
224 }
225
226 # Toggle watchlist content (all recent edits or just the latest)
227 if( $values['extended'] ) {
228 $limitWatchlist = $user->getIntOption( 'wllimit' );
229 $usePage = false;
230 } else {
231 # Top log Ids for a page are not stored
232 $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG;
233 $limitWatchlist = 0;
234 $usePage = true;
235 }
236
237 # Show a message about slave lag, if applicable
238 $lag = wfGetLB()->safeGetLag( $dbr );
239 if( $lag > 0 ) {
240 $output->showLagWarning( $lag );
241 }
242
243 # Create output form
244 $form = Xml::fieldset( $this->msg( 'watchlist-options' )->text(), false, array( 'id' => 'mw-watchlist-options' ) );
245
246 # Show watchlist header
247 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse() . "\n";
248
249 if( $user->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
250 $form .= $this->msg( 'wlheader-enotif' )->parseAsBlock() . "\n";
251 }
252 if( $wgShowUpdatedMarker ) {
253 $form .= Xml::openElement( 'form', array( 'method' => 'post',
254 'action' => $this->getTitle()->getLocalUrl(),
255 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
256 $this->msg( 'wlheader-showupdated' )->parse() .
257 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
258 Html::hidden( 'reset', 'all' ) . "\n";
259 foreach ( $nondefaults as $key => $value ) {
260 $form .= Html::hidden( $key, $value ) . "\n";
261 }
262 $form .= Xml::closeElement( 'form' ) . "\n";
263 }
264 $form .= "<hr />\n";
265
266 $tables = array( 'recentchanges', 'watchlist' );
267 $fields = RecentChange::selectFields();
268 $join_conds = array(
269 'watchlist' => array(
270 'INNER JOIN',
271 array(
272 'wl_user' => $user->getId(),
273 'wl_namespace=rc_namespace',
274 'wl_title=rc_title'
275 ),
276 ),
277 );
278 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
279 if( $wgShowUpdatedMarker ) {
280 $fields[] = 'wl_notificationtimestamp';
281 }
282 if( $limitWatchlist ) {
283 $options['LIMIT'] = $limitWatchlist;
284 }
285
286 $rollbacker = $user->isAllowed( 'rollback' );
287 if ( $usePage || $rollbacker ) {
288 $tables[] = 'page';
289 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
290 if ( $rollbacker ) {
291 $fields[] = 'page_latest';
292 }
293 }
294
295 ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
296 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields ) );
297
298 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
299 $numRows = $res->numRows();
300
301 /* Start bottom header */
302
303 $lang = $this->getLanguage();
304 $wlInfo = '';
305 if( $values['days'] > 0 ) {
306 $timestamp = wfTimestampNow();
307 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $values['days'] * 24 ) )->params(
308 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
309 }
310
311 $cutofflinks = $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
312
313 # Spit out some control panel links
314 $filters = array(
315 'hideMinor' => 'rcshowhideminor',
316 'hideBots' => 'rcshowhidebots',
317 'hideAnons' => 'rcshowhideanons',
318 'hideLiu' => 'rcshowhideliu',
319 'hideOwn' => 'rcshowhidemine',
320 'hidePatrolled' => 'rcshowhidepatr'
321 );
322 foreach ( $this->customFilters as $key => $params ) {
323 $filters[$key] = $params['msg'];
324 }
325 // Disable some if needed
326 if ( !$user->useNPPatrol() ) {
327 unset( $filters['hidePatrolled'] );
328 }
329
330 $links = array();
331 foreach( $filters as $name => $msg ) {
332 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
333 }
334
335 $hiddenFields = $nondefaults;
336 unset( $hiddenFields['namespace'] );
337 unset( $hiddenFields['invert'] );
338 unset( $hiddenFields['associated'] );
339
340 # Namespace filter and put the whole form together.
341 $form .= $wlInfo;
342 $form .= $cutofflinks;
343 $form .= $lang->pipeList( $links ) . "\n";
344 $form .= Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalUrl(), 'id' => 'mw-watchlist-form-namespaceselector' ) ) . "\n";
345 $form .= "<hr />\n<p>";
346 $form .= Html::namespaceSelector(
347 array(
348 'selected' => $nameSpace,
349 'all' => '',
350 'label' => $this->msg( 'namespace' )->text()
351 ), array(
352 'name' => 'namespace',
353 'id' => 'namespace',
354 'class' => 'namespaceselector',
355 )
356 ) . '&#160;';
357 $form .= Xml::checkLabel(
358 $this->msg( 'invert' )->text(),
359 'invert',
360 'nsinvert',
361 $invert,
362 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
363 ) . '&#160;';
364 $form .= Xml::checkLabel(
365 $this->msg( 'namespace_association' )->text(),
366 'associated',
367 'associated',
368 $associated,
369 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
370 ) . '&#160;';
371 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
372 foreach ( $hiddenFields as $key => $value ) {
373 $form .= Html::hidden( $key, $value ) . "\n";
374 }
375 $form .= Xml::closeElement( 'form' ) . "\n";
376 $form .= Xml::closeElement( 'fieldset' ) . "\n";
377 $output->addHTML( $form );
378
379 # If there's nothing to show, stop here
380 if( $numRows == 0 ) {
381 $output->addWikiMsg( 'watchnochange' );
382 return;
383 }
384
385 /* End bottom header */
386
387 /* Do link batch query */
388 $linkBatch = new LinkBatch;
389 foreach ( $res as $row ) {
390 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
391 if ( $row->rc_user != 0 ) {
392 $linkBatch->add( NS_USER, $userNameUnderscored );
393 }
394 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
395
396 $linkBatch->add( $row->rc_namespace, $row->rc_title );
397 }
398 $linkBatch->execute();
399 $dbr->dataSeek( $res, 0 );
400
401 $list = ChangesList::newFromContext( $this->getContext() );
402 $list->setWatchlistDivs();
403
404 $s = $list->beginRecentChangesList();
405 $counter = 1;
406 foreach ( $res as $obj ) {
407 # Make RC entry
408 $rc = RecentChange::newFromRow( $obj );
409 $rc->counter = $counter++;
410
411 if ( $wgShowUpdatedMarker ) {
412 $updated = $obj->wl_notificationtimestamp;
413 } else {
414 $updated = false;
415 }
416
417 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
418 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
419 'COUNT(*)',
420 array(
421 'wl_namespace' => $obj->rc_namespace,
422 'wl_title' => $obj->rc_title,
423 ),
424 __METHOD__ );
425 } else {
426 $rc->numberofWatchingusers = 0;
427 }
428
429 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
430 if ( $changeLine !== false ) {
431 $s .= $changeLine;
432 }
433 }
434 $s .= $list->endRecentChangesList();
435
436 $output->addHTML( $s );
437 }
438
439 protected function showHideLink( $options, $message, $name, $value ) {
440 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
441 $options[$name] = 1 - (int) $value;
442
443 return $this->msg( $message )->rawParams( Linker::linkKnown( $this->getTitle(), $label, array(), $options ) )->escaped();
444 }
445
446 protected function hoursLink( $h, $options = array() ) {
447 $options['days'] = ( $h / 24.0 );
448
449 return Linker::linkKnown(
450 $this->getTitle(),
451 $this->getLanguage()->formatNum( $h ),
452 array(),
453 $options
454 );
455 }
456
457 protected function daysLink( $d, $options = array() ) {
458 $options['days'] = $d;
459 $message = ( $d ? $this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
460
461 return Linker::linkKnown(
462 $this->getTitle(),
463 $message,
464 array(),
465 $options
466 );
467 }
468
469 /**
470 * Returns html
471 *
472 * @return string
473 */
474 protected function cutoffLinks( $days, $options = array() ) {
475 $hours = array( 1, 2, 6, 12 );
476 $days = array( 1, 3, 7 );
477 $i = 0;
478 foreach( $hours as $h ) {
479 $hours[$i++] = $this->hoursLink( $h, $options );
480 }
481 $i = 0;
482 foreach( $days as $d ) {
483 $days[$i++] = $this->daysLink( $d, $options );
484 }
485 return $this->msg( 'wlshowlast' )->rawParams(
486 $this->getLanguage()->pipeList( $hours ),
487 $this->getLanguage()->pipeList( $days ),
488 $this->daysLink( 0, $options ) )->parse();
489 }
490
491 /**
492 * Count the number of items on a user's watchlist
493 *
494 * @param $dbr A database connection
495 * @return Integer
496 */
497 protected function countItems( $dbr ) {
498 # Fetch the raw count
499 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
500 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
501 $row = $dbr->fetchObject( $res );
502 $count = $row->count;
503
504 return floor( $count / 2 );
505 }
506 }