Config.php 4.57 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Laconica - a distributed open-source microblogging tool
 * Copyright (C) 2008, 2009, Control Yourself, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.     If not, see <http://www.gnu.org/licenses/>.
 */

20 21 22
if (!defined('STATUSNET')) {
    exit(1);
}
23

24 25 26
/**
 * Table Definition for config
 */
27 28

require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
29

30
class Config extends Managed_DataObject
31 32 33 34 35 36 37 38 39 40 41
{
    ###START_AUTOCODE
    /* the code below is auto generated do not remove the above tag */

    public $__table = 'config';                          // table name
    public $section;                         // varchar(32)  primary_key not_null
    public $setting;                         // varchar(32)  primary_key not_null
    public $value;                           // varchar(255)

    /* the code above is auto generated do not remove the tag below */
    ###END_AUTOCODE
42

43 44 45 46 47 48 49 50 51 52 53 54
    public static function schemaDef()
    {
        return array(
            'fields' => array(
                'section' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'default' => '', 'description' => 'configuration section'),
                'setting' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'default' => '', 'description' => 'configuration setting'),
                'value' => array('type' => 'varchar', 'length' => 255, 'description' => 'configuration value'),
            ),
            'primary key' => array('section', 'setting'),
        );
    }

55 56 57 58
    const settingsKey = 'config:settings';

    static function loadSettings()
    {
59 60 61 62 63 64 65
        try {
            $settings = self::_getSettings();
            if (!empty($settings)) {
                self::_applySettings($settings);
            }
        } catch (Exception $e) {
            return;
66 67 68 69 70 71 72 73
        }
    }

    static function _getSettings()
    {
        $c = self::memcache();

        if (!empty($c)) {
74
            $settings = $c->get(Cache::key(self::settingsKey));
75
            if ($settings !== false) {
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
                return $settings;
            }
        }

        $settings = array();

        $config = new Config();

        $config->find();

        while ($config->fetch()) {
            $settings[] = array($config->section, $config->setting, $config->value);
        }

        $config->free();

        if (!empty($c)) {
93
            $c->set(Cache::key(self::settingsKey), $settings);
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        }

        return $settings;
    }

    static function _applySettings($settings)
    {
        global $config;

        foreach ($settings as $s) {
            list($section, $setting, $value) = $s;
            $config[$section][$setting] = $value;
        }
    }

    function insert()
    {
        $result = parent::insert();
        if ($result) {
            Config::_blowSettingsCache();
        }
        return $result;
    }

    function delete()
    {
        $result = parent::delete();
        if ($result) {
            Config::_blowSettingsCache();
        }
        return $result;
    }

    function update($orig=null)
    {
        $result = parent::update($orig);
        if ($result) {
            Config::_blowSettingsCache();
        }
        return $result;
    }

136
    function pkeyGet($kv)
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    {
        return Memcached_DataObject::pkeyGet('Config', $kv);
    }

    static function save($section, $setting, $value)
    {
        $result = null;

        $config = Config::pkeyGet(array('section' => $section,
                                        'setting' => $setting));

        if (!empty($config)) {
            $orig = clone($config);
            $config->value = $value;
            $result = $config->update($orig);
        } else {
            $config = new Config();

            $config->section = $section;
            $config->setting = $setting;
            $config->value   = $value;

            $result = $config->insert();
        }

        return $result;
    }

165 166 167 168 169
    function _blowSettingsCache()
    {
        $c = self::memcache();

        if (!empty($c)) {
170
            $c->delete(Cache::key(self::settingsKey));
171 172
        }
    }
173
}