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