Initial commit of configuration management backend proposal. Feedback desired before...
authorChad Horohoe <demon@users.mediawiki.org>
Mon, 16 May 2011 21:04:55 +0000 (21:04 +0000)
committerChad Horohoe <demon@users.mediawiki.org>
Mon, 16 May 2011 21:04:55 +0000 (21:04 +0000)
* Common use case is Conf::get( 'myVar' );
* Support for default install (new `config` table) added, should be trivial to add backends for CDB, Memcache, etc...
*

includes/AutoLoader.php
includes/conf/Conf.php [new file with mode: 0755]
includes/conf/DatabaseConf.php [new file with mode: 0644]
includes/conf/DefaultSettings.php [new file with mode: 0644]
maintenance/archives/patch-config.sql [new file with mode: 0644]
maintenance/tables.sql

index a6f34eb..64b2e02 100644 (file)
@@ -343,6 +343,7 @@ $wgAutoloadLocalClasses = array(
        'ApiUpload' => 'includes/api/ApiUpload.php',
        'ApiUserrights' => 'includes/api/ApiUserrights.php',
        'ApiWatch' => 'includes/api/ApiWatch.php',
+       'UsageException' => 'includes/api/ApiMain.php',
 
        # includes/cache
        'CacheDependency' => 'includes/cache/CacheDependency.php',
@@ -360,7 +361,10 @@ $wgAutoloadLocalClasses = array(
        'TitleDependency' => 'includes/cache/CacheDependency.php',
        'TitleListDependency' => 'includes/cache/CacheDependency.php',
 
-       'UsageException' => 'includes/api/ApiMain.php',
+       # includes/conf
+       'Conf' => 'includes/conf/Conf.php',
+       'DatabaseConf' => 'includes/conf/DatabaseConf.php',
+       'DefaultSettings' => 'includes/conf/DefaultSettings.php',
 
        # includes/db
        'Blob' => 'includes/db/Database.php',
diff --git a/includes/conf/Conf.php b/includes/conf/Conf.php
new file mode 100755 (executable)
index 0000000..99dc38b
--- /dev/null
@@ -0,0 +1,136 @@
+<?php
+/**
+ * Base configuration class.
+ *
+ * Get some configuration variable:
+ *   $mySetting = Conf::get( 'mySetting' );
+ *
+ * Copyright © 2011 Chad Horohoe <chadh@wikimedia.org>
+ * http://www.mediawiki.org/
+ *
+ * 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
+ *
+ * @file
+ * @defgroup Config Config
+ * @ingroup Config
+ */
+abstract class Conf {
+       /**
+        * The Wiki ID (usually $wgDBname)
+        * @var String
+        */
+       private $wikiId;
+
+       /**
+        * Singleton
+        * @var Conf
+        */
+       private static $__instance;
+
+       /**
+        * Stores of the core defaults, extension defaults and wiki overrides
+        *
+        * @var array
+        */
+       protected $defaults, $extensionDefaults, $values = array();
+
+       /**
+        * Constructor. Children should call this if implementing.
+        * @param $confConfig Array of config vars
+        */
+       protected function __construct( $confConfig ) {
+               $this->wikiId = $confConfig['wikiId'];
+               $this->defaults = (array)(new DefaultSettings);
+               // @todo implement this:
+               // $this->initExtensionDefaults();
+               $this->initChangedSettings();
+       }
+
+       /**
+        * Load customized settings from whatever the data store is
+        */
+       abstract protected function initChangedSettings();
+
+       /**
+        * Initialize a new child class based on a configuration array
+        * @param $conf Array of configuration settings, see $wgConfiguration
+        *   for details
+        * @return Conf
+        */
+       private static function newFromSettings( $conf ) {
+               $class = ucfirst( $conf['type'] ) . 'Conf';
+               if( !class_exists( $class ) ) {
+                       throw new MWException( '$wgConfiguration misconfigured with invalid "type"' );
+               }
+               return new $class( $conf );
+       }
+
+       /**
+        * Get the singleton if we don't want a specific wiki
+        * @param $wiki String An id for a remote wiki
+        * @return Conf child
+        */
+       public static function load( $wiki = false ) {
+               if( !self::$__instance ) {
+                       global $wgConfiguration;
+                       self::$__instance = self::newFromSettings( $wgConfiguration );
+               }
+               if( $wiki && $wiki != self::$__instance->getWikiId() ) {
+                       // Load configuration for a different wiki, not sure how
+                       // we're gonna do this yet
+                       return null;
+               }
+               return self::$__instance;
+       }
+
+       /**
+        * Get a property from the configuration database, falling back
+        * to DefaultSettings if undefined
+        * @param $name String Name of setting to retrieve.
+        * @param $wiki String An id for a remote wiki
+        * @return mixed
+        */
+       public static function get( $name, $wiki = false ) {
+               return self::load( $wiki )->retrieveSetting( $name );
+       }
+
+       /**
+        * Actually get the setting, checking overrides, extensions, then core.
+        * On failure, 
+        * @param $name String Name of setting to get
+        * @return mixed
+        */
+       public function retrieveSetting( $name ) {
+               if( isset( $this->values[$name] ) ) {
+                       return $this->values[$name];
+               } elseif( isset( $this->extensionDefaults[$name] ) ) {
+                       return $this->extensionDefaults[$name];
+               } elseif( isset( $this->defaults[$name] ) ) {
+                       return $this->defaults[$name];
+               } else {
+                       wfDebug( __METHOD__ . " called for unknown configuration item '$name'\n" );
+                       return null;
+               }
+       }
+
+       /**
+        * What is the wiki ID for this site?
+        * @return String
+        */
+       public function getWikiId() {
+               return $this->wikiId;
+       }
+}
diff --git a/includes/conf/DatabaseConf.php b/includes/conf/DatabaseConf.php
new file mode 100644 (file)
index 0000000..9b1f6ef
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+/**
+ * Database configuration class
+ *
+ * Copyright © 2011 Chad Horohoe <chadh@wikimedia.org>
+ * http://www.mediawiki.org/
+ *
+ * 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
+ *
+ * @file
+ * @ingroup Config
+ */
+class DatabaseConf extends Conf {
+       protected function initChangedSettings() {
+               $res = wfGetDB( DB_MASTER )->select( 'config', '*', array(), __METHOD__ );
+               foreach( $res as $row ) {
+                       $this->values[$row->cf_name] = $row->cf_value;
+               }
+       }
+}
diff --git a/includes/conf/DefaultSettings.php b/includes/conf/DefaultSettings.php
new file mode 100644 (file)
index 0000000..4601f04
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/**
+ * Utility class for holding all of our default settings.
+ *
+ * Copyright © 2011 Chad Horohoe <chadh@wikimedia.org>
+ * http://www.mediawiki.org/
+ *
+ * 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
+ *
+ * @file
+ * @ingroup Config
+ */
+final class DefaultSettings {
+       public $mySetting = 'defaultValue';
+}
diff --git a/maintenance/archives/patch-config.sql b/maintenance/archives/patch-config.sql
new file mode 100644 (file)
index 0000000..afaeb76
--- /dev/null
@@ -0,0 +1,9 @@
+-- Table for holding configuration changes
+CREATE TABLE /*_*/config (
+  -- Config var name
+  cf_name varbinary(255) NOT NULL PRIMARY KEY,
+  -- Config var value
+  cf_value blob NOT NULL,
+) /*$wgDBTableOptions*/;
+-- Should cover *most* configuration - strings, ints, bools, etc.
+CREATE INDEX /*i*/cf_name_value ON /*_*/config (cf_name,cf_value(255));
index 5ccc61e..69630e6 100644 (file)
@@ -1409,4 +1409,14 @@ CREATE TABLE /*_*/module_deps (
 ) /*$wgDBTableOptions*/;
 CREATE UNIQUE INDEX /*i*/md_module_skin ON /*_*/module_deps (md_module, md_skin);
 
+-- Table for holding configuration changes
+CREATE TABLE /*_*/config (
+  -- Config var name
+  cf_name varbinary(255) NOT NULL PRIMARY KEY,
+  -- Config var value
+  cf_value blob NOT NULL,
+) /*$wgDBTableOptions*/;
+-- Should cover *most* configuration - strings, ints, bools, etc.
+CREATE INDEX /*i*/cf_name_value ON /*_*/config (cf_name,cf_value(255));
+
 -- vim: sw=2 sts=2 et