Skip to content

Commit

Permalink
PageImages support for mode=gallery on non file pages (#275)
Browse files Browse the repository at this point in the history
In gallery mode, attempting to automatically detect whether:
- PageImages is enabled
- If it is, if a non file page has a PageImage
If both are true, it should try to use the PageImage for the gallery and the page name for the image caption.

Right now, it also makes the page name in the caption a link, and points the image link to the page as well. Could not figure out how to make this behavior optional.

Gallery mode for file pages should be untouched.

Notes:
- This behavior is similar, but not a perfect match to DPL_(Wikimedia)'s PageImages in gallery mode.
- Unsure if some of the solutions are the most optimal. The way its detecting if PageImages is enabled is a bit clumsy.

Co-authored-by: CosmicAlpha <[email protected]>
  • Loading branch information
domino-1 and Universal-Omega authored May 24, 2024
1 parent b58a5cb commit df4ff96
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
25 changes: 20 additions & 5 deletions includes/Lister/GalleryList.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MediaWiki\Extension\DynamicPageList3\Lister;

use ExtensionRegistry;
use MediaWiki\Extension\DynamicPageList3\Article;

class GalleryList extends Lister {
Expand Down Expand Up @@ -50,12 +51,26 @@ class GalleryList extends Lister {
public function formatItem( Article $article, $pageText = null ) {
$item = $article->mTitle;

if ( $pageText !== null ) {
// Include parsed/processed wiki markup content after each item before the closing tag.
$item .= $pageText;
}
// If PageImages is loaded and we are not in the file namespace, attempt to assemble a gallery of PageImages
if ( $article->mNamespace !== NS_FILE && ExtensionRegistry::getInstance()->isLoaded( 'PageImages' ) ) {

$pageImage = $this->getPageImage( $article->mID ) ?: false;

if ( $pageImage ) {
// Successfully got a page image, wrapping it
$item = $this->getItemStart() . $pageImage . '| [[' . $item . ']]' . $this->itemEnd . 'link=' . $item;
} else {
// Failed to get a page image
$item = $this->getItemStart() . $item . $this->itemEnd . '[[' . $item . ']]';
}
} else {
if ( $pageText !== null ) {
// Include parsed/processed wiki markup content after each item before the closing tag.
$item .= $pageText;
}

$item = $this->getItemStart() . $item . $this->itemEnd;
$item = $this->getItemStart() . $item . $this->itemEnd;
}

$item = $this->replaceTagParameters( $item, $article );

Expand Down
32 changes: 32 additions & 0 deletions includes/Lister/Lister.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MediaWiki\Extension\DynamicPageList3\Lister;

use ExtensionRegistry;
use MediaWiki\Extension\DynamicPageList3\Article;
use MediaWiki\Extension\DynamicPageList3\LST;
use MediaWiki\Extension\DynamicPageList3\Parameters;
Expand Down Expand Up @@ -970,6 +971,7 @@ private function cutAt( $lim, $text ) {
*/
protected function parseImageUrlWithPath( $article ) {
$repoGroup = MediaWikiServices::getInstance()->getRepoGroup();
$pageImagesEnabled = ExtensionRegistry::getInstance()->isLoaded( 'PageImages' );

$imageUrl = '';
if ( $article instanceof Article ) {
Expand All @@ -984,6 +986,17 @@ protected function parseImageUrlWithPath( $article ) {
$fileTitle = Title::makeTitleSafe( NS_FILE, $article->mTitle->getDBKey() );
$imageUrl = $repoGroup->getLocalRepo()->newFile( $fileTitle )->getPath();
}
} elseif ( $pageImagesEnabled ) {
$pageImage = self::getPageImage( $article->mID ) ?: false;
if ( !$pageImage ) {
return '';
}
$img = $repoGroup->findFile( Title::makeTitle( NS_FILE, $pageImage ) );
if ( $img && $img->exists() ) {
$imageUrl = $img->getURL();
} else {
$imageUrl = '';
}
}
} else {
$title = Title::newfromText( 'File:' . $article );
Expand Down Expand Up @@ -1301,4 +1314,23 @@ protected function joinSectionTagPieces( $piece, $start, $end ) {
public function getRowCount() {
return $this->rowCount;
}

public function getPageImage( int $pageID ) {
$dbr = MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection( DB_REPLICA );
// In the future, a check could be made for page_image too, but page_image_free is the default, should do for now
$propValue = $dbr->selectField(
// Table to use
'page_props',
// Field to select
'pp_value',
// Where conditions
[
'pp_page' => $pageID,
'pp_propname' => 'page_image_free',
],
__METHOD__
);

return $propValue;
}
}

0 comments on commit df4ff96

Please sign in to comment.