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