SpecialWatchlist.php 15 KB

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