first import

This commit is contained in:
Bachir Soussi Chiadmi
2015-04-08 11:40:19 +02:00
commit 1bc61b12ad
8435 changed files with 1582817 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
Drupal module: Variable API Database storage
============================================
Provides database storage for realm variables.
This module provides some CRUD API to store and read your custom variables
and some _set() and _get() methods that are tightly integrated with Variable Realms
An example of a module using this API is Internationalization's i18n_variable module.

View File

@@ -0,0 +1,46 @@
<?php
/**
* @file
* Variable realm controller
*/
class VariableStoreRealmStore extends VariableRealmDefaultStore {
/**
* Initialize realm.
*/
public function variable_init() {
if (!isset($this->variables)) {
$this->variables = &variable_store($this->realm, $this->key);
}
}
/**
* Set single variable.
*
* @param $name
* Variable name
* @param $value
* Variable value
*/
public function variable_set($name, $value) {
// Since $variables is a reference we just need to set the store value.
variable_store_set($this->realm, $this->key, $name, $value);
}
/**
* Delete single variable.
*
* @param $name
* Variable name
*/
public function variable_del($name) {
// Since $variables is a reference we just need to delete the store value.
variable_store_del($this->realm, $this->key, $name);
}
/**
* Implements 'magic' _sleep method.
*
* If serialized, variables should not be saved, but rebuilt from store on wake up.
*/
public function __sleep(){
return array('realm', 'key');
}
}

View File

@@ -0,0 +1,16 @@
name = Variable store
description = Database storage for variable realms. This is an API module.
dependencies[] = variable
package = Variable
core = 7.x
version = 7.x-2.x
files[] = variable_store.class.inc
files[] = variable_store.test
; Information added by drupal.org packaging script on 2013-01-13
version = "7.x-2.2"
core = "7.x"
project = "variable"
datestamp = "1358075138"

View File

@@ -0,0 +1,64 @@
<?php
/**
* @file
* Variable API module install file
*/
/**
* Implements hook_install()
*/
function variable_store_install() {
// Set module weight for it to run before most modules and initialize variable realms
db_query("UPDATE {system} SET weight = -1000 WHERE name = 'variable_store' AND type = 'module'");
}
/**
* Implementation of hook_schema().
*/
function variable_store_schema() {
$schema['variable_store'] = array(
'description' => 'Named variable/value pairs created by modules using Variable API database storage. All variables are cached in memory at the start of every Drupal request so developers should not be careless about what is stored here.',
'fields' => array(
'realm' => array(
'description' => 'The realm domain of this variable.',
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'default' => ''),
'realm_key' => array(
'description' => 'The realm key of this variable.',
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'default' => ''),
'name' => array(
'description' => 'The name of the variable.',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => ''),
'value' => array(
'description' => 'The value of the variable.',
'type' => 'text',
'not null' => TRUE,
'size' => 'big'),
'serialized' => array(
'description' => 'A flag to indicate whether content is serialized (1) or not (0).',
'type' => 'int',
'size' => 'small',
'not null' => TRUE,
'default' => 1),
),
'primary key' => array('realm', 'realm_key', 'name'),
'indexes' => array('realm_value' => array('realm', 'realm_key')),
);
return $schema;
}
/**
* Reduce realm key field length so it can be used on the primary key
*/
function variable_store_update_7000() {
$schema = variable_store_schema();
db_change_field('variable_store', 'realm_key', 'realm_key', $schema['variable_store']['fields']['realm_key']);
}

View File

