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