[ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 4 lt_include( PLOG_CLASS_PATH."class/config/configabstractstorage.class.php" ); 5 lt_include( PLOG_CLASS_PATH."class/cache/cachemanager.class.php" ); 6 lt_include( PLOG_CLASS_PATH.'class/dao/daocacheconstants.properties.php' ); 7 8 /** 9 * \ingroup Config 10 * 11 * Storage backend that stores/retrieves the data from the plog_config 12 * table. 13 * The structore of the table is as follows: 14 * 15 * - id: setting identifier 16 * - config_key: Name of the setting. Can't be empty 17 * - config_value: Value assigned to the key 18 * - value_type: This field can take several values and gives the class 19 * a hint regardign the type of the value: 20 * -- 1: integer. The config_value field represents is value. 21 * -- 2: boolean. It is saved as 1 == true and 0 == false. 22 * -- 3: string. It is saved as is. 23 * -- 4: object. The object is saved in a seralized way. 24 * -- 5: array. The arrays are also saved serialized. This is transparently 25 * done inside the save() and saveValue() methods, and therefore the user 26 * does not have to worry about doing it. 27 * -- 6: float. It is saved as is. 28 * 29 * Type detection is provided via the built-in mechanisms that PHP offers. 30 * </ul> 31 */ 32 class ConfigDbStorage extends ConfigAbstractStorage 33 { 34 // ADOdb handler 35 var $_db; 36 37 // array used to store the options 38 var $_data = array(); 39 40 // information needed to connect to the db server 41 var $_dbPrefix; 42 43 // cache object 44 var $_cache; 45 46 /** 47 * Connects to the database using the parameters in the config file. 48 * 49 */ 50 function ConfigDbStorage() 51 { 52 $this->_cache =& CacheManager::getCache(); 53 54 // load data from the databas, but only if it was already not in the cache 55 if( (!$this->_data = $this->_cache->getData( CACHE_CONFIGDBSTORAGE, CACHE_GLOBAL ))) { 56 $this->_loadAllValuesFromDatabase(); 57 } 58 } 59 60 function _loadAllValuesFromDatabase() 61 { 62 // initialize the database 63 $this->_initializeDatabase(); 64 65 // load the whole data 66 $this->_loadData(); 67 68 // and build the cache 69 $this->_cache->setData( CACHE_CONFIGDBSTORAGE, CACHE_GLOBAL, $this->_data ); 70 } 71 72 /** 73 * Initialize the Database to allow db access 74 * 75 */ 76 function _initializeDatabase() 77 { 78 if ($this->_db == null) { 79 // source the neccessary class files 80 lt_include( PLOG_CLASS_PATH."class/database/db.class.php" ); 81 82 // initialize the connection 83 $this->_db =& Db::getDb(); 84 // get the prefix 85 $this->_dbPrefix = Db::getPrefix(); 86 } 87 } 88 89 /** 90 * Internal function that loads all the data from the table and puts in into 91 * our array. It should be apparently faster that making an SQL query every time 92 * we need to get a value. 93 * 94 * @private 95 */ 96 function _loadData() 97 { 98 $this->_data = Array(); 99 100 // build and execute the query 101 $query = "SELECT * FROM ".$this->_dbPrefix."config"; 102 $result = $this->_db->Execute( $query ); 103 104 // this is a severe error 105 if( !$result ) { 106 print($this->_db->ErrorMsg()); 107 throw( new Exception( "There was an error loading the configuration data from the database. And this is bad..." )); 108 die(); 109 } 110 111 // otherwise, go through the records and put them in the array 112 while( $row = $result->FetchRow()) { 113 $key = $row["config_key"]; 114 $value = $row["config_value"]; 115 $dataType = $row["value_type"]; 116 117 // arrays and objects are saved serialized so we should check 118 // the type and deserialize that if necessary 119 if( $dataType == TYPE_OBJECT || $dataType == TYPE_ARRAY ) { 120 $this->_data[$key] = unserialize( stripslashes($value)); 121 if( $dataType == TYPE_ARRAY && $this->_data[$key] == "" ) 122 $this->_data[$key] = Array(); 123 } 124 else 125 $this->_data[$key] = $value; 126 } 127 $result->Close(); 128 129 return true; 130 } 131 132 /** 133 * @see ConfigAbstractStorage::getValue() 134 */ 135 function getValue( $key, $defaultValue = null ) 136 { 137 if( array_key_exists($key, $this->_data) ) { 138 if ($this->_data[$key] == "" || $this->_data[$key] == null) { 139 return $defaultValue; 140 } else { 141 return $this->_data[$key]; 142 } 143 } else { 144 return $defaultValue; 145 } 146 } 147 148 /** 149 * @see ConfigAbstractStorage::setValue() 150 */ 151 function setValue( $key, $value ) 152 { 153 $this->_data[$key] = $value; 154 155 return true; 156 } 157 158 /** 159 * @see ConfigAbstractStorage::getAsArray() 160 */ 161 function getAsArray() 162 { 163 return $this->_data; 164 } 165 166 /** 167 * @see ConfigAbstractStorage::getConfigFileName() 168 */ 169 function getConfigFileName() 170 { 171 return "database"; 172 } 173 174 /** 175 * Resets the current configuration settings and loads them from the database. 176 * 177 * @see ConfigAbstractStorage::reload() 178 */ 179 function reload() 180 { 181 $this->_loadData(); 182 } 183 184 /** 185 * @see ConfigAbstractStorage::getKeys() 186 */ 187 function getKeys() 188 { 189 return array_keys($this->_data); 190 } 191 192 /** 193 * @see ConfigAbstractStorage::getValues() 194 */ 195 function getValues() 196 { 197 return array_values($this->_data); 198 } 199 200 /** 201 * Internal function that returns true if the given key exists in the database. 202 * 203 * @private 204 * @param key The name of the key we'd like to check 205 * @return Returns true if it exists or false otherwise. 206 */ 207 function _keyExists( $key ) 208 { 209 // initialize the database 210 $this->_initializeDatabase(); 211 212 $query = "SELECT * FROM ".$this->_dbPrefix."config WHERE config_key = '$key'"; 213 214 //$this->_db->debug=true; 215 $result = $this->_db->Execute( $query ); 216 217 if( !$result ) 218 return false; 219 220 $ret = ($result->RowCount() > 0); 221 $result->Close(); 222 if($ret) 223 return true; 224 else 225 return false; 226 } 227 228 /** 229 * @private 230 */ 231 function _updateValue( $key, $value ) 232 { 233 // initialize the database 234 $this->_initializeDatabase(); 235 236 // if the key exists, we have to update it 237 $type = $this->_getType( $value ); 238 switch( $type ) { 239 case TYPE_INTEGER: 240 case TYPE_BOOLEAN: 241 case TYPE_FLOAT: 242 $query = "UPDATE ".$this->_dbPrefix."config SET config_value = 243 '$value', value_type = $type WHERE config_key = '$key'"; 244 break; 245 case TYPE_STRING: // need to add quotes here 246 $query = "UPDATE ".$this->_dbPrefix."config SET config_value = 247 '".Db::qstr($value)."', value_type = $type WHERE config_key = '$key'"; 248 break; 249 case TYPE_ARRAY: 250 case TYPE_OBJECT: // need to serialize here 251 $serValue = addslashes(serialize( $value )); 252 $query = "UPDATE ".$this->_dbPrefix."config SET config_value = 253 '$serValue', value_type = $type WHERE config_key = '$key'"; 254 break; 255 default: 256 throw( new Exception( "_updateValue: _getType produced an unexpected value of $type when checking value \"$value\"")); 257 die(); 258 } 259 260 $result = $this->_db->Execute( $query ); 261 262 if( $result ) 263 return true; 264 else 265 return false; 266 } 267 268 /** 269 * @private 270 */ 271 function _insertValue( $key, $value ) 272 { 273 // initialize the database 274 $this->_initializeDatabase(); 275 276 $type = $this->_getType( $value ); 277 switch( $type ) { 278 case TYPE_INTEGER: 279 case TYPE_BOOLEAN: 280 case TYPE_FLOAT: 281 $query = "INSERT INTO ".$this->_dbPrefix."config (config_key,config_value,value_type) 282 VALUES( '$key', '$value', $type )"; 283 break; 284 case TYPE_STRING: // need to add quotes here 285 $query = "INSERT INTO ".$this->_dbPrefix."config (config_key,config_value,value_type) 286 VALUES( '$key', '".Db::qstr($value)."', $type )"; 287 break; 288 case TYPE_ARRAY: 289 case TYPE_OBJECT: // need to serialize here 290 $serValue = addslashes(serialize( $value )); 291 $query = "INSERT INTO ".$this->_dbPrefix."config (config_key,config_value,value_type) 292 VALUES( '$key', '$serValue', $type )"; 293 break; 294 default: 295 throw( new Exception( "_insertValue: _getType produced an unexpected value of $type" )); 296 die(); 297 } 298 299 $result = $this->_db->Execute( $query ); 300 301 if( $result ) 302 return true; 303 else 304 return false; 305 } 306 307 /** 308 * Puts all the settings back to the database. 309 * 310 * It is done so that we first check if the key exists. If it does, we then 311 * send an update query and update it. Otherwise, we add it. 312 * 313 * @param key The name of the key 314 * @param The value. 315 * @return True if successful or false otherwise 316 */ 317 function save() 318 { 319 // load all the data 320 foreach( $this->_data as $key => $value ) { 321 $this->saveValue( $key, $value ); 322 } 323 324 // update the cache 325 $this->_cache->setData( CACHE_CONFIGDBSTORAGE, CACHE_GLOBAL, $this->_data ); 326 327 // saveValue is already reloading the data for us everytime! 328 return true; 329 } 330 331 /** 332 * Puts just one setting back to the database. 333 * 334 * It is done so that we first check if the key exists. If it does, we then 335 * send an update query and update it. Otherwise, we add it. 336 * 337 * @param key The name of the key 338 * @param The value. 339 * @return True if successful or false otherwise 340 */ 341 function saveValue( $key, $value ) 342 { 343 if( $this->_keyExists( $key )) { 344 // just update it in the db 345 $result = $this->_updateValue( $key, $value ); 346 } 347 else { 348 // we have to first insert the data into the db 349 $result = $this->_insertValue( $key, $value ); 350 } 351 352 // update the cache 353 $this->_data[$key] = $value; 354 $this->_cache->removeData( CACHE_CONFIGDBSTORAGE, CACHE_GLOBAL ); 355 356 // we better reload the data just in case 357 // $this->reload(); 358 359 return $result; 360 } 361 } 362 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |