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