Removed some nonsense and fixed ugly HTML
[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 = $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 $sql = "SELECT COUNT(*) AS n FROM $page, $revision WHERE rev_timestamp>'$cutoff' AND page_id=rev_page";
131 $res = $dbr->query( $sql, $fname );
132 $s = $dbr->fetchObject( $res );
133 $npages = $s->n;
134
135 }
136
137 /* Edit watchlist form */
138 if($wgRequest->getBool('edit') || $par == 'edit' ) {
139 $wgOut->addWikiText( wfMsg( 'watchlistcontains', $wgLang->formatNum( $nitems ) ) .
140 "\n\n" . wfMsg( 'watcheditlist' ) );
141
142 $wgOut->addHTML( '<form action=\'' .
143 $specialTitle->escapeLocalUrl( 'action=submit' ) .
144 "' method='post'>\n" );
145
146 # Patch A2
147 # The following was proposed by KTurner 07.11.2004 to T.Gries
148 # $sql = "SELECT distinct (wl_namespace & ~1),wl_title FROM $watchlist WHERE wl_user=$uid";
149 $sql = "SELECT wl_namespace,wl_title FROM $watchlist WHERE wl_user=$uid";
150
151 $res = $dbr->query( $sql, $fname );
152 $sk = $wgUser->getSkin();
153
154 $list = array();
155 while( $s = $dbr->fetchObject( $res ) ) {
156 $list[$s->wl_namespace][] = $s->wl_title;
157 }
158
159 // TODO: Display a TOC
160 foreach($list as $ns => $titles) {
161 if (Namespace::isTalk($ns))
162 continue;
163 if ($ns != NS_MAIN)
164 $wgOut->addHTML( '<h2>' . $wgContLang->getFormattedNsText( $ns ) . '</h2>' );
165 $wgOut->addHTML( '<ul>' );
166 foreach($titles as $title) {
167 $t = Title::makeTitle( $ns, $title );
168 if( is_null( $t ) ) {
169 $wgOut->addHTML(
170 '<!-- bad title "' .
171 htmlspecialchars( $s->wl_title ) . '" in namespace ' . $s->wl_namespace . " -->\n"
172 );
173 } else {
174 $t = $t->getPrefixedText();
175 $wgOut->addHTML(
176 '<li><input type="checkbox" name="id[]" value="' . htmlspecialchars($t) . '" />' .
177 $sk->makeLink( $t, $t ) .
178 "</li>\n"
179 );
180 }
181 }
182 $wgOut->addHTML( '</ul>' );
183 }
184 $wgOut->addHTML(
185 "<input type='submit' name='remove' value=\"" .
186 htmlspecialchars( wfMsg( "removechecked" ) ) . "\" />\n" .
187 "</form>\n"
188 );
189
190 return;
191 }
192
193 # If the watchlist is relatively short, it's simplest to zip
194 # down its entirety and then sort the results.
195
196 # If it's relatively long, it may be worth our while to zip
197 # through the time-sorted page list checking for watched items.
198
199 # Up estimate of watched items by 15% to compensate for talk pages...
200 if( $cutoff && ( $nitems*1.15 > $npages ) ) {
201 $x = 'rev_timestamp';
202 $y = wfMsg( 'watchmethod-recent' );
203 # TG patch: here we do not consider pages and their talk pages equivalent - why should we ?
204 # The change results in talk-pages not automatically included in watchlists, when their parent page is included
205 # $z = "wl_namespace=cur_namespace & ~1";
206 $z = 'wl_namespace=page_namespace';
207 } else {
208 $x = 'page_timestamp';
209 $y = wfMsg( 'watchmethod-list' );
210 # TG patch: here we do not consider pages and their talk pages equivalent - why should we ?
211 # The change results in talk-pages not automatically included in watchlists, when their parent page is included
212 # $z = "(wl_namespace=cur_namespace OR wl_namespace+1=cur_namespace)";
213 $z = 'wl_namespace=page_namespace';
214 }
215
216 $andHideOwn = $hideOwn ? "AND (rev_user <> $uid)" : '';
217
218 # Show watchlist header
219 $header = '';
220 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
221 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
222 }
223 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
224 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
225 }
226
227 $header .= wfMsg( 'watchdetails', $wgLang->formatNum( $nitems / 2 ),
228 $wgLang->formatNum( $npages ), $y,
229 $specialTitle->getFullUrl( 'edit=yes' ) );
230 $wgOut->addWikiText( $header );
231
232 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
233 $wgOut->addHTML( '<form action="' .
234 $specialTitle->escapeLocalUrl() .
235 '" method="post"><input type="submit" name="dummy" value="' .
236 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
237 '" /><input type="hidden" name="reset" value="all" /></form>' .
238 "\n\n" );
239 }
240
241 $use_index = $dbr->useIndexClause( $x );
242 $sql = "SELECT
243 page_namespace,page_title,rev_comment, page_id,
244 rev_user,rev_user_text,rev_timestamp,rev_minor_edit,rev_id,page_is_new,wl_notificationtimestamp
245 FROM $watchlist,$page,$revision $use_index
246 WHERE wl_user=$uid
247 $andHideOwn
248 AND $z
249 AND wl_title=page_title
250 AND page_latest=rev_id
251 $docutoff
252 ORDER BY rev_timestamp DESC";
253
254
255 $res = $dbr->query( $sql, $fname );
256 $numRows = $dbr->numRows( $res );
257
258 /* Start bottom header */
259 $wgOut->addHTML( "<hr />\n<p>" );
260
261 if($days >= 1)
262 $wgOut->addWikiText( wfMsg( 'rcnote', $wgLang->formatNum( $numRows ),
263 $wgLang->formatNum( $days ) ) . '<br />' , false );
264 elseif($days > 0)
265 $wgOut->addWikiText( wfMsg( 'wlnote', $wgLang->formatNum( $numRows ),
266 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
267
268 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
269
270 $sk = $wgUser->getSkin();
271 $s = $sk->makeKnownLink(
272 $wgContLang->specialPage( 'Watchlist' ),
273 (0 == $hideOwn) ? wfMsgHtml( 'wlhide' ) : wfMsgHtml( 'wlshow' ),
274 wfArrayToCGI( array('hideOwn' => 1-$hideOwn ), $nondefaults ) );
275
276 $wgOut->addHTML( wfMsgHtml( "wlhideshowown", $s ) );
277
278 if ( $numRows == 0 ) {
279 $wgOut->addWikitext( "<br />" . wfMsg( 'watchnochange' ), false );
280 $wgOut->addHTML( "</p>\n" );
281 return;
282 }
283
284 $wgOut->addHTML( "</p>\n" );
285 /* End bottom header */
286
287 $sk = $wgUser->getSkin();
288 $list =& new ChangesList( $sk );
289 $s = $list->beginRecentChangesList();
290 $counter = 1;
291 while ( $obj = $dbr->fetchObject( $res ) ) {
292 # Make fake RC entry
293 $rc = RecentChange::newFromCurRow( $obj );
294 $rc->counter = $counter++;
295
296 if ( $wgShowUpdatedMarker ) {
297 $updated = $obj->wl_notificationtimestamp;
298 } else {
299 // Same visual appearance as MW 1.4
300 $updated = true;
301 }
302
303 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
304 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" .wfStrencode($obj->page_title). "' AND wl_namespace='{$obj->page_namespace}'" ;
305 $res3 = $dbr->query( $sql3, DB_READ, $fname );
306 $x = $dbr->fetchObject( $res3 );
307 $rc->numberofWatchingusers = $x->n;
308 } else {
309 $rc->numberofWatchingusers = 0;
310 }
311
312 $s .= $list->recentChangesLine( $rc, $updated );
313 }
314 $s .= $list->endRecentChangesList();
315
316 $dbr->freeResult( $res );
317 $wgOut->addHTML( $s );
318
319 if ( $wgUseWatchlistCache ) {
320 $wgMemc->set( $memckey, $s, $wgWLCacheTimeout);
321 }
322
323 }
324
325 function wlHoursLink( $h, $page, $options = array() ) {
326 global $wgUser, $wgLang, $wgContLang;
327 $sk = $wgUser->getSkin();
328 $s = $sk->makeKnownLink(
329 $wgContLang->specialPage( $page ),
330 $wgLang->formatNum( $h ),
331 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
332 return $s;
333 }
334
335 function wlDaysLink( $d, $page, $options = array() ) {
336 global $wgUser, $wgLang, $wgContLang;
337 $sk = $wgUser->getSkin();
338 $s = $sk->makeKnownLink(
339 $wgContLang->specialPage( $page ),
340 ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
341 wfArrayToCGI( array('days' => $d), $options ) );
342 return $s;
343 }
344
345 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
346 $hours = array( 1, 2, 6, 12 );
347 $days = array( 1, 3, 7 );
348 $cl = '';
349 $i = 0;
350 foreach( $hours as $h ) {
351 $hours[$i++] = wlHoursLink( $h, $page, $options );
352 }
353 $i = 0;
354 foreach( $days as $d ) {
355 $days[$i++] = wlDaysLink( $d, $page, $options );
356 }
357 return wfMsg ('wlshowlast',
358 implode(' | ', $hours),
359 implode(' | ', $days),
360 wlDaysLink( 0, $page, $options ) );
361 }
362
363 ?>