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