Jeroen De Dauw has submitted this change and it was merged.

Change subject: Improve settings system
......................................................................


Improve settings system

* No longer forces global state usage
* Default settings are defined in standard MW fashion rather than in a method 
in the settings class (which clearly violates SRP)

Change-Id: I84a4ba1ff170a9408655fe167d5d765dc76607d2
---
M EducationProgram.php
M EducationProgram.settings.php
A includes/Settings.php
3 files changed, 151 insertions(+), 126 deletions(-)

Approvals:
  Jeroen De Dauw: Verified; Looks good to me, approved



diff --git a/EducationProgram.php b/EducationProgram.php
index e528d00..be697a5 100644
--- a/EducationProgram.php
+++ b/EducationProgram.php
@@ -59,7 +59,6 @@
 
 // Autoloading
 $wgAutoloadClasses['EducationProgram\Hooks']                                   
        = $dir . '/EducationProgram.hooks.php';
-$wgAutoloadClasses['EducationProgram\Settings']                                
        = $dir . '/EducationProgram.settings.php';
 
 // includes/actions (deriving from Action)
 $wgAutoloadClasses['EducationProgram\EditCourseAction']                        
= $dir . '/includes/actions/EditCourseAction.php';
@@ -161,6 +160,7 @@
 $wgAutoloadClasses['EducationProgram\RevisionDiff']                            
= $dir . '/includes/RevisionDiff.php';
 $wgAutoloadClasses['EducationProgram\DiffTable']                               
        = $dir . '/includes/DiffTable.php';
 $wgAutoloadClasses['EducationProgram\Menu']                                    
        = $dir . '/includes/Menu.php';
+$wgAutoloadClasses['EducationProgram\Settings']                                
        = $dir . '/includes/Settings.php';
 $wgAutoloadClasses['EducationProgram\Timeline']                                
        = $dir . '/includes/Timeline.php';
 $wgAutoloadClasses['EducationProgram\TimelineGroup']                           
= $dir . '/includes/TimelineGroup.php';
 
@@ -688,7 +688,7 @@
 
 unset( $moduleTemplate );
 
-$egEPSettings = array();
+require_once 'EducationProgram.settings.php';
 
 // The default value for the user preferences.
 $wgDefaultUserOptions['ep_showtoplink'] = false;
diff --git a/EducationProgram.settings.php b/EducationProgram.settings.php
index 551f438..7445df4 100644
--- a/EducationProgram.settings.php
+++ b/EducationProgram.settings.php
@@ -1,129 +1,56 @@
 <?php
 
-namespace EducationProgram;
-use MWException;
+global $wgExtensionAssetsPath, $wgScriptPath;
 
