From 116bf43c5ab68cc5f124627f4fa099e5c6a27c4a Mon Sep 17 00:00:00 2001 From: NekoCharm Date: Wed, 13 Dec 2023 00:07:46 +0800 Subject: [PATCH] Editcount: Migrate to fluent style query Change-Id: Ica309a5986aed42b04c5150d45376c629df01ac6 --- extension.json | 2 +- src/Editcount.php | 49 +++++++++++++++++++++++++---------------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/extension.json b/extension.json index 6616ca2..5c6f325 100644 --- a/extension.json +++ b/extension.json @@ -9,7 +9,7 @@ "license-name": "GPL-2.0-or-later", "type": "specialpage", "requires": { - "MediaWiki": ">= 1.32.0" + "MediaWiki": ">= 1.36.0" }, "SpecialPages": { "Editcount": "Editcount" diff --git a/src/Editcount.php b/src/Editcount.php index 0a50af6..4d18ef9 100644 --- a/src/Editcount.php +++ b/src/Editcount.php @@ -69,22 +69,24 @@ class Editcount extends IncludableSpecialPage { return []; } - $dbr = wfGetDB( DB_REPLICA ); - $actorWhere = ActorMigration::newMigration()->getWhere( $dbr, 'rev_user', $user ); - $res = $dbr->select( - [ 'revision', 'page' ] + $actorWhere['tables'], - [ 'page_namespace', 'COUNT(*) AS count' ], - [ $actorWhere['conds'] ], - __METHOD__, - [ 'GROUP BY' => 'page_namespace' ], - [ 'page' => [ 'JOIN', 'page_id = rev_page' ] ] + $actorWhere['joins'] - ); + $dbr = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getReplicaDatabase(); + $actorNormalization = MediaWikiServices::getInstance()->getActorNormalization(); + $actorId = $actorNormalization->findActorId( $user, $dbr ); + $query = $dbr->newSelectQueryBuilder() + ->caller( __METHOD__ ) + ->select( [ 'page_namespace', 'count' => 'COUNT(*)' ] ) + ->from( 'revision' ) + ->join( 'page', null, 'page_id = rev_page' ) + ->join( 'actor', null, 'rev_actor = actor_id' ) + ->where( [ "actor_id" => $actorId ] ) + ->groupBy( 'page_namespace' ); + $res = $query->fetchResultSet(); - $nscount = []; + $nsCount = []; foreach ( $res as $row ) { - $nscount[$row->page_namespace] = (int)$row->count; + $nsCount[$row->page_namespace] = (int)$row->count; } - return $nscount; + return $nsCount; } /** @@ -99,15 +101,16 @@ class Editcount extends IncludableSpecialPage { return 0; } - $dbr = wfGetDB( DB_REPLICA ); - $actorWhere = ActorMigration::newMigration()->getWhere( $dbr, 'rev_user', $user ); - return (int)$dbr->selectField( - [ 'revision', 'page' ] + $actorWhere['tables'], - 'COUNT(*)', - [ 'page_namespace' => $ns, $actorWhere['conds'] ], - __METHOD__, - [], - [ 'page' => [ 'JOIN', 'page_id = rev_page' ] ] + $actorWhere['joins'] - ); + $dbr = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getReplicaDatabase(); + $actorNormalization = MediaWikiServices::getInstance()->getActorNormalization(); + $actorId = $actorNormalization->findActorId( $user, $dbr ); + $query = $dbr->newSelectQueryBuilder() + ->caller( __METHOD__ ) + ->select( 'COUNT(*)' ) + ->from( 'revision' ) + ->join( 'page', null, 'page_id = rev_page' ) + ->join( 'actor', null, 'rev_actor = actor_id' ) + ->where( [ "actor_id" => $actorId, "page_namespace" => $ns ] ); + return $query->fetchField(); } }