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