Load balancing enabled in places where it's thought to be reasonably safe
[lhc/web/wiklou.git] / includes / SpecialWatchlist.php
1 <?php
2 require_once( "SpecialRecentchanges.php" );
3 require_once( "WatchedItem.php" );
4
5 function wfSpecialWatchlist()
6 {
7 global $wgUser, $wgOut, $wgLang, $wgTitle, $wgMemc, $wgLoadBalancer;
8 global $wgUseWatchlistCache, $wgWLCacheTimeout, $wgDBname, $wgIsMySQL;
9 global $days, $limit, $target; # From query string
10 $fname = "wfSpecialWatchlist";
11
12 $wgOut->setPagetitle( wfMsg( "watchlist" ) );
13 $sub = wfMsg( "watchlistsub", $wgUser->getName() );
14 $wgOut->setSubtitle( $sub );
15 $wgOut->setRobotpolicy( "noindex,nofollow" );
16
17 $specialTitle = Title::makeTitle( NS_SPECIAL, "Watchlist" );
18
19 $uid = $wgUser->getID();
20 if( $uid == 0 ) {
21 $wgOut->addHTML( wfMsg( "nowatchlist" ) );
22 return;
23 }
24
25 global $action,$remove,$id;
26 if(($action == "submit") && isset($remove) && is_array($id)) {
27 $wgOut->addHTML( wfMsg( "removingchecked" ) );
28 foreach($id as $one) {
29 $t = Title::newFromURL( $one );
30 if($t->getDBkey() != "") {
31 $wl = WatchedItem::fromUserTitle( $wgUser, $t );
32 if( $wl->removeWatch() === false ) {
33 $wgOut->addHTML( "<br />\n" . wfMsg( "couldntremove", htmlspecialchars($one) ) );
34 } else {
35 $wgOut->addHTML( " (" . htmlspecialchars($one) . ")" );
36 }
37 } else {
38 $wgOut->addHTML( "<br />\n" . wfMsg( "iteminvalidname", htmlspecialchars($one) ) );
39 }
40 }
41 $wgOut->addHTML( "done.\n<p>" );
42 }
43
44 if ( $wgUseWatchlistCache ) {
45 $memckey = "$wgDBname:watchlist:id:" . $wgUser->getId();
46 $cache_s = @$wgMemc->get( $memckey );
47 if( $cache_s ){
48 $wgOut->addHTML( wfMsg("wlsaved") );
49 $wgOut->addHTML( $cache_s );
50 return;
51 }
52 }
53
54 $wgLoadBalancer->force(-1);
55 $sql = "SELECT COUNT(*) AS n FROM watchlist WHERE wl_user=$uid";
56 $res = wfQuery( $sql, DB_READ );
57 $s = wfFetchObject( $res );
58 $nitems = $s->n;
59 $wgLoadBalancer->force(0);
60 if($nitems == 0) {
61 $wgOut->addHTML( wfMsg( "nowatchlist" ) );
62 return;
63 }
64
65 $wgLoadBalancer->force(-1);
66
67 if ( ! isset( $days ) ) {
68 $big = 1000;
69 if($nitems > $big) {
70 # Set default cutoff shorter
71 $days = (12.0 / 24.0); # 12 hours...
72 } else {
73 $days = 3; # longer cutoff for shortlisters
74 }
75 } else {
76 $days = floatval($days);
77 }
78
79 if ( $days <= 0 ) {
80 $docutoff = '';
81 $cutoff = false;
82 $npages = wfMsg( "all" );
83 } else {
84 $docutoff = "AND cur_timestamp > '" .
85 ( $cutoff = wfUnix2Timestamp( time() - intval( $days * 86400 ) ) )
86 . "'";
87 $sql = "SELECT COUNT(*) AS n FROM cur WHERE cur_timestamp>'$cutoff'";
88 $res = wfQuery( $sql, DB_READ );
89 $s = wfFetchObject( $res );
90 $npages = $s->n;
91
92 }
93
94 if(isset($_REQUEST['magic'])) {
95 $wgOut->addHTML( wfMsg( "watchlistcontains", $wgLang->formatNum( $nitems ) ) .
96 "<p>" . wfMsg( "watcheditlist" ) . "</p>\n" );
97
98 $wgOut->addHTML( "<form action='" .
99 $specialTitle->escapeLocalUrl( "action=submit" ) .
100 "' method='post'>\n" .
101 "<ul>\n" );
102 $sql = "SELECT wl_namespace,wl_title FROM watchlist WHERE wl_user=$uid";
103 $res = wfQuery( $sql, DB_READ );
104 global $wgUser, $wgLang;
105 $sk = $wgUser->getSkin();
106 while( $s = wfFetchObject( $res ) ) {
107 $t = Title::makeTitle( $s->wl_namespace, $s->wl_title );
108 if( is_null( $t ) ) {
109 $wgOut->addHTML( '<!-- bad title "' . htmlspecialchars( $s->wl_title ) . '" in namespace ' . IntVal( $s->wl_namespace ) . " -->\n" );
110 } else {
111 $t = $t->getPrefixedText();
112 $wgOut->addHTML( "<li><input type='checkbox' name='id[]' value=\"" . htmlspecialchars($t) . "\" />" .
113 $sk->makeKnownLink( $t, $t ) .
114 "</li>\n" );
115 }
116 }
117 $wgOut->addHTML( "</ul>\n" .
118 "<input type='submit' name='remove' value='" .
119 wfMsg( "removechecked" ) . "' />\n" .
120 "</form>\n" );
121
122 $wgLoadBalancer->force(0);
123 return;
124 }
125
126 # If the watchlist is relatively short, it's simplest to zip
127 # down its entirety and then sort the results.
128
129 # If it's relatively long, it may be worth our while to zip
130 # through the time-sorted page list checking for watched items.
131
132 # Up estimate of watched items by 15% to compensate for talk pages...
133 if( $cutoff && ( $nitems*1.15 > $npages ) ) {
134 $x = "cur_timestamp";
135 $y = wfMsg( "watchmethod-recent" );
136 $z = "wl_namespace=cur_namespace&65534";
137 } else {
138 $x = "name_title_timestamp";
139 $y = wfMsg( "watchmethod-list" );
140 $z = "(wl_namespace=cur_namespace OR wl_namespace+1=cur_namespace)";
141 }
142
143
144 $wgOut->addHTML( "<i>" . wfMsg( "watchdetails",
145 $wgLang->formatNum( $nitems ), $wgLang->formatNum( $npages ), $y,
146 $specialTitle->escapeLocalUrl( "magic=yes" ) ) . "</i><br />\n" );
147
148 $use_index=$wgIsMySQL?"USE INDEX ($x)":"";
149 $sql = "SELECT
150 cur_namespace,cur_title,cur_comment, cur_id,
151 cur_user,cur_user_text,cur_timestamp,cur_minor_edit,cur_is_new
152 FROM watchlist,cur $use_index
153 WHERE wl_user=$uid
154 AND $z
155 AND wl_title=cur_title
156 $docutoff
157 ORDER BY cur_timestamp DESC";
158
159
160 $res = wfQuery( $sql, DB_READ, $fname );
161
162 if($days >= 1)
163 $note = wfMsg( "rcnote", $wgLang->formatNum( $limit ), $wgLang->formatNum( $days ) );
164 elseif($days > 0)
165 $note = wfMsg( "wlnote", $wgLang->formatNum( $limit ), $wgLang->formatNum( round($days*24) ) );
166 else
167 $note = "";
168 $wgOut->addHTML( "\n<hr />\n{$note}\n<br />" );
169 $note = wlCutoffLinks( $days, $limit );
170 $wgOut->addHTML( "{$note}\n" );
171
172 if ( wfNumRows( $res ) == 0 ) {
173 $wgOut->addHTML( "<p><i>" . wfMsg( "watchnochange" ) . "</i></p>" );
174 $wgLoadBalancer->force(0);
175 return;
176 }
177
178 $sk = $wgUser->getSkin();
179 $s = $sk->beginRecentChangesList();
180 $counter = 1;
181 while ( $obj = wfFetchObject( $res ) ) {
182 # Make fake RC entry
183 $rc = RecentChange::newFromCurRow( $obj );
184 $rc->counter = $counter++;
185 $s .= $sk->recentChangesLine( $rc, true );
186 }
187 $s .= $sk->endRecentChangesList();
188
189 wfFreeResult( $res );
190 $wgOut->addHTML( $s );
191
192 if ( $wgUseWatchlistCache ) {
193 $wgMemc->set( $memckey, $s, $wgWLCacheTimeout);
194 }
195
196 $wgLoadBalancer->force(0);
197 }
198
199
200 function wlHoursLink( $h, $page ) {
201 global $wgUser, $wgLang;
202 $sk = $wgUser->getSkin();
203 $s = $sk->makeKnownLink(
204 $wgLang->specialPage( $page ),
205 $wgLang->formatNum( $h ),
206 "days=" . ($h / 24.0) );
207 return $s;
208 }
209
210
211 function wlDaysLink( $d, $page ) {
212 global $wgUser, $wgLang;
213 $sk = $wgUser->getSkin();
214 $s = $sk->makeKnownLink(
215 $wgLang->specialPage( $page ),
216 ($d ? $wgLang->formatNum( $d ) : wfMsg( "all" ) ), "days=$d" );
217 return $s;
218 }
219
220 function wlCutoffLinks( $days, $limit, $page = "Watchlist" )
221 {
222 $hours = array( 1, 2, 6, 12 );
223 $days = array( 1, 3, 7 );
224 $cl = "";
225 $i = 0;
226 foreach( $hours as $h ) {
227 $hours[$i++] = wlHoursLink( $h, $page );
228 }
229 $i = 0;
230 foreach( $days as $d ) {
231 $days[$i++] = wlDaysLink( $d, $page );
232 }
233 return wfMsg ("wlshowlast",
234 implode(" | ", $hours),
235 implode(" | ", $days),
236 wlDaysLink( 0, $page ) );
237 }
238
239 ?>