e09eeaf7227bce48d8e7a196cf21928f6f87ec9c
[lhc/web/wiklou.git] / includes / SpecialWatchlist.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 *
10 */
11 require_once( 'SpecialRecentchanges.php' );
12 require_once( 'WatchedItem.php' );
13
14 /**
15 * constructor
16 */
17 function wfSpecialWatchlist( $par ) {
18 global $wgUser, $wgOut, $wgLang, $wgMemc, $wgRequest, $wgContLang;
19 global $wgUseWatchlistCache, $wgWLCacheTimeout, $wgDBname;
20 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
21 global $wgEnotifWatchlist, $wgFilterRobotsWL;
22 $fname = 'wfSpecialWatchlist';
23
24 $wgOut->setPagetitle( wfMsg( 'watchlist' ) );
25 $sub = htmlspecialchars( wfMsg( 'watchlistsub', $wgUser->getName() ) );
26 $wgOut->setSubtitle( $sub );
27 $wgOut->setRobotpolicy( 'noindex,nofollow' );
28
29 $specialTitle = Title::makeTitle( NS_SPECIAL, 'Watchlist' );
30
31 if( $wgUser->isAnon() ) {
32 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
33 return;
34 }
35
36 $defaults = array(
37 /* float */ 'days' => 3.0, /* or 0.5, watch further below */
38 /* bool */ 'hideOwn' => false,
39 /* bool */ 'hideBots' => false,
40 'nameSpace' => 'all',
41 );
42
43 extract($defaults);
44
45 # Get query variables
46 $days = $wgRequest->getVal( 'days' );
47 $hideOwn = $wgRequest->getBool( 'hideOwn' );
48 $hideBots = $wgRequest->getBool( 'hideBots' );
49
50 # Get namespace value, if supplied, and prepare a WHERE fragment
51 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
52 if( !is_null( $nameSpace ) ) {
53 $nameSpace = intval( $nameSpace );
54 $nameSpaceClause = " AND rc_namespace = $nameSpace";
55 } else {
56 $nameSpace = '';
57 $nameSpaceClause = '';
58 }
59
60 # Watchlist editing
61 $action = $wgRequest->getVal( 'action' );
62 $remove = $wgRequest->getVal( 'remove' );
63 $id = $wgRequest->getArray( 'id' );
64
65 $uid = $wgUser->getID();
66 if( $wgEnotifWatchlist && $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
67 $wgUser->clearAllNotifications( $uid );
68 }
69
70 # Deleting items from watchlist
71 if(($action == 'submit') && isset($remove) && is_array($id)) {
72 $wgOut->addWikiText( wfMsg( 'removingchecked' ) );
73 $wgOut->addHTML( '<p>' );
74 foreach($id as $one) {
75 $t = Title::newFromURL( $one );
76 if( !is_null( $t ) ) {
77 $wl = WatchedItem::fromUserTitle( $wgUser, $t );
78 if( $wl->removeWatch() === false ) {
79 $wgOut->addHTML( "<br />\n" . wfMsg( 'couldntremove', htmlspecialchars($one) ) );
80 } else {
81 $wgOut->addHTML( ' (' . htmlspecialchars($one) . ')' );
82 }
83 } else {
84 $wgOut->addHTML( "<br />\n" . wfMsg( 'iteminvalidname', htmlspecialchars($one) ) );
85 }
86 }
87 $wgOut->addHTML( "done.</p>\n" );
88 }
89
90 if ( $wgUseWatchlistCache ) {
91 $memckey = "$wgDBname:watchlist:id:" . $wgUser->getId();
92 $cache_s = @$wgMemc->get( $memckey );
93 if( $cache_s ){
94 $wgOut->addWikiText( wfMsg('wlsaved') );
95 $wgOut->addHTML( $cache_s );
96 return;
97 }
98 }
99
100 $dbr =& wfGetDB( DB_SLAVE );
101 extract( $dbr->tableNames( 'page', 'revision', 'watchlist', 'recentchanges' ) );
102
103 $sql = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_user=$uid";
104 $res = $dbr->query( $sql, $fname );
105 $s = $dbr->fetchObject( $res );
106
107 # Patch *** A1 *** (see A2 below)
108 # adjust for page X, talk:page X, which are both stored separately, but treated together
109 $nitems = floor($s->n / 2);
110 # $nitems = $s->n;
111
112 if($nitems == 0) {
113 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
114 return;
115 }
116
117 if( is_null($days) || !is_numeric($days) ) {
118 $big = 1000; /* The magical big */
119 if($nitems > $big) {
120 # Set default cutoff shorter
121 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
122 } else {
123 $days = $defaults['days']; # default cutoff for shortlisters
124 }
125 } else {
126 $days = floatval($days);
127 }
128
129 // Dump everything here
130 $nondefaults = array();
131
132 wfAppendToArrayIfNotDefault( 'days', $days, $defaults, $nondefaults);
133 wfAppendToArrayIfNotDefault( 'hideOwn', $hideOwn, $defaults, $nondefaults);
134 wfAppendToArrayIfNotDefault( 'hideBots', $hideBots, $defaults, $nondefaults);
135 wfAppendToArrayIfNotDefault( 'namespace', $nameSpace, $defaults, $nondefaults );
136
137 if ( $days <= 0 ) {
138 $docutoff = '';
139 $cutoff = false;
140 $npages = wfMsg( 'watchlistall1' );
141 } else {
142 $docutoff = "AND rev_timestamp > '" .
143 ( $cutoff = $dbr->timestamp( time() - intval( $days * 86400 ) ) )
144 . "'";
145 /*
146 $sql = "SELECT COUNT(*) AS n FROM $page, $revision WHERE rev_timestamp>'$cutoff' AND page_id=rev_page";
147 $res = $dbr->query( $sql, $fname );
148 $s = $dbr->fetchObject( $res );
149 $npages = $s->n;
150 */
151 $npages = 40000 * $days;
152
153 }
154
155 /* Edit watchlist form */
156 if($wgRequest->getBool('edit') || $par == 'edit' ) {
157 $wgOut->addWikiText( wfMsg( 'watchlistcontains', $wgLang->formatNum( $nitems ) ) .
158 "\n\n" . wfMsg( 'watcheditlist' ) );
159
160 $wgOut->addHTML( '<form action=\'' .
161 $specialTitle->escapeLocalUrl( 'action=submit' ) .
162 "' method='post'>\n" );
163
164 # Patch A2
165 # The following was proposed by KTurner 07.11.2004 to T.Gries
166 # $sql = "SELECT distinct (wl_namespace & ~1),wl_title FROM $watchlist WHERE wl_user=$uid";
167 $sql = "SELECT wl_namespace, wl_title, page_is_redirect FROM $page LEFT JOIN $watchlist ON wl_namespace = page_namespace AND wl_title = page_title WHERE wl_user=$uid";
168
169 $res = $dbr->query( $sql, $fname );
170
171 # Batch existence check
172 $linkBatch = new LinkBatch();
173 while( $row = $dbr->fetchObject( $res ) )
174 $linkBatch->addObj( Title::makeTitleSafe( $row->wl_namespace, $row->wl_title ) );
175 $linkBatch->execute();
176 if( $dbr->numRows( $res ) > 0 )
177 $dbr->dataSeek( $res, 0 ); # Let's do the time warp again!
178
179 $sk = $wgUser->getSkin();
180
181 $list = array();
182 while( $s = $dbr->fetchObject( $res ) ) {
183 $list[$s->wl_namespace][$s->wl_title] = $s->page_is_redirect;
184 }
185
186 // TODO: Display a TOC
187 foreach($list as $ns => $titles) {
188 if (Namespace::isTalk($ns))
189 continue;
190 if ($ns != NS_MAIN)
191 $wgOut->addHTML( '<h2>' . $wgContLang->getFormattedNsText( $ns ) . '</h2>' );
192 $wgOut->addHTML( '<ul>' );
193 foreach( $titles as $title => $redir ) {
194 $titleObj = Title::makeTitle( $ns, $title );
195 if( is_null( $titleObj ) ) {
196 $wgOut->addHTML(
197 '<!-- bad title "' .
198 htmlspecialchars( $s->wl_title ) . '" in namespace ' . $s->wl_namespace . " -->\n"
199 );
200 } else {
201 global $wgContLang;
202 $titleText = $titleObj->getPrefixedText();
203 $pageLink = $sk->makeLinkObj( $titleObj );
204 $talkLink = $sk->makeLinkObj( $titleObj->getTalkPage(), $wgLang->getNsText( NS_TALK ) );
205 $checkbox = '<input type="checkbox" name="id[]" value="' . htmlspecialchars( $titleObj->getPrefixedText() ) . '" /> ' . ( $wgContLang->isRTL() ? '&rlm;' : '&lrm;' );
206 if( $redir ) {
207 $spanopen = '<span class="watchlistredir">';
208 $spanclosed = '</span>';
209 } else {
210 $spanopen = $spanclosed = '';
211 }
212
213 $wgOut->addHTML( "<li>{$checkbox}{$spanopen}{$pageLink}{$spanclosed} ({$talkLink})</li>\n" );
214 }
215 }
216 $wgOut->addHTML( '</ul>' );
217 }
218 $wgOut->addHTML(
219 "<input type='submit' name='remove' value=\"" .
220 htmlspecialchars( wfMsg( "removechecked" ) ) . "\" />\n" .
221 "</form>\n"
222 );
223
224 return;
225 }
226
227 # If the watchlist is relatively short, it's simplest to zip
228 # down its entirety and then sort the results.
229
230 # If it's relatively long, it may be worth our while to zip
231 # through the time-sorted page list checking for watched items.
232
233 # Up estimate of watched items by 15% to compensate for talk pages...
234
235 $andHideOwn = $hideOwn ? "AND (rc_user <> $uid)" : '';
236 if( $wgFilterRobotsWL ) {
237 $andHideBotsOptional = $hideBots ? "AND (rc_bot = 0)" : '';
238 } else {
239 $andHideBotsOptional = "AND rc_this_oldid=page_latest";
240 }
241
242
243 # Show watchlist header
244 $header = '';
245 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
246 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
247 }
248 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
249 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
250 }
251
252 # TODO: Consider removing the third parameter
253 $header .= wfMsg( 'watchdetails', $wgLang->formatNum( $nitems ),
254 $wgLang->formatNum( $npages ), '',
255 $specialTitle->getFullUrl( 'edit=yes' ) );
256 $wgOut->addWikiText( $header );
257
258 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
259 $wgOut->addHTML( '<form action="' .
260 $specialTitle->escapeLocalUrl() .
261 '" method="post"><input type="submit" name="dummy" value="' .
262 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
263 '" /><input type="hidden" name="reset" value="all" /></form>' .
264 "\n\n" );
265 }
266
267 $sql = "SELECT
268 rc_namespace page_namespace,rc_title page_title,
269 rc_comment rev_comment, rc_cur_id page_id,
270 rc_user rev_user,rc_user_text rev_user_text,
271 rc_timestamp rev_timestamp,rc_minor rev_minor_edit,
272 rc_this_oldid rev_id,
273 rc_last_oldid,
274 rc_new page_is_new,wl_notificationtimestamp
275 FROM $watchlist,$recentchanges,$page
276 WHERE wl_user=$uid
277 AND wl_namespace=rc_namespace
278 AND wl_title=rc_title
279 AND rc_timestamp > '$cutoff'
280 AND rc_cur_id=page_id
281 $andHideOwn
282 $andHideBotsOptional
283 $nameSpaceClause
284 ORDER BY rc_timestamp DESC";
285
286 $res = $dbr->query( $sql, $fname );
287 $numRows = $dbr->numRows( $res );
288
289 /* Start bottom header */
290 $wgOut->addHTML( "<hr />\n<p>" );
291
292 if($days >= 1)
293 $wgOut->addWikiText( wfMsg( 'rcnote', $wgLang->formatNum( $numRows ),
294 $wgLang->formatNum( $days ) ) . '<br />' , false );
295 elseif($days > 0)
296 $wgOut->addWikiText( wfMsg( 'wlnote', $wgLang->formatNum( $numRows ),
297 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
298
299 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
300
301 $sk = $wgUser->getSkin();
302 $s = $sk->makeKnownLink(
303 $wgContLang->specialPage( 'Watchlist' ),
304 (0 == $hideOwn) ? wfMsgHtml( 'wlhide' ) : wfMsgHtml( 'wlshow' ),
305 wfArrayToCGI( array('hideOwn' => 1-$hideOwn ), $nondefaults ) );
306 $wgOut->addHTML( wfMsgHtml( "wlhideshowown", $s ) );
307
308 if( $wgFilterRobotsWL ) {
309 $s = $sk->makeKnownLink(
310 $wgContLang->specialPage( 'Watchlist' ),
311 (0 == $hideBots) ? wfMsgHtml( 'wlhide' ) : wfMsgHtml( 'wlshow' ),
312 wfArrayToCGI( array('hideBots' => 1-$hideBots ), $nondefaults ) );
313 $wgOut->addHTML( wfMsgHtml( "wlhideshowbots", " $s" ) );
314 }
315
316 # Form for namespace filtering
317 $thisTitle = Title::makeTitle( NS_SPECIAL, 'Watchlist' );
318 $thisAction = $thisTitle->escapeLocalUrl();
319 $nsForm = "<form method=\"post\" action=\"{$thisAction}\">\n";
320 $nsForm .= "<label for=\"namespace\">" . wfMsg( 'namespace' ) . "</label> ";
321 $nsForm .= HTMLnamespaceselector( $nameSpace, '' ) . "\n";
322 $nsForm .= ( $hideOwn ? "<input type=\"hidden\" name=\"hideown\" value=\"1\" />\n" : "" );
323 $nsForm .= ( $hideBots ? "<input type=\"hidden\" name=\"hidebots\" value=\"1\" />\n" : "" );
324 $nsForm .= "<input type=\"hidden\" name=\"days\" value=\"" . $days . "\" />\n";
325 $nsForm .= "<input type=\"submit\" name=\"submit\" value=\"" . wfMsgHtml( 'allpagessubmit' ) . "\" />\n";
326 $nsForm .= "</form>\n";
327 $wgOut->addHTML( $nsForm );
328
329 if ( $numRows == 0 ) {
330 $wgOut->addWikitext( "<br />" . wfMsg( 'watchnochange' ), false );
331 $wgOut->addHTML( "</p>\n" );
332 return;
333 }
334
335 $wgOut->addHTML( "</p>\n" );
336 /* End bottom header */
337
338 $list = ChangesList::newFromUser( $wgUser );
339
340 $s = $list->beginRecentChangesList();
341 $counter = 1;
342 while ( $obj = $dbr->fetchObject( $res ) ) {
343 # Make fake RC entry
344 $rc = RecentChange::newFromCurRow( $obj, $obj->rc_last_oldid );
345 $rc->counter = $counter++;
346
347 if ( $wgShowUpdatedMarker ) {
348 $updated = $obj->wl_notificationtimestamp;
349 } else {
350 // Same visual appearance as MW 1.4
351 $updated = true;
352 }
353
354 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
355 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" .wfStrencode($obj->page_title). "' AND wl_namespace='{$obj->page_namespace}'" ;
356 $res3 = $dbr->query( $sql3, DB_READ, $fname );
357 $x = $dbr->fetchObject( $res3 );
358 $rc->numberofWatchingusers = $x->n;
359 } else {
360 $rc->numberofWatchingusers = 0;
361 }
362
363 $s .= $list->recentChangesLine( $rc, $updated );
364 }
365 $s .= $list->endRecentChangesList();
366
367 $dbr->freeResult( $res );
368 $wgOut->addHTML( $s );
369
370 if ( $wgUseWatchlistCache ) {
371 $wgMemc->set( $memckey, $s, $wgWLCacheTimeout);
372 }
373
374 }
375
376 function wlHoursLink( $h, $page, $options = array() ) {
377 global $wgUser, $wgLang, $wgContLang;
378 $sk = $wgUser->getSkin();
379 $s = $sk->makeKnownLink(
380 $wgContLang->specialPage( $page ),
381 $wgLang->formatNum( $h ),
382 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
383 return $s;
384 }
385
386 function wlDaysLink( $d, $page, $options = array() ) {
387 global $wgUser, $wgLang, $wgContLang;
388 $sk = $wgUser->getSkin();
389 $s = $sk->makeKnownLink(
390 $wgContLang->specialPage( $page ),
391 ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
392 wfArrayToCGI( array('days' => $d), $options ) );
393 return $s;
394 }
395
396 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
397 $hours = array( 1, 2, 6, 12 );
398 $days = array( 1, 3, 7 );
399 $cl = '';
400 $i = 0;
401 foreach( $hours as $h ) {
402 $hours[$i++] = wlHoursLink( $h, $page, $options );
403 }
404 $i = 0;
405 foreach( $days as $d ) {
406 $days[$i++] = wlDaysLink( $d, $page, $options );
407 }
408 return wfMsg ('wlshowlast',
409 implode(' | ', $hours),
410 implode(' | ', $days),
411 wlDaysLink( 0, $page, $options ) );
412 }
413
414 ?>