diff --git a/includes/Lister/GalleryList.php b/includes/Lister/GalleryList.php index 92c8d174..1ac2a13c 100644 --- a/includes/Lister/GalleryList.php +++ b/includes/Lister/GalleryList.php @@ -2,6 +2,7 @@ namespace MediaWiki\Extension\DynamicPageList3\Lister; +use ExtensionRegistry; use MediaWiki\Extension\DynamicPageList3\Article; class GalleryList extends Lister { @@ -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 ); diff --git a/includes/Lister/Lister.php b/includes/Lister/Lister.php index e6976728..fe55a02e 100644 --- a/includes/Lister/Lister.php +++ b/includes/Lister/Lister.php @@ -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; @@ -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 ) { @@ -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 ); @@ -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; + } }