* Fixed a bug where the watchlist count without talk pages would be off by a
[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, $wgTitle, $wgMemc, $wgRequest, $wgContLang;;
19 global $wgUseWatchlistCache, $wgWLCacheTimeout, $wgDBname;
20 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
21 $fname = 'wfSpecialWatchlist';
22
23 $wgOut->setPagetitle( wfMsg( 'watchlist' ) );
24 $sub = wfMsg( 'watchlistsub', $wgUser->getName() );
25 $wgOut->setSubtitle( $sub );
26 $wgOut->setRobotpolicy( 'noindex,nofollow' );
27
28 $specialTitle = Title::makeTitle( NS_SPECIAL, 'Watchlist' );
29
30 if( $wgUser->isAnon() ) {
31 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
32 return;
33 }
34
35 # Get query variables
36 $days = $wgRequest->getVal( 'days' );
37 $action = $wgRequest->getVal( 'action' );
38 $remove = $wgRequest->getVal( 'remove' );
39 $hideOwn = $wgRequest->getBool( 'hideOwn' );
40 $id = $wgRequest->getArray( 'id' );
41
42 $uid = $wgUser->getID();
43 if( $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
44 $wgUser->clearAllNotifications( $uid );
45 }
46
47
48 if(($action == 'submit') && isset($remove) && is_array($id)) {
49 $wgOut->addWikiText( wfMsg( 'removingchecked' ) );
50 $wgOut->addHTML( '<p>' );
51 foreach($id as $one) {
52 $t = Title::newFromURL( $one );
53 if( !is_null( $t ) ) {
54 $wl = WatchedItem::fromUserTitle( $wgUser, $t );
55 if( $wl->removeWatch() === false ) {
56 $wgOut->addHTML( "<br />\n" . wfMsg( 'couldntremove', htmlspecialchars($one) ) );
57 } else {
58 $wgOut->addHTML( ' (' . htmlspecialchars($one) . ')' );
59 }
60 } else {
61 $wgOut->addHTML( "<br />\n" . wfMsg( 'iteminvalidname', htmlspecialchars($one) ) );
62 }
63 }
64 $wgOut->addHTML( "done.</p>\n" );
65 }
66
67 if ( $wgUseWatchlistCache ) {
68 $memckey = "$wgDBname:watchlist:id:" . $wgUser->getId();
69 $cache_s = @$wgMemc->get( $memckey );
70 if( $cache_s ){
71 $wgOut->addWikiText( wfMsg('wlsaved') );
72 $wgOut->addHTML( $cache_s );
73 return;
74 }
75 }
76
77 $dbr =& wfGetDB( DB_SLAVE );
78 extract( $dbr->tableNames( 'page', 'revision', 'watchlist', 'recentchanges' ) );
79
80 $sql = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_user=$uid";
81 $res = $dbr->query( $sql, $fname );
82 $s = $dbr->fetchObject( $res );
83
84 # Patch *** A1 *** (see A2 below)
85 # adjust for page X, talk:page X, which are both stored separately, but treated together
86 # $nitems = $s->n / 2;
87 $nitems = $s->n;
88
89 if($nitems == 0) {
90 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
91 return;
92 }
93
94 if ( is_null( $days ) ) {
95 $big = 1000;
96 if($nitems > $big) {
97 # Set default cutoff shorter
98 $days = (12.0 / 24.0); # 12 hours...
99 } else {
100 $days = 3; # longer cutoff for shortlisters
101 }
102 } else {
103 $days = floatval($days);
104 }
105
106 if ( $days <= 0 ) {
107 $docutoff = '';
108 $cutoff = false;
109 $npages = wfMsg( 'watchlistall1' );
110 } else {
111 $docutoff = "AND rev_timestamp > '" .
112 ( $cutoff = $dbr->timestamp( time() - intval( $days * 86400 ) ) )
113 . "'";
114 $sql = "SELECT COUNT(*) AS n FROM $page, $revision WHERE rev_timestamp>'$cutoff' AND page_id=rev_page";
115 $res = $dbr->query( $sql, $fname );
116 $s = $dbr->fetchObject( $res );
117 $npages = $s->n;
118
119 }
120
121 if($wgRequest->getBool('edit') || $par == 'edit' ) {
122 $wgOut->addWikiText( wfMsg( 'watchlistcontains', $wgLang->formatNum( $nitems ) ) .
123 "\n\n" . wfMsg( 'watcheditlist' ) );
124
125 $wgOut->addHTML( '<form action=\'' .
126 $specialTitle->escapeLocalUrl( 'action=submit' ) .
127 "' method='post'>\n" );
128
129 # Patch A2
130 # The following was proposed by KTurner 07.11.2004 to T.Gries
131 # $sql = "SELECT distinct (wl_namespace & ~1),wl_title FROM $watchlist WHERE wl_user=$uid";
132 $sql = "SELECT wl_namespace,wl_title FROM $watchlist WHERE wl_user=$uid";
133
134 $res = $dbr->query( $sql, $fname );
135 $sk = $wgUser->getSkin();
136
137 $list = array();
138 while( $s = $dbr->fetchObject( $res ) ) {
139 $list[$s->wl_namespace][] = $s->wl_title;
140 }
141
142 // TODO: Display a TOC
143 foreach($list as $ns => $titles) {
144 if (Namespace::isTalk($ns))
145 continue;
146 if ($ns != NS_MAIN)
147 $wgOut->addHTML( '<h2>' . $wgContLang->getFormattedNsText( $ns ) . '</h2>' );
148 $wgOut->addHTML( '<ul>' );
149 foreach($titles as $title) {
150 $t = Title::makeTitle( $ns, $title );
151 if( is_null( $t ) ) {
152 $wgOut->addHTML(
153 '<!-- bad title "' .
154 htmlspecialchars( $s->wl_title ) . '" in namespace ' . $s->wl_namespace . " -->\n"
155 );
156 } else {
157 $t = $t->getPrefixedText();
158 $wgOut->addHTML(
159 '<li><input type="checkbox" name="id[]" value="' . htmlspecialchars($t) . '" />' .
160 $sk->makeLink( $t, $t ) .
161 "</li>\n"
162 );
163 }
164 }
165 $wgOut->addHTML( '</ul>' );
166 }
167 $wgOut->addHTML(
168 "<input type='submit' name='remove' value=\"" .
169 htmlspecialchars( wfMsg( "removechecked" ) ) . "\" />\n" .
170 "</form>\n"
171 );
172
173 return;
174 }
175
176 # If the watchlist is relatively short, it's simplest to zip
177 # down its entirety and then sort the results.
178
179 # If it's relatively long, it may be worth our while to zip
180 # through the time-sorted page list checking for watched items.
181
182 # Up estimate of watched items by 15% to compensate for talk pages...
183 if( $cutoff && ( $nitems*1.15 > $npages ) ) {
184 $x = 'rev_timestamp';
185 $y = wfMsg( 'watchmethod-recent' );
186 # TG patch: here we do not consider pages and their talk pages equivalent - why should we ?
187 # The change results in talk-pages not automatically included in watchlists, when their parent page is included
188 # $z = "wl_namespace=cur_namespace & ~1";
189 $z = 'wl_namespace=page_namespace';
190 } else {
191 $x = 'page_timestamp';
192 $y = wfMsg( 'watchmethod-list' );
193 # TG patch: here we do not consider pages and their talk pages equivalent - why should we ?
194 # The change results in talk-pages not automatically included in watchlists, when their parent page is included
195 # $z = "(wl_namespace=cur_namespace OR wl_namespace+1=cur_namespace)";
196 $z = 'wl_namespace=page_namespace';
197 }
198
199 $andHideOwn = $hideOwn ? "AND (rev_user <> $uid)" : '';
200
201 # Show watchlist header
202 $header = '';
203 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
204 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
205 }
206 if ( $wgShowUpdatedMarker ) {
207 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
208 }
209
210 $header .= wfMsg( 'watchdetails', $wgLang->formatNum( $nitems / 2 ),
211 $wgLang->formatNum( $npages ), $y,
212 $specialTitle->getFullUrl( 'edit=yes' ) );
213 $wgOut->addWikiText( $header );
214
215 if ( $wgShowUpdatedMarker ) {
216 $wgOut->addHTML( '<form action="' .
217 $specialTitle->escapeLocalUrl() .
218 '" method="post"><input type="submit" name="dummy" value="' .
219 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
220 '" /><input type="hidden" name="reset" value="all" /></form>' .
221 "\n\n" );
222 }
223
224 $use_index = $dbr->useIndexClause( $x );
225 $sql = "SELECT
226 page_namespace,page_title,rev_comment, page_id,
227 rev_user,rev_user_text,rev_timestamp,rev_minor_edit,page_is_new,wl_notificationtimestamp
228 FROM $watchlist,$page,$revision $use_index
229 WHERE wl_user=$uid
230 $andHideOwn
231 AND $z
232 AND wl_title=page_title
233 AND page_latest=rev_id
234 $docutoff
235 ORDER BY rev_timestamp DESC";
236
237
238 $res = $dbr->query( $sql, $fname );
239 $numRows = $dbr->numRows( $res );
240 if($days >= 1)
241 $note = wfMsg( 'rcnote', $wgLang->formatNum( $numRows ), $wgLang->formatNum( $days ) );
242 elseif($days > 0)
243 $note = wfMsg( 'wlnote', $wgLang->formatNum( $numRows ), $wgLang->formatNum( round($days*24) ) );
244 else
245 $note = '';
246 $wgOut->addHTML( "\n<hr />\n{$note}\n<br />" );
247 $note = wlCutoffLinks( $days );
248 $wgOut->addHTML( "{$note}\n" );
249
250 $sk = $wgUser->getSkin();
251 $s = $sk->makeKnownLink(
252 $wgContLang->specialPage( 'Watchlist' ),
253 (0 == $hideOwn) ? wfMsg( 'wlhide' ) : wfMsg( 'wlshow' ),
254 'hideOwn=' . $wgLang->formatNum( 1-$hideOwn ) );
255
256 $note = wfMsg( "wlhideshowown", $s );
257 $wgOut->addHTML( "\n<br />{$note}\n<br />" );
258
259 if ( $numRows == 0 ) {
260 $wgOut->addHTML( '<p><i>' . wfMsg( 'watchnochange' ) . '</i></p>' );
261 return;
262 }
263
264 $sk = $wgUser->getSkin();
265 $list =& new ChangesList( $sk );
266 $s = $list->beginRecentChangesList();
267 $counter = 1;
268 while ( $obj = $dbr->fetchObject( $res ) ) {
269 # Make fake RC entry
270 $rc = RecentChange::newFromCurRow( $obj );
271 $rc->counter = $counter++;
272
273 if ( $wgShowUpdatedMarker ) {
274 $updated = $obj->wl_notificationtimestamp;
275 } else {
276 // Same visual appearance as MW 1.4
277 $updated = true;
278 }
279
280 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
281 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" .wfStrencode($obj->page_title). "' AND wl_namespace='{$obj->page_namespace}'" ;
282 $res3 = $dbr->query( $sql3, DB_READ, $fname );
283 $x = $dbr->fetchObject( $res3 );
284 $rc->numberofWatchingusers = $x->n;
285 } else {
286 $rc->numberofWatchingusers = 0;
287 }
288
289 $s .= $list->recentChangesLine( $rc, $updated );
290 }
291 $s .= $list->endRecentChangesList();
292
293 $dbr->freeResult( $res );
294 $wgOut->addHTML( $s );
295
296 if ( $wgUseWatchlistCache ) {
297 $wgMemc->set( $memckey, $s, $wgWLCacheTimeout);
298 }
299
300 }
301
302
303 function wlHoursLink( $h, $page ) {
304 global $wgUser, $wgLang, $wgContLang;
305 $sk = $wgUser->getSkin();
306 $s = $sk->makeKnownLink(
307 $wgContLang->specialPage( $page ),
308 $wgLang->formatNum( $h ),
309 'days=' . ($h / 24.0) );
310 return $s;
311 }
312
313
314 function wlDaysLink( $d, $page ) {
315 global $wgUser, $wgLang, $wgContLang;
316 $sk = $wgUser->getSkin();
317 $s = $sk->makeKnownLink(
318 $wgContLang->specialPage( $page ),
319 ($d ? $wgLang->formatNum( $d ) : wfMsg( 'watchlistall2' ) ), "days=$d" );
320 return $s;
321 }
322
323 function wlCutoffLinks( $days, $page = 'Watchlist' )
324 {
325 $hours = array( 1, 2, 6, 12 );
326 $days = array( 1, 3, 7 );
327 $cl = '';
328 $i = 0;
329 foreach( $hours as $h ) {
330 $hours[$i++] = wlHoursLink( $h, $page );
331 }
332 $i = 0;
333 foreach( $days as $d ) {
334 $days[$i++] = wlDaysLink( $d, $page );
335 }
336 return wfMsg ('wlshowlast',
337 implode(' | ', $hours),
338 implode(' | ', $days),
339 wlDaysLink( 0, $page ) );
340 }
341
342 ?>