43 protected static function load() {
44 if ( self::$row ===
null ) {
53 $config = MediaWikiServices::getInstance()->getMainConfig();
57 wfDebug( __METHOD__ .
": reading site_stats from replica DB" );
58 $row = self::doLoadFromDB(
$dbr );
60 if ( !self::isRowSensible( $row ) && $lb->hasOrMadeRecentPrimaryChanges() ) {
62 wfDebug( __METHOD__ .
": site_stats damaged or missing on replica DB" );
63 $row = self::doLoadFromDB( $lb->getConnectionRef(
DB_PRIMARY ) );
66 if ( !self::isRowSensible( $row ) ) {
67 if ( $config->get( MainConfigNames::MiserMode ) ) {
76 wfDebug( __METHOD__ .
": initializing damaged or missing site_stats" );
80 $row = self::doLoadFromDB( $lb->getConnectionRef(
DB_PRIMARY ) );
83 if ( !self::isRowSensible( $row ) ) {
84 wfDebug( __METHOD__ .
": site_stats persistently nonsensical o_O" );
86 $row = self::salvageIncorrectRow( $row );
95 public static function edits() {
98 return (
int)self::$row->ss_total_edits;
107 return (
int)self::$row->ss_good_articles;
116 return (
int)self::$row->ss_total_pages;
125 return (
int)self::$row->ss_users;
134 return (
int)self::$row->ss_active_users;
143 return (
int)self::$row->ss_images;
152 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
155 return $cache->getWithSetCallback(
156 $cache->makeKey(
'SiteStats',
'groupcounts', $group ),
158 function ( $oldValue, &$ttl, array &$setOpts ) use ( $group, $fname ) {
160 $setOpts += Database::getCacheSetOptions(
$dbr );
161 return (
int)
$dbr->newSelectQueryBuilder()
162 ->select(
'COUNT(*)' )
163 ->from(
'user_groups' )
166 'ug_group' => $group,
167 'ug_expiry IS NULL OR ug_expiry >= ' .
$dbr->addQuotes(
$dbr->timestamp() )
173 [
'pcTTL' => $cache::TTL_PROC_LONG ]
181 public static function jobs() {
182 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
184 return $cache->getWithSetCallback(
185 $cache->makeKey(
'SiteStats',
'jobscount' ),
187 static function ( $oldValue, &$ttl, array &$setOpts ) {
189 $jobs = array_sum( MediaWikiServices::getInstance()->getJobQueueGroup()->getQueueSizes() );
195 [
'pcTTL' => $cache::TTL_PROC_LONG ]
204 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
207 return $cache->getWithSetCallback(
208 $cache->makeKey(
'SiteStats',
'page-in-namespace', $ns ),
210 function ( $oldValue, &$ttl, array &$setOpts ) use ( $ns, $fname ) {
212 $setOpts += Database::getCacheSetOptions(
$dbr );
214 return (
int)
$dbr->selectField(
217 [
'page_namespace' => $ns ],
221 [
'pcTTL' => $cache::TTL_PROC_LONG ]
243 private static function doLoadFromDB(
IDatabase $db ) {
247 ->from(
'site_stats' )
248 ->caller( __METHOD__ )
250 $finalRow =
new stdClass();
251 foreach ( $rows as $row ) {
252 foreach ( $fields as $field ) {
253 $finalRow->$field = $finalRow->$field ?? 0;
254 if ( $row->$field ) {
255 $finalRow->$field += $row->$field;
271 private static function isRowSensible( $row ) {
273 || $row->ss_total_pages < $row->ss_good_articles
274 || $row->ss_total_edits < $row->ss_total_pages
286 if ( $row->$member < 0 ) {
298 private static function salvageIncorrectRow( $row ) {
299 $map = $row ? (array)$row : [];
301 $map += array_fill_keys( self::selectFields(), 0 );
303 foreach ( $map as $field => $value ) {
304 $map[$field] = max( 0, $value );
313 private static function getLB() {
314 return MediaWikiServices::getInstance()->getDBLoadBalancer();
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
A class containing constants representing the names of configuration variables.
static doAllAndCommit( $database, array $options=[])
Do all updates and commit them.
static doPlaceholderInit()
Insert a dummy row with all zeroes if no row is present.
Static accessor class for site_stats and related things.
static jobs()
Total number of jobs in the job queue.
static unload()
Trigger a reload next time a field is accessed.
static numberingroup( $group)
Find the number of users in a given user group.