render.ml updated to support newer ImageMagick and relative filepaths
[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;
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
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
60 if($nitems == 0) {
61 $wgOut->addHTML( wfMsg( "nowatchlist" ) );
62 return;
63 }
64
65 if ( ! isset( $days ) ) {
66 $big = 1000;
67 if($nitems > $big) {
68 # Set default cutoff shorter
69 $days = (12.0 / 24.0); # 12 hours...
70 } else {
71 $days = 3; # longer cutoff for shortlisters
72 }
73 } else {
74 $days = floatval($days);
75 }
76
77 if ( $days <= 0 ) {
78 $docutoff = '';
79 $cutoff = false;
80 $npages = wfMsg( "all" );
81 } else {
82 $docutoff = "AND cur_timestamp > '" .
83 ( $cutoff = wfUnix2Timestamp( time() - intval( $days * 86400 ) ) )
84 . "'";
85 $sql = "SELECT COUNT(*) AS n FROM cur WHERE cur_timestamp>'$cutoff'";
86 $res = wfQuery( $sql, DB_READ );
87 $s = wfFetchObject( $res );
88 $npages = $s->n;
89 }
90
91 if(isset($_REQUEST['magic'])) {
92 $wgOut->addHTML( wfMsg( "watchlistcontains", $wgLang->formatNum( $nitems ) ) .
93 "<p>" . wfMsg( "watcheditlist" ) . "</p>\n" );
94
95 $wgOut->addHTML( "<form action='" .
96 $specialTitle->escapeLocalUrl( "action=submit" ) .
97 "' method='post'>\n" .
98 "<ul>\n" );
99 $sql = "SELECT wl_namespace,wl_title FROM watchlist WHERE wl_user=$uid";
100 $res = wfQuery( $sql, DB_READ );
101 global $wgUser, $wgLang;
102 $sk = $wgUser->getSkin();
103 while( $s = wfFetchObject( $res ) ) {
104 $t = Title::makeTitle( $s->wl_namespace, $s->wl_title );
105 if( is_null( $t ) ) {
106 $wgOut->addHTML( '<!-- bad title "' . htmlspecialchars( $s->wl_title ) . '" in namespace ' . IntVal( $s->wl_namespace ) . " -->\n" );
107 } else {
108 $t = $t->getPrefixedText();
109 $wgOut->addHTML( "<li><input type='checkbox' name='id[]' value=\"" . htmlspecialchars($t) . "\" />" .
110 $sk->makeKnownLink( $t, $t ) .
111 "</li>\n" );
112 }
113 }
114 $wgOut->addHTML( "</ul>\n" .
115 "<input type='submit' name='remove' value='" .
116 wfMsg( "removechecked" ) . "' />\n" .
117 "</form>\n" );
118
119 return;
120 }
121
122 # If the watchlist is relatively short, it's simplest to zip
123 # down its entirety and then sort the results.
124
125 # If it's relatively long, it may be worth our while to zip
126 # through the time-sorted page list checking for watched items.
127
128 # Up estimate of watched items by 15% to compensate for talk pages...
129 if( $cutoff && ( $nitems*1.15 > $npages ) ) {
130 $x = "cur_timestamp";
131 $y = wfMsg( "watchmethod-recent" );
132 $z = "wl_namespace=cur_namespace&65534";
133 } else {
134 $x = "name_title_timestamp";
135 $y = wfMsg( "watchmethod-list" );
136 $z = "(wl_namespace=cur_namespace OR wl_namespace+1=cur_namespace)";
137 }
138
139
140 $wgOut->addHTML( "<i>" . wfMsg( "watchdetails",
141 $wgLang->formatNum( $nitems ), $wgLang->formatNum( $npages ), $y,
142 $specialTitle->escapeLocalUrl( "magic=yes" ) ) . "</i><br />\n" );
143
144 $use_index=$wgIsMySQL?"USE INDEX ($x)":"";
145 $sql = "SELECT
146 cur_namespace,cur_title,cur_comment, cur_id,
147 cur_user,cur_user_text,cur_timestamp,cur_minor_edit,cur_is_new
148 FROM watchlist,cur $use_index
149 WHERE wl_user=$uid
150 AND $z
151 AND wl_title=cur_title
152 $docutoff
153 ORDER BY cur_timestamp DESC";
154
155
156 $res = wfQuery( $sql, DB_READ, $fname );
157
158 if($days >= 1)
159 $note = wfMsg( "rcnote", $wgLang->formatNum( $limit ), $wgLang->formatNum( $days ) );
160 elseif($days > 0)
161 $note = wfMsg( "wlnote", $wgLang->formatNum( $limit ), $wgLang->formatNum( round($days*24) ) );
162 else
163 $note = "";
164 $wgOut->addHTML( "\n<hr />\n{$note}\n<br />" );
165 $note = wlCutoffLinks( $days, $limit );
166 $wgOut->addHTML( "{$note}\n" );
167
168 if ( wfNumRows( $res ) == 0 ) {
169 $wgOut->addHTML( "<p><i>" . wfMsg( "watchnochange" ) . "</i></p>" );
170 return;
171 }
172
173 $sk = $wgUser->getSkin();
174 $s = $sk->beginRecentChangesList();
175 $counter = 1;
176 while ( $obj = wfFetchObject( $res ) ) {
177 # Make fake RC entry
178 $rc = RecentChange::newFromCurRow( $obj );
179 $rc->counter = $counter++;
180 $s .= $sk->recentChangesLine( $rc, true );
181 }
182 $s .= $sk->endRecentChangesList();
183
184 wfFreeResult( $res );
185 $wgOut->addHTML( $s );
186
187 if ( $wgUseWatchlistCache ) {
188 $wgMemc->set( $memckey, $s, $wgWLCacheTimeout);
189 }
190 }
191
192
193 function wlHoursLink( $h, $page ) {
194 global $wgUser, $wgLang;
195 $sk = $wgUser->getSkin();
196 $s = $sk->makeKnownLink(
197 $wgLang->specialPage( $page ),
198 $wgLang->formatNum( $h ),
199 "days=" . ($h / 24.0) );
200 return $s;
201 }
202
203
204 function wlDaysLink( $d, $page ) {
205 global $wgUser, $wgLang;
206 $sk = $wgUser->getSkin();
207 $s = $sk->makeKnownLink(
208 $wgLang->specialPage( $page ),
209 ($d ? $wgLang->formatNum( $d ) : wfMsg( "all" ) ), "days=$d" );
210 return $s;
211 }
212
213 function wlCutoffLinks( $days, $limit, $page = "Watchlist" )
214 {
215 $hours = array( 1, 2, 6, 12 );
216 $days = array( 1, 3, 7 );
217 $cl = "";
218 $i = 0;
219 foreach( $hours as $h ) {
220 $hours[$i++] = wlHoursLink( $h, $page );
221 }
222 $i = 0;
223 foreach( $days as $d ) {
224 $days[$i++] = wlDaysLink( $d, $page );
225 }
226 return wfMsg ("wlshowlast",
227 implode(" | ", $hours),
228 implode(" | ", $days),
229 wlDaysLink( 0, $page ) );
230 }
231
232 ?>