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