Migrate to IReadableDatabase::newSelectQueryBuilder

Also use expression builder to avoid raw sql

Bug: T312501
Bug: T350988
Change-Id: I0ea6aa6edcd68cba067260dad560b87283cca020
This commit is contained in:
Umherirrender 2024-04-21 12:41:53 +02:00
parent dd36c6e13e
commit d0c0dc9caf
2 changed files with 33 additions and 33 deletions

View file

@ -189,15 +189,16 @@ class PageImages implements
}
$dbr = $this->dbProvider->getReplicaDatabase();
$fileName = $dbr->selectField( 'page_props',
'pp_value',
[
$fileName = $dbr->newSelectQueryBuilder()
->select( 'pp_value' )
->from( 'page_props' )
->where( [
'pp_page' => $pageId,
'pp_propname' => [ self::PROP_NAME, self::PROP_NAME_FREE ]
],
__METHOD__,
[ 'ORDER BY' => 'pp_propname' ]
);
] )
->orderBy( 'pp_propname' )
->caller( __METHOD__ )
->fetchField();
if ( !$fileName ) {
// Return not found without caching.
return false;

View file

@ -45,41 +45,40 @@ class InitImageData extends Maintenance {
}
do {
$tables = [ 'page', 'imagelinks' ];
$conds = [
'page_id > ' . (int)$lastId,
'il_from IS NOT NULL',
'page_is_redirect' => 0,
];
$fields = [ 'page_id' ];
$joinConds = [ 'imagelinks' => [
'LEFT JOIN', 'page_id = il_from',
] ];
$dbr = $this->getServiceContainer()->getDBLoadBalancerFactory()
->getReplicaDatabase();
$queryBuilder = $dbr->newSelectQueryBuilder()
->select( 'page_id' )
->from( 'page' )
->leftJoin( 'imagelinks', null, 'page_id = il_from' )
->where( [
$dbr->expr( 'page_id', '>', (int)$lastId ),
$dbr->expr( 'il_from', '!=', null ),
'page_is_redirect' => 0,
] )
->orderBy( 'page_id' )
->groupBy( 'page_id' )
->limit( $this->mBatchSize )
->caller( __METHOD__ );
if ( $this->hasOption( 'namespaces' ) ) {
$ns = explode( ',', $this->getOption( 'namespaces' ) );
$conds['page_namespace'] = $ns;
$queryBuilder->andWhere( [ 'page_namespace' => $ns ] );
} else {
$conds['page_namespace'] = $this->getServiceContainer()->getMainConfig()->get( 'PageImagesNamespaces' );
$queryBuilder->andWhere( [
'page_namespace' => $this->getServiceContainer()->getMainConfig()->get( 'PageImagesNamespaces' )
] );
}
if ( $this->hasOption( 'earlier-than' ) ) {
$conds[] = 'page_touched < '
. $dbr->addQuotes( $dbr->timestamp( $this->getOption( 'earlier-than' ) ) );
$queryBuilder->andWhere(
$dbr->expr( 'page_touched', '<', $dbr->timestamp( $this->getOption( 'earlier-than' ) ) )
);
}
if ( $this->hasOption( 'later-than' ) ) {
$conds[] = 'page_touched > '
. $dbr->addQuotes( $dbr->timestamp( $this->getOption( 'later-than' ) ) );
}
$res = $dbr->select( $tables, $fields, $conds, __METHOD__,
[ 'LIMIT' => $this->mBatchSize, 'ORDER_BY' => 'page_id', 'GROUP BY' => 'page_id' ],
$joinConds
$queryBuilder->andWhere(
$dbr->expr( 'page_touched', '>', $dbr->timestamp( $this->getOption( 'later-than' ) ) )
);
$pageIds = [];
foreach ( $res as $row ) {
$pageIds[] = $row->page_id;
}
$pageIds = $queryBuilder->fetchFieldValues();
$job = new InitImageDataJob(
Title::newMainPage(),
[ 'page_ids' => $pageIds ],
@ -93,7 +92,7 @@ class InitImageData extends Maintenance {
}
$lastId = end( $pageIds );
$this->output( "$lastId\n" );
} while ( $res->numRows() );
} while ( $pageIds );
$this->output( "done\n" );
}