The war on redundant ampersand usage!
[lhc/web/wiklou.git] / includes / SpecialWatchlist.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 *
9 */
10 require_once( '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, $wgContLang;
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 } else {
34 $wgOut->setPageTitle( wfMsg( 'watchlist' ) );
35 $wgOut->setSubtitle( wfMsgWikiHtml( 'watchlistfor', htmlspecialchars( $wgUser->getName() ) ) );
36 }
37
38 if( wlHandleClear( $wgOut, $wgRequest, $par ) ) {
39 return;
40 }
41
42 $defaults = array(
43 /* float */ 'days' => floatval( $wgUser->getOption( 'watchlistdays' ) ), /* 3.0 or 0.5, watch further below */
44 /* bool */ 'hideOwn' => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
45 /* bool */ 'hideBots' => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
46 /* bool */ 'hideMinor' => (int)$wgUser->getBoolOption( 'watchlisthideminor' ),
47 /* ? */ 'namespace' => 'all',
48 );
49
50 extract($defaults);
51
52 # Extract variables from the request, falling back to user preferences or
53 # other default values if these don't exist
54 $prefs['days' ] = floatval( $wgUser->getOption( 'watchlistdays' ) );
55 $prefs['hideown' ] = $wgUser->getBoolOption( 'watchlisthideown' );
56 $prefs['hidebots'] = $wgUser->getBoolOption( 'watchlisthidebots' );
57 $prefs['hideminor'] = $wgUser->getBoolOption( 'watchlisthideminor' );
58
59 # Get query variables
60 $days = $wgRequest->getVal( 'days', $prefs['days'] );
61 $hideOwn = $wgRequest->getBool( 'hideOwn', $prefs['hideown'] );
62 $hideBots = $wgRequest->getBool( 'hideBots', $prefs['hidebots'] );
63 $hideMinor = $wgRequest->getBool( 'hideMinor', $prefs['hideminor'] );
64
65 # Get namespace value, if supplied, and prepare a WHERE fragment
66 $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
67 if( !is_null( $nameSpace ) ) {
68 $nameSpace = intval( $nameSpace );
69 $nameSpaceClause = " AND rc_namespace = $nameSpace";
70 } else {
71 $nameSpace = '';
72 $nameSpaceClause = '';
73 }
74
75 # Watchlist editing
76 $action = $wgRequest->getVal( 'action' );
77 $remove = $wgRequest->getVal( 'remove' );
78 $id = $wgRequest->getArray( 'id' );
79
80 $uid = $wgUser->getID();
81 if( $wgEnotifWatchlist && $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
82 $wgUser->clearAllNotifications( $uid );
83 }
84
85 # Deleting items from watchlist
86 if(($action == 'submit') && isset($remove) && is_array($id)) {
87 $wgOut->addWikiText( wfMsg( 'removingchecked' ) );
88 $wgOut->addHTML( '<p>' );
89 foreach($id as $one) {
90 $t = Title::newFromURL( $one );
91 if( !is_null( $t ) ) {
92 $wl = WatchedItem::fromUserTitle( $wgUser, $t );
93 if( $wl->removeWatch() === false ) {
94 $wgOut->addHTML( wfMsg( 'couldntremove', htmlspecialchars($one) ) . "<br />\n" );
95 } else {
96 wfRunHooks('UnwatchArticle', array(&$wgUser, new Article($t)));
97 $wgOut->addHTML( '(' . htmlspecialchars($one) . ')<br />' );
98 }
99 } else {
100 $wgOut->addHTML( wfMsg( 'iteminvalidname', htmlspecialchars($one) ) . "<br />\n" );
101 }
102 }
103 $wgOut->addHTML( "</p>\n<p>" . wfMsg( 'wldone' ) . "</p>\n" );
104 }
105
106 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
107 list( $page, $watchlist, $recentchanges ) = $dbr->tableNamesN( 'page', 'watchlist', 'recentchanges' );
108
109 $sql = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_user=$uid";
110 $res = $dbr->query( $sql, $fname );
111 $s = $dbr->fetchObject( $res );
112
113 # Patch *** A1 *** (see A2 below)
114 # adjust for page X, talk:page X, which are both stored separately, but treated together
115 $nitems = floor($s->n / 2);
116 # $nitems = $s->n;
117
118 if($nitems == 0) {
119 $wgOut->addWikiText( wfMsg( 'nowatchlist' ) );
120 return;
121 }
122
123 if( is_null($days) || !is_numeric($days) ) {
124 $big = 1000; /* The magical big */
125 if($nitems > $big) {
126 # Set default cutoff shorter
127 $days = $defaults['days'] = (12.0 / 24.0); # 12 hours...
128 } else {
129 $days = $defaults['days']; # default cutoff for shortlisters
130 }
131 } else {
132 $days = floatval($days);
133 }
134
135 // Dump everything here
136 $nondefaults = array();
137
138 wfAppendToArrayIfNotDefault('days' , $days , $defaults, $nondefaults);
139 wfAppendToArrayIfNotDefault('hideOwn' , (int)$hideOwn , $defaults, $nondefaults);
140 wfAppendToArrayIfNotDefault('hideBots' , (int)$hideBots, $defaults, $nondefaults);
141 wfAppendToArrayIfNotDefault( 'hideMinor', (int)$hideMinor, $defaults, $nondefaults );
142 wfAppendToArrayIfNotDefault('namespace', $nameSpace , $defaults, $nondefaults);
143
144 if ( $days <= 0 ) {
145 $andcutoff = '';
146 $npages = wfMsg( 'watchlistall1' );
147 } else {
148 $andcutoff = "AND rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
149 /*
150 $sql = "SELECT COUNT(*) AS n FROM $page, $revision WHERE rev_timestamp>'$cutoff' AND page_id=rev_page";
151 $res = $dbr->query( $sql, $fname );
152 $s = $dbr->fetchObject( $res );
153 $npages = $s->n;
154 */
155 $npages = 40000 * $days;
156 }
157
158 /* Edit watchlist form */
159 if($wgRequest->getBool('edit') || $par == 'edit' ) {
160 $wgOut->addWikiText( wfMsg( 'watchlistcontains', $wgLang->formatNum( $nitems ) ) .
161 "\n\n" . wfMsg( 'watcheditlist' ) );
162
163 $wgOut->addHTML( '<form action=\'' .
164 $specialTitle->escapeLocalUrl( 'action=submit' ) .
165 "' method='post'>\n" );
166
167 # Patch A2
168 # The following was proposed by KTurner 07.11.2004 to T.Gries
169 # $sql = "SELECT distinct (wl_namespace & ~1),wl_title FROM $watchlist WHERE wl_user=$uid";
170 $sql = "SELECT wl_namespace, wl_title, page_is_redirect FROM $watchlist LEFT JOIN $page ON wl_namespace = page_namespace AND wl_title = page_title WHERE wl_user=$uid";
171
172 $res = $dbr->query( $sql, $fname );
173
174 # Batch existence check
175 $linkBatch = new LinkBatch();
176 while( $row = $dbr->fetchObject( $res ) )
177 $linkBatch->addObj( Title::makeTitleSafe( $row->wl_namespace, $row->wl_title ) );
178 $linkBatch->execute();
179
180 if( $dbr->numRows( $res ) > 0 )
181 $dbr->dataSeek( $res, 0 ); # Let's do the time warp again!
182
183 $sk = $wgUser->getSkin();
184
185 $list = array();
186 while( $s = $dbr->fetchObject( $res ) ) {
187 $list[$s->wl_namespace][$s->wl_title] = $s->page_is_redirect;
188 }
189
190 // TODO: Display a TOC
191 foreach($list as $ns => $titles) {
192 if (Namespace::isTalk($ns))
193 continue;
194 if ($ns != NS_MAIN)
195 $wgOut->addHTML( '<h2>' . $wgContLang->getFormattedNsText( $ns ) . '</h2>' );
196 $wgOut->addHTML( '<ul>' );
197 foreach( $titles as $title => $redir ) {
198 $titleObj = Title::makeTitle( $ns, $title );
199 if( is_null( $titleObj ) ) {
200 $wgOut->addHTML(
201 '<!-- bad title "' .
202 htmlspecialchars( $s->wl_title ) . '" in namespace ' . $s->wl_namespace . " -->\n"
203 );
204 } else {
205 global $wgContLang;
206 $toolLinks = array();
207 $pageLink = $sk->makeLinkObj( $titleObj );
208 $toolLinks[] = $sk->makeLinkObj( $titleObj->getTalkPage(), $wgLang->getNsText( NS_TALK ) );
209 if( $titleObj->exists() )
210 $toolLinks[] = $sk->makeKnownLinkObj( $titleObj, wfMsgHtml( 'history_short' ), 'action=history' );
211 $toolLinks = '(' . implode( ' | ', $toolLinks ) . ')';
212 $checkbox = '<input type="checkbox" name="id[]" value="' . htmlspecialchars( $titleObj->getPrefixedText() ) . '" /> ' . ( $wgContLang->isRTL() ? '&rlm;' : '&lrm;' );
213 if( $redir ) {
214 $spanopen = '<span class="watchlistredir">';
215 $spanclosed = '</span>';
216 } else {
217 $spanopen = $spanclosed = '';
218 }
219
220 $wgOut->addHTML( "<li>{$checkbox}{$spanopen}{$pageLink}{$spanclosed} {$toolLinks}</li>\n" );
221 }
222 }
223 $wgOut->addHTML( '</ul>' );
224 }
225 $wgOut->addHTML(
226 wfSubmitButton( wfMsg('removechecked'), array('name' => 'remove') ) .
227 "\n</form>\n"
228 );
229
230 return;
231 }
232
233 # If the watchlist is relatively short, it's simplest to zip
234 # down its entirety and then sort the results.
235
236 # If it's relatively long, it may be worth our while to zip
237 # through the time-sorted page list checking for watched items.
238
239 # Up estimate of watched items by 15% to compensate for talk pages...
240
241 # Toggles
242 $andHideOwn = $hideOwn ? "AND (rc_user <> $uid)" : '';
243 $andHideBots = $hideBots ? "AND (rc_bot = 0)" : '';
244 $andHideMinor = $hideMinor ? 'AND rc_minor = 0' : '';
245
246 # Show watchlist header
247 $header = '';
248 if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
249 $header .= wfMsg( 'wlheader-enotif' ) . "\n";
250 }
251 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
252 $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
253 }
254
255 # Toggle watchlist content (all recent edits or just the latest)
256 if( $wgUser->getOption( 'extendwatchlist' )) {
257 $andLatest='';
258 $limitWatchlist = 'LIMIT ' . intval( $wgUser->getOption( 'wllimit' ) );
259 } else {
260 $andLatest= 'AND rc_this_oldid=page_latest';
261 $limitWatchlist = '';
262 }
263
264 # TODO: Consider removing the third parameter
265 $header .= wfMsgExt( 'watchdetails', array( 'parsemag' ), $wgLang->formatNum( $nitems ),
266 $wgLang->formatNum( $npages ), '',
267 $specialTitle->getFullUrl( 'edit=yes' ) );
268 $wgOut->addWikiText( $header );
269
270 if ( $wgEnotifWatchlist && $wgShowUpdatedMarker ) {
271 $wgOut->addHTML( '<form action="' .
272 $specialTitle->escapeLocalUrl() .
273 '" method="post"><input type="submit" name="dummy" value="' .
274 htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
275 '" /><input type="hidden" name="reset" value="all" /></form>' .
276 "\n\n" );
277 }
278
279 $sql = "SELECT *
280 FROM $watchlist,$recentchanges,$page
281 WHERE wl_user=$uid
282 AND wl_namespace=rc_namespace
283 AND wl_title=rc_title
284 AND rc_cur_id=page_id
285 $andcutoff
286 $andLatest
287 $andHideOwn
288 $andHideBots
289 $andHideMinor
290 $nameSpaceClause
291 ORDER BY rc_timestamp DESC
292 $limitWatchlist";
293
294 $res = $dbr->query( $sql, $fname );
295 $numRows = $dbr->numRows( $res );
296
297 /* Start bottom header */
298 $wgOut->addHTML( "<hr />\n" );
299
300 if($days >= 1) {
301 $wgOut->addWikiText( wfMsg( 'rcnote', $wgLang->formatNum( $numRows ),
302 $wgLang->formatNum( $days ), $wgLang->timeAndDate( wfTimestampNow(), true ) ) . '<br />' , false );
303 } elseif($days > 0) {
304 $wgOut->addWikiText( wfMsg( 'wlnote', $wgLang->formatNum( $numRows ),
305 $wgLang->formatNum( round($days*24) ) ) . '<br />' , false );
306 }
307
308 $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
309
310 # Spit out some control panel links
311 $thisTitle = SpecialPage::getTitleFor( 'Watchlist' );
312 $skin = $wgUser->getSkin();
313
314 # Hide/show bot edits
315 $label = $hideBots ? wfMsgHtml( 'watchlist-show-bots' ) : wfMsgHtml( 'watchlist-hide-bots' );
316 $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
317 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
318
319 # Hide/show own edits
320 $label = $hideOwn ? wfMsgHtml( 'watchlist-show-own' ) : wfMsgHtml( 'watchlist-hide-own' );
321 $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
322 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
323
324 # Hide/show minor edits
325 $label = $hideMinor ? wfMsgHtml( 'watchlist-show-minor' ) : wfMsgHtml( 'watchlist-hide-minor' );
326 $linkBits = wfArrayToCGI( array( 'hideMinor' => 1 - (int)$hideMinor ), $nondefaults );
327 $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
328
329 $wgOut->addHTML( implode( ' | ', $links ) );
330
331 # Form for namespace filtering
332 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl() ) );
333 $form .= '<p>';
334 $form .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&nbsp;';
335 $form .= Xml::namespaceSelector( $nameSpace, '' ) . '&nbsp;';
336 $form .= Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
337 $form .= Xml::hidden( 'days', $days );
338 if( $hideOwn )
339 $form .= Xml::hidden( 'hideOwn', 1 );
340 if( $hideBots )
341 $form .= Xml::hidden( 'hideBots', 1 );
342 if( $hideMinor )
343 $form .= Xml::hidden( 'hideMinor', 1 );
344 $form .= Xml::closeElement( 'form' );
345 $wgOut->addHtml( $form );
346
347 # If there's nothing to show, stop here
348 if( $numRows == 0 ) {
349 $wgOut->addWikiText( wfMsgNoTrans( 'watchnochange' ) );
350 return;
351 }
352
353 /* End bottom header */
354
355 /* Do link batch query */
356 $linkBatch = new LinkBatch;
357 while ( $row = $dbr->fetchObject( $res ) ) {
358 $userNameUnderscored = str_replace( ' ', '_', $row->rc_user_text );
359 if ( $row->rc_user != 0 ) {
360 $linkBatch->add( NS_USER, $userNameUnderscored );
361 }
362 $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
363 }
364 $linkBatch->execute();
365 $dbr->dataSeek( $res, 0 );
366
367 $list = ChangesList::newFromUser( $wgUser );
368
369 $s = $list->beginRecentChangesList();
370 $counter = 1;
371 while ( $obj = $dbr->fetchObject( $res ) ) {
372 # Make RC entry
373 $rc = RecentChange::newFromRow( $obj );
374 $rc->counter = $counter++;
375
376 if ( $wgShowUpdatedMarker ) {
377 $updated = $obj->wl_notificationtimestamp;
378 } else {
379 // Same visual appearance as MW 1.4
380 $updated = true;
381 }
382
383 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
384 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" .$dbr->strencode($obj->page_title). "' AND wl_namespace='{$obj->page_namespace}'" ;
385 $res3 = $dbr->query( $sql3, $fname );
386 $x = $dbr->fetchObject( $res3 );
387 $rc->numberofWatchingusers = $x->n;
388 } else {
389 $rc->numberofWatchingusers = 0;
390 }
391
392 $s .= $list->recentChangesLine( $rc, $updated );
393 }
394 $s .= $list->endRecentChangesList();
395
396 $dbr->freeResult( $res );
397 $wgOut->addHTML( $s );
398
399 }
400
401 function wlHoursLink( $h, $page, $options = array() ) {
402 global $wgUser, $wgLang, $wgContLang;
403 $sk = $wgUser->getSkin();
404 $s = $sk->makeKnownLink(
405 $wgContLang->specialPage( $page ),
406 $wgLang->formatNum( $h ),
407 wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
408 return $s;
409 }
410
411 function wlDaysLink( $d, $page, $options = array() ) {
412 global $wgUser, $wgLang, $wgContLang;
413 $sk = $wgUser->getSkin();
414 $s = $sk->makeKnownLink(
415 $wgContLang->specialPage( $page ),
416 ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
417 wfArrayToCGI( array('days' => $d), $options ) );
418 return $s;
419 }
420
421 /**
422 * Returns html
423 */
424 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
425 $hours = array( 1, 2, 6, 12 );
426 $days = array( 1, 3, 7 );
427 $i = 0;
428 foreach( $hours as $h ) {
429 $hours[$i++] = wlHoursLink( $h, $page, $options );
430 }
431 $i = 0;
432 foreach( $days as $d ) {
433 $days[$i++] = wlDaysLink( $d, $page, $options );
434 }
435 return wfMsgExt('wlshowlast',
436 array('parseinline', 'replaceafter'),
437 implode(' | ', $hours),
438 implode(' | ', $days),
439 wlDaysLink( 0, $page, $options ) );
440 }
441
442 /**
443 * Count the number of items on a user's watchlist
444 *
445 * @param $talk Include talk pages
446 * @return integer
447 */
448 function wlCountItems( &$user, $talk = true ) {
449 $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
450
451 # Fetch the raw count
452 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId ), 'wlCountItems' );
453 $row = $dbr->fetchObject( $res );
454 $count = $row->count;
455 $dbr->freeResult( $res );
456
457 # Halve to remove talk pages if needed
458 if( !$talk )
459 $count = floor( $count / 2 );
460
461 return( $count );
462 }
463
464 /**
465 * Allow the user to clear their watchlist
466 *
467 * @param $out Output object
468 * @param $request Request object
469 * @param $par Parameters passed to the watchlist page
470 * @return bool True if it's been taken care of; false indicates the watchlist
471 * code needs to do something further
472 */
473 function wlHandleClear( &$out, &$request, $par ) {
474 global $wgLang;
475
476 # Check this function has something to do
477 if( $request->getText( 'action' ) == 'clear' || $par == 'clear' ) {
478 global $wgUser;
479 $out->setPageTitle( wfMsgHtml( 'clearwatchlist' ) );
480 $count = wlCountItems( $wgUser );
481 if( $count > 0 ) {
482 # See if we're clearing or confirming
483 if( $request->wasPosted() && $wgUser->matchEditToken( $request->getText( 'token' ), 'clearwatchlist' ) ) {
484 # Clearing, so do it and report the result
485 $dbw = wfGetDB( DB_MASTER );
486 $dbw->delete( 'watchlist', array( 'wl_user' => $wgUser->mId ), 'wlHandleClear' );
487 $out->addWikiText( wfMsgExt( 'watchlistcleardone', array( 'parsemag', 'escape'), $wgLang->formatNum( $count ) ) );
488 $out->returnToMain();
489 } else {
490 # Confirming, so show a form
491 $wlTitle = SpecialPage::getTitleFor( 'Watchlist' );
492 $out->addHTML( wfElement( 'form', array( 'method' => 'post', 'action' => $wlTitle->getLocalUrl( 'action=clear' ) ), NULL ) );
493 $out->addWikiText( wfMsgExt( 'watchlistcount', array( 'parsemag', 'escape'), $wgLang->formatNum( $count ) ) );
494 $out->addWikiText( wfMsg( 'watchlistcleartext' ) );
495 $out->addHTML(
496 wfHidden( 'token', $wgUser->editToken( 'clearwatchlist' ) ) .
497 wfElement( 'input', array( 'type' => 'submit', 'name' => 'submit', 'value' => wfMsgHtml( 'watchlistclearbutton' ) ), '' ) .
498 wfCloseElement( 'form' )
499 );
500 }
501 return( true );
502 } else {
503 # Nothing on the watchlist; nothing to do here
504 $out->addWikiText( wfMsg( 'nowatchlist' ) );
505 $out->returnToMain();
506 return( true );
507 }
508 } else {
509 return( false );
510 }
511 }
512 ?>