Merge "Declare visibility for class properties in DatabaseSqlite"
[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 * Execute
39 * @param $par Parameter passed to the page
40 */
41 function execute( $par ) {
42 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
43
44 $user = $this->getUser();
45 $output = $this->getOutput();
46 $output->addModuleStyles( 'mediawiki.special.changeslist' );
47 $output->addModules( 'mediawiki.special.changeslist.js' );
48
49 # Anons don't get a watchlist
50 $this->requireLogin( 'watchlistanontext' );
51
52 // Check permissions
53 $this->checkPermissions();
54
55 // Add feed links
56 $wlToken = $user->getTokenFromOption( 'watchlisttoken' );
57 if ( $wlToken ) {
58 $this->addFeedLinks( array(
59 'action' => 'feedwatchlist',
60 'allrev' => 1,
61 'wlowner' => $user->getName(),
62 'wltoken' => $wlToken,
63 ) );
64 }
65
66 $this->setHeaders();
67 $this->outputHeader();
68
69 $output->addSubtitle(
70 $this->msg( 'watchlistfor2', $user->getName() )
71 ->rawParams( SpecialEditWatchlist::buildTools( null ) )
72 );
73
74 $request = $this->getRequest();
75
76 $mode = SpecialEditWatchlist::getMode( $request, $par );
77 if ( $mode !== false ) {
78 if ( $mode === SpecialEditWatchlist::EDIT_RAW ) {
79 $title = SpecialPage::getTitleFor( 'EditWatchlist', 'raw' );
80 } else {
81 $title = SpecialPage::getTitleFor( 'EditWatchlist' );
82 }
83
84 $output->redirect( $title->getLocalURL() );
85 return;
86 }
87
88 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
89
90 $nitems = $this->countItems( $dbr );
91 if ( $nitems == 0 ) {
92 $output->addWikiMsg( 'nowatchlist' );
93 return;
94 }
95
96 // @todo use FormOptions!
97 $defaults = array(
98 /* float */ 'days' => floatval( $user->getOption( 'watchlistdays' ) ),
99 /* bool */ 'hideMinor' => (int)$user->getBoolOption( 'watchlisthideminor' ),
100 /* bool */ 'hideBots' => (int)$user->getBoolOption( 'watchlisthidebots' ),
101 /* bool */ 'hideAnons' => (int)$user->getBoolOption( 'watchlisthideanons' ),
102 /* bool */ 'hideLiu' => (int)$user->getBoolOption( 'watchlisthideliu' ),
103 /* bool */ 'hidePatrolled' => (int)$user->getBoolOption( 'watchlisthidepatrolled' ),
104 /* bool */ 'hideOwn' => (int)$user->getBoolOption( 'watchlisthideown' ),
105 /* bool */ 'extended' => (int)$user->getBoolOption( 'extendwatchlist' ),
106 /* ? */ 'namespace' => '', //means all
107 /* ? */ 'invert' => false,
108 /* bool */ 'associated' => false,
109 );
110 $this->customFilters = array();
111 wfRunHooks( 'SpecialWatchlistFilters', array( $this, &$this->customFilters ) );
112 foreach ( $this->customFilters as $key => $params ) {
113 $defaults[$key] = $params['default'];
114 }
115
116 # Extract variables from the request, falling back to user preferences or
117 # other default values if these don't exist
118 $values = array();
119 $values['days'] = floatval( $request->getVal( 'days', $defaults['days'] ) );
120 $values['hideMinor'] = (int)$request->getBool( 'hideMinor', $defaults['hideMinor'] );
121 $values['hideBots'] = (int)$request->getBool( 'hideBots', $defaults['hideBots'] );
122 $values['hideAnons'] = (int)$request->getBool( 'hideAnons', $defaults['hideAnons'] );
123 $values['hideLiu'] = (int)$request->getBool( 'hideLiu', $defaults['hideLiu'] );
124 $values['hideOwn'] = (int)$request->getBool( 'hideOwn', $defaults['hideOwn'] );
125 $values['hidePatrolled'] = (int)$request->getBool( 'hidePatrolled', $defaults['hidePatrolled'] );
126 $values['extended'] = (int)$request->getBool( 'extended', $defaults['extended'] );
127 foreach ( $this->customFilters as $key => $params ) {
128 $values[$key] = (int)$request->getBool( $key, $defaults[$key] );
129 }
130
131 # Get namespace value, if supplied, and prepare a WHERE fragment
132 $nameSpace = $request->getIntOrNull( 'namespace' );
133 $invert = $request->getBool( 'invert' );
134 $associated = $request->getBool( 'associated' );
135 if ( !is_null( $nameSpace ) ) {
136 $eq_op = $invert ? '!=' : '=';
137 $bool_op = $invert ? 'AND' : 'OR';
138 $nameSpace = intval( $nameSpace ); // paranioa
139 if ( !$associated ) {
140 $nameSpaceClause = "rc_namespace $eq_op $nameSpace";
141 } else {
142 $associatedNS = MWNamespace::getAssociated( $nameSpace );
143 $nameSpaceClause =
144 "rc_namespace $eq_op $nameSpace " .
145 $bool_op .
146 " rc_namespace $eq_op $associatedNS";
147 }
148 } else {
149 $nameSpace = '';
150 $nameSpaceClause = '';
151 }
152 $values['namespace'] = $nameSpace;
153 $values['invert'] = $invert;
154 $values['associated'] = $associated;
155
156 // Dump everything here
157 $nondefaults = array();
158 foreach ( $defaults as $name => $defValue ) {
159 wfAppendToArrayIfNotDefault( $name, $values[$name], $defaults, $nondefaults );
160 }
161
162 if ( ( $wgEnotifWatchlist || $wgShowUpdatedMarker ) && $request->getVal( 'reset' )
163 && $request->wasPosted()
164 ) {
165 $user->clearAllNotifications();
166 $output->redirect( $this->getPageTitle()->getFullURL( $nondefaults ) );
167 return;
168 }
169
170 # Possible where conditions
171 $conds = array();
172
173 if ( $values['days'] > 0 ) {
174 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $values['days'] * 86400 ) ) );
175 }
176
177 # Toggles
178 if ( $values['hideOwn'] ) {
179 $conds[] = 'rc_user != ' . $user->getId();
180 }
181 if ( $values['hideBots'] ) {
182 $conds[] = 'rc_bot = 0';
183 }
184 if ( $values['hideMinor'] ) {
185 $conds[] = 'rc_minor = 0';
186 }
187 if ( $values['hideLiu'] ) {
188 $conds[] = 'rc_user = 0';
189 }
190 if ( $values['hideAnons'] ) {
191 $conds[] = 'rc_user != 0';
192 }
193 if ( $user->useRCPatrol() && $values['hidePatrolled'] ) {
194 $conds[] = 'rc_patrolled != 1';
195 }
196 if ( $nameSpaceClause ) {
197 $conds[] = $nameSpaceClause;
198 }
199
200 # Toggle watchlist content (all recent edits or just the latest)
201 if ( $values['extended'] ) {
202 $limitWatchlist = $user->getIntOption( 'wllimit' );
203 $usePage = false;
204 } else {
205 # Top log Ids for a page are not stored
206 $nonRevisionTypes = array( RC_LOG );
207 wfRunHooks( 'SpecialWatchlistGetNonRevisionTypes', array( &$nonRevisionTypes ) );
208 if ( $nonRevisionTypes ) {
209 $conds[] = $dbr->makeList(
210 array(
211 'rc_this_oldid=page_latest',
212 'rc_type' => $nonRevisionTypes,
213 ),
214 LIST_OR
215 );
216 }
217 $limitWatchlist = 0;
218 $usePage = true;
219 }
220
221 # Show a message about slave lag, if applicable
222 $lag = wfGetLB()->safeGetLag( $dbr );
223 if ( $lag > 0 ) {
224 $output->showLagWarning( $lag );
225 }
226
227 # Create output
228 $form = '';
229
230 # Show watchlist header
231 $form .= "<p>";
232 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse() . "\n";
233 if ( $wgEnotifWatchlist && $user->getOption( 'enotifwatchlistpages' ) ) {
234 $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
235 }
236 if ( $wgShowUpdatedMarker ) {
237 $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
238 }
239 $form .= "</p>";
240
241 if ( $wgShowUpdatedMarker ) {
242 $form .= Xml::openElement( 'form', array( 'method' => 'post',
243 'action' => $this->getPageTitle()->getLocalURL(),
244 'id' => 'mw-watchlist-resetbutton' ) ) . "\n" .
245 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) . "\n" .
246 Html::hidden( 'reset', 'all' ) . "\n";
247 foreach ( $nondefaults as $key => $value ) {
248 $form .= Html::hidden( $key, $value ) . "\n";
249 }
250 $form .= Xml::closeElement( 'form' ) . "\n";
251 }
252
253 $form .= Xml::openElement( 'form', array(
254 'method' => 'post',
255 'action' => $this->getPageTitle()->getLocalURL(),
256 'id' => 'mw-watchlist-form'
257 ) );
258 $form .= Xml::fieldset(
259 $this->msg( 'watchlist-options' )->text(),
260 false,
261 array( 'id' => 'mw-watchlist-options' )
262 );
263
264 $form .= SpecialRecentChanges::makeLegend( $this->getContext() );
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, $values ) );
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 .= "<hr />\n<p>";
345 $form .= Html::namespaceSelector(
346 array(
347 'selected' => $nameSpace,
348 'all' => '',
349 'label' => $this->msg( 'namespace' )->text()
350 ), array(
351 'name' => 'namespace',
352 'id' => 'namespace',
353 'class' => 'namespaceselector',
354 )
355 ) . '&#160;';
356 $form .= Xml::checkLabel(
357 $this->msg( 'invert' )->text(),
358 'invert',
359 'nsinvert',
360 $invert,
361 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
362 ) . '&#160;';
363 $form .= Xml::checkLabel(
364 $this->msg( 'namespace_association' )->text(),
365 'associated',
366 'associated',
367 $associated,
368 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
369 ) . '&#160;';
370 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "</p>\n";
371 foreach ( $hiddenFields as $key => $value ) {
372 $form .= Html::hidden( $key, $value ) . "\n";
373 }
374 $form .= Xml::closeElement( 'fieldset' ) . "\n";
375 $form .= Xml::closeElement( 'form' ) . "\n";
376 $output->addHTML( $form );
377
378 # If there's nothing to show, stop here
379 if ( $numRows == 0 ) {
380 $output->wrapWikiMsg(
381 "<div class='mw-changeslist-empty'>\n$1\n</div>", 'recentchanges-noresult'
382 );
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->getPageTitle(), $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->getPageTitle(),
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->getPageTitle(),
464 $message,
465 array(),
466 $options
467 );
468 }
469
470 /**
471 * Returns html
472 *
473 * @param int $days This gets overwritten, so is not used
474 * @param array $options Query parameters for URL
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 * @param DatabaseBase $dbr A database connection
498 * @return Integer
499 */
500 protected function countItems( $dbr ) {
501 # Fetch the raw count
502 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
503 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
504 $row = $dbr->fetchObject( $res );
505 $count = $row->count;
506
507 return floor( $count / 2 );
508 }
509 }