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