Skip to content

Commit

Permalink
Add $wgRelatedImagesDoNotRecommendExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardspec committed Sep 15, 2024
1 parent 58946dd commit 1ba0c75
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ $wgRelatedImagesThumbnailHeight = 100; // Default: 50
// If the wiki uses [https://commons.wikimedia.org/wiki/Help:Image-Annotator],
// this should disable annotations on recommendation thumbnails.
$wgRelatedImagesBoxExtraCssClass = 'wpImageAnnotatorControl wpImageAnnotatorOff';

// Default values:
$wgRelatedImagesDisableForExtensions = [ 'ogg', 'pdf' ];
$wgRelatedImagesDoNotRecommendExtensions = [ 'ogg', 'pdf' ];
```
4 changes: 4 additions & 0 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
"RelatedImagesDisableForExtensions": {
"value": [ "ogg", "pdf" ],
"description": "Related Images box won't be shown on File pages with these file extensions."
},
"RelatedImagesDoNotRecommendExtensions": {
"value": [ "ogg", "pdf" ],
"description": "Related Images box won't recommend files with these file extensions."
}
},
"ResourceModules": {
Expand Down
70 changes: 40 additions & 30 deletions includes/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use ContentHandler;
use DeferredUpdates;
use File;
use FileRepo;
use FormatJson;
use ImagePage;
Expand Down Expand Up @@ -87,7 +88,8 @@ public function onImagePageAfterImageLinks( $imagePage, &$html ) {
$wgRelatedImagesMaxImagesPerCategory,
$wgRelatedImagesBoxExtraCssClass,
$wgRelatedImagesExperimentalPregenerateThumbnails,
$wgRelatedImagesDisableForExtensions;
$wgRelatedImagesDisableForExtensions,
$wgRelatedImagesDoNotRecommendExtensions;

if ( in_array( $imagePage->getFile()->getExtension(), $wgRelatedImagesDisableForExtensions ) ) {
// Not needed for this kind of file.
Expand Down Expand Up @@ -134,35 +136,39 @@ public function onImagePageAfterImageLinks( $imagePage, &$html ) {
$filenamesPerCategory = []; # [ 'Category_name' => [ 'filename', ... ], ... ]
$seenFilenames = [];

// We request more titles than needed, so that duplicates wouldn't result in less recommendations.
$limit = $wgRelatedImagesMaxImagesPerCategory * count( $categoryNames );
foreach ( $categoryNames as $category ) {
// Because the number of categories is low, and the number of images in them can very high,
// it's preferable to do 1 SQL query per category (limited by $wgRelatedImagesMaxImagesPerCategory)
// rather than do only 1 SQL query for all categories, but without the limit.
$filenames = $dbr->newSelectQueryBuilder()
->select( [ 'DISTINCT page_title' ] )
->from( 'categorylinks' )
->join( 'page', null, [
'page_id=cl_from',
'page_namespace' => NS_FILE
] )
->where( [
'cl_to' => $category,
'cl_from <> ' . $articleID
] )
->limit( $limit )
->orderBy( 'cl_sortkey' )
->caller( __METHOD__ )
->fetchFieldValues();

// Eliminate duplicates (files that have already been found in previous categories).
$filenames = array_diff( $filenames, $seenFilenames );
$filenames = array_slice( $filenames, 0, $wgRelatedImagesMaxImagesPerCategory );

array_push( $seenFilenames, ...$filenames );

$filenamesPerCategory[$category] = $filenames;
$res = $dbr->newSelectQueryBuilder()
->select( [
'DISTINCT page_title AS filename',
'cl_to AS category'
] )
->from( 'categorylinks' )
->join( 'page', null, [
'page_id=cl_from',
'page_namespace' => NS_FILE
] )
->where( [
'cl_to' => $categoryNames,
'cl_from <> ' . $articleID
] )
->orderBy( 'cl_sortkey' )
->caller( __METHOD__ )
->fetchResultSet();

foreach ( $res as $row ) {
$filenameParts = explode( '.', $row->filename );
$extension = File::normalizeExtension( $filenameParts[count( $filenameParts ) - 1] );
if ( in_array( $extension, $wgRelatedImagesDoNotRecommendExtensions ) ) {
// We don't want this file to be recommended.
continue;
}

if ( !isset( $filenamesPerCategory[$row->category] ) ) {
$filenamesPerCategory[$row->category] = [];
}

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

$logger = LoggerFactory::getInstance( 'RelatedImages' );
Expand All @@ -180,6 +186,10 @@ public function onImagePageAfterImageLinks( $imagePage, &$html ) {
$numFilesCount = 0;
$numCategoriesCount = 0;
foreach ( $filenamesPerCategory as $category => $filenames ) {
if ( !$filenames ) {
continue;
}

$found = $this->repoGroup->findFiles( $filenames, FileRepo::NAME_AND_TIME_ONLY );
if ( !$found ) {
// No files found in this category (can happen even if File pages exist).
Expand Down

0 comments on commit 1ba0c75

Please sign in to comment.