ec36663e74f7024f9d6b739137abcb79f5ae6e07
[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, $wgContLang;
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 }
51
52 $defaults = array(
53 /* float */ 'days' => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
54 /* bool */ 'hideOwn' => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
55 /* bool */ 'hideBots' => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
56 /* bool */ 'hideMinor' => (int)$wgUser->getBoolOption( 'watchlisthideminor' ),
57 /* ? */ 'namespace' => 'all',
58 );
59
60 extract($defaults);
61
62 # Extract variables from the request, falling back to user preferences or
63 # other default values if these don't exist
64 $prefs['days' ] = floatval( $wgUser->getOption( 'watchlistdays' ) );
65 $prefs['hideown' ] = $wgUser->getBoolOption( 'watchlisthideown' );
66 $prefs['hidebots'] = $wgUser->getBoolOption( 'watchlisthidebots' );
67 $prefs['hideminor'] = $wgUser->getBoolOption( 'watchlisthideminor' );
68
69 # Get query variables
70 $days = $wgRequest->getVal( 'days', $prefs['days'] );
71 $hideOwn = $wgRequest->getBool( 'hideOwn', $prefs['hideown'] );
72 $hideBots = $wgRequest->getBool( 'hideBots', $prefs['hidebots'] );
73 $hideMinor = $wgRequest->getBool( 'hideMinor', $prefs['hideminor'] );
74
75 # Get namespace value, if supplied, and prepare a WHERE fragment
76 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
77 if( !is_null( $nameSpace ) ) {
78 $nameSpace = intval( $nameSpace );
79 $nameSpaceClause = " AND rc_namespace = $nameSpace";
80 } else {
81 $nameSpace = '';
82 $nameSpaceClause = '';
83 }
84
85 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
86 list( $page, $watchlist, $recentchanges ) = $dbr->tableNamesN( 'page', 'watchlist', 'recentchanges' );
87
88 $sql = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_user=$uid";
89 $res = $dbr->query( $sql, $fname );
90 $s = $dbr->fetchObject( $res );
91
92 # Patch *** A1 *** (see A2 below)
93 # adjust for page X, talk:page X, which are both stored separately, but treated together
94 $nitems = floor($s->n / 2);
95 # $nitems = $s->n;
96
97 if($nitems == 0) {
98 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
99 return;
100 }
101
102 if( is_null($days) || !is_numeric($days) ) {
103 $big = 1000; /* The magical big */
104 if($nitems > $big) {
105 # Set default cutoff shorter
106 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
107 } else {
108 $days = $defaults['days']; # default cutoff for shortlisters
109 }
110 } else {
111 $days = floatval($days);
112 }
113
114 // Dump everything here
115 $nondefaults = array();
116
117 wfAppendToArrayIfNotDefault('days' , $days , $defaults, $nondefaults);
118 wfAppendToArrayIfNotDefault('hideOwn' , (int)$hideOwn , $defaults, $nondefaults);
119 wfAppendToArrayIfNotDefault('hideBots' , (int)$hideBots, $defaults, $nondefaults);
120 wfAppendToArrayIfNotDefault( 'hideMinor', (int)$hideMinor, $defaults, $nondefaults );
121 wfAppendToArrayIfNotDefault('namespace', $nameSpace , $defaults, $nondefaults);
122
123 if ( $days <= 0 ) {
124 $andcutoff = '';
125 $npages = wfMsg( 'watchlistall1' );
126 } else {
127 $andcutoff = "AND rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
128 /*
129 $sql = "SELECT COUNT(*) AS n FROM $page, $revision WHERE rev_timestamp>'$cutoff' AND page_id=rev_page";
130 $res = $dbr->query( $sql, $fname );
131 $s = $dbr->fetchObject( $res );
132 $npages = $s->n;
133 */
134 $npages = 40000 * $days;
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 # TODO: Consider removing the third parameter
169 $header .= wfMsgExt( 'watchdetails', array( 'parsemag' ), $wgLang->formatNum( $nitems ),
170 $wgLang->formatNum( $npages ), '',
171 $specialTitle->getFullUrl( 'edit=yes' ) );
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 ( $wgEnotifWatchlist && $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
187 $sql = "SELECT *
188 FROM $watchlist,$recentchanges,$page
189 WHERE wl_user=$uid
190 AND wl_namespace=rc_namespace
191 AND wl_title=rc_title
192 AND rc_cur_id=page_id
193 $andcutoff
194 $andLatest
195 $andHideOwn
196 $andHideBots
197 $andHideMinor
198 $nameSpaceClause
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->addWikiText( wfMsgNoTrans( '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 // Same visual appearance as MW 1.4
288 $updated = true;
289 }
290
291 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
292 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" .$dbr->strencode($obj->page_title). "' AND wl_namespace='{$obj->page_namespace}'" ;
293 $res3 = $dbr->query( $sql3, $fname );
294 $x = $dbr->fetchObject( $res3 );
295 $rc->numberofWatchingusers = $x->n;
296 } else {
297 $rc->numberofWatchingusers = 0;
298 }
299
300 $s .= $list->recentChangesLine( $rc, $updated );
301 }
302 $s .= $list->endRecentChangesList();
303
304 $dbr->freeResult( $res );
305 $wgOut->addHTML( $s );
306
307 }
308
309 function wlHoursLink( $h, $page, $options = array() ) {
310 global $wgUser, $wgLang, $wgContLang;
311 $sk = $wgUser->getSkin();
312 $s = $sk->makeKnownLink(
313 $wgContLang->specialPage( $page ),
314 $wgLang->formatNum( $h ),
315 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
316 return $s;
317 }
318
319 function wlDaysLink( $d, $page, $options = array() ) {
320 global $wgUser, $wgLang, $wgContLang;
321 $sk = $wgUser->getSkin();
322 $s = $sk->makeKnownLink(
323 $wgContLang->specialPage( $page ),
324 ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
325 wfArrayToCGI( array('days' => $d), $options ) );
326 return $s;
327 }
328
329 /**
330 * Returns html
331 */
332 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
333 $hours = array( 1, 2, 6, 12 );
334 $days = array( 1, 3, 7 );
335 $i = 0;
336 foreach( $hours as $h ) {
337 $hours[$i++] = wlHoursLink( $h, $page, $options );
338 }
339 $i = 0;
340 foreach( $days as $d ) {
341 $days[$i++] = wlDaysLink( $d, $page, $options );
342 }
343 return wfMsgExt('wlshowlast',
344 array('parseinline', 'replaceafter'),
345 implode(' | ', $hours),
346 implode(' | ', $days),
347 wlDaysLink( 0, $page, $options ) );
348 }
349
350 /**
351 * Count the number of items on a user's watchlist
352 *
353 * @param $talk Include talk pages
354 * @return integer
355 */
356 function wlCountItems( &$user, $talk = true ) {
357 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
358
359 # Fetch the raw count
360 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId ), 'wlCountItems' );
361 $row = $dbr->fetchObject( $res );
362 $count = $row->count;
363 $dbr->freeResult( $res );
364
365 # Halve to remove talk pages if needed
366 if( !$talk )
367 $count = floor( $count / 2 );
368
369 return( $count );
370 }