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