4b622ba06604ba477dcca338497a432bd5ccff0d
[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
24 /**
25 * Constructor
26 *
27 * @param $par Parameter passed to the page
28 */
29 function wfSpecialWatchlist( $par ) {
30 global $wgUser, $wgOut, $wgLang, $wgRequest;
31 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
32
33 // Add feed links
34 $wlToken = $wgUser->getOption( 'watchlisttoken' );
35 if (!$wlToken) {
36 $wlToken = sha1( mt_rand() . microtime( true ) );
37 $wgUser->setOption( 'watchlisttoken', $wlToken );
38 $wgUser->saveSettings();
39 }
40
41 global $wgFeedClasses;
42 $apiParams = array( 'action' => 'feedwatchlist', 'allrev' => 'allrev',
43 'wlowner' => $wgUser->getName(), 'wltoken' => $wlToken );
44 $feedTemplate = wfScript('api').'?';
45
46 foreach( $wgFeedClasses as $format => $class ) {
47 $theseParams = $apiParams + array( 'feedformat' => $format );
48 $url = $feedTemplate . wfArrayToCGI( $theseParams );
49 $wgOut->addFeedLink( $format, $url );
50 }
51
52 $skin = $wgUser->getSkin();
53 $specialTitle = SpecialPage::getTitleFor( 'Watchlist' );
54 $wgOut->setRobotPolicy( 'noindex,nofollow' );
55
56 # Anons don't get a watchlist
57 if( $wgUser->isAnon() ) {
58 $wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
59 $llink = $skin->linkKnown(
60 SpecialPage::getTitleFor( 'Userlogin' ),
61 wfMsgHtml( 'loginreqlink' ),
62 array(),
63 array( 'returnto' => $specialTitle->getPrefixedText() )
64 );
65 $wgOut->addHTML( wfMsgWikiHtml( 'watchlistanontext', $llink ) );
66 return;
67 }
68
69 $wgOut->setPageTitle( wfMsg( 'watchlist' ) );
70
71 $sub = wfMsgExt( 'watchlistfor2', array( 'parseinline', 'replaceafter' ), $wgUser->getName(), WatchlistEditor::buildTools( $wgUser->getSkin() ) );
72 $wgOut->setSubtitle( $sub );
73
74 if( ( $mode = WatchlistEditor::getMode( $wgRequest, $par ) ) !== false ) {
75 $editor = new WatchlistEditor();
76 $editor->execute( $wgUser, $wgOut, $wgRequest, $mode );
77 return;
78 }
79
80 $uid = $wgUser->getId();
81 if( ($wgEnotifWatchlist || $wgShowUpdatedMarker) && $wgRequest->getVal( 'reset' ) &&
82 $wgRequest->wasPosted() )
83 {
84 $wgUser->clearAllNotifications( $uid );
85 $wgOut->redirect( $specialTitle->getFullUrl() );
86 return;
87 }
88
89 $defaults = array(
90 /* float */ 'days' => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
91 /* bool */ 'hideMinor' => (int)$wgUser->getBoolOption( 'watchlisthideminor' ),
92 /* bool */ 'hideBots' => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
93 /* bool */ 'hideAnons' => (int)$wgUser->getBoolOption( 'watchlisthideanons' ),
94 /* bool */ 'hideLiu' => (int)$wgUser->getBoolOption( 'watchlisthideliu' ),
95 /* bool */ 'hidePatrolled' => (int)$wgUser->getBoolOption( 'watchlisthidepatrolled' ),
96 /* bool */ 'hideOwn' => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
97 /* ? */ 'namespace' => 'all',
98 /* ? */ 'invert' => false,
99 );
100
101 extract($defaults);
102
103 # Extract variables from the request, falling back to user preferences or
104 # other default values if these don't exist
105 $prefs['days'] = floatval( $wgUser->getOption( 'watchlistdays' ) );
106 $prefs['hideminor'] = $wgUser->getBoolOption( 'watchlisthideminor' );
107 $prefs['hidebots'] = $wgUser->getBoolOption( 'watchlisthidebots' );
108 $prefs['hideanons'] = $wgUser->getBoolOption( 'watchlisthideanon' );
109 $prefs['hideliu'] = $wgUser->getBoolOption( 'watchlisthideliu' );
110 $prefs['hideown' ] = $wgUser->getBoolOption( 'watchlisthideown' );
111 $prefs['hidepatrolled' ] = $wgUser->getBoolOption( 'watchlisthidepatrolled' );
112
113 # Get query variables
114 $days = $wgRequest->getVal( 'days' , $prefs['days'] );
115 $hideMinor = $wgRequest->getBool( 'hideMinor', $prefs['hideminor'] );
116 $hideBots = $wgRequest->getBool( 'hideBots' , $prefs['hidebots'] );
117 $hideAnons = $wgRequest->getBool( 'hideAnons', $prefs['hideanons'] );
118 $hideLiu = $wgRequest->getBool( 'hideLiu' , $prefs['hideliu'] );
119 $hideOwn = $wgRequest->getBool( 'hideOwn' , $prefs['hideown'] );
120 $hidePatrolled = $wgRequest->getBool( 'hidePatrolled' , $prefs['hidepatrolled'] );
121
122 # Get namespace value, if supplied, and prepare a WHERE fragment
123 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
124 $invert = $wgRequest->getIntOrNull( 'invert' );
125 if( !is_null( $nameSpace ) ) {
126 $nameSpace = intval( $nameSpace );
127 if( $invert && $nameSpace !== 'all' )
128 $nameSpaceClause = "rc_namespace != $nameSpace";
129 else
130 $nameSpaceClause = "rc_namespace = $nameSpace";
131 } else {
132 $nameSpace = '';
133 $nameSpaceClause = '';
134 }
135
136 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
137 $recentchanges = $dbr->tableName( 'recentchanges' );
138
139 $watchlistCount = $dbr->selectField( 'watchlist', 'COUNT(*)',
140 array( 'wl_user' => $uid ), __METHOD__ );
141 // Adjust for page X, talk:page X, which are both stored separately,
142 // but treated together
143 $nitems = floor($watchlistCount / 2);
144
145 if( is_null($days) || !is_numeric($days) ) {
146 $big = 1000; /* The magical big */
147 if($nitems > $big) {
148 # Set default cutoff shorter
149 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
150 } else {
151 $days = $defaults['days']; # default cutoff for shortlisters
152 }
153 } else {
154 $days = floatval($days);
155 }
156
157 // Dump everything here
158 $nondefaults = array();
159
160 wfAppendToArrayIfNotDefault( 'days' , $days , $defaults, $nondefaults);
161 wfAppendToArrayIfNotDefault( 'hideMinor', (int)$hideMinor, $defaults, $nondefaults );
162 wfAppendToArrayIfNotDefault( 'hideBots' , (int)$hideBots , $defaults, $nondefaults);
163 wfAppendToArrayIfNotDefault( 'hideAnons', (int)$hideAnons, $defaults, $nondefaults );
164 wfAppendToArrayIfNotDefault( 'hideLiu' , (int)$hideLiu , $defaults, $nondefaults );
165 wfAppendToArrayIfNotDefault( 'hideOwn' , (int)$hideOwn , $defaults, $nondefaults);
166 wfAppendToArrayIfNotDefault( 'namespace', $nameSpace , $defaults, $nondefaults);
167 wfAppendToArrayIfNotDefault( 'hidePatrolled', (int)$hidePatrolled, $defaults, $nondefaults );
168
169 if( $nitems == 0 ) {
170 $wgOut->addWikiMsg( 'nowatchlist' );
171 return;
172 }
173
174 # Possible where conditions
175 $conds = array();
176
177 if( $days <= 0 ) {
178 $andcutoff = '';
179 } else {
180 $conds[] = "rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
181 }
182
183 # If the watchlist is relatively short, it's simplest to zip
184 # down its entirety and then sort the results.
185
186 # If it's relatively long, it may be worth our while to zip
187 # through the time-sorted page list checking for watched items.
188
189 # Up estimate of watched items by 15% to compensate for talk pages...
190
191 # Toggles
192 if( $hideOwn ) {
193 $conds[] = "rc_user != $uid";
194 }
195 if( $hideBots ) {
196 $conds[] = 'rc_bot = 0';
197 }
198 if( $hideMinor ) {
199 $conds[] = 'rc_minor = 0';
200 }
201 if( $hideLiu ) {
202 $conds[] = 'rc_user = 0';
203 }
204 if( $hideAnons ) {
205 $conds[] = 'rc_user != 0';
206 }
207 if ( $wgUser->useRCPatrol() && $hidePatrolled ) {
208 $conds[] = 'rc_patrolled != 1';
209 }
210 if( $nameSpaceClause ) {
211 $conds[] = $nameSpaceClause;
212 }
213
214 # Toggle watchlist content (all recent edits or just the latest)
215 if( $wgUser->getOption( 'extendwatchlist' )) {
216 $limitWatchlist = intval( $wgUser->getOption( 'wllimit' ) );
217 $usePage = false;
218 } else {
219 # Top log Ids for a page are not stored
220 $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG;
221 $limitWatchlist = 0;
222 $usePage = true;
223 }
224
225 # Show a message about slave lag, if applicable
226 if( ( $lag = $dbr->getLag() ) > 0 )
227 $wgOut->showLagWarning( $lag );
228
229 # Create output form
230 $form = Xml::fieldset( wfMsg( 'watchlist-options' ), false, array( 'id' => 'mw-watchlist-options' ) );
231
232 # Show watchlist header
233 $form .= wfMsgExt( 'watchlist-details', array( 'parseinline' ), $wgLang->formatNum( $nitems ) );
234
235 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
236 $form .= wfMsgExt( 'wlheader-enotif', 'parse' ) . "\n";
237 }
238 if( $wgShowUpdatedMarker ) {
239 $form .= Xml::openElement( 'form', array( 'method' => 'post',
240 'action' => $specialTitle->getLocalUrl(),
241 'id' => 'mw-watchlist-resetbutton' ) ) .
242 wfMsgExt( 'wlheader-showupdated', array( 'parseinline' ) ) . ' ' .
243 Xml::submitButton( wfMsg( 'enotif_reset' ), array( 'name' => 'dummy' ) ) .
244 Html::hidden( 'reset', 'all' ) .
245 Xml::closeElement( 'form' );
246 }
247 $form .= '<hr />';
248
249 $tables = array( 'recentchanges', 'watchlist' );
250 $fields = array( "{$recentchanges}.*" );
251 $join_conds = array(
252 'watchlist' => array('INNER JOIN',"wl_user='{$uid}' AND wl_namespace=rc_namespace AND wl_title=rc_title"),
253 );
254 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
255 if( $wgShowUpdatedMarker ) {
256 $fields[] = 'wl_notificationtimestamp';
257 }
258 if( $limitWatchlist ) {
259 $options['LIMIT'] = $limitWatchlist;
260 }
261
262 $rollbacker = $wgUser->isAllowed('rollback');
263 if ( $usePage || $rollbacker ) {
264 $tables[] = 'page';
265 $join_conds['page'] = array('LEFT JOIN','rc_cur_id=page_id');
266 if ($rollbacker)
267 $fields[] = 'page_latest';
268 }
269
270 ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
271 wfRunHooks('SpecialWatchlistQuery', array(&$conds,&$tables,&$join_conds,&$fields) );
272
273 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
274 $numRows = $dbr->numRows( $res );
275
276 /* Start bottom header */
277
278 $wlInfo = '';
279 if( $days >= 1 ) {
280 $wlInfo = wfMsgExt( 'rcnote', 'parseinline',
281 $wgLang->formatNum( $numRows ),
282 $wgLang->formatNum( $days ),
283 $wgLang->timeAndDate( wfTimestampNow(), true ),
284 $wgLang->date( wfTimestampNow(), true ),
285 $wgLang->time( wfTimestampNow(), true )
286 ) . '<br />';
287 } elseif( $days > 0 ) {
288 $wlInfo = wfMsgExt( 'wlnote', 'parseinline',
289 $wgLang->formatNum( $numRows ),
290 $wgLang->formatNum( round($days*24) )
291 ) . '<br />';
292 }
293
294 $cutofflinks = "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n";
295
296 $thisTitle = SpecialPage::getTitleFor( 'Watchlist' );
297
298 # Spit out some control panel links
299 $links[] = wlShowHideLink( $nondefaults, 'rcshowhideminor', 'hideMinor', $hideMinor );
300 $links[] = wlShowHideLink( $nondefaults, 'rcshowhidebots', 'hideBots', $hideBots );
301 $links[] = wlShowHideLink( $nondefaults, 'rcshowhideanons', 'hideAnons', $hideAnons );
302 $links[] = wlShowHideLink( $nondefaults, 'rcshowhideliu', 'hideLiu', $hideLiu );
303 $links[] = wlShowHideLink( $nondefaults, 'rcshowhidemine', 'hideOwn', $hideOwn );
304
305 if( $wgUser->useRCPatrol() ) {
306 $links[] = wlShowHideLink( $nondefaults, 'rcshowhidepatr', 'hidePatrolled', $hidePatrolled );
307 }
308
309 # Namespace filter and put the whole form together.
310 $form .= $wlInfo;
311 $form .= $cutofflinks;
312 $form .= $wgLang->pipeList( $links );
313 $form .= Xml::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl(), 'id' => 'mw-watchlist-form-namespaceselector' ) );
314 $form .= '<hr /><p>';
315 $form .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&#160;';
316 $form .= Xml::namespaceSelector( $nameSpace, '' ) . '&#160;';
317 $form .= Xml::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $invert ) . '&#160;';
318 $form .= Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
319 $form .= Html::hidden( 'days', $days );
320 if( $hideMinor )
321 $form .= Html::hidden( 'hideMinor', 1 );
322 if( $hideBots )
323 $form .= Html::hidden( 'hideBots', 1 );
324 if( $hideAnons )
325 $form .= Html::hidden( 'hideAnons', 1 );
326 if( $hideLiu )
327 $form .= Html::hidden( 'hideLiu', 1 );
328 if( $hideOwn )
329 $form .= Html::hidden( 'hideOwn', 1 );
330 $form .= Xml::closeElement( 'form' );
331 $form .= Xml::closeElement( 'fieldset' );
332 $wgOut->addHTML( $form );
333
334 # If there's nothing to show, stop here
335 if( $numRows == 0 ) {
336 $wgOut->addWikiMsg( 'watchnochange' );
337 return;
338 }
339
340 /* End bottom header */
341
342 /* Do link batch query */
343 $linkBatch = new LinkBatch;
344 foreach ( $res as $row ) {
345 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
346 if ( $row->rc_user != 0 ) {
347 $linkBatch->add( NS_USER, $userNameUnderscored );
348 }
349 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
350
351 $linkBatch->add( $row->rc_namespace, $row->rc_title );
352 }
353 $linkBatch->execute();
354 $dbr->dataSeek( $res, 0 );
355
356 $list = ChangesList::newFromUser( $wgUser );
357 $list->setWatchlistDivs();
358
359 $s = $list->beginRecentChangesList();
360 $counter = 1;
361 foreach ( $res as $obj ) {
362 # Make RC entry
363 $rc = RecentChange::newFromRow( $obj );
364 $rc->counter = $counter++;
365
366 if ( $wgShowUpdatedMarker ) {
367 $updated = $obj->wl_notificationtimestamp;
368 } else {
369 $updated = false;
370 }
371
372 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
373 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
374 'COUNT(*)',
375 array(
376 'wl_namespace' => $obj->rc_namespace,
377 'wl_title' => $obj->rc_title,
378 ),
379 __METHOD__ );
380 } else {
381 $rc->numberofWatchingusers = 0;
382 }
383
384 $s .= $list->recentChangesLine( $rc, $updated, $counter );
385 }
386 $s .= $list->endRecentChangesList();
387
388 $wgOut->addHTML( $s );
389 }
390
391 function wlShowHideLink( $options, $message, $name, $value ) {
392 global $wgUser;
393
394 $showLinktext = wfMsgHtml( 'show' );
395 $hideLinktext = wfMsgHtml( 'hide' );
396 $title = SpecialPage::getTitleFor( 'Watchlist' );
397 $skin = $wgUser->getSkin();
398
399 $label = $value ? $showLinktext : $hideLinktext;
400 $options[$name] = 1 - (int) $value;
401
402 return wfMsgHtml( $message, $skin->linkKnown( $title, $label, array(), $options ) );
403 }
404
405
406 function wlHoursLink( $h, $page, $options = array() ) {
407 global $wgUser, $wgLang, $wgContLang;
408
409 $sk = $wgUser->getSkin();
410 $title = Title::newFromText( $wgContLang->specialPage( $page ) );
411 $options['days'] = ($h / 24.0);
412
413 $s = $sk->linkKnown(
414 $title,
415 $wgLang->formatNum( $h ),
416 array(),
417 $options
418 );
419
420 return $s;
421 }
422
423 function wlDaysLink( $d, $page, $options = array() ) {
424 global $wgUser, $wgLang, $wgContLang;
425
426 $sk = $wgUser->getSkin();
427 $title = Title::newFromText( $wgContLang->specialPage( $page ) );
428 $options['days'] = $d;
429 $message = ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) );
430
431 $s = $sk->linkKnown(
432 $title,
433 $message,
434 array(),
435 $options
436 );
437
438 return $s;
439 }
440
441 /**
442 * Returns html
443 */
444 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
445 global $wgLang;
446
447 $hours = array( 1, 2, 6, 12 );
448 $days = array( 1, 3, 7 );
449 $i = 0;
450 foreach( $hours as $h ) {
451 $hours[$i++] = wlHoursLink( $h, $page, $options );
452 }
453 $i = 0;
454 foreach( $days as $d ) {
455 $days[$i++] = wlDaysLink( $d, $page, $options );
456 }
457 return wfMsgExt('wlshowlast',
458 array('parseinline', 'replaceafter'),
459 $wgLang->pipeList( $hours ),
460 $wgLang->pipeList( $days ),
461 wlDaysLink( 0, $page, $options ) );
462 }
463
464 /**
465 * Count the number of items on a user's watchlist
466 *
467 * @param $user User object
468 * @param $talk Boolean: include talk pages
469 * @return Integer
470 */
471 function wlCountItems( &$user, $talk = true ) {
472 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
473
474 # Fetch the raw count
475 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count',
476 array( 'wl_user' => $user->mId ), 'wlCountItems' );
477 $row = $dbr->fetchObject( $res );
478 $count = $row->count;
479
480 # Halve to remove talk pages if needed
481 if( !$talk )
482 $count = floor( $count / 2 );
483
484 return( $count );
485 }