Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Total user count should return a count for all wikis including delete…
…d wikis
  • Loading branch information
rosalieper committed Dec 17, 2025
commit 16e915d8ae3f67e5308e27a197532d8ad9d40f32
15 changes: 1 addition & 14 deletions app/Metrics/App/WikiMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function saveMetrics(Wiki $wiki): void {
$quarterlyActions = $this->getNumberOfActions(self::INTERVAL_QUARTERLY);
$numberOfEntities = $this->getNumberOfEntities();
$monthlyNumberOfUsersPerActivityType = $this->getNumberOfUsersPerActivityType();
$numberOfUsersPerWiki = $this->getTotalUserCount();
$numberOfUsersPerWiki = $wiki->wikiSiteStats()->first()->users ?? 0;

$dailyMetrics = new WikiDailyMetrics([
'id' => $wiki->id . '_' . date('Y-m-d'),
Expand Down Expand Up @@ -212,17 +212,4 @@ private function getNumberOfEntities(): array {

return $result;
}

private function getTotalUserCount() {
$wikiDb = $this->wiki->wikiDb;
$tableUser = "{$wikiDb->name}.{$wikiDb->prefix}_user";
$query = "SELECT COUNT(*) AS total_users FROM $tableUser";
$manager = app()->db;
$manager->purge('mw');
$conn = $manager->connection('mw');
$pdo = $conn->getPdo();
$result = $pdo->query($query)->fetch(PDO::FETCH_ASSOC);

return $result['total_users'] ?? null;
}
}
59 changes: 0 additions & 59 deletions tests/Metrics/WikiMetricsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,63 +363,4 @@ public function testSavesEntityCountsCorrectly($expectedItemCount, $expectedProp
'entity_schema_count' => $expectedEntitySchemaCount, // redirects should be ignored
]);
}

public function testSavesTotalUserCountCorrectly() {
$wiki = Wiki::factory()->create([
'domain' => 'usercounttest.wikibase.cloud',
]);

$users = [
[
'user_name' => 'user1',
'user_real_name' => 'user1',
'user_password' => 'user1',
'user_new_password' => 'user1',
'user_email' => 'user1@email.com',
'user_touched' => random_bytes(10),
],
[
'user_name' => 'user2',
'user_real_name' => 'user2',
'user_password' => 'user2',
'user_new_password' => 'user2',
'user_email' => 'user2@email.com',
'user_touched' => random_bytes(10),
],
];
$wikiDb = WikiDb::first();
$wikiDb->update(['wiki_id' => $wiki->id]);

$tableUser = $wikiDb->name . '.' . $wikiDb->prefix . '_user';
Schema::dropIfExists($tableUser);
Schema::create($tableUser, function (Blueprint $table) {
$table->increments('user_id');
$table->string('user_name');
$table->string('user_real_name')->default(0);
$table->string('user_password', 255);
$table->string('user_new_password');
$table->string('user_email');
$table->binary('user_touched');
});

// Insert dummy data
DB::table($tableUser)->insert($users);
WikiDailyMetrics::create([
'id' => $wiki->id . '_' . now()->subDay()->toDateString(),
'wiki_id' => $wiki->id,
'date' => now()->subDay()->toDateString(),
'pages' => 0,
'is_deleted' => 0,
]);

(new WikiMetrics)->saveMetrics($wiki);

// clean up after the test
$wiki->forceDelete();

$this->assertDatabaseHas('wiki_daily_metrics', [
'wiki_id' => $wiki->id,
'total_user_count' => count($users),
]);
}
}
Loading