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