Prevent registration/login with the username "MediaWiki default"
[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 );
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
159 # Batch existence check
160 $linkBatch = new LinkBatch();
161 while( $row = $dbr->fetchObject( $res ) )
162 $linkBatch->addObj( Title::makeTitleSafe( $row->wl_namespace, $row->wl_title ) );
163 $linkBatch->execute();
164 if( $dbr->numRows( $res ) > 0 )
165 $dbr->dataSeek( $res, 0 ); # Let's do the time warp again!
166
167 $sk = $wgUser->getSkin();
168
169 $list = array();
170 while( $s = $dbr->fetchObject( $res ) ) {
171 $list[$s->wl_namespace][] = $s->wl_title;
172 }
173
174 // TODO: Display a TOC
175 foreach($list as $ns => $titles) {
176 if (Namespace::isTalk($ns))
177 continue;
178 if ($ns != NS_MAIN)
179 $wgOut->addHTML( '<h2>' . $wgContLang->getFormattedNsText( $ns ) . '</h2>' );
180 $wgOut->addHTML( '<ul>' );
181 foreach($titles as $title) {
182 $titleObj = Title::makeTitle( $ns, $title );
183 if( is_null( $titleObj ) ) {
184 $wgOut->addHTML(
185 '<!-- bad title "' .
186 htmlspecialchars( $s->wl_title ) . '" in namespace ' . $s->wl_namespace . " -->\n"
187 );
188 } else {
189 global $wgContLang;
190 $titleText = $titleObj->getPrefixedText();
191 $talkLink = $sk->makeLinkObj( $titleObj->getTalkPage(), $wgLang->getNsText( NS_TALK ) );
192 $wgOut->addHTML(
193 '<li><input type="checkbox" name="id[]" value="' . htmlspecialchars( $titleObj->getPrefixedText() ) . '" />' . ' ' . ( $wgContLang->isRTL() ? '&rlm;' : '&lrm;' ) .
194 $sk->makeLinkObj( $titleObj ) . " ({$talkLink}) </li>\n"
195 );
196 }
197 }
198 $wgOut->addHTML( '</ul>' );
199 }
200 $wgOut->addHTML(
201 "<input type='submit' name='remove' value=\"" .
202 htmlspecialchars( wfMsg( "removechecked" ) ) . "\" />\n" .
203 "</form>\n"
204 );
205
206 return;
207 }
208
209 # If the watchlist is relatively short, it's simplest to zip
210 # down its entirety and then sort the results.
211
212 # If it's relatively long, it may be worth our while to zip
213 # through the time-sorted page list checking for watched items.
214
215 # Up estimate of watched items by 15% to compensate for talk pages...
216
217 $andHideOwn = $hideOwn ? "AND (rc_user <> $uid)" : '';
218 if( $wgFilterRobotsWL ) {
219 $andHideBotsOptional = $hideBots ? "AND (rc_bot = 0)" : '';
220 } else {
221 $andHideBotsOptional = "AND rc_this_oldid=page_latest";
222 }
223
224
225 # Show watchlist header
226 $header = '';
227 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
228 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
229 }
230 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
231 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
232 }
233
234 # TODO: Consider removing the third parameter
235 $header .= wfMsg( 'watchdetails', $wgLang->formatNum( $nitems ),
236 $wgLang->formatNum( $npages ), '',
237 $specialTitle->getFullUrl( 'edit=yes' ) );
238 $wgOut->addWikiText( $header );
239
240 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
241 $wgOut->addHTML( '<form action="' .
242 $specialTitle->escapeLocalUrl() .
243 '" method="post"><input type="submit" name="dummy" value="' .
244 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
245 '" /><input type="hidden" name="reset" value="all" /></form>' .
246 "\n\n" );
247 }
248
249 $sql = "SELECT
250 rc_namespace page_namespace,rc_title page_title,
251 rc_comment rev_comment, rc_cur_id page_id,
252 rc_user rev_user,rc_user_text rev_user_text,
253 rc_timestamp rev_timestamp,rc_minor rev_minor_edit,
254 rc_this_oldid rev_id,
255 rc_last_oldid,
256 rc_new page_is_new,wl_notificationtimestamp
257 FROM $watchlist,$recentchanges,$page
258 WHERE wl_user=$uid
259 AND wl_namespace=rc_namespace
260 AND wl_title=rc_title
261 AND rc_timestamp > '$cutoff'
262 AND rc_cur_id=page_id
263 $andHideOwn
264 $andHideBotsOptional
265 ORDER BY rc_timestamp DESC";
266
267 $res = $dbr->query( $sql, $fname );
268 $numRows = $dbr->numRows( $res );
269
270 /* Start bottom header */
271 $wgOut->addHTML( "<hr />\n<p>" );
272
273 if($days >= 1)
274 $wgOut->addWikiText( wfMsg( 'rcnote', $wgLang->formatNum( $numRows ),
275 $wgLang->formatNum( $days ) ) . '<br />' , false );
276 elseif($days > 0)
277 $wgOut->addWikiText( wfMsg( 'wlnote', $wgLang->formatNum( $numRows ),
278 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
279
280 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
281
282 $sk = $wgUser->getSkin();
283 $s = $sk->makeKnownLink(
284 $wgContLang->specialPage( 'Watchlist' ),
285 (0 == $hideOwn) ? wfMsgHtml( 'wlhide' ) : wfMsgHtml( 'wlshow' ),
286 wfArrayToCGI( array('hideOwn' => 1-$hideOwn ), $nondefaults ) );
287 $wgOut->addHTML( wfMsgHtml( "wlhideshowown", $s ) );
288
289 if( $wgFilterRobotsWL ) {
290 $s = $sk->makeKnownLink(
291 $wgContLang->specialPage( 'Watchlist' ),
292 (0 == $hideBots) ? wfMsgHtml( 'wlhide' ) : wfMsgHtml( 'wlshow' ),
293 wfArrayToCGI( array('hideBots' => 1-$hideBots ), $nondefaults ) );
294 $wgOut->addHTML( wfMsgHtml( "wlhideshowbots", " $s" ) );
295 }
296
297 if ( $numRows == 0 ) {
298 $wgOut->addWikitext( "<br />" . wfMsg( 'watchnochange' ), false );
299 $wgOut->addHTML( "</p>\n" );
300 return;
301 }
302
303 $wgOut->addHTML( "</p>\n" );
304 /* End bottom header */
305
306 $list = ChangesList::newFromUser( $wgUser );
307
308 $s = $list->beginRecentChangesList();
309 $counter = 1;
310 while ( $obj = $dbr->fetchObject( $res ) ) {
311 # Make fake RC entry
312 $rc = RecentChange::newFromCurRow( $obj, $obj->rc_last_oldid );
313 $rc->counter = $counter++;
314
315 if ( $wgShowUpdatedMarker ) {
316 $updated = $obj->wl_notificationtimestamp;
317 } else {
318 // Same visual appearance as MW 1.4
319 $updated = true;
320 }
321
322 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
323 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" .wfStrencode($obj->page_title). "' AND wl_namespace='{$obj->page_namespace}'" ;
324 $res3 = $dbr->query( $sql3, DB_READ, $fname );
325 $x = $dbr->fetchObject( $res3 );
326 $rc->numberofWatchingusers = $x->n;
327 } else {
328 $rc->numberofWatchingusers = 0;
329 }
330
331 $s .= $list->recentChangesLine( $rc, $updated );
332 }
333 $s .= $list->endRecentChangesList();
334
335 $dbr->freeResult( $res );
336 $wgOut->addHTML( $s );
337
338 if ( $wgUseWatchlistCache ) {
339 $wgMemc->set( $memckey, $s, $wgWLCacheTimeout);
340 }
341
342 }
343
344 function wlHoursLink( $h, $page, $options = array() ) {
345 global $wgUser, $wgLang, $wgContLang;
346 $sk = $wgUser->getSkin();
347 $s = $sk->makeKnownLink(
348 $wgContLang->specialPage( $page ),
349 $wgLang->formatNum( $h ),
350 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
351 return $s;
352 }
353
354 function wlDaysLink( $d, $page, $options = array() ) {
355 global $wgUser, $wgLang, $wgContLang;
356 $sk = $wgUser->getSkin();
357 $s = $sk->makeKnownLink(
358 $wgContLang->specialPage( $page ),
359 ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
360 wfArrayToCGI( array('days' => $d), $options ) );
361 return $s;
362 }
363
364 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
365 $hours = array( 1, 2, 6, 12 );
366 $days = array( 1, 3, 7 );
367 $cl = '';
368 $i = 0;
369 foreach( $hours as $h ) {
370 $hours[$i++] = wlHoursLink( $h, $page, $options );
371 }
372 $i = 0;
373 foreach( $days as $d ) {
374 $days[$i++] = wlDaysLink( $d, $page, $options );
375 }
376 return wfMsg ('wlshowlast',
377 implode(' | ', $hours),
378 implode(' | ', $days),
379 wlDaysLink( 0, $page, $options ) );
380 }
381
382 ?>