SiteStats.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. use Wikimedia\Rdbms\LoadBalancer;
  26. /**
  27. * Static accessor class for site_stats and related things
  28. */
  29. class SiteStats {
  30. /** @var stdClass */
  31. private static $row;
  32. /**
  33. * Trigger a reload next time a field is accessed
  34. */
  35. public static function unload() {
  36. self::$row = null;
  37. }
  38. protected static function load() {
  39. if ( self::$row === null ) {
  40. self::$row = self::loadAndLazyInit();
  41. }
  42. }
  43. /**
  44. * @return stdClass
  45. */
  46. protected static function loadAndLazyInit() {
  47. $config = MediaWikiServices::getInstance()->getMainConfig();
  48. $lb = self::getLB();
  49. $dbr = $lb->getConnectionRef( DB_REPLICA );
  50. wfDebug( __METHOD__ . ": reading site_stats from replica DB\n" );
  51. $row = self::doLoadFromDB( $dbr );
  52. if ( !self::isRowSane( $row ) && $lb->hasOrMadeRecentMasterChanges() ) {
  53. // Might have just been initialized during this request? Underflow?
  54. wfDebug( __METHOD__ . ": site_stats damaged or missing on replica DB\n" );
  55. $row = self::doLoadFromDB( $lb->getConnectionRef( DB_MASTER ) );
  56. }
  57. if ( !self::isRowSane( $row ) ) {
  58. if ( $config->get( 'MiserMode' ) ) {
  59. // Start off with all zeroes, assuming that this is a new wiki or any
  60. // repopulations where done manually via script.
  61. SiteStatsInit::doPlaceholderInit();
  62. } else {
  63. // Normally the site_stats table is initialized at install time.
  64. // Some manual construction scenarios may leave the table empty or
  65. // broken, however, for instance when importing from a dump into a
  66. // clean schema with mwdumper.
  67. wfDebug( __METHOD__ . ": initializing damaged or missing site_stats\n" );
  68. SiteStatsInit::doAllAndCommit( $dbr );
  69. }
  70. $row = self::doLoadFromDB( $lb->getConnectionRef( DB_MASTER ) );
  71. }
  72. if ( !self::isRowSane( $row ) ) {
  73. wfDebug( __METHOD__ . ": site_stats persistently nonsensical o_O\n" );
  74. // Always return a row-like object
  75. $row = self::salvageInsaneRow( $row );
  76. }
  77. return $row;
  78. }
  79. /**
  80. * @return int
  81. */
  82. public static function edits() {
  83. self::load();
  84. return (int)self::$row->ss_total_edits;
  85. }
  86. /**
  87. * @return int
  88. */
  89. public static function articles() {
  90. self::load();
  91. return (int)self::$row->ss_good_articles;
  92. }
  93. /**
  94. * @return int
  95. */
  96. public static function pages() {
  97. self::load();
  98. return (int)self::$row->ss_total_pages;
  99. }
  100. /**
  101. * @return int
  102. */
  103. public static function users() {
  104. self::load();
  105. return (int)self::$row->ss_users;
  106. }
  107. /**
  108. * @return int
  109. */
  110. public static function activeUsers() {
  111. self::load();
  112. return (int)self::$row->ss_active_users;
  113. }
  114. /**
  115. * @return int
  116. */
  117. public static function images() {
  118. self::load();
  119. return (int)self::$row->ss_images;
  120. }
  121. /**
  122. * Find the number of users in a given user group.
  123. * @param string $group Name of group
  124. * @return int
  125. */
  126. public static function numberingroup( $group ) {
  127. $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
  128. $fname = __METHOD__;
  129. return $cache->getWithSetCallback(
  130. $cache->makeKey( 'SiteStats', 'groupcounts', $group ),
  131. $cache::TTL_HOUR,
  132. function ( $oldValue, &$ttl, array &$setOpts ) use ( $group, $fname ) {
  133. $dbr = self::getLB()->getConnectionRef( DB_REPLICA );
  134. $setOpts += Database::getCacheSetOptions( $dbr );
  135. return (int)$dbr->selectField(
  136. 'user_groups',
  137. 'COUNT(*)',
  138. [
  139. 'ug_group' => $group,
  140. 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() )
  141. ],
  142. $fname
  143. );
  144. },
  145. [ 'pcTTL' => $cache::TTL_PROC_LONG ]
  146. );
  147. }
  148. /**
  149. * Total number of jobs in the job queue.
  150. * @return int
  151. */
  152. public static function jobs() {
  153. $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
  154. return $cache->getWithSetCallback(
  155. $cache->makeKey( 'SiteStats', 'jobscount' ),
  156. $cache::TTL_MINUTE,
  157. function ( $oldValue, &$ttl, array &$setOpts ) {
  158. try{
  159. $jobs = array_sum( JobQueueGroup::singleton()->getQueueSizes() );
  160. } catch ( JobQueueError $e ) {
  161. $jobs = 0;
  162. }
  163. return $jobs;
  164. },
  165. [ 'pcTTL' => $cache::TTL_PROC_LONG ]
  166. );
  167. }
  168. /**
  169. * @param int $ns
  170. * @return int
  171. */
  172. public static function pagesInNs( $ns ) {
  173. $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
  174. $fname = __METHOD__;
  175. return $cache->getWithSetCallback(
  176. $cache->makeKey( 'SiteStats', 'page-in-namespace', $ns ),
  177. $cache::TTL_HOUR,
  178. function ( $oldValue, &$ttl, array &$setOpts ) use ( $ns, $fname ) {
  179. $dbr = self::getLB()->getConnectionRef( DB_REPLICA );
  180. $setOpts += Database::getCacheSetOptions( $dbr );
  181. return (int)$dbr->selectField(
  182. 'page',
  183. 'COUNT(*)',
  184. [ 'page_namespace' => $ns ],
  185. $fname
  186. );
  187. },
  188. [ 'pcTTL' => $cache::TTL_PROC_LONG ]
  189. );
  190. }
  191. /**
  192. * @return array
  193. */
  194. public static function selectFields() {
  195. return [
  196. 'ss_total_edits',
  197. 'ss_good_articles',
  198. 'ss_total_pages',
  199. 'ss_users',
  200. 'ss_active_users',
  201. 'ss_images',
  202. ];
  203. }
  204. /**
  205. * @param IDatabase $db
  206. * @return stdClass|bool
  207. */
  208. private static function doLoadFromDB( IDatabase $db ) {
  209. return $db->selectRow(
  210. 'site_stats',
  211. self::selectFields(),
  212. [ 'ss_row_id' => 1 ],
  213. __METHOD__
  214. );
  215. }
  216. /**
  217. * Is the provided row of site stats sane, or should it be regenerated?
  218. *
  219. * Checks only fields which are filled by SiteStatsInit::refresh.
  220. *
  221. * @param bool|object $row
  222. * @return bool
  223. */
  224. private static function isRowSane( $row ) {
  225. if ( $row === false
  226. || $row->ss_total_pages < $row->ss_good_articles
  227. || $row->ss_total_edits < $row->ss_total_pages
  228. ) {
  229. return false;
  230. }
  231. // Now check for underflow/overflow
  232. foreach ( [
  233. 'ss_total_edits',
  234. 'ss_good_articles',
  235. 'ss_total_pages',
  236. 'ss_users',
  237. 'ss_images',
  238. ] as $member ) {
  239. if ( $row->$member < 0 ) {
  240. return false;
  241. }
  242. }
  243. return true;
  244. }
  245. /**
  246. * @param stdClass|bool $row
  247. * @return stdClass
  248. */
  249. private static function salvageInsaneRow( $row ) {
  250. $map = $row ? (array)$row : [];
  251. // Fill in any missing values with zero
  252. $map += array_fill_keys( self::selectFields(), 0 );
  253. // Convert negative values to zero
  254. foreach ( $map as $field => $value ) {
  255. $map[$field] = max( 0, $value );
  256. }
  257. return (object)$row;
  258. }
  259. /**
  260. * @return LoadBalancer
  261. */
  262. private static function getLB() {
  263. return MediaWikiServices::getInstance()->getDBLoadBalancer();
  264. }
  265. }