Skip to content

Commit

Permalink
Prevent high-prio categories from reserving more candidates than needed
Browse files Browse the repository at this point in the history
This mitigates a problem where a more important (according to sortkey order)
category A is reserving images that would otherwise be the only possible
recommendations for a less important category B,
resulting in category B not getting any recommendations at all.

It doesn't eliminate the problem completely, because images of B can be
among the first $wgRelatedImagesMaxImagesPerCategory images in A.
  • Loading branch information
edwardspec committed Sep 22, 2024
1 parent 4a4e8d3 commit 0f278f5
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions includes/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,13 @@ public function onImagePageAfterImageLinks( $imagePage, &$html ) {
// List of candidates to recommend (in the same order as $categoryNames):
// [ 'Category_name' => [ 'filename', ... ], ... ]
$filenamesPerCategory = array_fill_keys( $categoryNames, [] );
$seenFilenames = [];
$usedFilenames = [];

foreach ( $res as $row ) {
if ( isset( $seenFilenames[$row->filename] ) ) {
if ( isset( $usedFilenames[$row->filename] ) ) {
// Already recommended in another category.
continue;
}
$seenFilenames[$row->filename] = true;

$filenameParts = explode( '.', $row->filename );
$extension = File::normalizeExtension( $filenameParts[count( $filenameParts ) - 1] );
Expand All @@ -172,6 +171,7 @@ public function onImagePageAfterImageLinks( $imagePage, &$html ) {

if ( count( $filenamesPerCategory[$row->category] ) < $wgRelatedImagesMaxImagesPerCategory ) {
$filenamesPerCategory[$row->category][] = $row->filename;
$usedFilenames[$row->filename] = true;
}
}

Expand Down

0 comments on commit 0f278f5

Please sign in to comment.