c98cd5f6028155ed272558d0361b680252d9390b
[lhc/web/wiklou.git] / includes / SpecialWatchlist.php
1 <?php
2 include_once( "SpecialRecentchanges.php" );
3 include_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
55 $sql = "SELECT COUNT(*) AS n FROM watchlist WHERE wl_user=$uid";
56 $res = wfQuery( $sql, DB_READ );
57 $s = wfFetchObject( $res );
58 $nitems = $s->n;
59
60 if($nitems == 0) {
61 $wgOut->addHTML( wfMsg( "nowatchlist" ) );
62 return;
63 }
64
65 if ( ! isset( $days ) ) {
66 $big = 1000;
67 if($nitems > $big) {
68 # Set default cutoff shorter
69 $days = (12.0 / 24.0); # 12 hours...
70 } else {
71 $days = 3; # longer cutoff for shortlisters
72 }
73 } else {
74 $days = floatval($days);
75 }
76
77 if ( $days <= 0 ) {
78 $docutoff = '';
79 $cutoff = false;
80 $npages = wfMsg( "all" );
81 } else {
82 $docutoff = "AND cur_timestamp > '" .
83 ( $cutoff = wfUnix2Timestamp( time() - intval( $days * 86400 ) ) )
84 . "'";
85 $sql = "SELECT COUNT(*) AS n FROM cur WHERE cur_timestamp>'$cutoff'";
86 $res = wfQuery( $sql, DB_READ );
87 $s = wfFetchObject( $res );
88 $npages = $s->n;
89 }
90
91 if(isset($_REQUEST['magic'])) {
92 $wgOut->addHTML( wfMsg( "watchlistcontains", $wgLang->formatNum( $nitems ) ) .
93 "<p>" . wfMsg( "watcheditlist" ) . "</p>\n" );
94
95 $wgOut->addHTML( "<form action='" .
96 $specialTitle->escapeLocalUrl( "action=submit" ) .
97 "' method='post'>\n" .
98 "<ul>\n" );
99 $sql = "SELECT wl_namespace,wl_title FROM watchlist WHERE wl_user=$uid";
100 $res = wfQuery( $sql, DB_READ );
101 global $wgUser, $wgLang;
102 $sk = $wgUser->getSkin();
103 while( $s = wfFetchObject( $res ) ) {
104 $t = Title::makeTitle( $s->wl_namespace, $s->wl_title );
105 $t = $t->getPrefixedText();
106 $wgOut->addHTML( "<li><input type='checkbox' name='id[]' value=\"" . htmlspecialchars($t) . "\" />" .
107 $sk->makeKnownLink( $t, $t ) .
108 "</li>\n" );
109 }
110 $wgOut->addHTML( "</ul>\n" .
111 "<input type='submit' name='remove' value='" .
112 wfMsg( "removechecked" ) . "' />\n" .
113 "</form>\n" );
114
115 return;
116 }
117
118 # If the watchlist is relatively short, it's simplest to zip
119 # down its entirety and then sort the results.
120
121 # If it's relatively long, it may be worth our while to zip
122 # through the time-sorted page list checking for watched items.
123
124 # Up estimate of watched items by 15% to compensate for talk pages...
125 if( $cutoff && ( $nitems*1.15 > $npages ) ) {
126 $x = "cur_timestamp";
127 $y = wfMsg( "watchmethod-recent" );
128 $z = "wl_namespace=cur_namespace&65534";
129 } else {
130 $x = "name_title_timestamp";
131 $y = wfMsg( "watchmethod-list" );
132 $z = "(wl_namespace=cur_namespace OR wl_namespace+1=cur_namespace)";
133 }
134
135
136 $wgOut->addHTML( "<i>" . wfMsg( "watchdetails",
137 $wgLang->formatNum( $nitems ), $wgLang->formatNum( $npages ), $y,
138 $specialTitle->escapeLocalUrl( "magic=yes" ) ) . "</i><br />\n" );
139
140
141 $sql = "SELECT
142 cur_namespace,cur_title,cur_comment, cur_id,
143 cur_user,cur_user_text,cur_timestamp,cur_minor_edit,cur_is_new
144 FROM watchlist,cur USE INDEX ($x)
145 WHERE wl_user=$uid
146 AND $z
147 AND wl_title=cur_title
148 $docutoff
149 ORDER BY cur_timestamp DESC";
150
151
152 $res = wfQuery( $sql, DB_READ, $fname );
153
154 if($days >= 1)
155 $note = wfMsg( "rcnote", $wgLang->formatNum( $limit ), $wgLang->formatNum( $days ) );
156 elseif($days > 0)
157 $note = wfMsg( "wlnote", $wgLang->formatNum( $limit ), $wgLang->formatNum( round($days*24) ) );
158 else
159 $note = "";
160 $wgOut->addHTML( "\n<hr />\n{$note}\n<br />" );
161 $note = wlCutoffLinks( $days, $limit );
162 $wgOut->addHTML( "{$note}\n" );
163
164 if ( wfNumRows( $res ) == 0 ) {
165 $wgOut->addHTML( "<p><i>" . wfMsg( "watchnochange" ) . "</i></p>" );
166 return;
167 }
168
169 $sk = $wgUser->getSkin();
170 $s = $sk->beginRecentChangesList();
171 $counter = 1;
172 while ( $obj = wfFetchObject( $res ) ) {
173 # Make fake RC entry
174 $rc = RecentChange::newFromCurRow( $obj );
175 $rc->counter = $counter++;
176 $s .= $sk->recentChangesLine( $rc, true );
177 }
178 $s .= $sk->endRecentChangesList();
179
180 wfFreeResult( $res );
181 $wgOut->addHTML( $s );
182
183 if ( $wgUseWatchlistCache ) {
184 $wgMemc->set( $memckey, $s, $wgWLCacheTimeout);
185 }
186 }
187
188
189 function wlHoursLink( $h, $page ) {
190 global $wgUser, $wgLang;
191 $sk = $wgUser->getSkin();
192 $s = $sk->makeKnownLink(
193 $wgLang->specialPage( $page ),
194 $wgLang->formatNum( $h ),
195 "days=" . ($h / 24.0) );
196 return $s;
197 }
198
199
200 function wlDaysLink( $d, $page ) {
201 global $wgUser, $wgLang;
202 $sk = $wgUser->getSkin();
203 $s = $sk->makeKnownLink(
204 $wgLang->specialPage( $page ),
205 ($d ? $wgLang->formatNum( $d ) : wfMsg( "all" ) ), "days=$d" );
206 return $s;
207 }
208
209 function wlCutoffLinks( $days, $limit, $page = "Watchlist" )
210 {
211 $hours = array( 1, 2, 6, 12 );
212 $days = array( 1, 3, 7 );
213 $cl = "";
214 $i = 0;
215 foreach( $hours as $h ) {
216 $hours[$i++] = wlHoursLink( $h, $page );
217 }
218 $i = 0;
219 foreach( $days as $d ) {
220 $days[$i++] = wlDaysLink( $d, $page );
221 }
222 return wfMsg ("wlshowlast",
223 implode(" | ", $hours),
224 implode(" | ", $days),
225 wlDaysLink( 0, $page ) );
226 }
227
228 ?>