@@ -0,0 +1,148 @@
<?php
/**
* @file
* Variable API module - Database storage
*
* This module provides database storage for variable realms
*/
/**
* Get variable store
*/
function &variable_store($realm, $key) {
$variable_store = &drupal_static('variable_store');
if (!isset($variable_store[$realm][$key])) {
$variable_store[$realm][$key] = _variable_store_load($realm, $key);
}
return $variable_store[$realm][$key];
}
/**
* Implementation of hook_boot()
*/
function variable_store_boot() {
// Do nothing, we just want this module to be available for boot.
}
/**
* Delete variable from db
*/
function variable_store_del($realm, $key, $name) {
$store = &variable_store($realm, $key);
db_delete('variable_store')
->condition('realm', $realm)
->condition('realm_key', $key)
->condition('name', $name)
->execute();
unset($store[$name]);
cache_clear_all('variable:' . $realm . ':' . $key, 'cache_bootstrap');
}
/**
* Get single variable from store
*/
function variable_store_get($realm, $key, $name, $default = NULL) {
$variables = variable_store($realm, $key);
return $variables && isset($variables[$name]) ? $variables[$name] : $default;
}
/**
* Delete realm variable or full realm from store.
*
* @param $realm
* Realm name to delete. NULL to delete all realms.
* @param $key
* Realm key to delete. NULL to delete all realm keys.
* @param $name
* Variable name to delete. NULL to delete all variables for that realm, key
*/
function variable_store_delete_all($realm, $key, $name = NULL) {
_variable_store_reset();
$query = db_delete('variable_store');
if (isset($realm)) {
$query->condition('realm', $realm);
}
if (isset($key)) {
$query->condition('realm_key', $key);
}
if (isset($name)) {
$query->condition('name', $name);
}
return $query->execute();
}
/**
* List all variable names from a realm.
*
* @param $realm
* Realm name to list. NULL to list all realms.
* @param $key
* Realm key to list. NULL to list all realm keys.
*
* @return array
* List of variable names.
*/
function variable_store_list_all($realm, $key = NULL) {
$query = db_select('variable_store', 'vs')
->fields('vs', array('name'))
->distinct();
if ($realm) {
$query->condition('realm', $realm);
}
if ($key) {
$query->condition('realm_key', $key);
}
return $query->execute()->fetchCol();
}
/**
* Load realm from db store
*/
function _variable_store_load($realm, $key) {
$cacheid = 'variable:' . $realm . ':' . $key;
if ($cached = cache_get($cacheid, 'cache_bootstrap')) {
$variables = $cached->data;
}
else {
$result = db_select('variable_store', 's')
->fields('s', array('name', 'value', 'serialized'))
->condition('realm', $realm)
->condition('realm_key', $key)
->execute();
$variables = array();
foreach ($result as $variable) {
$variables[$variable->name] = $variable->serialized ? unserialize($variable->value) : $variable->value;
}
cache_set($cacheid, $variables, 'cache_bootstrap');
}
return $variables;
}
/**
* Reset caches and static variables.
*/
function _variable_store_reset() {
$store = &drupal_static('variable_store', array());
foreach ($store as $realm => &$realm_store) {
foreach (array_keys($realm_store) as $key) {
$realm_store[$key] = NULL;
}
}
cache_clear_all('variable:', 'cache_bootstrap', TRUE);
}
/**
* Set variable value
*/
function variable_store_set($realm, $key, $name, $value, $rebuild = TRUE) {
$store = &variable_store($realm, $key);
$serialize = !is_int($value) && !is_string($value);
db_merge('variable_store')
->key(array('realm' => $realm, 'realm_key' => $key, 'name' => $name))
->fields(array('value' => $serialize ? serialize($value) : $value, 'serialized' => $serialize ? 1 : 0))
->execute();
cache_clear_all('variable:' . $realm . ':' . $key, 'cache_bootstrap');
$store[$name] = $value;
}

View File

@@ -0,0 +1,78 @@
<?php
/**
* @file
* Tests for variable.module.
*/
/**
* Helper class for module test cases.
*/
class VariableStoreTestCase extends DrupalWebTestCase {
protected $admin_user;
public static function getInfo() {
return array(
'name' => 'Test Variable store and realms',
'description' => 'Exercise the Variable API, default values, save and delete variables, etc.',
'group' => 'Variable',
);
}
function setUp() {
parent::setUp('variable', 'variable_realm', 'variable_store', 'variable_example');
$this->admin_user = $this->drupalCreateUser(array('access administration pages', 'administer site configuration'));
$this->drupalLogin($this->admin_user);
}
/**
* Test that all core modules can be enabled, disabled and uninstalled.
*/
function testVariableStoreAPI() {
// Store two values for the 'example' realm.
$realm = 'example';
$store = array(
'first' => 'My first test site',
'second' => 'My second test site',
);
$values = array(
'default' => 'Drupal',
) + $store;
foreach ($store as $key => $value) {
variable_store_set($realm, $key, 'site_name', $value);
}
// Check we've got stored the right value for each realm.
foreach ($store as $key => $value) {
$this->assertEqual(variable_store_get($realm, $key, 'site_name'), $value, 'Variable has been saved to the store.');
// Check the value is displayed with example page.
$this->drupalGet('variable/realm/' . $realm . '/' . $key);
$this->assertText($value, "The right site name is displayed for realm $realm:$key");
}
// Load realms from store and set them as different realm.
foreach ($store as $key => $value) {
$variables = variable_store($realm, $key);
variable_realm_add($realm, $key, $variables);
}
// Switch realms and get the right values.
foreach ($values as $key => $value) {
// Check value before setting realm
$this->assertEqual(variable_realm_get($realm, $key, 'site_name', 'Drupal'), $value, "We get the right value for realm $realm:$key");
variable_realm_switch($realm, $key);
$this->assertEqual(variable_get_value('site_name'), $value, "We get the right value with Variable API after switching to realm $realm:$key");
$this->assertEqual(variable_get('site_name', 'Drupal'), $value, "We get the right value with Drupal API after switching to realm $realm:$key");
}
// Delete variable and check it has been deleted, first from realms and then from store.
variable_delete('site_name');
// Check we've got stored the right value for each realm.
foreach ($store as $key => $value) {
$this->assertFalse(variable_store_get($realm, $key, 'site_name'), "Variable has been deleted from the realm for $realm:$key.");
$this->assertFalse(variable_store_get($realm, $key, 'site_name'), "Variable has been deleted from the store for $realm:$key.");
}
// Finally check variable has returned to default value. Use any of the realms.
variable_realm_switch($realm, 'first');
$this->assertEqual($value = variable_get_value('site_name'), 'Drupal', "We get the right value with Variable API after deleting the variable: $value");
$this->assertFalse($value = variable_get('site_name'), "We get the right value with Drupal API after deleting the variable: $value");
}
}