API: Updating action=help credits to give Yuri and Vasiliev due credit
[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 $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 ( $wgEnotifWatchlist && $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 $andLatest= 'AND rc_this_oldid=page_latest';
167 $limitWatchlist = '';
168 }
169
170 $header .= wfMsgExt( 'watchlist-details', array( 'parsemag' ), $wgLang->formatNum( $nitems ) );
171 $wgOut->addWikiText( $header );
172
173 # Show a message about slave lag, if applicable
174 if( ( $lag = $dbr->getLag() ) > 0 )
175 $wgOut->showLagWarning( $lag );
176
177 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
178 $wgOut->addHTML( '<form action="' .
179 $specialTitle->escapeLocalUrl() .
180 '" method="post"><input type="submit" name="dummy" value="' .
181 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
182 '" /><input type="hidden" name="reset" value="all" /></form>' .
183 "\n\n" );
184 }
185 if ( $wgShowUpdatedMarker ) {
186 $wltsfield=", ${watchlist}.wl_notificationtimestamp ";
187 }
188 $sql = "SELECT ${recentchanges}.* ${wltsfield}
189 FROM $watchlist,$recentchanges,$page
190 WHERE wl_user=$uid
191 AND wl_namespace=rc_namespace
192 AND wl_title=rc_title
193 AND rc_cur_id=page_id
194 $andcutoff
195 $andLatest
196 $andHideOwn
197 $andHideBots
198 $andHideMinor
199 $nameSpaceClause
200 $hookSql
201 ORDER BY rc_timestamp DESC
202 $limitWatchlist";
203
204 $res = $dbr->query( $sql, $fname );
205 $numRows = $dbr->numRows( $res );
206
207 /* Start bottom header */
208 $wgOut->addHTML( "<hr />\n" );
209
210 if($days >= 1) {
211 $wgOut->addWikiText( wfMsgExt( 'rcnote', array( 'parseinline' ), $wgLang->formatNum( $numRows ),
212 $wgLang->formatNum( $days ), $wgLang->timeAndDate( wfTimestampNow(), true ) ) . '<br />' , false );
213 } elseif($days > 0) {
214 $wgOut->addWikiText( wfMsgExt( 'wlnote', array( 'parseinline' ), $wgLang->formatNum( $numRows ),
215 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
216 }
217
218 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
219
220 # Spit out some control panel links
221 $thisTitle = SpecialPage::getTitleFor( 'Watchlist' );
222 $skin = $wgUser->getSkin();
223
224 # Hide/show bot edits
225 $label = $hideBots ? wfMsgHtml( 'watchlist-show-bots' ) : wfMsgHtml( 'watchlist-hide-bots' );
226 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
227 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
228
229 # Hide/show own edits
230 $label = $hideOwn ? wfMsgHtml( 'watchlist-show-own' ) : wfMsgHtml( 'watchlist-hide-own' );
231 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
232 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
233
234 # Hide/show minor edits
235 $label = $hideMinor ? wfMsgHtml( 'watchlist-show-minor' ) : wfMsgHtml( 'watchlist-hide-minor' );
236 $linkBits = wfArrayToCGI( array( 'hideMinor' => 1 - (int)$hideMinor ), $nondefaults );
237 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
238
239 $wgOut->addHTML( implode( ' | ', $links ) );
240
241 # Form for namespace filtering
242 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl() ) );
243 $form .= '<p>';
244 $form .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&nbsp;';
245 $form .= Xml::namespaceSelector( $nameSpace, '' ) . '&nbsp;';
246 $form .= Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
247 $form .= Xml::hidden( 'days', $days );
248 if( $hideOwn )
249 $form .= Xml::hidden( 'hideOwn', 1 );
250 if( $hideBots )
251 $form .= Xml::hidden( 'hideBots', 1 );
252 if( $hideMinor )
253 $form .= Xml::hidden( 'hideMinor', 1 );
254 $form .= Xml::closeElement( 'form' );
255 $wgOut->addHtml( $form );
256
257 # If there's nothing to show, stop here
258 if( $numRows == 0 ) {
259 $wgOut->addWikiMsg( 'watchnochange' );
260 return;
261 }
262
263 /* End bottom header */
264
265 /* Do link batch query */
266 $linkBatch = new LinkBatch;
267 while ( $row = $dbr->fetchObject( $res ) ) {
268 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
269 if ( $row->rc_user != 0 ) {
270 $linkBatch->add( NS_USER, $userNameUnderscored );
271 }
272 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
273 }
274 $linkBatch->execute();
275 $dbr->dataSeek( $res, 0 );
276
277 $list = ChangesList::newFromUser( $wgUser );
278
279 $s = $list->beginRecentChangesList();
280 $counter = 1;
281 while ( $obj = $dbr->fetchObject( $res ) ) {
282 # Make RC entry
283 $rc = RecentChange::newFromRow( $obj );
284 $rc->counter = $counter++;
285
286 if ( $wgShowUpdatedMarker ) {
287 $updated = $obj->wl_notificationtimestamp;
288 } else {
289 // Same visual appearance as MW 1.4
290 $updated = true;
291 }
292
293 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
294 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
295 'COUNT(*)',
296 array(
297 'wl_namespace' => $obj->rc_namespace,
298 'wl_title' => $obj->rc_title,
299 ),
300 __METHOD__ );
301 } else {
302 $rc->numberofWatchingusers = 0;
303 }
304
305 $s .= $list->recentChangesLine( $rc, $updated );
306 }
307 $s .= $list->endRecentChangesList();
308
309 $dbr->freeResult( $res );
310 $wgOut->addHTML( $s );
311
312 }
313
314 function wlHoursLink( $h, $page, $options = array() ) {
315 global $wgUser, $wgLang, $wgContLang;
316 $sk = $wgUser->getSkin();
317 $s = $sk->makeKnownLink(
318 $wgContLang->specialPage( $page ),
319 $wgLang->formatNum( $h ),
320 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
321 return $s;
322 }
323
324 function wlDaysLink( $d, $page, $options = array() ) {
325 global $wgUser, $wgLang, $wgContLang;
326 $sk = $wgUser->getSkin();
327 $s = $sk->makeKnownLink(
328 $wgContLang->specialPage( $page ),
329 ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
330 wfArrayToCGI( array('days' => $d), $options ) );
331 return $s;
332 }
333
334 /**
335 * Returns html
336 */
337 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
338 $hours = array( 1, 2, 6, 12 );
339 $days = array( 1, 3, 7 );
340 $i = 0;
341 foreach( $hours as $h ) {
342 $hours[$i++] = wlHoursLink( $h, $page, $options );
343 }
344 $i = 0;
345 foreach( $days as $d ) {
346 $days[$i++] = wlDaysLink( $d, $page, $options );
347 }
348 return wfMsgExt('wlshowlast',
349 array('parseinline', 'replaceafter'),
350 implode(' | ', $hours),
351 implode(' | ', $days),
352 wlDaysLink( 0, $page, $options ) );
353 }
354
355 /**
356 * Count the number of items on a user's watchlist
357 *
358 * @param $talk Include talk pages
359 * @return integer
360 */
361 function wlCountItems( &$user, $talk = true ) {
362 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
363
364 # Fetch the raw count
365 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId ), 'wlCountItems' );
366 $row = $dbr->fetchObject( $res );
367 $count = $row->count;
368 $dbr->freeResult( $res );
369
370 # Halve to remove talk pages if needed
371 if( !$talk )
372 $count = floor( $count / 2 );
373
374 return( $count );
375 }