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