http://www.mediawiki.org/wiki/Special:Code/MediaWiki/88270
Revision: 88270
Author: demon
Date: 2011-05-16 21:04:55 +0000 (Mon, 16 May 2011)
Log Message:
-----------
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...
*
Modified Paths:
--------------
trunk/phase3/includes/AutoLoader.php
trunk/phase3/maintenance/tables.sql
Added Paths:
-----------
trunk/phase3/includes/conf/
trunk/phase3/includes/conf/Conf.php
trunk/phase3/includes/conf/DatabaseConf.php
trunk/phase3/includes/conf/DefaultSettings.php
trunk/phase3/maintenance/archives/patch-config.sql
Modified: trunk/phase3/includes/AutoLoader.php
===================================================================
--- trunk/phase3/includes/AutoLoader.php 2011-05-16 20:59:38 UTC (rev
88269)
+++ trunk/phase3/includes/AutoLoader.php 2011-05-16 21:04:55 UTC (rev
88270)
@@ -343,6 +343,7 @@
'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 @@
'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',
Added: trunk/phase3/includes/conf/Conf.php
===================================================================
--- trunk/phase3/includes/conf/Conf.php (rev 0)
+++ trunk/phase3/includes/conf/Conf.php 2011-05-16 21:04:55 UTC (rev 88270)
@@ -0,0 +1,136 @@
+<?php
+/**
+ * Base configuration class.
+ *
+ * Get some configuration variable:
+ * $mySetting = Conf::get( 'mySetting' );
+ *
+ * Copyright © 2011 Chad Horohoe <[email protected]>
+ * 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;
+ }
+}
Property changes on: trunk/phase3/includes/conf/Conf.php
___________________________________________________________________
Added: svn:executable
+ *
Added: svn:eol-style
+ native
Added: trunk/phase3/includes/conf/DatabaseConf.php
===================================================================
--- trunk/phase3/includes/conf/DatabaseConf.php (rev 0)
+++ trunk/phase3/includes/conf/DatabaseConf.php 2011-05-16 21:04:55 UTC (rev
88270)
@@ -0,0 +1,33 @@
+<?php
+/**
+ * Database configuration class
+ *
+ * Copyright © 2011 Chad Horohoe <[email protected]>
+ * 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;
+ }
+ }
+}
Property changes on: trunk/phase3/includes/conf/DatabaseConf.php
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/phase3/includes/conf/DefaultSettings.php
===================================================================
--- trunk/phase3/includes/conf/DefaultSettings.php
(rev 0)
+++ trunk/phase3/includes/conf/DefaultSettings.php 2011-05-16 21:04:55 UTC
(rev 88270)
@@ -0,0 +1,28 @@
+<?php
+/**
+ * Utility class for holding all of our default settings.
+ *
+ * Copyright © 2011 Chad Horohoe <[email protected]>
+ * 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';
+}
Property changes on: trunk/phase3/includes/conf/DefaultSettings.php
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/phase3/maintenance/archives/patch-config.sql
===================================================================
--- trunk/phase3/maintenance/archives/patch-config.sql
(rev 0)
+++ trunk/phase3/maintenance/archives/patch-config.sql 2011-05-16 21:04:55 UTC
(rev 88270)
@@ -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));
Property changes on: trunk/phase3/maintenance/archives/patch-config.sql
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: trunk/phase3/maintenance/tables.sql
===================================================================
--- trunk/phase3/maintenance/tables.sql 2011-05-16 20:59:38 UTC (rev 88269)
+++ trunk/phase3/maintenance/tables.sql 2011-05-16 21:04:55 UTC (rev 88270)
@@ -1409,4 +1409,14 @@
) /*$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
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs