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