2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2009-01-04 14:38:48 +00:00
|
|
|
* an abstract interface for altering a name:value pair list
|
|
|
|
*/
|
|
|
|
interface Config {
|
2010-01-12 15:01:34 +00:00
|
|
|
/**
|
|
|
|
* Save the list of name:value pairs to wherever they came from,
|
|
|
|
* so that the next time a page is loaded it will use the new
|
|
|
|
* configuration
|
|
|
|
*/
|
2012-02-07 19:55:31 +00:00
|
|
|
public function save(/*string*/ $name=null);
|
2009-01-04 14:38:48 +00:00
|
|
|
|
2010-01-12 15:01:34 +00:00
|
|
|
/** @name set_*
|
|
|
|
* Set a configuration option to a new value, regardless
|
|
|
|
* of what the value is at the moment
|
|
|
|
*/
|
|
|
|
//@{
|
2012-02-07 19:55:31 +00:00
|
|
|
public function set_int(/*string*/ $name, $value);
|
|
|
|
public function set_string(/*string*/ $name, $value);
|
|
|
|
public function set_bool(/*string*/ $name, $value);
|
|
|
|
public function set_array(/*string*/ $name, $value);
|
2010-01-12 15:01:34 +00:00
|
|
|
//@}
|
2009-01-04 14:38:48 +00:00
|
|
|
|
2010-01-12 15:01:34 +00:00
|
|
|
/** @name set_default_*
|
|
|
|
* Set a configuration option to a new value, if there is no
|
|
|
|
* value currently. Extensions should generally call these
|
|
|
|
* from their InitExtEvent handlers. This has the advantage
|
|
|
|
* that the values will show up in the "advanced" setup page
|
|
|
|
* where they can be modified, while calling get_* with a
|
|
|
|
* "default" paramater won't show up.
|
|
|
|
*/
|
|
|
|
//@{
|
2012-02-07 19:55:31 +00:00
|
|
|
public function set_default_int(/*string*/ $name, $value);
|
|
|
|
public function set_default_string(/*string*/ $name, $value);
|
|
|
|
public function set_default_bool(/*string*/ $name, $value);
|
|
|
|
public function set_default_array(/*string*/ $name, $value);
|
2010-01-12 15:01:34 +00:00
|
|
|
//@}
|
2009-01-04 14:38:48 +00:00
|
|
|
|
2010-01-12 15:01:34 +00:00
|
|
|
/** @name get_*
|
|
|
|
* pick a value out of the table by name, cast to the
|
|
|
|
* appropritate data type
|
|
|
|
*/
|
|
|
|
//@{
|
2012-02-07 19:55:31 +00:00
|
|
|
public function get_int(/*string*/ $name, $default=null);
|
|
|
|
public function get_string(/*string*/ $name, $default=null);
|
|
|
|
public function get_bool(/*string*/ $name, $default=null);
|
|
|
|
public function get_array(/*string*/ $name, $default=array());
|
2010-01-12 15:01:34 +00:00
|
|
|
//@}
|
2009-01-04 14:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2009-01-04 14:38:48 +00:00
|
|
|
* Common methods for manipulating the list, loading and saving is
|
|
|
|
* left to the concrete implementation
|
|
|
|
*/
|
|
|
|
abstract class BaseConfig implements Config {
|
|
|
|
var $values = array();
|
|
|
|
|
2012-02-07 19:55:31 +00:00
|
|
|
public function set_int(/*string*/ $name, $value) {
|
2009-01-04 14:38:48 +00:00
|
|
|
$this->values[$name] = parse_shorthand_int($value);
|
|
|
|
$this->save($name);
|
|
|
|
}
|
2012-02-07 19:55:31 +00:00
|
|
|
public function set_string(/*string*/ $name, $value) {
|
2009-01-04 14:38:48 +00:00
|
|
|
$this->values[$name] = $value;
|
|
|
|
$this->save($name);
|
|
|
|
}
|
2012-02-07 19:55:31 +00:00
|
|
|
public function set_bool(/*string*/ $name, $value) {
|
2009-01-04 14:38:48 +00:00
|
|
|
$this->values[$name] = (($value == 'on' || $value === true) ? 'Y' : 'N');
|
|
|
|
$this->save($name);
|
|
|
|
}
|
2012-02-07 19:55:31 +00:00
|
|
|
public function set_array(/*string*/ $name, $value) {
|
2013-10-04 21:53:26 +00:00
|
|
|
assert(isset($value) && is_array($value));
|
2009-07-11 11:43:18 +00:00
|
|
|
$this->values[$name] = implode(",", $value);
|
|
|
|
$this->save($name);
|
|
|
|
}
|
2009-01-04 14:38:48 +00:00
|
|
|
|
2012-02-07 19:55:31 +00:00
|
|
|
public function set_default_int(/*string*/ $name, $value) {
|
2009-01-04 14:38:48 +00:00
|
|
|
if(is_null($this->get($name))) {
|
|
|
|
$this->values[$name] = parse_shorthand_int($value);
|
|
|
|
}
|
|
|
|
}
|
2012-02-07 19:55:31 +00:00
|
|
|
public function set_default_string(/*string*/ $name, $value) {
|
2009-01-04 14:38:48 +00:00
|
|
|
if(is_null($this->get($name))) {
|
|
|
|
$this->values[$name] = $value;
|
|
|
|
}
|
|
|
|
}
|
2012-02-07 19:55:31 +00:00
|
|
|
public function set_default_bool(/*string*/ $name, $value) {
|
2009-01-04 14:38:48 +00:00
|
|
|
if(is_null($this->get($name))) {
|
|
|
|
$this->values[$name] = (($value == 'on' || $value === true) ? 'Y' : 'N');
|
|
|
|
}
|
|
|
|
}
|
2012-02-07 19:55:31 +00:00
|
|
|
public function set_default_array(/*string*/ $name, $value) {
|
2013-10-04 21:53:26 +00:00
|
|
|
assert(isset($value) && is_array($value));
|
2009-07-11 11:43:18 +00:00
|
|
|
if(is_null($this->get($name))) {
|
|
|
|
$this->values[$name] = implode(",", $value);
|
|
|
|
}
|
|
|
|
}
|
2009-01-04 14:38:48 +00:00
|
|
|
|
2012-02-07 19:55:31 +00:00
|
|
|
public function get_int(/*string*/ $name, $default=null) {
|
2009-01-04 14:38:48 +00:00
|
|
|
return (int)($this->get($name, $default));
|
|
|
|
}
|
2012-02-07 19:55:31 +00:00
|
|
|
public function get_string(/*string*/ $name, $default=null) {
|
2009-01-04 14:38:48 +00:00
|
|
|
return $this->get($name, $default);
|
|
|
|
}
|
2012-02-07 19:55:31 +00:00
|
|
|
public function get_bool(/*string*/ $name, $default=null) {
|
2012-04-15 23:28:27 +00:00
|
|
|
return bool_escape($this->get($name, $default));
|
2009-01-04 14:38:48 +00:00
|
|
|
}
|
2012-02-07 19:55:31 +00:00
|
|
|
public function get_array(/*string*/ $name, $default=array()) {
|
2009-07-11 11:43:18 +00:00
|
|
|
return explode(",", $this->get($name, ""));
|
|
|
|
}
|
2009-01-04 14:38:48 +00:00
|
|
|
|
2012-02-07 19:55:31 +00:00
|
|
|
private function get(/*string*/ $name, $default=null) {
|
2009-01-04 14:38:48 +00:00
|
|
|
if(isset($this->values[$name])) {
|
|
|
|
return $this->values[$name];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-15 17:31:28 +00:00
|
|
|
/**
|
|
|
|
* For testing, mostly
|
|
|
|
*/
|
|
|
|
class HardcodeConfig extends BaseConfig {
|
|
|
|
public function __construct($dict) {
|
|
|
|
$this->values = $dict;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function save(/*string*/ $name=null) {
|
|
|
|
// static config is static
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2009-01-04 15:57:54 +00:00
|
|
|
* Loads the config list from a PHP file; the file should be in the format:
|
|
|
|
*
|
|
|
|
* <?php
|
|
|
|
* $config['foo'] = "bar";
|
|
|
|
* $config['baz'] = "qux";
|
|
|
|
* ?>
|
|
|
|
*/
|
|
|
|
class StaticConfig extends BaseConfig {
|
|
|
|
public function __construct($filename) {
|
|
|
|
if(file_exists($filename)) {
|
|
|
|
require_once $filename;
|
|
|
|
if(isset($config)) {
|
|
|
|
$this->values = $config;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Exception("Config file '$filename' doesn't contain any config");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Exception("Config file '$filename' missing");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-07 19:55:31 +00:00
|
|
|
public function save(/*string*/ $name=null) {
|
2009-01-04 15:57:54 +00:00
|
|
|
// static config is static
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2009-01-04 15:57:54 +00:00
|
|
|
* Loads the config list from a table in a given database, the table should
|
|
|
|
* be called config and have the schema:
|
|
|
|
*
|
2009-07-21 06:36:12 +00:00
|
|
|
* \code
|
2009-01-04 15:57:54 +00:00
|
|
|
* CREATE TABLE config(
|
|
|
|
* name VARCHAR(255) NOT NULL,
|
|
|
|
* value TEXT
|
|
|
|
* );
|
2009-07-21 06:36:12 +00:00
|
|
|
* \endcode
|
2007-12-06 11:01:18 +00:00
|
|
|
*/
|
2009-01-04 14:40:35 +00:00
|
|
|
class DatabaseConfig extends BaseConfig {
|
2007-07-28 13:53:31 +00:00
|
|
|
var $database = null;
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2007-12-06 11:01:18 +00:00
|
|
|
/*
|
|
|
|
* Load the config table from a database
|
|
|
|
*/
|
2014-03-22 09:00:59 +00:00
|
|
|
public function __construct(Database $database) {
|
2007-07-28 13:53:31 +00:00
|
|
|
$this->database = $database;
|
2009-01-19 18:47:33 +00:00
|
|
|
|
2009-01-22 07:21:03 +00:00
|
|
|
$cached = $this->database->cache->get("config");
|
2009-01-19 18:47:33 +00:00
|
|
|
if($cached) {
|
|
|
|
$this->values = $cached;
|
|
|
|
}
|
|
|
|
else {
|
2011-01-01 15:28:30 +00:00
|
|
|
$this->values = array();
|
|
|
|
foreach($this->database->get_all("SELECT name, value FROM config") as $row) {
|
|
|
|
$this->values[$row["name"]] = $row["value"];
|
|
|
|
}
|
2009-01-22 07:21:03 +00:00
|
|
|
$this->database->cache->set("config", $this->values);
|
2009-01-19 18:47:33 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2007-12-06 11:01:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Save the current values as the new config table
|
|
|
|
*/
|
2012-02-07 19:55:31 +00:00
|
|
|
public function save(/*string*/ $name=null) {
|
2007-04-16 11:58:25 +00:00
|
|
|
if(is_null($name)) {
|
2012-02-04 20:35:21 +00:00
|
|
|
reset($this->values); // rewind the array to the first element
|
2007-04-16 11:58:25 +00:00
|
|
|
foreach($this->values as $name => $value) {
|
2012-02-07 19:55:31 +00:00
|
|
|
$this->save(/*string*/ $name);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2011-01-01 15:28:30 +00:00
|
|
|
$this->database->Execute("DELETE FROM config WHERE name = :name", array("name"=>$name));
|
|
|
|
$this->database->Execute("INSERT INTO config VALUES (:name, :value)", array("name"=>$name, "value"=>$this->values[$name]));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2013-08-05 19:21:46 +00:00
|
|
|
// rather than deleting and having some other request(s) do a thundering
|
|
|
|
// herd of race-conditioned updates, just save the updated version once here
|
|
|
|
$this->database->cache->set("config", $this->values);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-16 21:58:47 +00:00
|
|
|
|
|
|
|
class MockConfig extends HardcodeConfig {
|
|
|
|
public function __construct($config=array()) {
|
|
|
|
$config["db_version"] = "999";
|
|
|
|
$config["anon_id"] = "0";
|
|
|
|
parent::__construct($config);
|
|
|
|
}
|
|
|
|
}
|
2014-04-24 23:01:47 +00:00
|
|
|
|