7ec0fdd2970b9650aca2470ca629fec081278dde
[lhc/web/wiklou.git] / includes / SpecialWatchlist.php
1 <?
2 include_once( "SpecialRecentchanges.php" );
3 include_once( "WatchedItem.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 = wfMsg( "watchlistsub", $wgUser->getName() );
13 $wgOut->setSubtitle( $sub );
14 $wgOut->setRobotpolicy( "noindex,nofollow" );
15
16 $uid = $wgUser->getID();
17 if( $uid == 0 ) {
18 $wgOut->addHTML( wfMsg( "nowatchlist" ) );
19 return;
20 }
21
22 global $action,$remove,$id;
23 if(($action == "submit") && isset($remove) && is_array($id)) {
24 $wgOut->addHTML( wfMsg( "removingchecked" ) );
25 foreach($id as $one) {
26 $t = Title::newFromURL( $one );
27 if($t->getDBkey() != "") {
28 $wl = WatchedItem::fromUserTitle( $wgUser, $t );
29 if( $wl->removeWatch() === false ) {
30 $wgOut->addHTML( "<br />\n" . wfMsg( "couldntremove", htmlspecialchars($one) ) );
31 } else {
32 $wgOut->addHTML( " (" . htmlspecialchars($one) . ")" );
33 }
34 } else {
35 $wgOut->addHTML( "<br />\n" . wfMsg( "iteminvalidname", htmlspecialchars($one) ) );
36 }
37 }
38 $wgOut->addHTML( "done.\n<p>" );
39 }
40
41 $sql = "SELECT COUNT(*) AS n FROM watchlist WHERE wl_user=$uid";
42 $res = wfQuery( $sql, DB_READ );
43 $s = wfFetchObject( $res );
44 $nitems = $s->n;
45
46 if($nitems == 0) {
47 $wgOut->addHTML( wfMsg( "nowatchlist" ) );
48 return;
49 }
50
51 if ( ! isset( $days ) ) {
52 $big = 250;
53 if($nitems > $big) {
54 # Set default cutoff shorter
55 $days = (1.0 / 24.0); # 1 hour...
56 } else {
57 $days = 0; # no time cutoff for shortlisters
58 }
59 } else {
60 $days = floatval($days);
61 }
62
63 if ( $days <= 0 ) {
64 $docutoff = '';
65 $cutoff = false;
66 $npages = wfMsg( "all" );
67 } else {
68 $docutoff = "AND cur_timestamp > '" .
69 ( $cutoff = wfUnix2Timestamp( time() - intval( $days * 86400 ) ) )
70 . "'";
71 $sql = "SELECT COUNT(*) AS n FROM cur WHERE cur_timestamp>'$cutoff'";
72 $res = wfQuery( $sql, DB_READ );
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, DB_READ );
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 watched items.
109
110 # Up estimate of watched items by 15% to compensate for talk pages...
111 if( $cutoff && ( $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 $docutoff
133 ORDER BY cur_timestamp DESC";
134
135
136 $res = wfQuery( $sql, DB_READ, $fname );
137
138 if($days >= 1)
139 $note = wfMsg( "rcnote", $limit, $days );
140 elseif($days > 0)
141 $note = wfMsg( "wlnote", $limit, round($days*24) );
142 else
143 $note = "";
144 $wgOut->addHTML( "\n<hr>\n{$note}\n<br>" );
145 $note = wlCutoffLinks( $days, $limit );
146 $wgOut->addHTML( "{$note}\n" );
147
148 if ( wfNumRows( $res ) == 0 ) {
149 $wgOut->addHTML( "<p><i>" . wfMsg( "watchnochange" ) . "</i></p>" );
150 return;
151 }
152
153 $sk = $wgUser->getSkin();
154 $s = $sk->beginRecentChangesList();
155
156 while ( $obj = wfFetchObject( $res ) ) {
157 $ts = $obj->cur_timestamp;
158 $u = $obj->cur_user;
159 $ut = $obj->cur_user_text;
160 $ns = $obj->cur_namespace;
161 $ttl = $obj->cur_title;
162 $com = $obj->cur_comment;
163 $me = ( $obj->cur_minor_edit > 0 );
164 $new = ( $obj->cur_is_new > 0 );
165 $watched = true;
166
167 $s .= $sk->recentChangesLine( $ts, $u, $ut, $ns, $ttl, $com, $me, $new, $watched );
168 }
169 $s .= $sk->endRecentChangesList();
170
171 wfFreeResult( $res );
172 $wgOut->addHTML( $s );
173 }
174
175
176 function wlHoursLink( $h, $page ) {
177 global $wgUser, $wgLang;
178 $sk = $wgUser->getSkin();
179 $s = $sk->makeKnownLink(
180 $wgLang->specialPage( $page ),
181 $h, "days=" . ($h / 24.0) );
182 return $s;
183 }
184
185
186 function wlDaysLink( $d, $page ) {
187 global $wgUser, $wgLang;
188 $sk = $wgUser->getSkin();
189 $s = $sk->makeKnownLink(
190 $wgLang->specialPage( $page ),
191 ($d ? $d : wfMsg( "all" ) ), "days=$d" );
192 return $s;
193 }
194
195 function wlCutoffLinks( $days, $limit, $page = "Watchlist" )
196 {
197 $hours = array( 1, 2, 6, 12 );
198 $days = array( 1, 3, 7 );
199 $cl = "";
200 $i = 0;
201 foreach( $hours as $h ) {
202 $hours[$i++] = wlHoursLink( $h, $page );
203 }
204 $i = 0;
205 foreach( $days as $d ) {
206 $days[$i++] = wlDaysLink( $d, $page );
207 }
208 return wfMsg ("wlshowlast",
209 implode(" | ", $hours),
210 implode(" | ", $days),
211 wlDaysLink( 0, $page ) );
212 }
213
214 ?>