From 2844ef7307e91e8e5650481fde0557dfcfc97c94 Mon Sep 17 00:00:00 2001 From: MusikAnimal Date: Tue, 16 Jan 2024 00:20:21 -0500 Subject: [PATCH] Always JOIN on page table to avoid counting orphan revisions Bug: T355027 --- src/Repository/EditCounterRepository.php | 23 ++++++++++++++++++- .../SimpleEditCounterRepository.php | 5 ++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Repository/EditCounterRepository.php b/src/Repository/EditCounterRepository.php index 382e760ed..0c9b71df0 100644 --- a/src/Repository/EditCounterRepository.php +++ b/src/Repository/EditCounterRepository.php @@ -57,6 +57,10 @@ public function getPairData(Project $project, User $user): array // Prepare the queries and execute them. $archiveTable = $project->getTableName('archive'); $revisionTable = $project->getTableName('revision'); + $pageTable = $project->getTableName('page'); + + // Always JOIN on page, see T355027 + $pageJoin = "JOIN $pageTable ON rev_page = page_id"; if ($user->isIpRange()) { $ipcTable = $project->getTableName('ip_changes'); @@ -86,26 +90,32 @@ public function getPairData(Project $project, User $user): array -- Revision counts. SELECT 'live' AS `key`, COUNT(rev_id) AS val FROM $revisionTable + $pageJoin $ipcJoin WHERE $whereClause ) UNION ( SELECT 'day' AS `key`, COUNT(rev_id) AS val FROM $revisionTable + $pageJoin $ipcJoin WHERE $whereClause AND rev_timestamp >= DATE_SUB(NOW(), INTERVAL 1 DAY) ) UNION ( SELECT 'week' AS `key`, COUNT(rev_id) AS val FROM $revisionTable + $pageJoin $ipcJoin WHERE $whereClause AND rev_timestamp >= DATE_SUB(NOW(), INTERVAL 1 WEEK) ) UNION ( SELECT 'month' AS `key`, COUNT(rev_id) AS val FROM $revisionTable + $pageJoin $ipcJoin WHERE $whereClause AND rev_timestamp >= DATE_SUB(NOW(), INTERVAL 1 MONTH) ) UNION ( SELECT 'year' AS `key`, COUNT(rev_id) AS val FROM $revisionTable + $pageJoin $ipcJoin WHERE $whereClause AND rev_timestamp >= DATE_SUB(NOW(), INTERVAL 1 YEAR) ) UNION ( SELECT 'minor' AS `key`, COUNT(rev_id) AS val FROM $revisionTable + $pageJoin $ipcJoin WHERE $whereClause AND rev_minor_edit = 1 @@ -113,11 +123,13 @@ public function getPairData(Project $project, User $user): array ) UNION ( SELECT 'edited-live' AS `key`, COUNT(DISTINCT rev_page) AS `val` FROM $revisionTable + $pageJoin $ipcJoin WHERE $whereClause ) UNION ( SELECT 'created-live' AS `key`, COUNT(DISTINCT rev_page) AS `val` FROM $revisionTable + $pageJoin $ipcJoin WHERE $whereClause AND rev_parent_id = 0 )"; @@ -503,17 +515,23 @@ public function getTimeCard(Project $project, User $user): array } $hourInterval = 1; + $revisionTable = $project->getTableName('revision'); + // Always JOIN on page, see T325492 + $pageTable = $project->getTableName('page'); if ($user->isIpRange()) { $column = 'ipc_rev_timestamp'; $table = $project->getTableName('ip_changes'); [$params['startIp'], $params['endIp']] = IPUtils::parseRange($user->getUsername()); $whereClause = 'ipc_hex BETWEEN :startIp AND :endIp'; + $joinClause = "JOIN $revisionTable ON rev_id = ipc_rev_id + JOIN $pageTable ON rev_page = page_id"; } else { $column = 'rev_timestamp'; - $table = $project->getTableName('revision'); + $table = $revisionTable; $whereClause = 'rev_actor = :actorId'; $params = ['actorId' => $user->getActorId($project)]; + $joinClause = "JOIN $pageTable ON rev_page = page_id"; } $xCalc = "ROUND(HOUR($column)/$hourInterval) * $hourInterval"; @@ -523,6 +541,7 @@ public function getTimeCard(Project $project, User $user): array $xCalc AS `hour`, COUNT($column) AS `value` FROM $table + $joinClause WHERE $whereClause GROUP BY DAYOFWEEK($column), $xCalc"; @@ -549,6 +568,7 @@ public function getEditSizeData(Project $project, User $user): array // Prepare the queries and execute them. $revisionTable = $project->getTableName('revision'); + $pageTable = $project->getTableName('page'); $ipcJoin = ''; $whereClause = 'revs.rev_actor = :actorId'; $params = ['actorId' => $user->getActorId($project)]; @@ -566,6 +586,7 @@ public function getEditSizeData(Project $project, User $user): array FROM ( SELECT (CAST(revs.rev_len AS SIGNED) - IFNULL(parentrevs.rev_len, 0)) AS size FROM $revisionTable AS revs + JOIN $pageTable ON revs.rev_page = page_id $ipcJoin LEFT JOIN $revisionTable AS parentrevs ON (revs.rev_parent_id = parentrevs.rev_id) WHERE $whereClause diff --git a/src/Repository/SimpleEditCounterRepository.php b/src/Repository/SimpleEditCounterRepository.php index d646852e2..ae168a0df 100644 --- a/src/Repository/SimpleEditCounterRepository.php +++ b/src/Repository/SimpleEditCounterRepository.php @@ -65,7 +65,8 @@ private function fetchDataNormal( $arDateConditions = $this->getDateConditions($start, $end, false, '', 'ar_timestamp'); $revDateConditions = $this->getDateConditions($start, $end); - $revNamespaceJoinSql = 'all' === $namespace ? '' : "JOIN $pageTable ON rev_page = page_id"; + // Always JOIN on page, see T325492 + $revNamespaceJoinSql = "JOIN $pageTable ON rev_page = page_id"; $revNamespaceWhereSql = 'all' === $namespace ? '' : "AND page_namespace = $namespace"; $arNamespaceWhereSql = 'all' === $namespace ? '' : "AND ar_namespace = $namespace"; @@ -126,7 +127,7 @@ private function fetchDataIpRange( $sql = "SELECT 'rev' AS source, COUNT(*) AS value FROM $ipcTable $revNamespaceJoinSql - WHERE ipc_hex BETWEEN :start AND :end + WHERE ipc_hex BETWEEN :start AND :end $revDateConditions $revNamespaceWhereSql";