c7a2d15ad132ceafef11a134feddb3b63b0e7ee6
[lhc/web/wiklou.git] / includes / SpecialWatchlist.php
1 <?
2 global $IP;
3 include_once( "$IP/SpecialRecentchanges.php" );
4
5 function wfSpecialWatchlist()
6 {
7 global $wgUser, $wgOut, $wgLang, $wgTitle;
8 global $days, $limit, $target; # From query string
9 $fname = "wfSpecialWatchlist";
10
11 $wgOut->setPagetitle( wfMsg( "watchlist" ) );
12 $sub = str_replace( "$1", $wgUser->getName(), wfMsg( "watchlistsub" ) );
13 $wgOut->setSubtitle( $sub );
14 $wgOut->setRobotpolicy( "index,follow" );
15
16 if ( ! isset( $days ) ) {
17 /*
18 $days = $wgUser->getOption( "rcdays" );
19 if ( ! $days ) { $days = 3; }
20 */
21 $days = (1.0 / 24.0); # 1 hour...
22 }
23 $days = floatval($days);
24
25 if ( $days <= 0 ) {
26 $docutoff = '';
27 } else {
28 $docutoff = "cur_timestamp > '" .
29 ( $cutoff = wfUnix2Timestamp( time() - intval( $days * 86400 ) ) )
30 . "' AND";
31 }
32
33 $uid = $wgUser->getID();
34 if( $uid == 0 ) {
35 $wgOut->addHTML( wfMsg( "nowatchlist" ) );
36 return;
37 }
38
39 global $action,$remove,$id;
40 if(($action == "submit") && isset($remove) && is_array($id)) {
41 $wgOut->addHTML( wfMsg( "removingchecked" ) );
42 foreach($id as $one) {
43 $t = Title::newFromURL( $one );
44 if($t->getDBkey() != "") {
45 $sql = "DELETE FROM watchlist WHERE wl_user=$uid AND " .
46 "wl_namespace=" . $t->getNamespace() . " AND " .
47 "wl_title='" . wfStrencode( $t->getDBkey() ) . "'";
48 $res = wfQuery( $sql );
49 if($res === FALSE) {
50 $wgOut->addHTML( "<br />\n" . wfMsg( "couldntremove", htmlspecialchars($one) ) );
51 } else {
52 $wgOut->addHTML( " (" . htmlspecialchars($one) . ")" );
53 }
54 } else {
55 $wgOut->addHTML( "<br />\n" . wfMsg( "iteminvalidname", htmlspecialchars($one) ) );
56 }
57 }
58 $wgOut->addHTML( "done.\n<p>" );
59 }
60
61 $sql = "SELECT COUNT(*) AS n FROM watchlist WHERE wl_user=$uid";
62 $res = wfQuery( $sql );
63 $s = wfFetchObject( $res );
64 $nitems = $s->n;
65
66 if($nitems == 0) {
67 $wgOut->addHTML( wfMsg( "nowatchlist" ) );
68 return;
69 }
70
71 $sql = "SELECT COUNT(*) AS n FROM cur WHERE cur_timestamp>'$cutoff'";
72 $res = wfQuery( $sql );
73 $s = wfFetchObject( $res );
74 $npages = $s->n;
75
76
77 if(isset($_REQUEST['magic'])) {
78 $wgOut->addHTML( wfMsg( "watchlistcontains", $nitems ) .
79 "<p>" . wfMsg( "watcheditlist" ) . "</p>\n" );
80
81 $wgOut->addHTML( "<form action='" .
82 wfLocalUrl( $wgLang->specialPage( "Watchlist" ), "action=submit" ) .
83 "' method='post'>\n" .
84 "<ul>\n" );
85 $sql = "SELECT wl_namespace,wl_title FROM watchlist WHERE wl_user=$uid";
86 $res = wfQuery( $sql );
87 global $wgUser, $wgLang;
88 $sk = $wgUser->getSkin();
89 while( $s = wfFetchObject( $res ) ) {
90 $t = Title::makeTitle( $s->wl_namespace, $s->wl_title );
91 $t = $t->getPrefixedText();
92 $wgOut->addHTML( "<li><input type='checkbox' name='id[]' value=\"" . htmlspecialchars($t) . "\">" .
93 $sk->makeKnownLink( $t, $t ) .
94 "</li>\n" );
95 }
96 $wgOut->addHTML( "</ul>\n" .
97 "<input type='submit' name='remove' value='" .
98 wfMsg( "removechecked" ) . "'>\n" .
99 "</form>\n" );
100
101 return;
102 }
103
104 # If the watchlist is relatively short, it's simplest to zip
105 # down its entirety and then sort the results.
106
107 # If it's relatively long, it may be worth our while to zip
108 # through the time-sorted page list checking for
109
110 # Up estimate of watched items by 15% to compensate for talk pages...
111 if( ( $nitems*1.15 > $npages ) ) {
112 $x = "cur_timestamp";
113 $y = wfMsg( "watchmethod-recent" );
114 $z = "wl_namespace=cur_namespace&65534";
115 } else {
116 $x = "name_title_timestamp";
117 $y = wfMsg( "watchmethod-list" );
118 $z = "(wl_namespace=cur_namespace OR wl_namespace+1=cur_namespace)";
119 }
120
121 $wgOut->addHTML( "<i>" . wfMsg( "watchdetails", $nitems, $npages, $y,
122 wfLocalUrl( $wgLang->specialPage("Watchlist"),"magic=yes" ) ) . "</i><br>\n" );
123
124
125 $sql = "SELECT
126 cur_namespace,cur_title,cur_comment,
127 cur_user,cur_user_text,cur_timestamp,cur_minor_edit,cur_is_new
128 FROM watchlist,cur USE INDEX ($x)
129 WHERE wl_user=$uid
130 AND $z
131 AND wl_title=cur_title
132 AND cur_timestamp>'$cutoff'
133 ORDER BY cur_timestamp DESC";
134
135
136 $res = wfQuery( $sql, $fname );
137
138 if($days >= 1)
139 $note = wfMsg( "rcnote", $limit, $days );
140 else
141 $note = wfMsg( "wlnote", $limit, round($days*24) );
142 $wgOut->addHTML( "\n<hr>\n{$note}\n<br>" );
143 $note = wlCutoffLinks( $days, $limit );
144 $wgOut->addHTML( "{$note}\n" );
145
146 if ( wfNumRows( $res ) == 0 ) {
147 $wgOut->addHTML( "<p><i>" . wfMsg( "watchnochange" ) . "</i></p>" );
148 return;
149 }
150
151 $sk = $wgUser->getSkin();
152 $s = $sk->beginRecentChangesList();
153
154 while ( $obj = wfFetchObject( $res ) ) {
155 $ts = $obj->cur_timestamp;
156 $u = $obj->cur_user;
157 $ut = $obj->cur_user_text;
158 $ns = $obj->cur_namespace;
159 $ttl = $obj->cur_title;
160 $com = $obj->cur_comment;
161 $me = ( $obj->cur_minor_edit > 0 );
162 $new = ( $obj->cur_is_new > 0 );
163 $watched = true;
164
165 $s .= $sk->recentChangesLine( $ts, $u, $ut, $ns, $ttl, $com, $me, $new, $watched );
166 }
167 $s .= $sk->endRecentChangesList();
168
169 wfFreeResult( $res );
170 $wgOut->addHTML( $s );
171 }
172
173
174 function wlHoursLink( $h, $page ) {
175 global $wgUser, $wgLang;
176 $sk = $wgUser->getSkin();
177 $s = $sk->makeKnownLink(
178 $wgLang->specialPage( $page ),
179 $h, "days=" . ($h / 24.0) );
180 return $s;
181 }
182
183
184 function wlDaysLink( $d, $page ) {
185 global $wgUser, $wgLang;
186 $sk = $wgUser->getSkin();
187 $s = $sk->makeKnownLink(
188 $wgLang->specialPage( $page ),
189 $d, "days=$d" );
190 return $s;
191 }
192
193 function wlCutoffLinks( $days, $limit, $page = "Watchlist" )
194 {
195 $hours = array( 1, 2, 6, 12 );
196 $days = array( 1, 3, 7 );
197 $cl = "";
198 $i = 0;
199 foreach( $hours as $h ) {
200 $hours[$i++] = wlHoursLink( $h, $page );
201 }
202 $i = 0;
203 foreach( $days as $d ) {
204 $days[$i++] = wlDaysLink( $d, $page );
205 }
206 return
207 "Show last " .
208 implode(" | ", $hours) . " hours " .
209 implode(" | ", $days) . " days";
210 # $note = wfMsg( "rclinks", $cl, $dl, $mlink );
211 }
212
213 ?>