Use config instead of globals for ImageGallery
authorumherirrender <umherirrender_de.wp@web.de>
Fri, 22 Aug 2014 20:36:59 +0000 (22:36 +0200)
committerumherirrender <umherirrender_de.wp@web.de>
Fri, 22 Aug 2014 20:36:59 +0000 (22:36 +0200)
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
includes/context/RequestContext.php
includes/gallery/ImageGalleryBase.php
includes/specialpage/ImageQueryPage.php
includes/specials/SpecialNewimages.php
includes/specials/SpecialUpload.php

index 22eb3d1..ec8efae 100644 (file)
@@ -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();
index 00733d8..be786e4 100644 (file)
@@ -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.
         */
index 2d3ea5a..b61a352 100644 (file)
@@ -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;
        }
 
index 3ff281f..272d533 100644 (file)
@@ -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]
index aff5947..546c191 100644 (file)
@@ -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 '';
index 4fd7cd4..5c5026c 100644 (file)
@@ -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() );