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