Skip to content

Commit

Permalink
Preliminary implementation of Special:SubcatImagesGallery
Browse files Browse the repository at this point in the history
This can be used to add the link "Show images from subcategories"
to category pages.
  • Loading branch information
edwardspec committed Nov 15, 2024
1 parent e9be43b commit 495a7dc
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 2 deletions.
14 changes: 14 additions & 0 deletions RelatedImages.alias.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* Aliases for Special:SubcatImagesGallery
*
* @file
*/

$specialPageAliases = [];

/** English */
$specialPageAliases['en'] = [
'SubcatImagesGallery' => [ 'SubcatImagesGallery' ]
];
18 changes: 17 additions & 1 deletion extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
},
"type": "other",
"AutoloadClasses": {
"MediaWiki\\RelatedImages\\Hooks": "includes/Hooks.php"
"MediaWiki\\RelatedImages\\Hooks": "includes/Hooks.php",
"MediaWiki\\RelatedImages\\SpecialSubcatImagesGallery": "includes/specials/SpecialSubcatImagesGallery.php"
},
"ExtensionMessagesFiles": {
"RelatedImagesAlias": "RelatedImages.alias.php"
},
"HookHandlers": {
"main": {
Expand Down Expand Up @@ -71,6 +75,10 @@
"RelatedImagesDoNotRecommendExtensions": {
"value": [ "ogg", "pdf" ],
"description": "Related Images box won't recommend files with these file extensions."
},
"RelatedImagesMaxSubcatImages": {
"value": 100,
"description": "Maximum number of additional thumbnails to show when user clicks \"Show images from subcategories\" link on category pages."
}
},
"ResourceModules": {
Expand Down Expand Up @@ -104,5 +112,13 @@
"localBasePath": "modules",
"remoteExtPath": "RelatedImages/modules"
},
"SpecialPages": {
"SubcatImagesGallery": {
"class": "MediaWiki\\RelatedImages\\SpecialSubcatImagesGallery",
"services": [
"DBLoadBalancer"
]
}
},
"manifest_version": 2
}
3 changes: 2 additions & 1 deletion i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"relatedimages-desc": "Displays \"Related images\" navigation box on File pages.",
"relatedimages-header": "Related media",
"relatedimages-more": "Show more",
"relatedimages-more-loading": "Loading..."
"relatedimages-more-loading": "Loading...",
"subcatimagesgallery-empty": "No images found."
}
111 changes: 111 additions & 0 deletions includes/specials/SpecialSubcatImagesGallery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

/**
* Implements RelatedImages extension for MediaWiki.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/

namespace MediaWiki\RelatedImages;

use ImageGalleryBase;
use Title;
use UnlistedSpecialPage;
use Wikimedia\Rdbms\ILoadBalancer;

/**
* Implements hidden special page [[Special:SubcatImagesGallery/CategoryNameHere]],
* which returns HTML of the gallery of images from subcategories of CategoryNameHere.
* This can be used to add the link "Show images from subcategories" to category pages.
*/
class SpecialSubcatImagesGallery extends UnlistedSpecialPage {
/** @var ILoadBalancer */
protected $loadBalancer;

/**
* @param ILoadBalancer $loadBalancer
*/
public function __construct( ILoadBalancer $loadBalancer ) {
parent::__construct( 'SubcatImagesGallery' );

$this->loadBalancer = $loadBalancer;
}

/**
* @param string|null $param
*/
public function execute( $param ) {
$this->setHeaders();

$target = $param ? Title::makeTitleSafe( NS_CATEGORY, $param ) : null;
if ( !$target || !$target->exists() ) {
$out = $this->getOutput();
$out->setStatusCode( 404 );
$out->addWikiMsg( 'badtitletext' );
return;
}

$this->displayGallery( $target );
}

/**
* Find all images in subcategories of $categoryTitle and output HTML of the resulting gallery.
* @param Title $categoryTitle
*/
public function displayGallery( Title $categoryTitle ) {
$dbr = $this->loadBalancer->getConnection( DB_REPLICA );
$filenames = $dbr->newSelectQueryBuilder()
->select( [
'DISTINCT subcatpage.page_title'
] )
->from( 'categorylinks', 'topcat' )
->join( 'page', 'topcatpage', [
'topcatpage.page_id=topcat.cl_from',
'topcatpage.page_namespace' => NS_CATEGORY
] )
->join( 'categorylinks', 'subcat', [
'subcat.cl_to=topcatpage.page_title'
] )
->join( 'page', 'subcatpage', [
'subcatpage.page_id=subcat.cl_from',
'subcatpage.page_namespace' => NS_FILE
] )
->where( [
'topcat.cl_to' => $categoryTitle->getDbKey()
] )
->limit( $this->getConfig()->get( 'RelatedImagesMaxSubcatImages' ) )
->caller( __METHOD__ )
->fetchFieldValues();

$out = $this->getOutput();
if ( !$filenames ) {
$out->addWikiMsg( 'subcatimagesgallery-empty' );
return;
}

$gallery = ImageGalleryBase::factory( false, $this->getContext() );
$gallery->setHideBadImages( true );

foreach ( $filenames as $filename ) {
$title = Title::makeTitle( NS_FILE, $filename );
$gallery->add( $title );
}

$out->addHTML( $gallery->toHTML() );
}
}

0 comments on commit 495a7dc

Please sign in to comment.