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