From a5ed2df4456a90cfc231d8f8bb373616c6d8d23f Mon Sep 17 00:00:00 2001 From: umherirrender Date: Fri, 22 Aug 2014 22:36:59 +0200 Subject: [PATCH] Use config instead of globals for ImageGallery Have to pass a context to the constructor to acutally use it for settings. Also adds a RequestContext::getMainAndWarn to get a default warning, when using the main request, but it would be better to pass one. Change-Id: I1628a1790c45d44aa4239701486b8b1b7c59a0e6 --- includes/CategoryViewer.php | 5 ++-- includes/context/RequestContext.php | 14 +++++++++++ includes/gallery/ImageGalleryBase.php | 32 ++++++++++++++++--------- includes/specialpage/ImageQueryPage.php | 3 +-- includes/specials/SpecialNewimages.php | 5 ++-- includes/specials/SpecialUpload.php | 3 +-- 6 files changed, 41 insertions(+), 21 deletions(-) diff --git a/includes/CategoryViewer.php b/includes/CategoryViewer.php index 22eb3d1ce7..ec8efae7b7 100644 --- a/includes/CategoryViewer.php +++ b/includes/CategoryViewer.php @@ -154,14 +154,13 @@ class CategoryViewer extends ContextSource { // Note that null for mode is taken to mean use default. $mode = $this->getRequest()->getVal( 'gallerymode', null ); try { - $this->gallery = ImageGalleryBase::factory( $mode ); + $this->gallery = ImageGalleryBase::factory( $mode, $this->getContext() ); } catch ( MWException $e ) { // User specified something invalid, fallback to default. - $this->gallery = ImageGalleryBase::factory(); + $this->gallery = ImageGalleryBase::factory( false, $this->getContext() ); } $this->gallery->setHideBadImages(); - $this->gallery->setContext( $this->getContext() ); } else { $this->imgsNoGallery = array(); $this->imgsNoGallery_start_char = array(); diff --git a/includes/context/RequestContext.php b/includes/context/RequestContext.php index 00733d8169..be786e484c 100644 --- a/includes/context/RequestContext.php +++ b/includes/context/RequestContext.php @@ -421,6 +421,20 @@ class RequestContext implements IContextSource { return self::$instance; } + /** + * Get the RequestContext object associated with the main request + * and gives a warning to the log, to find places, where a context maybe is missing. + * + * @return RequestContext + * @since 1.24 + */ + public static function getMainAndWarn( $func = __METHOD__ ) { + wfDebug( $func . ' called without context. ' . + "Using RequestContext::getMain() for sanity\n" ); + + return self::getMain(); + } + /** * Resets singleton returned by getMain(). Should be called only from unit tests. */ diff --git a/includes/gallery/ImageGalleryBase.php b/includes/gallery/ImageGalleryBase.php index 2d3ea5a1a6..b61a3529a4 100644 --- a/includes/gallery/ImageGalleryBase.php +++ b/includes/gallery/ImageGalleryBase.php @@ -86,19 +86,24 @@ abstract class ImageGalleryBase extends ContextSource { * should use to get a gallery. * * @param string|bool $mode Mode to use. False to use the default + * @param IContextSource|null $context * @throws MWException */ - static function factory( $mode = false ) { - global $wgGalleryOptions, $wgContLang; + static function factory( $mode = false, IContextSource $context = null ) { + global $wgContLang; self::loadModes(); + if ( !$context ) { + $context = RequestContext::getMainAndWarn( __METHOD__ ); + } if ( !$mode ) { - $mode = $wgGalleryOptions['mode']; + $galleryOpions = $context->getConfig()->get( 'GalleryOptions' ); + $mode = $galleryOpions['mode']; } $mode = $wgContLang->lc( $mode ); if ( isset( self::$modeMapping[$mode] ) ) { - return new self::$modeMapping[$mode]( $mode ); + return new self::$modeMapping[$mode]( $mode, $context ); } else { throw new MWException( "No gallery class registered for mode $mode" ); } @@ -124,18 +129,23 @@ abstract class ImageGalleryBase extends ContextSource { * You should not call this directly, but instead use * ImageGalleryBase::factory(). * @param string $mode + * @param IContextSource|null $context */ - function __construct( $mode = 'traditional' ) { - global $wgGalleryOptions; + function __construct( $mode = 'traditional', IContextSource $context = null ) { + if ( $context ) { + $this->setContext( $context ); + } + + $galleryOptions = $this->getConfig()->get( 'GalleryOptions' ); $this->mImages = array(); - $this->mShowBytes = $wgGalleryOptions['showBytes']; + $this->mShowBytes = $galleryOptions['showBytes']; $this->mShowFilename = true; $this->mParser = false; $this->mHideBadImages = false; - $this->mPerRow = $wgGalleryOptions['imagesPerRow']; - $this->mWidths = $wgGalleryOptions['imageWidth']; - $this->mHeights = $wgGalleryOptions['imageHeight']; - $this->mCaptionLength = $wgGalleryOptions['captionLength']; + $this->mPerRow = $galleryOptions['imagesPerRow']; + $this->mWidths = $galleryOptions['imageWidth']; + $this->mHeights = $galleryOptions['imageHeight']; + $this->mCaptionLength = $galleryOptions['captionLength']; $this->mMode = $mode; } diff --git a/includes/specialpage/ImageQueryPage.php b/includes/specialpage/ImageQueryPage.php index 3ff281f952..272d533734 100644 --- a/includes/specialpage/ImageQueryPage.php +++ b/includes/specialpage/ImageQueryPage.php @@ -42,8 +42,7 @@ abstract class ImageQueryPage extends QueryPage { */ protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) { if ( $num > 0 ) { - $gallery = ImageGalleryBase::factory(); - $gallery->setContext( $this->getContext() ); + $gallery = ImageGalleryBase::factory( false, $this->getContext() ); # $res might contain the whole 1,000 rows, so we read up to # $num [should update this to use a Pager] diff --git a/includes/specials/SpecialNewimages.php b/includes/specials/SpecialNewimages.php index aff5947cc8..546c191405 100644 --- a/includes/specials/SpecialNewimages.php +++ b/includes/specials/SpecialNewimages.php @@ -140,12 +140,11 @@ class NewFilesPager extends ReverseChronologicalPager { // Note that null for mode is taken to mean use default. $mode = $this->getRequest()->getVal( 'gallerymode', null ); try { - $this->gallery = ImageGalleryBase::factory( $mode ); + $this->gallery = ImageGalleryBase::factory( $mode, $this->getContext() ); } catch ( MWException $e ) { // User specified something invalid, fallback to default. - $this->gallery = ImageGalleryBase::factory(); + $this->gallery = ImageGalleryBase::factory( false, $this->getContext() ); } - $this->gallery->setContext( $this->getContext() ); } return ''; diff --git a/includes/specials/SpecialUpload.php b/includes/specials/SpecialUpload.php index 4fd7cd4380..5c5026c675 100644 --- a/includes/specials/SpecialUpload.php +++ b/includes/specials/SpecialUpload.php @@ -725,8 +725,7 @@ class SpecialUpload extends SpecialPage { return ''; } - $gallery = ImageGalleryBase::factory(); - $gallery->setContext( $this->getContext() ); + $gallery = ImageGalleryBase::factory( false, $this->getContext() ); $gallery->setShowBytes( false ); foreach ( $dupes as $file ) { $gallery->add( $file->getTitle() ); -- 2.20.1