SiteStats.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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->getConnection( 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->getConnection( 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->getConnection( 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. return $cache->getWithSetCallback(
  129. $cache->makeKey( 'SiteStats', 'groupcounts', $group ),
  130. $cache::TTL_HOUR,
  131. function ( $oldValue, &$ttl, array &$setOpts ) use ( $group ) {
  132. $dbr = self::getLB()->getConnection( DB_REPLICA );
  133. $setOpts += Database::getCacheSetOptions( $dbr );
  134. return (int)$dbr->selectField(
  135. 'user_groups',
  136. 'COUNT(*)',
  137. [
  138. 'ug_group' => $group,
  139. 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() )
  140. ],
  141. __METHOD__
  142. );
  143. },
  144. [ 'pcTTL' => $cache::TTL_PROC_LONG ]
  145. );
  146. }
  147. /**
  148. * Total number of jobs in the job queue.
  149. * @return int
  150. */
  151. public static function jobs() {
  152. $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
  153. return $cache->getWithSetCallback(
  154. $cache->makeKey( 'SiteStats', 'jobscount' ),
  155. $cache::TTL_MINUTE,
  156. function ( $oldValue, &$ttl, array &$setOpts ) {
  157. try{
  158. $jobs = array_sum( JobQueueGroup::singleton()->getQueueSizes() );
  159. } catch ( JobQueueError $e ) {
  160. $jobs = 0;
  161. }
  162. return $jobs;
  163. },
  164. [ 'pcTTL' => $cache::TTL_PROC_LONG ]
  165. );
  166. }
  167. /**
  168. * @param int $ns
  169. * @return int
  170. */
  171. public static function pagesInNs( $ns ) {
  172. $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
  173. return $cache->getWithSetCallback(
  174. $cache->makeKey( 'SiteStats', 'page-in-namespace', $ns ),
  175. $cache::TTL_HOUR,
  176. function ( $oldValue, &$ttl, array &$setOpts ) use ( $ns ) {
  177. $dbr = self::getLB()->getConnection( DB_REPLICA );
  178. $setOpts += Database::getCacheSetOptions( $dbr );
  179. return (int)$dbr->selectField(
  180. 'page',
  181. 'COUNT(*)',
  182. [ 'page_namespace' => $ns ],
  183. __METHOD__
  184. );
  185. },
  186. [ 'pcTTL' => $cache::TTL_PROC_LONG ]
  187. );
  188. }
  189. /**
  190. * @return array
  191. */
  192. public static function selectFields() {
  193. return [
  194. 'ss_total_edits',
  195. 'ss_good_articles',
  196. 'ss_total_pages',
  197. 'ss_users',
  198. 'ss_active_users',
  199. 'ss_images',
  200. ];
  201. }
  202. /**
  203. * @param IDatabase $db
  204. * @return stdClass|bool
  205. */
  206. private static function doLoadFromDB( IDatabase $db ) {
  207. return $db->selectRow(
  208. 'site_stats',
  209. self::selectFields(),
  210. [ 'ss_row_id' => 1 ],
  211. __METHOD__
  212. );
  213. }
  214. /**
  215. * Is the provided row of site stats sane, or should it be regenerated?
  216. *
  217. * Checks only fields which are filled by SiteStatsInit::refresh.
  218. *
  219. * @param bool|object $row
  220. * @return bool
  221. */
  222. private static function isRowSane( $row ) {
  223. if ( $row === false
  224. || $row->ss_total_pages < $row->ss_good_articles
  225. || $row->ss_total_edits < $row->ss_total_pages
  226. ) {
  227. return false;
  228. }
  229. // Now check for underflow/overflow
  230. foreach ( [
  231. 'ss_total_edits',
  232. 'ss_good_articles',
  233. 'ss_total_pages',
  234. 'ss_users',
  235. 'ss_images',
  236. ] as $member ) {
  237. if ( $row->$member < 0 ) {
  238. return false;
  239. }
  240. }
  241. return true;
  242. }
  243. /**
  244. * @param stdClass|bool $row
  245. * @return stdClass
  246. */
  247. private static function salvageInsaneRow( $row ) {
  248. $map = $row ? (array)$row : [];
  249. // Fill in any missing values with zero
  250. $map += array_fill_keys( self::selectFields(), 0 );
  251. // Convert negative values to zero
  252. foreach ( $map as $field => $value ) {
  253. $map[$field] = max( 0, $value );
  254. }
  255. return (object)$row;
  256. }
  257. /**
  258. * @return LoadBalancer
  259. */
  260. private static function getLB() {
  261. return MediaWikiServices::getInstance()->getDBLoadBalancer();
  262. }
  263. }