-/**
- * File defining the settings for the Education Program extension.
- * More info can be found at 
https://www.mediawiki.org/wiki/Extension:Education_Program#Settings
- *
- * NOTICE:
- * Changing one of these settings can be done by assigning to $egSettings,
- * AFTER the inclusion of the extension itself.
- *
- * @since 0.1
- *
- * @file EducationProgram.settings.php
- * @ingroup EducationProgram
- *
- * @licence GNU GPL v2+
- * @author Jeroen De Dauw < [email protected] >
- */
-class Settings {
+$epResourceDir = $egSWLScriptPath = $wgExtensionAssetsPath === false ? 
$wgScriptPath . '/extensions' : $wgExtensionAssetsPath;
+$epResourceDir .= '/EducationProgram/resources/';
 
-       /**
-        * Returns the default values for the settings.
-        * setting name (string) => setting value (mixed)
-        *
-        * @since 0.1
-        *
-        * @return array
-        */
-       protected static function getDefaultSettings() {
-               global $wgExtensionAssetsPath, $wgScriptPath;
+$egEPSettings = array(
+       'enableTopLink' => true,
+       'ambassadorPictureDomains' => array(
+               'wikimedia.org'
+       ),
+       'ambassadorCommonsUrl' => 
'https://commons.wikimedia.org/wiki/Special:UploadWizard',
+       'citylessCountries' => array(
+               'BT', 'BV', 'IO', 'VG', 'TD', 'CX', 'CC', 'KM', 'DJ', 'GQ', 
'FK', 'FX', 'TF', 'GW', 'HM', 'KI', 'YT',
+               'MS', 'NR', 'NU', 'NF', 'PN', 'SH', 'PM', 'WS', 'SC', 'GS', 
'SJ', 'TK', 'TP', 'TV', 'UM', 'VU', 'EH'
+       ),
+       'ambassadorImgWidth' => 140,
+       'ambassadorImgHeight' => 140,
+       'recentActivityLimit' => 24 * 60 * 60,
+       'resourceDir' => $epResourceDir,
+       'imageDir' => $epResourceDir . 'images/',
+       'flagWidth' => 25,
+       'flagHeight' => 25,
+       'countryFlags' => array(
+               'US' => 'Flag of the United States.svg',
+               'BR' => 'Flag of Brazil.svg',
+               'CA' => 'Flag of Canada.svg',
+               'IN' => 'Flag of India.svg',
+               'EG' => 'Flag of Egypt.svg',
+               'IT' => 'Flag of Italy.svg',
+               'MK' => 'Flag of Macedonia.svg',
+               'MX' => 'Flag of Mexico.svg',
+               'RU' => 'Flag of Russia.svg',
+               'UK' => 'Flag of the United Kingdom.svg',
+               'DE' => 'Flag of Germany.svg',
+               'NZ' => 'Flag of New Zealand.svg',
+               'CZ' => 'Flag of the Czech Republic.svg',
+       ),
+       'fallbackFlag' => 'Nuvola unknown flag.svg',
+       'courseDescPage' => 'MediaWiki:Course description',
+       'courseOrgDescPage' => '$2/$1', // $1 = org name, $2 = courseDescPage 
setting
+       'useStudentRealNames' => false,
+       'timelineDurationLimit' => 2 *24 * 60 *60,
+       'timelineCountLimit' => 42,
+       'timelineUserLimit' => 3,
+       'dykCategory' => 'Wikipedia:Education Program Did You Know',
+       'dykOrgCategory' => '$2/$1', // $1 = org name, $2 = dykCategory setting
+       'enableDykSetting' => true,
+       'timelineMessageLengthLimit' => 250,
+       'requireRealName' => false,
+       'collectRealName' => false,
+       'enablePageCache' => true,
+);
 
-               $resourceDir = $egSWLScriptPath = $wgExtensionAssetsPath === 
false ? $wgScriptPath . '/extensions' : $wgExtensionAssetsPath;
-               $resourceDir .= '/EducationProgram/resources/';
-
-               return array(
-                       'enableTopLink' => true,
-                       'ambassadorPictureDomains' => array(
-                               'wikimedia.org'
-                       ),
-                       'ambassadorCommonsUrl' => 
'https://commons.wikimedia.org/wiki/Special:UploadWizard',
-                       'citylessCountries' => array(
-                               'BT', 'BV', 'IO', 'VG', 'TD', 'CX', 'CC', 'KM', 
'DJ', 'GQ', 'FK', 'FX', 'TF', 'GW', 'HM', 'KI', 'YT',
-                               'MS', 'NR', 'NU', 'NF', 'PN', 'SH', 'PM', 'WS', 
'SC', 'GS', 'SJ', 'TK', 'TP', 'TV', 'UM', 'VU', 'EH'
-                       ),
-                       'ambassadorImgWidth' => 140,
-                       'ambassadorImgHeight' => 140,
-                       'recentActivityLimit' => 24 * 60 * 60,
-                       'resourceDir' => $resourceDir,
-                       'imageDir' => $resourceDir . 'images/',
-                       'flagWidth' => 25,
-                       'flagHeight' => 25,
-                       'countryFlags' => array(
-                               'US' => 'Flag of the United States.svg',
-                               'BR' => 'Flag of Brazil.svg',
-                               'CA' => 'Flag of Canada.svg',
-                               'IN' => 'Flag of India.svg',
-                               'EG' => 'Flag of Egypt.svg',
-                               'IT' => 'Flag of Italy.svg',
-                               'MK' => 'Flag of Macedonia.svg',
-                               'MX' => 'Flag of Mexico.svg',
-                               'RU' => 'Flag of Russia.svg',
-                               'UK' => 'Flag of the United Kingdom.svg',
-                               'DE' => 'Flag of Germany.svg',
-                               'NZ' => 'Flag of New Zealand.svg',
-                               'CZ' => 'Flag of the Czech Republic.svg',
-                       ),
-                       'fallbackFlag' => 'Nuvola unknown flag.svg',
-                       'courseDescPage' => 'MediaWiki:Course description',
-                       'courseOrgDescPage' => '$2/$1', // $1 = org name, $2 = 
courseDescPage setting
-                       'useStudentRealNames' => false,
-                       'timelineDurationLimit' => 2 *24 * 60 *60,
-                       'timelineCountLimit' => 42,
-                       'timelineUserLimit' => 3,
-                       'dykCategory' => 'Wikipedia:Education Program Did You 
Know',
-                       'dykOrgCategory' => '$2/$1', // $1 = org name, $2 = 
dykCategory setting
-                       'enableDykSetting' => true,
-                       'timelineMessageLengthLimit' => 250,
-                       'requireRealName' => false,
-                       'collectRealName' => false,
-                       'enablePageCache' => true,
-               );
-       }
-
-       /**
-        * Retruns an array with all settings after making sure they are
-        * initialized (ie set settings have been merged with the defaults).
-        * setting name (string) => setting value (mixed)
-        *
-        * @since 0.1
-        *
-        * @return array
-        */
-       public static function getSettings() {
-               static $settings = false;
-
-               if ( $settings === false ) {
-                       $settings = array_merge(
-                               self::getDefaultSettings(),
-                               $GLOBALS['egEPSettings']
-                       );
-               }
-
-               return $settings;
-       }
-
-       /**
-        * Gets the value of the specified setting.
-        *
-        * @since 0.1
-        *
-        * @param string $settingName
-        *
-        * @throws MWException
-        * @return mixed
-        */
-       public static function get( $settingName ) {
-               $settings = self::getSettings();
-
-               if ( !array_key_exists( $settingName, $settings ) ) {
-                       throw new MWException( 'Attempt to get non-existing 
setting "' . $settingName . '"' );
-               }
-
-               return $settings[$settingName];
-       }
-
-}
+unset( $epResourceDir );
diff --git a/includes/Settings.php b/includes/Settings.php
new file mode 100644
index 0000000..34e1630
--- /dev/null
+++ b/includes/Settings.php
@@ -0,0 +1,98 @@
+<?php
+
+namespace EducationProgram;
+
+/**
+ * Container for the settings contained by this extension.
+ *
+ * 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
+ *
+ * @since 0.3
+ *
+ * @file
+ * @ingroup EducationProgram
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class Settings {
+
+       /**
+        * Constructs a new instance of the settings object from global state.
+        *
+        * @since 0.3
+        *
+        * @param array $globalVariables
+        *
+        * @return Settings
+        */
+       public static function newFromGlobals( array $globalVariables ) {
+               return new self( $globalVariables['egEPSettings'] );
+       }
+
+       /**
+        * @since 0.3
+        *
+        * @var array
+        */
+       protected $settings;
+
+       /**
+        * Constructor.
+        *
+        * @since 0.3
+        *
+        * @param array $settings
+        */
+       public function __construct( array $settings ) {
+               $this->settings = $settings;
+       }
+
+       /**
+        * Returns the setting with the provided name.
+        * The specified setting needs to exist.
+        *
+        * @since 0.3
+        *
+        * @param string $settingName
+        *
+        * @return mixed
+        */
+       public function getSetting( $settingName ) {
+               return $this->settings[$settingName];
+       }
+
+       /**
+        * Gets the value of the specified setting.
+        *
+        * @since 0.1
+        * @deprecated since 0.3, use non-global state
+        *
+        * @param string $settingName
+        *
+        * @return mixed
+        */
+       public static function get( $settingName ) {
+               static $settings = null;
+
+               if ( $settings === null ) {
+                       $settings = static::newFromGlobals( $GLOBALS );
+               }
+
+               return $settings->getSetting( $settingName );
+       }
+
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/49675
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I84a4ba1ff170a9408655fe167d5d765dc76607d2
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/EducationProgram
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: CSteipp <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Ragesoss <[email protected]>
Gerrit-Reviewer: Reedy <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to