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