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