Partial revert of r32982; old Title method is fine here
[lhc/web/wiklou.git] / includes / SpecialWatchlist.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 *
9 */
10 require_once( dirname(__FILE__) . '/SpecialRecentchanges.php' );
11
12 /**
13 * Constructor
14 *
15 * @param $par Parameter passed to the page
16 */
17 function wfSpecialWatchlist( $par ) {
18 global $wgUser, $wgOut, $wgLang, $wgRequest;
19 global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
20 global $wgEnotifWatchlist;
21 $fname = 'wfSpecialWatchlist';
22
23 $skin = $wgUser->getSkin();
24 $specialTitle = SpecialPage::getTitleFor( 'Watchlist' );
25 $wgOut->setRobotPolicy( 'noindex,nofollow' );
26
27 # Anons don't get a watchlist
28 if( $wgUser->isAnon() ) {
29 $wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
30 $llink = $skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Userlogin' ), wfMsgHtml( 'loginreqlink' ), 'returnto=' . $specialTitle->getPrefixedUrl() );
31 $wgOut->addHtml( wfMsgWikiHtml( 'watchlistanontext', $llink ) );
32 return;
33 }
34
35 $wgOut->setPageTitle( wfMsg( 'watchlist' ) );
36
37 $sub = wfMsgExt( 'watchlistfor', 'parseinline', $wgUser->getName() );
38 $sub .= '<br />' . WatchlistEditor::buildTools( $wgUser->getSkin() );
39 $wgOut->setSubtitle( $sub );
40
41 if( ( $mode = WatchlistEditor::getMode( $wgRequest, $par ) ) !== false ) {
42 $editor = new WatchlistEditor();
43 $editor->execute( $wgUser, $wgOut, $wgRequest, $mode );
44 return;
45 }
46
47 $uid = $wgUser->getId();
48 if( $wgEnotifWatchlist && $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
49 $wgUser->clearAllNotifications( $uid );
50 $wgOut->redirect( $specialTitle->getFullUrl() );
51 return;
52 }
53
54 $defaults = array(
55 /* float */ 'days' => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
56 /* bool */ 'hideOwn' => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
57 /* bool */ 'hideBots' => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
58 /* bool */ 'hideMinor' => (int)$wgUser->getBoolOption( 'watchlisthideminor' ),
59 /* ? */ 'namespace' => 'all',
60 );
61
62 extract($defaults);
63
64 # Extract variables from the request, falling back to user preferences or
65 # other default values if these don't exist
66 $prefs['days' ] = floatval( $wgUser->getOption( 'watchlistdays' ) );
67 $prefs['hideown' ] = $wgUser->getBoolOption( 'watchlisthideown' );
68 $prefs['hidebots'] = $wgUser->getBoolOption( 'watchlisthidebots' );
69 $prefs['hideminor'] = $wgUser->getBoolOption( 'watchlisthideminor' );
70
71 # Get query variables
72 $days = $wgRequest->getVal( 'days', $prefs['days'] );
73 $hideOwn = $wgRequest->getBool( 'hideOwn', $prefs['hideown'] );
74 $hideBots = $wgRequest->getBool( 'hideBots', $prefs['hidebots'] );
75 $hideMinor = $wgRequest->getBool( 'hideMinor', $prefs['hideminor'] );
76
77 # Get namespace value, if supplied, and prepare a WHERE fragment
78 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
79 if( !is_null( $nameSpace ) ) {
80 $nameSpace = intval( $nameSpace );
81 $nameSpaceClause = " AND rc_namespace = $nameSpace";
82 } else {
83 $nameSpace = '';
84 $nameSpaceClause = '';
85 }
86
87 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
88 list( $page, $watchlist, $recentchanges ) = $dbr->tableNamesN( 'page', 'watchlist', 'recentchanges' );
89
90 $watchlistCount = $dbr->selectField( 'watchlist', 'COUNT(*)',
91 array( 'wl_user' => $uid ), __METHOD__ );
92 // Adjust for page X, talk:page X, which are both stored separately,
93 // but treated together
94 $nitems = floor($watchlistCount / 2);
95
96 if( is_null($days) || !is_numeric($days) ) {
97 $big = 1000; /* The magical big */
98 if($nitems > $big) {
99 # Set default cutoff shorter
100 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
101 } else {
102 $days = $defaults['days']; # default cutoff for shortlisters
103 }
104 } else {
105 $days = floatval($days);
106 }
107
108 // Dump everything here
109 $nondefaults = array();
110
111 wfAppendToArrayIfNotDefault('days' , $days , $defaults, $nondefaults);
112 wfAppendToArrayIfNotDefault('hideOwn' , (int)$hideOwn , $defaults, $nondefaults);
113 wfAppendToArrayIfNotDefault('hideBots' , (int)$hideBots, $defaults, $nondefaults);
114 wfAppendToArrayIfNotDefault( 'hideMinor', (int)$hideMinor, $defaults, $nondefaults );
115 wfAppendToArrayIfNotDefault('namespace', $nameSpace , $defaults, $nondefaults);
116
117 $hookSql = "";
118 if( ! wfRunHooks('BeforeWatchlist', array($nondefaults, $wgUser, &$hookSql)) ) {
119 return;
120 }
121
122 if($nitems == 0) {
123 $wgOut->addWikiMsg( 'nowatchlist' );
124 return;
125 }
126
127 if ( $days <= 0 ) {
128 $andcutoff = '';
129 } else {
130 $andcutoff = "AND rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
131 /*
132 $sql = "SELECT COUNT(*) AS n FROM $page, $revision WHERE rev_timestamp>'$cutoff' AND page_id=rev_page";
133 $res = $dbr->query( $sql, $fname );
134 $s = $dbr->fetchObject( $res );
135 $npages = $s->n;
136 */
137 }
138
139 # If the watchlist is relatively short, it's simplest to zip
140 # down its entirety and then sort the results.
141
142 # If it's relatively long, it may be worth our while to zip
143 # through the time-sorted page list checking for watched items.
144
145 # Up estimate of watched items by 15% to compensate for talk pages...
146
147 # Toggles
148 $andHideOwn = $hideOwn ? "AND (rc_user <> $uid)" : '';
149 $andHideBots = $hideBots ? "AND (rc_bot = 0)" : '';
150 $andHideMinor = $hideMinor ? 'AND rc_minor = 0' : '';
151
152 # Show watchlist header
153 $header = '';
154 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
155 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
156 }
157 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
158 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
159 }
160
161 # Toggle watchlist content (all recent edits or just the latest)
162 if( $wgUser->getOption( 'extendwatchlist' )) {
163 $andLatest='';
164 $limitWatchlist = 'LIMIT ' . intval( $wgUser->getOption( 'wllimit' ) );
165 } else {
166 # Top log Ids for a page are not stored
167 $andLatest = 'AND (rc_this_oldid=page_latest OR rc_type=' . RC_LOG . ') ';
168 $limitWatchlist = '';
169 }
170
171 $header .= wfMsgExt( 'watchlist-details', array( 'parsemag' ), $wgLang->formatNum( $nitems ) );
172 $wgOut->addWikiText( $header );
173
174 # Show a message about slave lag, if applicable
175 if( ( $lag = $dbr->getLag() ) > 0 )
176 $wgOut->showLagWarning( $lag );
177
178 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
179 $wgOut->addHTML( '<form action="' .
180 $specialTitle->escapeLocalUrl() .
181 '" method="post"><input type="submit" name="dummy" value="' .
182 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
183 '" /><input type="hidden" name="reset" value="all" /></form>' .
184 "\n\n" );
185 }
186 if ( $wgShowUpdatedMarker ) {
187 $wltsfield=", ${watchlist}.wl_notificationtimestamp ";
188 }
189 $sql = "SELECT ${recentchanges}.* ${wltsfield}
190 FROM $watchlist,$recentchanges
191 LEFT JOIN $page ON rc_cur_id=page_id
192 WHERE wl_user=$uid
193 AND wl_namespace=rc_namespace
194 AND wl_title=rc_title
195 $andcutoff
196 $andLatest
197 $andHideOwn
198 $andHideBots
199 $andHideMinor
200 $nameSpaceClause
201 $hookSql
202 ORDER BY rc_timestamp DESC
203 $limitWatchlist";
204
205 $res = $dbr->query( $sql, $fname );
206 $numRows = $dbr->numRows( $res );
207
208 /* Start bottom header */
209 $wgOut->addHTML( "<hr />\n" );
210
211 if($days >= 1) {
212 $wgOut->addWikiText( wfMsgExt( 'rcnote', array( 'parseinline' ), $wgLang->formatNum( $numRows ),
213 $wgLang->formatNum( $days ), $wgLang->timeAndDate( wfTimestampNow(), true ) ) . '<br />' , false );
214 } elseif($days > 0) {
215 $wgOut->addWikiText( wfMsgExt( 'wlnote', array( 'parseinline' ), $wgLang->formatNum( $numRows ),
216 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
217 }
218
219 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
220
221 # Spit out some control panel links
222 $thisTitle = SpecialPage::getTitleFor( 'Watchlist' );
223 $skin = $wgUser->getSkin();
224
225 # Hide/show bot edits
226 $label = $hideBots ? wfMsgHtml( 'watchlist-show-bots' ) : wfMsgHtml( 'watchlist-hide-bots' );
227 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
228 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
229
230 # Hide/show own edits
231 $label = $hideOwn ? wfMsgHtml( 'watchlist-show-own' ) : wfMsgHtml( 'watchlist-hide-own' );
232 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
233 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
234
235 # Hide/show minor edits
236 $label = $hideMinor ? wfMsgHtml( 'watchlist-show-minor' ) : wfMsgHtml( 'watchlist-hide-minor' );
237 $linkBits = wfArrayToCGI( array( 'hideMinor' => 1 - (int)$hideMinor ), $nondefaults );
238 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
239
240 $wgOut->addHTML( implode( ' | ', $links ) );
241
242 # Form for namespace filtering
243 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl() ) );
244 $form .= '<p>';
245 $form .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&nbsp;';
246 $form .= Xml::namespaceSelector( $nameSpace, '' ) . '&nbsp;';
247 $form .= Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
248 $form .= Xml::hidden( 'days', $days );
249 if( $hideOwn )
250 $form .= Xml::hidden( 'hideOwn', 1 );
251 if( $hideBots )
252 $form .= Xml::hidden( 'hideBots', 1 );
253 if( $hideMinor )
254 $form .= Xml::hidden( 'hideMinor', 1 );
255 $form .= Xml::closeElement( 'form' );
256 $wgOut->addHtml( $form );
257
258 # If there's nothing to show, stop here
259 if( $numRows == 0 ) {
260 $wgOut->addWikiMsg( 'watchnochange' );
261 return;
262 }
263
264 /* End bottom header */
265
266 /* Do link batch query */
267 $linkBatch = new LinkBatch;
268 while ( $row = $dbr->fetchObject( $res ) ) {
269 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
270 if ( $row->rc_user != 0 ) {
271 $linkBatch->add( NS_USER, $userNameUnderscored );
272 }
273 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
274 }
275 $linkBatch->execute();
276 $dbr->dataSeek( $res, 0 );
277
278 $list = ChangesList::newFromUser( $wgUser );
279
280 $s = $list->beginRecentChangesList();
281 $counter = 1;
282 while ( $obj = $dbr->fetchObject( $res ) ) {
283 # Make RC entry
284 $rc = RecentChange::newFromRow( $obj );
285 $rc->counter = $counter++;
286
287 if ( $wgShowUpdatedMarker ) {
288 $updated = $obj->wl_notificationtimestamp;
289 } else {
290 // Same visual appearance as MW 1.4
291 $updated = true;
292 }
293
294 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
295 $rc->numberofWatchingusers = $dbr->selectField( 'watchlist',
296 'COUNT(*)',
297 array(
298 'wl_namespace' => $obj->rc_namespace,
299 'wl_title' => $obj->rc_title,
300 ),
301 __METHOD__ );
302 } else {
303 $rc->numberofWatchingusers = 0;
304 }
305
306 $s .= $list->recentChangesLine( $rc, $updated );
307 }
308 $s .= $list->endRecentChangesList();
309
310 $dbr->freeResult( $res );
311 $wgOut->addHTML( $s );
312
313 }
314
315 function wlHoursLink( $h, $page, $options = array() ) {
316 global $wgUser, $wgLang, $wgContLang;
317 $sk = $wgUser->getSkin();
318 $s = $sk->makeKnownLink(
319 $wgContLang->specialPage( $page ),
320 $wgLang->formatNum( $h ),
321 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
322 return $s;
323 }
324
325 function wlDaysLink( $d, $page, $options = array() ) {
326 global $wgUser, $wgLang, $wgContLang;
327 $sk = $wgUser->getSkin();
328 $s = $sk->makeKnownLink(
329 $wgContLang->specialPage( $page ),
330 ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
331 wfArrayToCGI( array('days' => $d), $options ) );
332 return $s;
333 }
334
335 /**
336 * Returns html
337 */
338 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
339 $hours = array( 1, 2, 6, 12 );
340 $days = array( 1, 3, 7 );
341 $i = 0;
342 foreach( $hours as $h ) {
343 $hours[$i++] = wlHoursLink( $h, $page, $options );
344 }
345 $i = 0;
346 foreach( $days as $d ) {
347 $days[$i++] = wlDaysLink( $d, $page, $options );
348 }
349 return wfMsgExt('wlshowlast',
350 array('parseinline', 'replaceafter'),
351 implode(' | ', $hours),
352 implode(' | ', $days),
353 wlDaysLink( 0, $page, $options ) );
354 }
355
356 /**
357 * Count the number of items on a user's watchlist
358 *
359 * @param $talk Include talk pages
360 * @return integer
361 */
362 function wlCountItems( &$user, $talk = true ) {
363 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
364
365 # Fetch the raw count
366 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId ), 'wlCountItems' );
367 $row = $dbr->fetchObject( $res );
368 $count = $row->count;
369 $dbr->freeResult( $res );
370
371 # Halve to remove talk pages if needed
372 if( !$talk )
373 $count = floor( $count / 2 );
374
375 return( $count );
376 }