Merge "Allow API results to wrap long lines"
[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 $prefs['days'] = floatval( $user->getOption( 'watchlistdays' ) );
125 $prefs['hideminor'] = $user->getBoolOption( 'watchlisthideminor' );
126 $prefs['hidebots'] = $user->getBoolOption( 'watchlisthidebots' );
127 $prefs['hideanons'] = $user->getBoolOption( 'watchlisthideanons' );
128 $prefs['hideliu'] = $user->getBoolOption( 'watchlisthideliu' );
129 $prefs['hideown'] = $user->getBoolOption( 'watchlisthideown' );
130 $prefs['hidepatrolled'] = $user->getBoolOption( 'watchlisthidepatrolled' );
131 $prefs['extended'] = $user->getBoolOption( 'extendwatchlist' );
132
133 # Get query variables
134 $values = array();
135 $values['days'] = $request->getVal( 'days', $prefs['days'] );
136 $values['hideMinor'] = (int)$request->getBool( 'hideMinor', $prefs['hideminor'] );
137 $values['hideBots'] = (int)$request->getBool( 'hideBots', $prefs['hidebots'] );
138 $values['hideAnons'] = (int)$request->getBool( 'hideAnons', $prefs['hideanons'] );
139 $values['hideLiu'] = (int)$request->getBool( 'hideLiu', $prefs['hideliu'] );
140 $values['hideOwn'] = (int)$request->getBool( 'hideOwn', $prefs['hideown'] );
141 $values['hidePatrolled'] = (int)$request->getBool( 'hidePatrolled', $prefs['hidepatrolled'] );
142 $values['extended'] = (int)$request->getBool( 'extended', $prefs['extended'] );
143 foreach( $this->customFilters as $key => $params ) {
144 $values[$key] = (int)$request->getBool( $key );
145 }
146
147 # Get namespace value, if supplied, and prepare a WHERE fragment
148 $nameSpace = $request->getIntOrNull( 'namespace' );
149 $invert = $request->getBool( 'invert' );
150 $associated = $request->getBool( 'associated' );
151 if ( !is_null( $nameSpace ) ) {
152 $eq_op = $invert ? '!=' : '=';
153 $bool_op = $invert ? 'AND' : 'OR';
154 $nameSpace = intval( $nameSpace ); // paranioa
155 if ( !$associated ) {
156 $nameSpaceClause = "rc_namespace $eq_op $nameSpace";
157 } else {
158 $associatedNS = MWNamespace::getAssociated( $nameSpace );
159 $nameSpaceClause =
160 "rc_namespace $eq_op $nameSpace " .
161 $bool_op .
162 " rc_namespace $eq_op $associatedNS";
163 }
164 } else {
165 $nameSpace = '';
166 $nameSpaceClause = '';
167 }
168 $values['namespace'] = $nameSpace;
169 $values['invert'] = $invert;
170 $values['associated'] = $associated;
171
172 if( is_null( $values['days'] ) || !is_numeric( $values['days'] ) ) {
173 $big = 1000; /* The magical big */
174 if( $nitems > $big ) {
175 # Set default cutoff shorter
176 $values['days'] = $defaults['days'] = (12.0 / 24.0); # 12 hours...
177 } else {
178 $values['days'] = $defaults['days']; # default cutoff for shortlisters
179 }
180 } else {
181 $values['days'] = floatval( $values['days'] );
182 }
183
184 // Dump everything here
185 $nondefaults = array();
186 foreach ( $defaults as $name => $defValue ) {
187 wfAppendToArrayIfNotDefault( $name, $values[$name], $defaults, $nondefaults );
188 }
189
190 if( ( $wgEnotifWatchlist || $wgShowUpdatedMarker ) && $request->getVal( 'reset' ) &&
191 $request->wasPosted() )
192 {
193 $user->clearAllNotifications();
194 $output->redirect( $this->getTitle()->getFullUrl( $nondefaults ) );
195 return;
196 }
197
198 # Possible where conditions
199 $conds = array();
200
201 if( $values['days'] > 0 ) {
202 $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( time() - intval( $values['days'] * 86400 ) ) );
203 }
204
205 # If the watchlist is relatively short, it's simplest to zip
206 # down its entirety and then sort the results.
207
208 # If it's relatively long, it may be worth our while to zip
209 # through the time-sorted page list checking for watched items.
210
211 # Up estimate of watched items by 15% to compensate for talk pages...
212
213
214 # Toggles
215 if( $values['hideOwn'] ) {
216 $conds[] = 'rc_user != ' . $user->getId();
217 }
218 if( $values['hideBots'] ) {
219 $conds[] = 'rc_bot = 0';
220 }
221 if( $values['hideMinor'] ) {
222 $conds[] = 'rc_minor = 0';
223 }
224 if( $values['hideLiu'] ) {
225 $conds[] = 'rc_user = 0';
226 }
227 if( $values['hideAnons'] ) {
228 $conds[] = 'rc_user != 0';
229 }
230 if ( $user->useRCPatrol() && $values['hidePatrolled'] ) {
231 $conds[] = 'rc_patrolled != 1';
232 }
233 if ( $nameSpaceClause ) {
234 $conds[] = $nameSpaceClause;
235 }
236
237 # Toggle watchlist content (all recent edits or just the latest)
238 if( $values['extended'] ) {
239 $limitWatchlist = intval( $user->getOption( 'wllimit' ) );
240 $usePage = false;
241 } else {
242 # Top log Ids for a page are not stored
243 $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG;
244 $limitWatchlist = 0;
245 $usePage = true;
246 }
247
248 # Show a message about slave lag, if applicable
249 $lag = wfGetLB()->safeGetLag( $dbr );
250 if( $lag > 0 ) {
251 $output->showLagWarning( $lag );
252 }
253
254 # Create output form
255 $form = Xml::fieldset( $this->msg( 'watchlist-options' )->text(), false, array( 'id' => 'mw-watchlist-options' ) );
256
257 # Show watchlist header
258 $form .= $this->msg( 'watchlist-details' )->numParams( $nitems )->parse();
259
260 if( $user->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
261 $form .= $this->msg( 'wlheader-enotif' )->parseAsBlock() . "\n";
262 }
263 if( $wgShowUpdatedMarker ) {
264 $form .= Xml::openElement( 'form', array( 'method' => 'post',
265 'action' => $this->getTitle()->getLocalUrl(),
266 'id' => 'mw-watchlist-resetbutton' ) ) .
267 $this->msg( 'wlheader-showupdated' )->parse() . ' ' .
268 Xml::submitButton( $this->msg( 'enotif_reset' )->text(), array( 'name' => 'dummy' ) ) .
269 Html::hidden( 'reset', 'all' );
270 foreach ( $nondefaults as $key => $value ) {
271 $form .= Html::hidden( $key, $value );
272 }
273 $form .= Xml::closeElement( 'form' );
274 }
275 $form .= '<hr />';
276
277 $tables = array( 'recentchanges', 'watchlist' );
278 $fields = RecentChange::selectFields();
279 $join_conds = array(
280 'watchlist' => array(
281 'INNER JOIN',
282 array(
283 'wl_user' => $user->getId(),
284 'wl_namespace=rc_namespace',
285 'wl_title=rc_title'
286 ),
287 ),
288 );
289 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
290 if( $wgShowUpdatedMarker ) {
291 $fields[] = 'wl_notificationtimestamp';
292 }
293 if( $limitWatchlist ) {
294 $options['LIMIT'] = $limitWatchlist;
295 }
296
297 $rollbacker = $user->isAllowed( 'rollback' );
298 if ( $usePage || $rollbacker ) {
299 $tables[] = 'page';
300 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
301 if ( $rollbacker ) {
302 $fields[] = 'page_latest';
303 }
304 }
305
306 ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
307 wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, &$join_conds, &$fields ) );
308
309 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
310 $numRows = $res->numRows();
311
312 /* Start bottom header */
313
314 $lang = $this->getLanguage();
315 $wlInfo = '';
316 if( $values['days'] > 0 ) {
317 $timestamp = wfTimestampNow();
318 $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $values['days'] * 24 ) )->params(
319 $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user ) )->parse() . '<br />';
320 }
321
322 $cutofflinks = "\n" . $this->cutoffLinks( $values['days'], $nondefaults ) . "<br />\n";
323
324 # Spit out some control panel links
325 $filters = array(
326 'hideMinor' => 'rcshowhideminor',
327 'hideBots' => 'rcshowhidebots',
328 'hideAnons' => 'rcshowhideanons',
329 'hideLiu' => 'rcshowhideliu',
330 'hideOwn' => 'rcshowhidemine',
331 'hidePatrolled' => 'rcshowhidepatr'
332 );
333 foreach ( $this->customFilters as $key => $params ) {
334 $filters[$key] = $params['msg'];
335 }
336 // Disable some if needed
337 if ( !$user->useNPPatrol() ) {
338 unset( $filters['hidePatrolled'] );
339 }
340
341 $links = array();
342 foreach( $filters as $name => $msg ) {
343 $links[] = $this->showHideLink( $nondefaults, $msg, $name, $values[$name] );
344 }
345
346 # Namespace filter and put the whole form together.
347 $form .= $wlInfo;
348 $form .= $cutofflinks;
349 $form .= $lang->pipeList( $links );
350 $form .= Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalUrl(), 'id' => 'mw-watchlist-form-namespaceselector' ) );
351 $form .= '<hr /><p>';
352 $form .= Html::namespaceSelector(
353 array(
354 'selected' => $nameSpace,
355 'all' => '',
356 'label' => $this->msg( 'namespace' )->text()
357 ), array(
358 'name' => 'namespace',
359 'id' => 'namespace',
360 'class' => 'namespaceselector',
361 )
362 ) . '&#160;';
363 $form .= Xml::checkLabel(
364 $this->msg( 'invert' )->text(),
365 'invert',
366 'nsinvert',
367 $invert,
368 array( 'title' => $this->msg( 'tooltip-invert' )->text() )
369 ) . '&#160;';
370 $form .= Xml::checkLabel(
371 $this->msg( 'namespace_association' )->text(),
372 'associated',
373 'associated',
374 $associated,
375 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text() )
376 ) . '&#160;';
377 $form .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . '</p>';
378 $form .= Html::hidden( 'days', $values['days'] );
379 foreach ( $filters as $key => $msg ) {
380 if ( $values[$key] ) {
381 $form .= Html::hidden( $key, 1 );
382 }
383 }
384 $form .= Xml::closeElement( 'form' );
385 $form .= Xml::closeElement( 'fieldset' );
386 $output->addHTML( $form );
387
388 # If there's nothing to show, stop here
389 if( $numRows == 0 ) {
390 $output->addWikiMsg( 'watchnochange' );
391 return;
392 }
393
394 /* End bottom header */
395
396 /* Do link batch query */
397 $linkBatch = new LinkBatch;
398 foreach ( $res as $row ) {
399 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
400 if ( $row->rc_user != 0 ) {
401 $linkBatch->add( NS_USER, $userNameUnderscored );
402 }
403 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
404
405 $linkBatch->add( $row->rc_namespace, $row->rc_title );
406 }
407 $linkBatch->execute();
408 $dbr->dataSeek( $res, 0 );
409
410 $list = ChangesList::newFromContext( $this->getContext() );
411 $list->setWatchlistDivs();
412
413 $s = $list->beginRecentChangesList();
414 $counter = 1;
415 foreach ( $res as $obj ) {
416 # Make RC entry
417 $rc = RecentChange::newFromRow( $obj );
418 $rc->counter = $counter++;
419
420 if ( $wgShowUpdatedMarker ) {
421 $updated = $obj->wl_notificationtimestamp;
422 } else {
423 $updated = false;
424 }
425
426 if ( $wgRCShowWatchingUsers && $user->getOption( 'shownumberswatching' ) ) {
427 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
428 'COUNT(*)',
429 array(
430 'wl_namespace' => $obj->rc_namespace,
431 'wl_title' => $obj->rc_title,
432 ),
433 __METHOD__ );
434 } else {
435 $rc->numberofWatchingusers = 0;
436 }
437
438 $changeLine = $list->recentChangesLine( $rc, $updated, $counter );
439 if ( $changeLine !== false ) {
440 $s .= $changeLine;
441 }
442 }
443 $s .= $list->endRecentChangesList();
444
445 $output->addHTML( $s );
446 }
447
448 protected function showHideLink( $options, $message, $name, $value ) {
449 $label = $this->msg( $value ? 'show' : 'hide' )->escaped();
450 $options[$name] = 1 - (int) $value;
451
452 return $this->msg( $message )->rawParams( Linker::linkKnown( $this->getTitle(), $label, array(), $options ) )->escaped();
453 }
454
455 protected function hoursLink( $h, $options = array() ) {
456 $options['days'] = ( $h / 24.0 );
457
458 return Linker::linkKnown(
459 $this->getTitle(),
460 $this->getLanguage()->formatNum( $h ),
461 array(),
462 $options
463 );
464 }
465
466 protected function daysLink( $d, $options = array() ) {
467 $options['days'] = $d;
468 $message = ( $d ? $this->getLanguage()->formatNum( $d ) : $this->msg( 'watchlistall2' )->escaped() );
469
470 return Linker::linkKnown(
471 $this->getTitle(),
472 $message,
473 array(),
474 $options
475 );
476 }
477
478 /**
479 * Returns html
480 *
481 * @return string
482 */
483 protected function cutoffLinks( $days, $options = array() ) {
484 $hours = array( 1, 2, 6, 12 );
485 $days = array( 1, 3, 7 );
486 $i = 0;
487 foreach( $hours as $h ) {
488 $hours[$i++] = $this->hoursLink( $h, $options );
489 }
490 $i = 0;
491 foreach( $days as $d ) {
492 $days[$i++] = $this->daysLink( $d, $options );
493 }
494 return $this->msg( 'wlshowlast' )->rawParams(
495 $this->getLanguage()->pipeList( $hours ),
496 $this->getLanguage()->pipeList( $days ),
497 $this->daysLink( 0, $options ) )->parse();
498 }
499
500 /**
501 * Count the number of items on a user's watchlist
502 *
503 * @param $dbr A database connection
504 * @return Integer
505 */
506 protected function countItems( $dbr ) {
507 # Fetch the raw count
508 $res = $dbr->select( 'watchlist', array( 'count' => 'COUNT(*)' ),
509 array( 'wl_user' => $this->getUser()->getId() ), __METHOD__ );
510 $row = $dbr->fetchObject( $res );
511 $count = $row->count;
512
513 return floor( $count / 2 );
514 }
515 }