From: Chad Horohoe Date: Mon, 16 May 2011 21:04:55 +0000 (+0000) Subject: Initial commit of configuration management backend proposal. Feedback desired before... X-Git-Tag: 1.31.0-rc.0~30120 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/comptes/ajouter.php?a=commitdiff_plain;h=7131283a7bcf26786ffe05fb4fbc1d3c3dd3ea11;p=lhc%2Fweb%2Fwiklou.git Initial commit of configuration management backend proposal. Feedback desired before I go much further. * 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... * --- diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index a6f34eb2e5..64b2e020c3 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -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 index 0000000000..99dc38b39c --- /dev/null +++ b/includes/conf/Conf.php @@ -0,0 +1,136 @@ + + * 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 index 0000000000..9b1f6efb0a --- /dev/null +++ b/includes/conf/DatabaseConf.php @@ -0,0 +1,33 @@ + + * 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 index 0000000000..4601f048a4 --- /dev/null +++ b/includes/conf/DefaultSettings.php @@ -0,0 +1,28 @@ + + * 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 index 0000000000..afaeb76e29 --- /dev/null +++ b/maintenance/archives/patch-config.sql @@ -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)); diff --git a/maintenance/tables.sql b/maintenance/tables.sql index 5ccc61e2aa..69630e6398 100644 --- a/maintenance/tables.sql +++ b/maintenance/tables.sql @@ -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