(bug 11562) API: Added a user_registration parameter/field to the list=allusers query...
[lhc/web/wiklou.git] / includes / SpecialWatchlist.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
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 && $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 $sql = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_user=$uid";
91 $res = $dbr->query( $sql, $fname );
92 $s = $dbr->fetchObject( $res );
93
94 # Patch *** A1 *** (see A2 below)
95 # adjust for page X, talk:page X, which are both stored separately, but treated together
96 $nitems = floor($s->n / 2);
97 # $nitems = $s->n;
98
99 if($nitems == 0) {
100 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
101 return;
102 }
103
104 if( is_null($days) || !is_numeric($days) ) {
105 $big = 1000; /* The magical big */
106 if($nitems > $big) {
107 # Set default cutoff shorter
108 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
109 } else {
110 $days = $defaults['days']; # default cutoff for shortlisters
111 }
112 } else {
113 $days = floatval($days);
114 }
115
116 // Dump everything here
117 $nondefaults = array();
118
119 wfAppendToArrayIfNotDefault('days' , $days , $defaults, $nondefaults);
120 wfAppendToArrayIfNotDefault('hideOwn' , (int)$hideOwn , $defaults, $nondefaults);
121 wfAppendToArrayIfNotDefault('hideBots' , (int)$hideBots, $defaults, $nondefaults);
122 wfAppendToArrayIfNotDefault( 'hideMinor', (int)$hideMinor, $defaults, $nondefaults );
123 wfAppendToArrayIfNotDefault('namespace', $nameSpace , $defaults, $nondefaults);
124
125 if ( $days <= 0 ) {
126 $andcutoff = '';
127 } else {
128 $andcutoff = "AND rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
129 /*
130 $sql = "SELECT COUNT(*) AS n FROM $page, $revision WHERE rev_timestamp>'$cutoff' AND page_id=rev_page";
131 $res = $dbr->query( $sql, $fname );
132 $s = $dbr->fetchObject( $res );
133 $npages = $s->n;
134 */
135 }
136
137 # If the watchlist is relatively short, it's simplest to zip
138 # down its entirety and then sort the results.
139
140 # If it's relatively long, it may be worth our while to zip
141 # through the time-sorted page list checking for watched items.
142
143 # Up estimate of watched items by 15% to compensate for talk pages...
144
145 # Toggles
146 $andHideOwn = $hideOwn ? "AND (rc_user <> $uid)" : '';
147 $andHideBots = $hideBots ? "AND (rc_bot = 0)" : '';
148 $andHideMinor = $hideMinor ? 'AND rc_minor = 0' : '';
149
150 # Show watchlist header
151 $header = '';
152 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
153 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
154 }
155 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
156 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
157 }
158
159 # Toggle watchlist content (all recent edits or just the latest)
160 if( $wgUser->getOption( 'extendwatchlist' )) {
161 $andLatest='';
162 $limitWatchlist = 'LIMIT ' . intval( $wgUser->getOption( 'wllimit' ) );
163 } else {
164 $andLatest= 'AND rc_this_oldid=page_latest';
165 $limitWatchlist = '';
166 }
167
168 $header .= wfMsgExt( 'watchlist-details', array( 'parsemag' ), $wgLang->formatNum( $nitems ) );
169 $wgOut->addWikiText( $header );
170
171 # Show a message about slave lag, if applicable
172 if( ( $lag = $dbr->getLag() ) > 0 )
173 $wgOut->showLagWarning( $lag );
174
175 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
176 $wgOut->addHTML( '<form action="' .
177 $specialTitle->escapeLocalUrl() .
178 '" method="post"><input type="submit" name="dummy" value="' .
179 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
180 '" /><input type="hidden" name="reset" value="all" /></form>' .
181 "\n\n" );
182 }
183
184 $sql = "SELECT *
185 FROM $watchlist,$recentchanges,$page
186 WHERE wl_user=$uid
187 AND wl_namespace=rc_namespace
188 AND wl_title=rc_title
189 AND rc_cur_id=page_id
190 $andcutoff
191 $andLatest
192 $andHideOwn
193 $andHideBots
194 $andHideMinor
195 $nameSpaceClause
196 ORDER BY rc_timestamp DESC
197 $limitWatchlist";
198
199 $res = $dbr->query( $sql, $fname );
200 $numRows = $dbr->numRows( $res );
201
202 /* Start bottom header */
203 $wgOut->addHTML( "<hr />\n" );
204
205 if($days >= 1) {
206 $wgOut->addWikiText( wfMsgExt( 'rcnote', array( 'parseinline' ), $wgLang->formatNum( $numRows ),
207 $wgLang->formatNum( $days ), $wgLang->timeAndDate( wfTimestampNow(), true ) ) . '<br />' , false );
208 } elseif($days > 0) {
209 $wgOut->addWikiText( wfMsgExt( 'wlnote', array( 'parseinline' ), $wgLang->formatNum( $numRows ),
210 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
211 }
212
213 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
214
215 # Spit out some control panel links
216 $thisTitle = SpecialPage::getTitleFor( 'Watchlist' );
217 $skin = $wgUser->getSkin();
218
219 # Hide/show bot edits
220 $label = $hideBots ? wfMsgHtml( 'watchlist-show-bots' ) : wfMsgHtml( 'watchlist-hide-bots' );
221 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
222 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
223
224 # Hide/show own edits
225 $label = $hideOwn ? wfMsgHtml( 'watchlist-show-own' ) : wfMsgHtml( 'watchlist-hide-own' );
226 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
227 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
228
229 # Hide/show minor edits
230 $label = $hideMinor ? wfMsgHtml( 'watchlist-show-minor' ) : wfMsgHtml( 'watchlist-hide-minor' );
231 $linkBits = wfArrayToCGI( array( 'hideMinor' => 1 - (int)$hideMinor ), $nondefaults );
232 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
233
234 $wgOut->addHTML( implode( ' | ', $links ) );
235
236 # Form for namespace filtering
237 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl() ) );
238 $form .= '<p>';
239 $form .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&nbsp;';
240 $form .= Xml::namespaceSelector( $nameSpace, '' ) . '&nbsp;';
241 $form .= Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
242 $form .= Xml::hidden( 'days', $days );
243 if( $hideOwn )
244 $form .= Xml::hidden( 'hideOwn', 1 );
245 if( $hideBots )
246 $form .= Xml::hidden( 'hideBots', 1 );
247 if( $hideMinor )
248 $form .= Xml::hidden( 'hideMinor', 1 );
249 $form .= Xml::closeElement( 'form' );
250 $wgOut->addHtml( $form );
251
252 # If there's nothing to show, stop here
253 if( $numRows == 0 ) {
254 $wgOut->addWikiText( wfMsgNoTrans( 'watchnochange' ) );
255 return;
256 }
257
258 /* End bottom header */
259
260 /* Do link batch query */
261 $linkBatch = new LinkBatch;
262 while ( $row = $dbr->fetchObject( $res ) ) {
263 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
264 if ( $row->rc_user != 0 ) {
265 $linkBatch->add( NS_USER, $userNameUnderscored );
266 }
267 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
268 }
269 $linkBatch->execute();
270 $dbr->dataSeek( $res, 0 );
271
272 $list = ChangesList::newFromUser( $wgUser );
273
274 $s = $list->beginRecentChangesList();
275 $counter = 1;
276 while ( $obj = $dbr->fetchObject( $res ) ) {
277 # Make RC entry
278 $rc = RecentChange::newFromRow( $obj );
279 $rc->counter = $counter++;
280
281 if ( $wgShowUpdatedMarker ) {
282 $updated = $obj->wl_notificationtimestamp;
283 } else {
284 // Same visual appearance as MW 1.4
285 $updated = true;
286 }
287
288 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
289 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" .$dbr->strencode($obj->page_title). "' AND wl_namespace='{$obj->page_namespace}'" ;
290 $res3 = $dbr->query( $sql3, $fname );
291 $x = $dbr->fetchObject( $res3 );
292 $rc->numberofWatchingusers = $x->n;
293 } else {
294 $rc->numberofWatchingusers = 0;
295 }
296
297 $s .= $list->recentChangesLine( $rc, $updated );
298 }
299 $s .= $list->endRecentChangesList();
300
301 $dbr->freeResult( $res );
302 $wgOut->addHTML( $s );
303
304 }
305
306 function wlHoursLink( $h, $page, $options = array() ) {
307 global $wgUser, $wgLang, $wgContLang;
308 $sk = $wgUser->getSkin();
309 $s = $sk->makeKnownLink(
310 $wgContLang->specialPage( $page ),
311 $wgLang->formatNum( $h ),
312 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
313 return $s;
314 }
315
316 function wlDaysLink( $d, $page, $options = array() ) {
317 global $wgUser, $wgLang, $wgContLang;
318 $sk = $wgUser->getSkin();
319 $s = $sk->makeKnownLink(
320 $wgContLang->specialPage( $page ),
321 ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
322 wfArrayToCGI( array('days' => $d), $options ) );
323 return $s;
324 }
325
326 /**
327 * Returns html
328 */
329 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
330 $hours = array( 1, 2, 6, 12 );
331 $days = array( 1, 3, 7 );
332 $i = 0;
333 foreach( $hours as $h ) {
334 $hours[$i++] = wlHoursLink( $h, $page, $options );
335 }
336 $i = 0;
337 foreach( $days as $d ) {
338 $days[$i++] = wlDaysLink( $d, $page, $options );
339 }
340 return wfMsgExt('wlshowlast',
341 array('parseinline', 'replaceafter'),
342 implode(' | ', $hours),
343 implode(' | ', $days),
344 wlDaysLink( 0, $page, $options ) );
345 }
346
347 /**
348 * Count the number of items on a user's watchlist
349 *
350 * @param $talk Include talk pages
351 * @return integer
352 */
353 function wlCountItems( &$user, $talk = true ) {
354 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
355
356 # Fetch the raw count
357 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId ), 'wlCountItems' );
358 $row = $dbr->fetchObject( $res );
359 $count = $row->count;
360 $dbr->freeResult( $res );
361
362 # Halve to remove talk pages if needed
363 if( !$talk )
364 $count = floor( $count / 2 );
365
366 return( $count );
367 }