diff --git a/includes/Parse.php b/includes/Parse.php index 3e466786d..c52ef2142 100644 --- a/includes/Parse.php +++ b/includes/Parse.php @@ -378,7 +378,7 @@ private function processQueryResults( array $rows, Parser $parser ): array { $imageContainer = $this->parameters->getParameter( 'imagecontainer' ) ?? []; if ( $imageContainer !== [] ) { $pageNamespace = NS_FILE; - $pageTitle = $row->il_to; + $pageTitle = $row->lt_title; } else { // Maybe non-existing title $pageNamespace = (int)$row->lt_namespace; diff --git a/includes/Query.php b/includes/Query.php index 4590759aa..c90e3ac9d 100644 --- a/includes/Query.php +++ b/includes/Query.php @@ -4,6 +4,7 @@ namespace MediaWiki\Extension\DynamicPageList4; +use InvalidArgumentException; use MediaWiki\Extension\DynamicPageList4\Exceptions\QueryException; use MediaWiki\ExternalLinks\LinkFilter; use MediaWiki\Linker\LinksMigration; @@ -151,8 +152,9 @@ public function buildAndSelect( bool $calcRows, string $profilingContext ): arra if ( $this->parameters->getParameter( 'openreferences' ) ) { if ( $this->parameters->getParameter( 'imagecontainer' ) ) { - $this->queryBuilder->select( 'il_to' ); $this->queryBuilder->table( 'imagelinks', 'ic' ); + $ilToField = $this->getImageLinksTitleField( 'ic', 'ic_lt' ); + $this->queryBuilder->select( [ 'lt_title' => $ilToField ] ); } else { $this->queryBuilder->table( 'pagelinks', 'pl' ); $this->queryBuilder->join( 'linktarget', 'lt', 'pl.pl_target_id = lt.lt_id' ); @@ -1037,13 +1039,15 @@ private function _hiddencategories( string $option ): never { */ private function _imagecontainer( array $option ): void { $this->queryBuilder->table( 'imagelinks', 'ic' ); - $this->queryBuilder->select( [ 'sortkey' => 'ic.il_to' ] ); + + $ilToField = $this->getImageLinksTitleField( 'ic', 'ic_lt' ); + $this->queryBuilder->select( [ 'sortkey' => $ilToField ] ); $where = []; if ( !$this->parameters->getParameter( 'openreferences' ) ) { $where = [ 'p.page_namespace = ' . NS_FILE, - 'p.page_title = ic.il_to', + "p.page_title = $ilToField", ]; } @@ -1067,7 +1071,9 @@ private function _imageused( array $option ): void { } $this->queryBuilder->table( 'imagelinks', 'il' ); - $this->queryBuilder->select( [ 'image_sel_title' => 'il.il_to' ] ); + + $ilToField = $this->getImageLinksTitleField( 'il', 'il_lt' ); + $this->queryBuilder->select( [ 'image_sel_title' => $ilToField ] ); $where = [ 'p.page_id = il.il_from' ]; @@ -1075,14 +1081,12 @@ private function _imageused( array $option ): void { foreach ( $option as $linkGroup ) { foreach ( $linkGroup as $link ) { $dbkey = $link->getDBkey(); - $fieldExpr = 'il.il_to'; - if ( $this->ignoreCase ) { - $ors[] = $this->caseInsensitiveComparison( $fieldExpr, '=', $dbkey ); + $ors[] = $this->caseInsensitiveComparison( $ilToField, '=', $dbkey ); continue; } - $ors[] = $this->dbr->expr( $fieldExpr, '=', $dbkey ); + $ors[] = $this->dbr->expr( $ilToField, '=', $dbkey ); } } @@ -2362,4 +2366,38 @@ private function getCategoryTitleFieldForAlias( string $clAlias, string $ltAlias return "$clAlias.cl_to"; } + + /** + * Add a linktarget join for imagelinks and return the title field expression. + * For MW 1.46+ adds linktarget join, for MW 1.45 returns empty (uses il_to directly). + * + * @param string $ilAlias Alias for the imagelinks table (e.g. 'il', 'ic') + * @param string $ltAlias Alias to use for the joined linktarget row + * @return string The field expression for the image file title + */ + private function getImageLinksTitleField( string $ilAlias, string $ltAlias ): string { + // LinksMigration covers imagelinks starting in MW 1.46. It will throw an + // InvalidArgumentException on earlier versions where it is not covered. + try { + // Check if the new schema is active (il_target_id column present). + $queryInfo = $this->linksMigration->getQueryInfo( 'imagelinks' ); + $isNew = in_array( 'linktarget', $queryInfo['tables'], true ); + } catch ( InvalidArgumentException ) { + // Pre-1.46 MW where LinksMigration doesn't know imagelinks + $isNew = false; + } + + if ( $isNew ) { + // MW 1.46+: join linktarget via il_target_id + $this->queryBuilder->join( 'linktarget', $ltAlias, [ + "$ilAlias.il_target_id = $ltAlias.lt_id", + "$ltAlias.lt_namespace" => NS_FILE, + ] ); + + return "$ltAlias.lt_title"; + } + + // MW 1.45: il_to is still the direct title column + return "$ilAlias.il_to"; + } }