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