Editcount: Migrate to fluent style query

Change-Id: Ica309a5986aed42b04c5150d45376c629df01ac6
This commit is contained in:
NekoCharm 2023-12-13 00:07:46 +08:00 committed by Winston Sung
parent b7b699d9ac
commit 116bf43c5a
2 changed files with 27 additions and 24 deletions

View file

@ -9,7 +9,7 @@
"license-name": "GPL-2.0-or-later", "license-name": "GPL-2.0-or-later",
"type": "specialpage", "type": "specialpage",
"requires": { "requires": {
"MediaWiki": ">= 1.32.0" "MediaWiki": ">= 1.36.0"
}, },
"SpecialPages": { "SpecialPages": {
"Editcount": "Editcount" "Editcount": "Editcount"

View file

@ -69,22 +69,24 @@ class Editcount extends IncludableSpecialPage {
return []; return [];
} }
$dbr = wfGetDB( DB_REPLICA ); $dbr = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getReplicaDatabase();
$actorWhere = ActorMigration::newMigration()->getWhere( $dbr, 'rev_user', $user ); $actorNormalization = MediaWikiServices::getInstance()->getActorNormalization();
$res = $dbr->select( $actorId = $actorNormalization->findActorId( $user, $dbr );
[ 'revision', 'page' ] + $actorWhere['tables'], $query = $dbr->newSelectQueryBuilder()
[ 'page_namespace', 'COUNT(*) AS count' ], ->caller( __METHOD__ )
[ $actorWhere['conds'] ], ->select( [ 'page_namespace', 'count' => 'COUNT(*)' ] )
__METHOD__, ->from( 'revision' )
[ 'GROUP BY' => 'page_namespace' ], ->join( 'page', null, 'page_id = rev_page' )
[ 'page' => [ 'JOIN', 'page_id = rev_page' ] ] + $actorWhere['joins'] ->join( 'actor', null, 'rev_actor = actor_id' )
); ->where( [ "actor_id" => $actorId ] )
->groupBy( 'page_namespace' );
$res = $query->fetchResultSet();
$nscount = []; $nsCount = [];
foreach ( $res as $row ) { 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; return 0;
} }
$dbr = wfGetDB( DB_REPLICA ); $dbr = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getReplicaDatabase();
$actorWhere = ActorMigration::newMigration()->getWhere( $dbr, 'rev_user', $user ); $actorNormalization = MediaWikiServices::getInstance()->getActorNormalization();
return (int)$dbr->selectField( $actorId = $actorNormalization->findActorId( $user, $dbr );
[ 'revision', 'page' ] + $actorWhere['tables'], $query = $dbr->newSelectQueryBuilder()
'COUNT(*)', ->caller( __METHOD__ )
[ 'page_namespace' => $ns, $actorWhere['conds'] ], ->select( 'COUNT(*)' )
__METHOD__, ->from( 'revision' )
[], ->join( 'page', null, 'page_id = rev_page' )
[ 'page' => [ 'JOIN', 'page_id = rev_page' ] ] + $actorWhere['joins'] ->join( 'actor', null, 'rev_actor = actor_id' )
); ->where( [ "actor_id" => $actorId, "page_namespace" => $ns ] );
return $query->fetchField();
} }
} }