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