Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion includes/Parse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
54 changes: 46 additions & 8 deletions includes/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MediaWiki\Extension\DynamicPageList4;

use InvalidArgumentException;
use MediaWiki\Extension\DynamicPageList4\Exceptions\QueryException;
use MediaWiki\ExternalLinks\LinkFilter;
use MediaWiki\Linker\LinksMigration;
Expand Down Expand Up @@ -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' );
Expand Down Expand Up @@ -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",
];
}

Expand All @@ -1067,22 +1071,22 @@ 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' ];

$ors = [];
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 );
}
}

Expand Down Expand Up @@ -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";
}
}
Loading