typo
[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 global $wgEnotifWatchlist;
22 $fname = 'wfSpecialWatchlist';
23
24 $wgOut->setPagetitle( wfMsg( 'watchlist' ) );
25 $sub = 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 );
40
41 extract($defaults);
42
43 # Get query variables
44 $days = $wgRequest->getVal( 'days' );
45 $hideOwn = $wgRequest->getBool( 'hideOwn' );
46
47 # Watchlist editing
48 $action = $wgRequest->getVal( 'action' );
49 $remove = $wgRequest->getVal( 'remove' );
50 $id = $wgRequest->getArray( 'id' );
51
52 $uid = $wgUser->getID();
53 if( $wgEnotifWatchlist && $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
54 $wgUser->clearAllNotifications( $uid );
55 }
56
57 # Deleting items from watchlist
58 if(($action == 'submit') && isset($remove) && is_array($id)) {
59 $wgOut->addWikiText( wfMsg( 'removingchecked' ) );
60 $wgOut->addHTML( '<p>' );
61 foreach($id as $one) {
62 $t = Title::newFromURL( $one );
63 if( !is_null( $t ) ) {
64 $wl = WatchedItem::fromUserTitle( $wgUser, $t );
65 if( $wl->removeWatch() === false ) {
66 $wgOut->addHTML( "<br />\n" . wfMsg( 'couldntremove', htmlspecialchars($one) ) );
67 } else {
68 $wgOut->addHTML( ' (' . htmlspecialchars($one) . ')' );
69 }
70 } else {
71 $wgOut->addHTML( "<br />\n" . wfMsg( 'iteminvalidname', htmlspecialchars($one) ) );
72 }
73 }
74 $wgOut->addHTML( "done.</p>\n" );
75 }
76
77 if ( $wgUseWatchlistCache ) {
78 $memckey = "$wgDBname:watchlist:id:" . $wgUser->getId();
79 $cache_s = @$wgMemc->get( $memckey );
80 if( $cache_s ){
81 $wgOut->addWikiText( wfMsg('wlsaved') );
82 $wgOut->addHTML( $cache_s );
83 return;
84 }
85 }
86
87 $dbr =& wfGetDB( DB_SLAVE );
88 extract( $dbr->tableNames( 'page', 'revision', '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', $hideOwn, $defaults, $nondefaults);
121
122 if ( $days <= 0 ) {
123 $docutoff = '';
124 $cutoff = false;
125 $npages = wfMsg( 'watchlistall1' );
126 } else {
127 $docutoff = "AND rev_timestamp > '" .
128 ( $cutoff = $dbr->timestamp( time() - intval( $days * 86400 ) ) )
129 . "'";
130 /*
131 $sql = "SELECT COUNT(*) AS n FROM $page, $revision WHERE rev_timestamp>'$cutoff' AND page_id=rev_page";
132 $res = $dbr->query( $sql, $fname );
133 $s = $dbr->fetchObject( $res );
134 $npages = $s->n;
135 */
136 $npages = 40000 * $days;
137
138 }
139
140 /* Edit watchlist form */
141 if($wgRequest->getBool('edit') || $par == 'edit' ) {
142 $wgOut->addWikiText( wfMsg( 'watchlistcontains', $wgLang->formatNum( $nitems ) ) .
143 "\n\n" . wfMsg( 'watcheditlist' ) );
144
145 $wgOut->addHTML( '<form action=\'' .
146 $specialTitle->escapeLocalUrl( 'action=submit' ) .
147 "' method='post'>\n" );
148
149 # Patch A2
150 # The following was proposed by KTurner 07.11.2004 to T.Gries
151 # $sql = "SELECT distinct (wl_namespace & ~1),wl_title FROM $watchlist WHERE wl_user=$uid";
152 $sql = "SELECT wl_namespace,wl_title FROM $watchlist WHERE wl_user=$uid";
153
154 $res = $dbr->query( $sql, $fname );
155 $sk = $wgUser->getSkin();
156
157 $list = array();
158 while( $s = $dbr->fetchObject( $res ) ) {
159 $list[$s->wl_namespace][] = $s->wl_title;
160 }
161
162 // TODO: Display a TOC
163 foreach($list as $ns => $titles) {
164 if (Namespace::isTalk($ns))
165 continue;
166 if ($ns != NS_MAIN)
167 $wgOut->addHTML( '<h2>' . $wgContLang->getFormattedNsText( $ns ) . '</h2>' );
168 $wgOut->addHTML( '<ul>' );
169 foreach($titles as $title) {
170 $t = Title::makeTitle( $ns, $title );
171 if( is_null( $t ) ) {
172 $wgOut->addHTML(
173 '<!-- bad title "' .
174 htmlspecialchars( $s->wl_title ) . '" in namespace ' . $s->wl_namespace . " -->\n"
175 );
176 } else {
177 $t = $t->getPrefixedText();
178 $wgOut->addHTML(
179 '<li><input type="checkbox" name="id[]" value="' . htmlspecialchars($t) . '" />' .
180 $sk->makeLink( $t, $t ) .
181 "</li>\n"
182 );
183 }
184 }
185 $wgOut->addHTML( '</ul>' );
186 }
187 $wgOut->addHTML(
188 "<input type='submit' name='remove' value=\"" .
189 htmlspecialchars( wfMsg( "removechecked" ) ) . "\" />\n" .
190 "</form>\n"
191 );
192
193 return;
194 }
195
196 # If the watchlist is relatively short, it's simplest to zip
197 # down its entirety and then sort the results.
198
199 # If it's relatively long, it may be worth our while to zip
200 # through the time-sorted page list checking for watched items.
201
202 # Up estimate of watched items by 15% to compensate for talk pages...
203
204 $andHideOwn = $hideOwn ? "AND (rc_user <> $uid)" : '';
205
206 # Show watchlist header
207 $header = '';
208 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
209 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
210 }
211 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
212 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
213 }
214
215 # TODO: Consider removing the third parameter
216 $header .= wfMsg( 'watchdetails', $wgLang->formatNum( $nitems ),
217 $wgLang->formatNum( $npages ), '',
218 $specialTitle->getFullUrl( 'edit=yes' ) );
219 $wgOut->addWikiText( $header );
220
221 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
222 $wgOut->addHTML( '<form action="' .
223 $specialTitle->escapeLocalUrl() .
224 '" method="post"><input type="submit" name="dummy" value="' .
225 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
226 '" /><input type="hidden" name="reset" value="all" /></form>' .
227 "\n\n" );
228 }
229
230 $sql = "SELECT
231 rc_namespace page_namespace,rc_title page_title,
232 rc_comment rev_comment, rc_cur_id page_id,
233 rc_user rev_user,rc_user_text rev_user_text,
234 rc_timestamp rev_timestamp,rc_minor rev_minor_edit,
235 rc_this_oldid rev_id,
236 rc_last_oldid,
237 rc_new page_is_new,wl_notificationtimestamp
238 FROM $watchlist,$recentchanges,$page
239 WHERE wl_user=$uid
240 AND wl_namespace=rc_namespace
241 AND wl_title=rc_title
242 AND rc_timestamp > '$cutoff'
243 AND rc_cur_id=page_id
244 AND rc_this_oldid=page_latest
245 $andHideOwn
246 ORDER BY rc_timestamp DESC";
247
248 $res = $dbr->query( $sql, $fname );
249 $numRows = $dbr->numRows( $res );
250
251 /* Start bottom header */
252 $wgOut->addHTML( "<hr />\n<p>" );
253
254 if($days >= 1)
255 $wgOut->addWikiText( wfMsg( 'rcnote', $wgLang->formatNum( $numRows ),
256 $wgLang->formatNum( $days ) ) . '<br />' , false );
257 elseif($days > 0)
258 $wgOut->addWikiText( wfMsg( 'wlnote', $wgLang->formatNum( $numRows ),
259 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
260
261 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
262
263 $sk = $wgUser->getSkin();
264 $s = $sk->makeKnownLink(
265 $wgContLang->specialPage( 'Watchlist' ),
266 (0 == $hideOwn) ? wfMsgHtml( 'wlhide' ) : wfMsgHtml( 'wlshow' ),
267 wfArrayToCGI( array('hideOwn' => 1-$hideOwn ), $nondefaults ) );
268
269 $wgOut->addHTML( wfMsgHtml( "wlhideshowown", $s ) );
270
271 if ( $numRows == 0 ) {
272 $wgOut->addWikitext( "<br />" . wfMsg( 'watchnochange' ), false );
273 $wgOut->addHTML( "</p>\n" );
274 return;
275 }
276
277 $wgOut->addHTML( "</p>\n" );
278 /* End bottom header */
279
280 $sk = $wgUser->getSkin();
281 $list =& new ChangesList( $sk );
282 $s = $list->beginRecentChangesList();
283 $counter = 1;
284 while ( $obj = $dbr->fetchObject( $res ) ) {
285 # Make fake RC entry
286 $rc = RecentChange::newFromCurRow( $obj, $obj->rc_last_oldid );
287 $rc->counter = $counter++;
288
289 if ( $wgShowUpdatedMarker ) {
290 $updated = $obj->wl_notificationtimestamp;
291 } else {
292 // Same visual appearance as MW 1.4
293 $updated = true;
294 }
295
296 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
297 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" .wfStrencode($obj->page_title). "' AND wl_namespace='{$obj->page_namespace}'" ;
298 $res3 = $dbr->query( $sql3, DB_READ, $fname );
299 $x = $dbr->fetchObject( $res3 );
300 $rc->numberofWatchingusers = $x->n;
301 } else {
302 $rc->numberofWatchingusers = 0;
303 }
304
305 $s .= $list->recentChangesLine( $rc, $updated );
306 }
307 $s .= $list->endRecentChangesList();
308
309 $dbr->freeResult( $res );
310 $wgOut->addHTML( $s );
311
312 if ( $wgUseWatchlistCache ) {
313 $wgMemc->set( $memckey, $s, $wgWLCacheTimeout);
314 }
315
316 }
317
318 function wlHoursLink( $h, $page, $options = array() ) {
319 global $wgUser, $wgLang, $wgContLang;
320 $sk = $wgUser->getSkin();
321 $s = $sk->makeKnownLink(
322 $wgContLang->specialPage( $page ),
323 $wgLang->formatNum( $h ),
324 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
325 return $s;
326 }
327
328 function wlDaysLink( $d, $page, $options = array() ) {
329 global $wgUser, $wgLang, $wgContLang;
330 $sk = $wgUser->getSkin();
331 $s = $sk->makeKnownLink(
332 $wgContLang->specialPage( $page ),
333 ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
334 wfArrayToCGI( array('days' => $d), $options ) );
335 return $s;
336 }
337
338 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
339 $hours = array( 1, 2, 6, 12 );
340 $days = array( 1, 3, 7 );
341 $cl = '';
342 $i = 0;
343 foreach( $hours as $h ) {
344 $hours[$i++] = wlHoursLink( $h, $page, $options );
345 }
346 $i = 0;
347 foreach( $days as $d ) {
348 $days[$i++] = wlDaysLink( $d, $page, $options );
349 }
350 return wfMsg ('wlshowlast',
351 implode(' | ', $hours),
352 implode(' | ', $days),
353 wlDaysLink( 0, $page, $options ) );
354 }
355
356 ?>