http://www.mediawiki.org/wiki/Special:Code/MediaWiki/88281

Revision: 88281
Author:   demon
Date:     2011-05-17 00:04:49 +0000 (Tue, 17 May 2011)
Log Message:
-----------
More config stuff: method for applying settings, getting defaults, doc fixes, 
etc

Modified Paths:
--------------
    trunk/phase3/includes/conf/Conf.php
    trunk/phase3/includes/conf/DatabaseConf.php

Modified: trunk/phase3/includes/conf/Conf.php
===================================================================
--- trunk/phase3/includes/conf/Conf.php 2011-05-16 23:36:39 UTC (rev 88280)
+++ trunk/phase3/includes/conf/Conf.php 2011-05-17 00:04:49 UTC (rev 88281)
@@ -29,6 +29,15 @@
  */
 abstract class Conf {
        /**
+        * A special value to return when default config items do not exist. Use
+        * this to differentiate from 'null' which may be a valid config value.
+        *
+        * Please don't ever make this a default (or accepted) value for your
+        * configuration. It's liable to Break Something.
+        */
+       const NO_SUCH_DEFAULT_CONFIG = 'mw-no-such-default-config';
+
+       /**
         * The Wiki ID (usually $wgDBname)
         * @var String
         */
@@ -79,6 +88,13 @@
        abstract protected function initChangedSettings();
 
        /**
+        * Apply a setting to the backend store
+        * @param $name String Name of the setting
+        * @param $value mixed Value to store
+        */
+       abstract protected function writeSetting( $name, $value );
+
+       /**
         * Initialize a new child class based on a configuration array
         * @param $conf Array of configuration settings, see $wgConfiguration
         *   for details
@@ -123,11 +139,12 @@
 
        /**
         * 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 ) {
+               // isset() is ok here, because the default is to return null 
anyway.
                if( isset( $this->values[$name] ) ) {
                        return $this->values[$name];
                } elseif( isset( $this->extensionDefaults[$name] ) ) {
@@ -141,6 +158,39 @@
        }
 
        /**
+        * Apply a setting to the configuration object.
+        * @param $name String Name of the config item
+        * @param $value mixed Any value to use for the key
+        * @param $write bool Whether to write to the static copy (db, file, 
etc)
+        */
+       public function applySetting( $name, $value, $write = false ) {
+               $this->values[$name] = $value;
+               if( $write && ( $value !== $this->getDefaultSetting( $name ) ) 
) {
+                       $this->writeSetting( $name, $value );
+               }
+       }
+
+       /**
+        * Get the default for a given setting name. Check core and then 
extensions.
+        * Will return NO_SUCH_DEFAULT_CONFIG if the config item does not exist.
+        *
+        * @param $name String Name of setting
+        * @return mixed
+        */
+       public function getDefaultSetting( $name ) {
+               // Use array_key_exists() here, to make sure we return a default
+               // that's really set to null.
+               if( array_key_exists( $name, $this->defaults ) ) {
+                       return $this->defaults[$name];
+               } elseif( array_key_exists( $name, $this->extensionDefaults ) ) 
{
+                       return $this->extensionDefaults[$name];
+               } else {
+                       wfDebug( __METHOD__ . " called for unknown 
configuration item '$name'\n" );
+                       return self::NO_SUCH_DEFAULT_CONFIG;
+               }
+       }
+
+       /**
         * What is the wiki ID for this site?
         * @return String
         */

Modified: trunk/phase3/includes/conf/DatabaseConf.php
===================================================================
--- trunk/phase3/includes/conf/DatabaseConf.php 2011-05-16 23:36:39 UTC (rev 
88280)
+++ trunk/phase3/includes/conf/DatabaseConf.php 2011-05-17 00:04:49 UTC (rev 
88281)
@@ -24,10 +24,29 @@
  * @ingroup Config
  */
 class DatabaseConf extends Conf {
+       /**
+        * @see Conf::initChangedSettings()
+        */
        protected function initChangedSettings() {
                $res = wfGetDB( DB_MASTER )->select( 'config', '*', array(), 
__METHOD__ );
                foreach( $res as $row ) {
-                       $this->values[$row->cf_name] = $row->cf_value;
+                       $this->values[$row->cf_name] = unserialize( 
$row->cf_value );
                }
        }
+
+       /**
+        * @see Conf::writeSetting()
+        */
+       protected function writeSetting( $name, $value ) {
+               $dbw = wfGetDB( DB_MASTER );
+               $value = serialize( $value );
+               if( $dbw->selectRow( 'config', 'cf_name', array( 'cf_name' => 
$name ), __METHOD__ ) ) {
+                       $dbw->update( 'config', array( 'cf_value' => $value ),
+                               array( 'cf_name' => $name ), __METHOD__ );
+               } else {
+                       $dbw->insert( 'config',
+                               array( 'cf_name' => $name, 'cf_value' => $value 
), __METHOD__ );
+               }
+               return (bool)$db->affectedRows();
+       }
 }


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

Reply via email to