SiteStats.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. /**
  3. * Accessors and mutators for the site-wide statistics.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. use Wikimedia\Rdbms\Database;
  23. use Wikimedia\Rdbms\IDatabase;
  24. use MediaWiki\MediaWikiServices;
  25. /**
  26. * Static accessor class for site_stats and related things
  27. */
  28. class SiteStats {
  29. /** @var bool|stdClass */
  30. private static $row;
  31. /** @var bool */
  32. private static $loaded = false;
  33. /** @var int[] */
  34. private static $pageCount = [];
  35. static function unload() {
  36. self::$loaded = false;
  37. }
  38. static function recache() {
  39. self::load( true );
  40. }
  41. /**
  42. * @param bool $recache
  43. */
  44. static function load( $recache = false ) {
  45. if ( self::$loaded && !$recache ) {
  46. return;
  47. }
  48. self::$row = self::loadAndLazyInit();
  49. # This code is somewhat schema-agnostic, because I'm changing it in a minor release -- TS
  50. if ( !isset( self::$row->ss_total_pages ) && self::$row->ss_total_pages == -1 ) {
  51. # Update schema
  52. $u = new SiteStatsUpdate( 0, 0, 0 );
  53. $u->doUpdate();
  54. self::$row = self::doLoad( wfGetDB( DB_REPLICA ) );
  55. }
  56. self::$loaded = true;
  57. }
  58. /**
  59. * @return bool|stdClass
  60. */
  61. static function loadAndLazyInit() {
  62. global $wgMiserMode;
  63. wfDebug( __METHOD__ . ": reading site_stats from replica DB\n" );
  64. $row = self::doLoad( wfGetDB( DB_REPLICA ) );
  65. if ( !self::isSane( $row ) ) {
  66. $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
  67. if ( $lb->hasOrMadeRecentMasterChanges() ) {
  68. // Might have just been initialized during this request? Underflow?
  69. wfDebug( __METHOD__ . ": site_stats damaged or missing on replica DB\n" );
  70. $row = self::doLoad( wfGetDB( DB_MASTER ) );
  71. }
  72. }
  73. if ( !$wgMiserMode && !self::isSane( $row ) ) {
  74. // Normally the site_stats table is initialized at install time.
  75. // Some manual construction scenarios may leave the table empty or
  76. // broken, however, for instance when importing from a dump into a
  77. // clean schema with mwdumper.
  78. wfDebug( __METHOD__ . ": initializing damaged or missing site_stats\n" );
  79. SiteStatsInit::doAllAndCommit( wfGetDB( DB_REPLICA ) );
  80. $row = self::doLoad( wfGetDB( DB_MASTER ) );
  81. }
  82. if ( !self::isSane( $row ) ) {
  83. wfDebug( __METHOD__ . ": site_stats persistently nonsensical o_O\n" );
  84. }
  85. return $row;
  86. }
  87. /**
  88. * @param IDatabase $db
  89. * @return bool|stdClass
  90. */
  91. static function doLoad( $db ) {
  92. return $db->selectRow( 'site_stats', [
  93. 'ss_row_id',
  94. 'ss_total_edits',
  95. 'ss_good_articles',
  96. 'ss_total_pages',
  97. 'ss_users',
  98. 'ss_active_users',
  99. 'ss_images',
  100. ], [], __METHOD__ );
  101. }
  102. /**
  103. * Return the total number of page views. Except we don't track those anymore.
  104. * Stop calling this function, it will be removed some time in the future. It's
  105. * kept here simply to prevent fatal errors.
  106. *
  107. * @deprecated since 1.25
  108. * @return int
  109. */
  110. static function views() {
  111. wfDeprecated( __METHOD__, '1.25' );
  112. return 0;
  113. }
  114. /**
  115. * @return int
  116. */
  117. static function edits() {
  118. self::load();
  119. return self::$row->ss_total_edits;
  120. }
  121. /**
  122. * @return int
  123. */
  124. static function articles() {
  125. self::load();
  126. return self::$row->ss_good_articles;
  127. }
  128. /**
  129. * @return int
  130. */
  131. static function pages() {
  132. self::load();
  133. return self::$row->ss_total_pages;
  134. }
  135. /**
  136. * @return int
  137. */
  138. static function users() {
  139. self::load();
  140. return self::$row->ss_users;
  141. }
  142. /**
  143. * @return int
  144. */
  145. static function activeUsers() {
  146. self::load();
  147. return self::$row->ss_active_users;
  148. }
  149. /**
  150. * @return int
  151. */
  152. static function images() {
  153. self::load();
  154. return self::$row->ss_images;
  155. }
  156. /**
  157. * Find the number of users in a given user group.
  158. * @param string $group Name of group
  159. * @return int
  160. */
  161. static function numberingroup( $group ) {
  162. $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
  163. return $cache->getWithSetCallback(
  164. $cache->makeKey( 'SiteStats', 'groupcounts', $group ),
  165. $cache::TTL_HOUR,
  166. function ( $oldValue, &$ttl, array &$setOpts ) use ( $group ) {
  167. $dbr = wfGetDB( DB_REPLICA );
  168. $setOpts += Database::getCacheSetOptions( $dbr );
  169. return $dbr->selectField(
  170. 'user_groups',
  171. 'COUNT(*)',
  172. [
  173. 'ug_group' => $group,
  174. 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() )
  175. ],
  176. __METHOD__
  177. );
  178. },
  179. [ 'pcTTL' => $cache::TTL_PROC_LONG ]
  180. );
  181. }
  182. /**
  183. * Total number of jobs in the job queue.
  184. * @return int
  185. */
  186. static function jobs() {
  187. $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
  188. return $cache->getWithSetCallback(
  189. $cache->makeKey( 'SiteStats', 'jobscount' ),
  190. $cache::TTL_MINUTE,
  191. function ( $oldValue, &$ttl, array &$setOpts ) {
  192. try{
  193. $jobs = array_sum( JobQueueGroup::singleton()->getQueueSizes() );
  194. } catch ( JobQueueError $e ) {
  195. $jobs = 0;
  196. }
  197. return $jobs;
  198. },
  199. [ 'pcTTL' => $cache::TTL_PROC_LONG ]
  200. );
  201. }
  202. /**
  203. * @param int $ns
  204. *
  205. * @return int
  206. */
  207. static function pagesInNs( $ns ) {
  208. if ( !isset( self::$pageCount[$ns] ) ) {
  209. $dbr = wfGetDB( DB_REPLICA );
  210. self::$pageCount[$ns] = (int)$dbr->selectField(
  211. 'page',
  212. 'COUNT(*)',
  213. [ 'page_namespace' => $ns ],
  214. __METHOD__
  215. );
  216. }
  217. return self::$pageCount[$ns];
  218. }
  219. /**
  220. * Is the provided row of site stats sane, or should it be regenerated?
  221. *
  222. * Checks only fields which are filled by SiteStatsInit::refresh.
  223. *
  224. * @param bool|object $row
  225. *
  226. * @return bool
  227. */
  228. private static function isSane( $row ) {
  229. if ( $row === false
  230. || $row->ss_total_pages < $row->ss_good_articles
  231. || $row->ss_total_edits < $row->ss_total_pages
  232. ) {
  233. return false;
  234. }
  235. // Now check for underflow/overflow
  236. foreach ( [
  237. 'ss_total_edits',
  238. 'ss_good_articles',
  239. 'ss_total_pages',
  240. 'ss_users',
  241. 'ss_images',
  242. ] as $member ) {
  243. if ( $row->$member > 2000000000 || $row->$member < 0 ) {
  244. return false;
  245. }
  246. }
  247. return true;
  248. }
  249. }
  250. /**
  251. * Class designed for counting of stats.
  252. */
  253. class SiteStatsInit {
  254. // Database connection
  255. private $db;
  256. // Various stats
  257. private $mEdits = null, $mArticles = null, $mPages = null;
  258. private $mUsers = null, $mFiles = null;
  259. /**
  260. * @param bool|IDatabase $database
  261. * - bool: Whether to use the master DB
  262. * - IDatabase: Database connection to use
  263. */
  264. public function __construct( $database = false ) {
  265. if ( $database instanceof IDatabase ) {
  266. $this->db = $database;
  267. } elseif ( $database ) {
  268. $this->db = wfGetDB( DB_MASTER );
  269. } else {
  270. $this->db = wfGetDB( DB_REPLICA, 'vslow' );
  271. }
  272. }
  273. /**
  274. * Count the total number of edits
  275. * @return int
  276. */
  277. public function edits() {
  278. $this->mEdits = $this->db->selectField( 'revision', 'COUNT(*)', '', __METHOD__ );
  279. $this->mEdits += $this->db->selectField( 'archive', 'COUNT(*)', '', __METHOD__ );
  280. return $this->mEdits;
  281. }
  282. /**
  283. * Count pages in article space(s)
  284. * @return int
  285. */
  286. public function articles() {
  287. global $wgArticleCountMethod;
  288. $tables = [ 'page' ];
  289. $conds = [
  290. 'page_namespace' => MWNamespace::getContentNamespaces(),
  291. 'page_is_redirect' => 0,
  292. ];
  293. if ( $wgArticleCountMethod == 'link' ) {
  294. $tables[] = 'pagelinks';
  295. $conds[] = 'pl_from=page_id';
  296. } elseif ( $wgArticleCountMethod == 'comma' ) {
  297. // To make a correct check for this, we would need, for each page,
  298. // to load the text, maybe uncompress it, maybe decode it and then
  299. // check if there's one comma.
  300. // But one thing we are sure is that if the page is empty, it can't
  301. // contain a comma :)
  302. $conds[] = 'page_len > 0';
  303. }
  304. $this->mArticles = $this->db->selectField( $tables, 'COUNT(DISTINCT page_id)',
  305. $conds, __METHOD__ );
  306. return $this->mArticles;
  307. }
  308. /**
  309. * Count total pages
  310. * @return int
  311. */
  312. public function pages() {
  313. $this->mPages = $this->db->selectField( 'page', 'COUNT(*)', '', __METHOD__ );
  314. return $this->mPages;
  315. }
  316. /**
  317. * Count total users
  318. * @return int
  319. */
  320. public function users() {
  321. $this->mUsers = $this->db->selectField( 'user', 'COUNT(*)', '', __METHOD__ );
  322. return $this->mUsers;
  323. }
  324. /**
  325. * Count total files
  326. * @return int
  327. */
  328. public function files() {
  329. $this->mFiles = $this->db->selectField( 'image', 'COUNT(*)', '', __METHOD__ );
  330. return $this->mFiles;
  331. }
  332. /**
  333. * Do all updates and commit them. More or less a replacement
  334. * for the original initStats, but without output.
  335. *
  336. * @param IDatabase|bool $database
  337. * - bool: Whether to use the master DB
  338. * - IDatabase: Database connection to use
  339. * @param array $options Array of options, may contain the following values
  340. * - activeUsers bool: Whether to update the number of active users (default: false)
  341. */
  342. public static function doAllAndCommit( $database, array $options = [] ) {
  343. $options += [ 'update' => false, 'activeUsers' => false ];
  344. // Grab the object and count everything
  345. $counter = new SiteStatsInit( $database );
  346. $counter->edits();
  347. $counter->articles();
  348. $counter->pages();
  349. $counter->users();
  350. $counter->files();
  351. $counter->refresh();
  352. // Count active users if need be
  353. if ( $options['activeUsers'] ) {
  354. SiteStatsUpdate::cacheUpdate( wfGetDB( DB_MASTER ) );
  355. }
  356. }
  357. /**
  358. * Refresh site_stats
  359. */
  360. public function refresh() {
  361. $values = [
  362. 'ss_row_id' => 1,
  363. 'ss_total_edits' => ( $this->mEdits === null ? $this->edits() : $this->mEdits ),
  364. 'ss_good_articles' => ( $this->mArticles === null ? $this->articles() : $this->mArticles ),
  365. 'ss_total_pages' => ( $this->mPages === null ? $this->pages() : $this->mPages ),
  366. 'ss_users' => ( $this->mUsers === null ? $this->users() : $this->mUsers ),
  367. 'ss_images' => ( $this->mFiles === null ? $this->files() : $this->mFiles ),
  368. ];
  369. $dbw = wfGetDB( DB_MASTER );
  370. $dbw->upsert( 'site_stats', $values, [ 'ss_row_id' ], $values, __METHOD__ );
  371. }
  372. }