Merge "Ensure variables in database classes are defined, used and correctly cased"
[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 SpecialRecentChanges {
24 protected $customFilters;
25
26 /**
27 * Constructor
28 */
29 public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
30 parent::__construct( $page, $restriction );
31 }
32
33 public function isIncludable() {
34 return false;
35 }
36
37 /**
38 * Map old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
39 * to the current ones.
40 *
41 * This creates derivative context and request, pokes with request's parameters, and sets them as
42 * the context for this class instance, mapping old keys to new ones completely transparently (as
43 * long as nothing tries to access the globals instead of current context).
44 */
45 private function mapCompatibilityRequestParameters() {
46 static $map = array(
47 'hideMinor' => 'hideminor',
48 'hideBots' => 'hidebots',
49 'hideAnons' => 'hideanons',
50 'hideLiu' => 'hideliu',
51 'hidePatrolled' => 'hidepatrolled',
52 'hideOwn' => 'hidemyself',
53 );
54
55 $params = $this->getRequest()->getValues();
56 foreach ( $map as $from => $to ) {
57 if ( isset( $params[$from] ) ) {
58 $params[$to] = $params[$from];
59 unset( $params[$from] );
60 }
61 }
62
63 $context = new DerivativeContext( $this->getContext() );
64 $request = new DerivativeRequest( $context->getRequest(), $params );
65 $context->setRequest( $request );
66 $this->setContext( $context );
67 }
68
69 /**
70 * Execute
71 * @param $par Parameter passed to the page
72 */
73 function execute( $par ) {
74 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
75
76 $this->mapCompatibilityRequestParameters();
77
78 $user = $this->getUser();
79 $output = $this->getOutput();
80 $output->addModuleStyles( 'mediawiki.special.changeslist' );
81 $output->addModules( 'mediawiki.special.changeslist.js' );
82
83 # Anons don't get a watchlist
84 $this->requireLogin( 'watchlistanontext' );
85
86 // Check permissions
87 $this->checkPermissions();
88
89 // Add feed links
90 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
91 if ( $wlToken ) {
92 $this->addFeedLinks( array(
93 'action' => 'feedwatchlist',
94 'allrev' => 1,
95 'wlowner' => $user->getName(),
96 'wltoken' => $wlToken,
97 ) );
98 }
99
100 $this->setHeaders();
101 $this->outputHeader();
102
103 $output->addSubtitle(
104 $this->msg( 'watchlistfor2', $user->getName() )
105 ->rawParams( SpecialEditWatchlist::buildTools( null ) )
106 );
107
108 $request = $this->getRequest();
109
110 $mode = SpecialEditWatchlist::getMode( $request, $par );
111 if ( $mode !== false ) {
112 if ( $mode === SpecialEditWatchlist::EDIT_RAW ) {
113 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'raw' );
114 } else {
115 $title = SpecialPage::getTitleFor( 'EditWatchlist' );
116 }
117
118 $output->redirect( $title->getLocalURL() );
119 return;
120 }
121
122 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
123
124 $nitems = $this->countItems( $dbr );
125 if ( $nitems == 0 ) {
126 $output->addWikiMsg( 'nowatchlist' );
127 return;
128 }
129
130 // @todo use FormOptions!
131 $defaults = array(
132 /* float */ 'days' => floatval( $user->getOption( 'watchlistdays' ) ),
133 /* bool */ 'hideminor' => (int)$user->getBoolOption( 'watchlisthideminor' ),
134 /* bool */ 'hidebots' => (int)$user->getBoolOption( 'watchlisthidebots' ),
135 /* bool */ 'hideanons' => (int)$user->getBoolOption( 'watchlisthideanons' ),
136 /* bool */ 'hideliu' => (int)$user->getBoolOption( 'watchlisthideliu' ),
137 /* bool */ 'hidepatrolled' => (int)$user->getBoolOption( 'watchlisthidepatrolled' ),
138 /* bool */ 'hidemyself' => (int)$user->getBoolOption( 'watchlisthideown' ),
139 /* bool */ 'extended' => (int)$user->getBoolOption( 'extendwatchlist' ),
140 /* ? */ 'namespace' => '', //means all
141 /* ? */ 'invert' => false,
142 /* bool */ 'associated' => false,
143 );
144 $this->customFilters = array();
145 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters ) );
146 foreach ( $this->customFilters as $key => $params ) {
147 $defaults[$key] = $params['default'];
148 }
149
150 # Extract variables from the request, falling back to user preferences or
151 # other default values if these don't exist
152 $values = array();
153 $values['days'] = floatval( $request->getVal( 'days', $defaults['days'] ) );
154 $values['hideminor'] = (int)$request->getBool( 'hideminor', $defaults['hideminor'] );
155 $values['hidebots'] = (int)$request->getBool( 'hidebots', $defaults['hidebots'] );
156 $values['hideanons'] = (int)$request->getBool( 'hideanons', $defaults['hideanons'] );
157 $values['hideliu'] = (int)$request->getBool( 'hideliu', $defaults['hideliu'] );
158 $values['hidemyself'] = (int)$request->getBool( 'hidemyself', $defaults['hidemyself'] );
159 $values['hidepatrolled'] = (int)$request->getBool( 'hidepatrolled', $defaults['hidepatrolled'] );
160 $values['extended'] = (int)$request->getBool( 'extended', $defaults['extended'] );
161 foreach ( $this->customFilters as $key => $params ) {
162 $values[$key] = (int)$request->getBool( $key, $defaults[$key] );
163 }
164
165 # Get namespace value, if supplied, and prepare a WHERE fragment
166 $nameSpace = $request->getIntOrNull( 'namespace' );
167 $invert = $request->getBool( 'invert' );
168 $associated = $request->getBool( 'associated' );
169 if ( !is_null( $nameSpace ) ) {
170 $eq_op = $invert ? '!=' : '=';
171 $bool_op = $invert ? 'AND' : 'OR';
172 $nameSpace = intval( $nameSpace ); // paranioa
173 if ( !$associated ) {
174 $nameSpaceClause = "rc_namespace $eq_op $nameSpace";
175 } else {
176 $associatedNS = MWNamespace::getAssociated( $nameSpace );
177 $nameSpaceClause =
178 "rc_namespace $eq_op $nameSpace " .
179 $bool_op .
180 " rc_namespace $eq_op $associatedNS";
181 }
182 } else {
183 $nameSpace = '';
184 $nameSpaceClause = '';
185 }
186 $values['namespace'] = $nameSpace;
187 $values['invert'] = $invert;
188 $values['associated'] = $associated;
189
190 // Dump everything here
191 $nondefaults = array();
192 foreach ( $defaults as $name => $defValue ) {
193 wfAppendToArrayIfNotDefault( $name, $values[$name], $defaults, $nondefaults );
194 }
195
196 if ( ( $wgEnotifWatchlist || $wgShowUpdatedMarker ) && $request->getVal( 'reset' )
197 && $request->wasPosted()
198 ) {
199 $user->clearAllNotifications();
200 $output->redirect( $this->getPageTitle()->getFullURL( $nondefaults ) );
201 return;
202 }
203
204 # Possible where conditions
205 $conds = array();
206
207 if ( $values['days'] > 0 ) {
208 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $values['days'] * 86400 ) ) );
209 }
210
211 # Toggles
212 if ( $values['hidemyself'] ) {
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 ( $values['extended'] ) {
236 $limitWatchlist = $user->getIntOption( 'wllimit' );
237 $usePage = false;
238 } else {
239 # Top log Ids for a page are not stored
240 $nonRevisionTypes = array( RC_LOG );
241 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
242 if ( $nonRevisionTypes ) {
243 $conds[] = $dbr->makeList(
244 array(
245 'rc_this_oldid=page_latest',
246 'rc_type' => $nonRevisionTypes,
247 ),
248 LIST_OR
249 );
250 }
251 $limitWatchlist = 0;
252 $usePage = true;
253 }
254
255 # Show a message about slave lag, if applicable
256 $lag = wfGetLB()->safeGetLag( $dbr );
257 if ( $lag > 0 ) {
258 $output->showLagWarning( $lag );
259 }
260
261 # Create output
262 $form = '';
263
264 # Show watchlist header
265 $form .= "<p>";
266 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse() . "\n";
267 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
268 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
269 }
270 if ( $wgShowUpdatedMarker ) {
271 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
272 }
273 $form .= "</p>";
274
275 if ( $wgShowUpdatedMarker ) {
276 $form .= Xml::openElement( 'form', array( 'method' => 'post',
277 'action' => $this->getPageTitle()->getLocalURL(),
278 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
279 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
280 Html::hidden( 'reset', 'all' ) . "\n";
281 foreach ( $nondefaults as $key => $value ) {
282 $form .= Html::hidden( $key, $value ) . "\n";
283 }
284 $form .= Xml::closeElement( 'form' ) . "\n";
285 }
286
287 $form .= Xml::openElement( 'form', array(
288 'method' => 'post',
289 'action' => $this->getPageTitle()->getLocalURL(),
290 'id' => 'mw-watchlist-form'
291 ) );
292 $form .= Xml::fieldset(
293 $this->msg( 'watchlist-options' )->text(),
294 false,
295 array( 'id' => 'mw-watchlist-options' )
296 );
297
298 $form .= SpecialRecentChanges::makeLegend( $this->getContext() );
299
300 $tables = array( 'recentchanges', 'watchlist' );
301 $fields = RecentChange::selectFields();
302 $join_conds = array(
303 'watchlist' => array(
304 'INNER JOIN',
305 array(
306 'wl_user' => $user->getId(),
307 'wl_namespace=rc_namespace',
308 'wl_title=rc_title'
309 ),
310 ),
311 );
312 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
313 if ( $wgShowUpdatedMarker ) {
314 $fields[] = 'wl_notificationtimestamp';
315 }
316 if ( $limitWatchlist ) {
317 $options['LIMIT'] = $limitWatchlist;
318 }
319
320 $rollbacker = $user->isAllowed( 'rollback' );
321 if ( $usePage || $rollbacker ) {
322 $tables[] = 'page';
323 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
324 if ( $rollbacker ) {
325 $fields[] = 'page_latest';
326 }
327 }
328
329 ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
330 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields, $values ) );
331
332 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
333 $numRows = $res->numRows();
334
335 /* Start bottom header */
336
337 $lang = $this->getLanguage();
338 $wlInfo = '';
339 if ( $values['days'] > 0 ) {
340 $timestamp = wfTimestampNow();
341 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $values['days'] * 24 ) )->params(
342 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
343 }
344
345 $cutofflinks = $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
346
347 # Spit out some control panel links
348 $filters = array(
349 'hideminor' => 'rcshowhideminor',
350 'hidebots' => 'rcshowhidebots',
351 'hideanons' => 'rcshowhideanons',
352 'hideliu' => 'rcshowhideliu',
353 'hidemyself' => 'rcshowhidemine',
354 'hidepatrolled' => 'rcshowhidepatr'
355 );
356 foreach ( $this->customFilters as $key => $params ) {
357 $filters[$key] = $params['msg'];
358 }
359 // Disable some if needed
360 if ( !$user->useNPPatrol() ) {
361 unset( $filters['hidepatrolled'] );
362 }
363
364 $links = array();
365 foreach ( $filters as $name => $msg ) {
366 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
367 }
368
369 $hiddenFields = $nondefaults;
370 unset( $hiddenFields['namespace'] );
371 unset( $hiddenFields['invert'] );
372 unset( $hiddenFields['associated'] );
373
374 # Namespace filter and put the whole form together.
375 $form .= $wlInfo;
376 $form .= $cutofflinks;
377 $form .= $lang->pipeList( $links ) . "\n";
378 $form .= "<hr />\n<p>";
379 $form .= Html::namespaceSelector(
380 array(
381 'selected' => $nameSpace,
382 'all' => '',
383 'label' => $this->msg( 'namespace' )->text()
384 ), array(
385 'name' => 'namespace',
386 'id' => 'namespace',
387 'class' => 'namespaceselector',
388 )
389 ) . '&#160;';
390 $form .= Xml::checkLabel(
391 $this->msg( 'invert' )->text(),
392 'invert',
393 'nsinvert',
394 $invert,
395 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
396 ) . '&#160;';
397 $form .= Xml::checkLabel(
398 $this->msg( 'namespace_association' )->text(),
399 'associated',
400 'associated',
401 $associated,
402 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
403 ) . '&#160;';
404 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
405 foreach ( $hiddenFields as $key => $value ) {
406 $form .= Html::hidden( $key, $value ) . "\n";
407 }
408 $form .= Xml::closeElement( 'fieldset' ) . "\n";
409 $form .= Xml::closeElement( 'form' ) . "\n";
410 $output->addHTML( $form );
411
412 # If there's nothing to show, stop here
413 if ( $numRows == 0 ) {
414 $output->wrapWikiMsg(
415 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
416 );
417 return;
418 }
419
420 /* End bottom header */
421
422 /* Do link batch query */
423 $linkBatch = new LinkBatch;
424 foreach ( $res as $row ) {
425 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
426 if ( $row->rc_user != 0 ) {
427 $linkBatch->add( NS_USER, $userNameUnderscored );
428 }
429 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
430
431 $linkBatch->add( $row->rc_namespace, $row->rc_title );
432 }
433 $linkBatch->execute();
434 $dbr->dataSeek( $res, 0 );
435
436 $list = ChangesList::newFromContext( $this->getContext() );
437 $list->setWatchlistDivs();
438
439 $s = $list->beginRecentChangesList();
440 $counter = 1;
441 foreach ( $res as $obj ) {
442 # Make RC entry
443 $rc = RecentChange::newFromRow( $obj );
444 $rc->counter = $counter++;
445
446 if ( $wgShowUpdatedMarker ) {
447 $updated = $obj->wl_notificationtimestamp;
448 } else {
449 $updated = false;
450 }
451
452 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
453 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
454 'COUNT(*)',
455 array(
456 'wl_namespace' => $obj->rc_namespace,
457 'wl_title' => $obj->rc_title,
458 ),
459 __METHOD__ );
460 } else {
461 $rc->numberofWatchingusers = 0;
462 }
463
464 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
465 if ( $changeLine !== false ) {
466 $s .= $changeLine;
467 }
468 }
469 $s .= $list->endRecentChangesList();
470
471 $output->addHTML( $s );
472 }
473
474 protected function showHideLink( $options, $message, $name, $value ) {
475 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
476 $options[$name] = 1 - (int)$value;
477
478 return $this->msg( $message )->rawParams( Linker::linkKnown( $this->getPageTitle(), $label, array(), $options ) )->escaped();
479 }
480
481 protected function hoursLink( $h, $options = array() ) {
482 $options['days'] = ( $h / 24.0 );
483
484 return Linker::linkKnown(
485 $this->getPageTitle(),
486 $this->getLanguage()->formatNum( $h ),
487 array(),
488 $options
489 );
490 }
491
492 protected function daysLink( $d, $options = array() ) {
493 $options['days'] = $d;
494 $message = ( $d ? $this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
495
496 return Linker::linkKnown(
497 $this->getPageTitle(),
498 $message,
499 array(),
500 $options
501 );
502 }
503
504 /**
505 * Returns html
506 *
507 * @param int $days This gets overwritten, so is not used
508 * @param array $options Query parameters for URL
509 * @return string
510 */
511 protected function cutoffLinks( $days, $options = array() ) {
512 $hours = array( 1, 2, 6, 12 );
513 $days = array( 1, 3, 7 );
514 $i = 0;
515 foreach ( $hours as $h ) {
516 $hours[$i++] = $this->hoursLink( $h, $options );
517 }
518 $i = 0;
519 foreach ( $days as $d ) {
520 $days[$i++] = $this->daysLink( $d, $options );
521 }
522 return $this->msg( 'wlshowlast' )->rawParams(
523 $this->getLanguage()->pipeList( $hours ),
524 $this->getLanguage()->pipeList( $days ),
525 $this->daysLink( 0, $options ) )->parse();
526 }
527
528 /**
529 * Count the number of items on a user's watchlist
530 *
531 * @param DatabaseBase $dbr A database connection
532 * @return Integer
533 */
534 protected function countItems( $dbr ) {
535 # Fetch the raw count
536 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
537 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
538 $row = $dbr->fetchObject( $res );
539 $count = $row->count;
540
541 return floor( $count / 2 );
542 }
543 }