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