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