diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index e94f8e1..ae0ecba 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -2480,11 +2480,23 @@ function drupal_container(Container $new_container = NULL, $rebuild = FALSE) { $container ->register('config.storage.staging', 'Drupal\Core\Config\FileStorage') ->addArgument(config_get_config_directory(CONFIG_STAGING_DIRECTORY)); + $container + ->register('state.storage', 'Drupal\Core\KeyValueStore\DatabaseStorage') + ->addArgument('state'); } return $container; } /** + * Returns the state storage service. + * + * @return Drupal\Core\KeyValueStore\KeyValueStoreInterface + */ +function state() { + return drupal_container()->get('state.storage'); +} + +/** * Returns the test prefix if this is an internal request from SimpleTest. * * @return diff --git a/core/includes/common.inc b/core/includes/common.inc index c9913a2..fc198f1 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -3168,7 +3168,9 @@ function drupal_pre_render_styles($elements) { function drupal_build_css_cache($css) { $data = ''; $uri = ''; - $map = variable_get('drupal_css_cache_files', array()); + if (!$map = state()->get('drupal_css_cache_files')) { + $map = array(); + } // Create a new array so that only the file names are used to create the hash. // This prevents new aggregates from being created unnecessarily. $css_data = array(); @@ -3236,7 +3238,7 @@ function drupal_build_css_cache($css) { } // Save the updated map. $map[$key] = $uri; - variable_set('drupal_css_cache_files', $map); + state()->set('drupal_css_cache_files', $map); } return $uri; } @@ -3401,7 +3403,7 @@ function _drupal_load_stylesheet($matches) { * Deletes old cached CSS files. */ function drupal_clear_css_cache() { - variable_del('drupal_css_cache_files'); + state()->delete('drupal_css_cache_files'); file_scan_directory('public://css', '/.*/', array('callback' => 'drupal_delete_file_if_stale')); } @@ -4711,7 +4713,9 @@ function drupal_add_tabledrag($table_id, $action, $relationship, $group, $subgro function drupal_build_js_cache($files) { $contents = ''; $uri = ''; - $map = variable_get('drupal_js_cache_files', array()); + if (!$map = state()->get('drupal_js_cache_files')) { + $map = array(); + } // Create a new array so that only the file names are used to create the hash. // This prevents new aggregates from being created unnecessarily. $js_data = array(); @@ -4756,7 +4760,7 @@ function drupal_build_js_cache($files) { } } $map[$key] = $uri; - variable_set('drupal_js_cache_files', $map); + state()->set('drupal_js_cache_files', $map); } return $uri; } diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc index 1b6c8a3..ca107cc 100644 --- a/core/includes/install.core.inc +++ b/core/includes/install.core.inc @@ -310,7 +310,9 @@ function install_begin_request(&$install_state) { $container->register('config.factory', 'Drupal\Core\Config\ConfigFactory') ->addArgument(new Reference('config.storage')) ->addArgument(new Reference('dispatcher')); - + $container + ->register('state.storage', 'Drupal\Core\KeyValueStore\DatabaseStorage') + ->addArgument('state'); drupal_container($container); } diff --git a/core/includes/menu.inc b/core/includes/menu.inc index 8574dbe..4aeda61 100644 --- a/core/includes/menu.inc +++ b/core/includes/menu.inc @@ -451,7 +451,7 @@ function menu_get_item($path = NULL, $router_item = NULL) { if (!isset($router_items[$path])) { // Rebuild if we know it's needed, or if the menu masks are missing which // occurs rarely, likely due to a race condition of multiple rebuilds. - if (variable_get('menu_rebuild_needed', FALSE) || !variable_get('menu_masks', array())) { + if (state()->get('menu_rebuild_needed') || !variable_get('menu_masks', array())) { menu_router_rebuild(); } $original_map = arg(NULL, $path); @@ -2666,7 +2666,7 @@ function menu_router_rebuild() { menu_cache_clear_all(); _menu_clear_page_cache(); // Indicate that the menu has been successfully rebuilt. - variable_del('menu_rebuild_needed'); + state()->delete('menu_rebuild_needed'); } catch (Exception $e) { $transaction->rollback(); diff --git a/core/includes/path.inc b/core/includes/path.inc index 07aeee5..71c10a3 100644 --- a/core/includes/path.inc +++ b/core/includes/path.inc @@ -72,7 +72,7 @@ function drupal_lookup_path($action, $path = '', $langcode = NULL) { // Retrieve the path alias whitelist. if (!isset($cache['whitelist'])) { - $cache['whitelist'] = variable_get('path_alias_whitelist', NULL); + $cache['whitelist'] = state()->get('path_alias_whitelist'); if (!isset($cache['whitelist'])) { $cache['whitelist'] = drupal_path_alias_whitelist_rebuild(); } @@ -391,7 +391,7 @@ function drupal_path_alias_whitelist_rebuild($source = NULL) { // When paths are inserted, only rebuild the whitelist if the system path // has a top level component which is not already in the whitelist. if (!empty($source)) { - $whitelist = variable_get('path_alias_whitelist', NULL); + $whitelist = state()->get('path_alias_whitelist'); if (isset($whitelist[strtok($source, '/')])) { return $whitelist; } @@ -404,7 +404,7 @@ function drupal_path_alias_whitelist_rebuild($source = NULL) { foreach ($result as $row) { $whitelist[$row->path] = TRUE; } - variable_set('path_alias_whitelist', $whitelist); + state()->set('path_alias_whitelist', $whitelist); return $whitelist; } diff --git a/core/includes/update.inc b/core/includes/update.inc index 9dd8456..c6d6569 100644 --- a/core/includes/update.inc +++ b/core/includes/update.inc @@ -130,6 +130,36 @@ function update_prepare_d8_bootstrap() { update_extra_requirements($requirements); if ($has_required_schema) { + if (!db_table_exists('keyvalue')) { + $specs = array( + 'description' => 'Generic key-value storage table.', + 'fields' => array( + 'name' => array( + 'description' => 'The key.', + 'type' => 'varchar', + 'length' => 128, + 'not null' => TRUE, + 'default' => '', + ), + 'collection' => array( + 'description' => 'The collection of the variable.', + 'type' => 'varchar', + 'length' => 128, + 'not null' => TRUE, + 'default' => '', + ), + 'value' => array( + 'description' => 'The value.', + 'type' => 'blob', + 'not null' => TRUE, + 'size' => 'big', + 'translatable' => TRUE, + ), + ), + 'primary key' => array('collection', 'name'), + ); + db_create_table('keyvalue', $specs); + } // Bootstrap variables so we can update theme while preparing the update // process. drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES); diff --git a/core/lib/Drupal/Core/KeyValueStore/AbstractStorage.php b/core/lib/Drupal/Core/KeyValueStore/AbstractStorage.php new file mode 100644 index 0000000..408b9a8 --- /dev/null +++ b/core/lib/Drupal/Core/KeyValueStore/AbstractStorage.php @@ -0,0 +1,54 @@ +collection = $collection; + } + + /** + * Implements KeyValueStore\Storage\StorageInterface::getCollectionName(). + */ + public function getCollectionName() { + return $this->collection; + } + + /** + * Implements KeyValueStore\Storage\StorageInterface::get(). + */ + public function get($key) { + $values = $this->getMultiple(array($key)); + return reset($values); + } + + /** + * Implements KeyValueStore\Storage\StorageInterface::setMultiple(). + */ + public function setMultiple(array $data) { + foreach ($data as $key => $value) { + $this->set($key, $value); + } + } + + /** + * Implements KeyValueStore\Storage\StorageInterface::delete(). + */ + public function delete($key) { + $this->deleteMultiple(array($key)); + } + +} diff --git a/core/lib/Drupal/Core/KeyValueStore/DatabaseStorage.php b/core/lib/Drupal/Core/KeyValueStore/DatabaseStorage.php new file mode 100644 index 0000000..78b19b2 --- /dev/null +++ b/core/lib/Drupal/Core/KeyValueStore/DatabaseStorage.php @@ -0,0 +1,89 @@ +table = isset($options['table']) ? $options['table'] : 'keyvalue'; + } + + /** + * Implements KeyValueStore\Storage\StorageInterface::getMultiple(). + */ + public function getMultiple(array $keys) { + try { + $result = db_query('SELECT name, value FROM {' . db_escape_table($this->table) . '} WHERE name IN (:keys) AND collection = :collection', array(':keys' => $keys, ':collection' => $this->collection))->fetchAllAssoc('name'); + $values = array(); + foreach ($keys as $key) { + if (isset($result[$key]) && ($value = unserialize($result[$key]->value))) { + $values[$key] = $value; + } + } + return $values; + } + catch (\Exception $e) { + // If the database is never going to be available, key/value requests should + // return FALSE in order to allow exception handling to occur. + return array(); + } + } + + /** + * Implements KeyValueStore\Storage\StorageInterface::getAll(). + */ + public function getAll() { + $result = db_query('SELECT name, value FROM {' . db_escape_table($this->table) . '} WHERE collection = :collection', array(':collection' => $this->collection)); + $values = array(); + + foreach ($result as $item) { + if ($item) { + $values[$item->name] = unserialize($item->value); + } + } + return $values; + } + + /** + * Implements KeyValueStore\Storage\StorageInterface::set(). + */ + public function set($key, $value) { + db_merge($this->table) + ->key(array('name' => $key)) + ->fields(array( + 'collection' => $this->collection, + 'value' => serialize($value), + )) + ->execute(); + } + + /** + * Implements KeyValueStore\Storage\StorageInterface::deleteMultiple(). + */ + public function deleteMultiple(array $keys) { + // Delete in chunks when a large array is passed. + do { + db_delete($this->table) + ->condition('name', array_splice($keys, 0, 1000)) + ->condition('collection', $this->collection) + ->execute(); + } + while (count($keys)); + } + +} diff --git a/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreInterface.php b/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreInterface.php new file mode 100644 index 0000000..d4d7b6c --- /dev/null +++ b/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreInterface.php @@ -0,0 +1,98 @@ +collection = $collection; + } + + /** + * Implements KeyValueStore\Storage\StorageInterface::getCollectionName(). + */ + public function getCollectionName() { + return $this->collection; + } + + /** + * Implements KeyValueStore\Storage\StorageInterface::get(). + */ + public function get($key) { + return isset($this->data[$key]) ? $this->data[$key] : FALSE; + } + + /** + * Implements KeyValueStore\Storage\StorageInterface::getMultiple(). + */ + public function getMultiple(array $keys) { + $results = array(); + // Foreach only needs to traverse $keys once, the PHP implementation + // array_intersect_key(array_flip(array_values($keys)), $this->data); + // would be slower. + foreach ($keys as $key) { + if (isset($this->data[$key])) { + $results[$key] = $this->data[$key]; + } + } + return $results; + } + + /** + * Implements KeyValueStore\Storage\StorageInterface::getAll(). + */ + public function getAll() { + return $this->data; + } + + /** + * Implements KeyValueStore\Storage\StorageInterface::set(). + */ + public function set($key, $value) { + $this->data[$key] = $value; + } + + /** + * Implements KeyValueStore\Storage\StorageInterface::setMultiple(). + */ + public function setMultiple(array $data) { + $this->data = $data + $this->data; + } + + /** + * Implements KeyValueStore\Storage\StorageInterface::delete(). + */ + public function delete($key) { + unset($this->data[$key]); + } + + /** + * Implements KeyValueStore\Storage\StorageInterface::deleteMultiple(). + */ + public function deleteMultiple(array $keys) { + foreach ($keys as $key) { + unset($this->data[$key]); + } + } + +} diff --git a/core/modules/color/lib/Drupal/color/Tests/ColorTest.php b/core/modules/color/lib/Drupal/color/Tests/ColorTest.php index 8902d4c..22e34c6 100644 --- a/core/modules/color/lib/Drupal/color/Tests/ColorTest.php +++ b/core/modules/color/lib/Drupal/color/Tests/ColorTest.php @@ -108,7 +108,9 @@ class ColorTest extends WebTestBase { $config->set('preprocess.css', 1); $config->save(); $this->drupalGet(''); - $stylesheets = variable_get('drupal_css_cache_files', array()); + if (!$stylesheets = state()->get('drupal_css_cache_files')) { + $stylesheets = array(); + } $stylesheet_content = ''; foreach ($stylesheets as $key => $uri) { $stylesheet_content .= join("\n", file(drupal_realpath($uri))); diff --git a/core/modules/field/field.api.php b/core/modules/field/field.api.php index 1f76def..7364d4f 100644 --- a/core/modules/field/field.api.php +++ b/core/modules/field/field.api.php @@ -1551,7 +1551,7 @@ function hook_field_available_languages_alter(&$langcodes, $context) { function hook_field_attach_create_bundle($entity_type, $bundle) { // When a new bundle is created, the menu needs to be rebuilt to add the // Field UI menu item tabs. - variable_set('menu_rebuild_needed', TRUE); + state()->set('menu_rebuild_needed', TRUE); } /** diff --git a/core/modules/field_ui/field_ui.module b/core/modules/field_ui/field_ui.module index bde3f35..63cf18b 100644 --- a/core/modules/field_ui/field_ui.module +++ b/core/modules/field_ui/field_ui.module @@ -309,7 +309,7 @@ function field_ui_element_info() { function field_ui_field_attach_create_bundle($entity_type, $bundle) { // When a new bundle is created, the menu needs to be rebuilt to add our // menu item tabs. - variable_set('menu_rebuild_needed', TRUE); + state()->set('menu_rebuild_needed', TRUE); } /** diff --git a/core/modules/image/image.module b/core/modules/image/image.module index 41b0fc3..2936529 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -253,7 +253,7 @@ function image_form_system_file_system_settings_alter(&$form, &$form_state) { */ function image_system_file_system_settings_submit($form, &$form_state) { if ($form['file_public_path']['#default_value'] !== $form_state['values']['file_public_path']) { - variable_set('menu_rebuild_needed', TRUE); + state()->set('menu_rebuild_needed', TRUE); } } diff --git a/core/modules/search/search.admin.inc b/core/modules/search/search.admin.inc index 9168510..57e5d6b 100644 --- a/core/modules/search/search.admin.inc +++ b/core/modules/search/search.admin.inc @@ -175,7 +175,7 @@ function search_admin_settings_submit($form, &$form_state) { if ($config->get('active_modules') != $new_modules) { $config->set('active_modules', $new_modules); drupal_set_message(t('The active search modules have been changed.')); - variable_set('menu_rebuild_needed', TRUE); + state()->set('menu_rebuild_needed', TRUE); } $config->save(); } diff --git a/core/modules/system/lib/Drupal/system/Tests/Common/JavaScriptTest.php b/core/modules/system/lib/Drupal/system/Tests/Common/JavaScriptTest.php index ba7a48b..a7afe31 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Common/JavaScriptTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Common/JavaScriptTest.php @@ -346,11 +346,12 @@ class JavaScriptTest extends WebTestBase { )); // Store the expected key for the first item in the cache. - $cache = array_keys(variable_get('drupal_js_cache_files', array())); + $cache_files = state()->get('drupal_js_cache_files'); + $cache = $cache_files ? array_keys($cache_files) : array(); $expected_key = $cache[0]; // Reset variables and add a file in a different scope first. - variable_del('drupal_js_cache_files'); + state()->del('drupal_js_cache_files'); drupal_static_reset('drupal_add_js'); drupal_add_library('system', 'drupal'); drupal_add_js('some/custom/javascript_file.js', array('scope' => 'footer')); @@ -365,7 +366,8 @@ class JavaScriptTest extends WebTestBase { )); // Compare the expected key for the first file to the current one. - $cache = array_keys(variable_get('drupal_js_cache_files', array())); + $cache_files = state()->get('drupal_js_cache_files'); + $cache = $cache_files ? array_keys($cache_files) : array(); $key = $cache[0]; $this->assertEqual($key, $expected_key, 'JavaScript aggregation is not affected by ordering in different scopes.'); } diff --git a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/DatabaseStorageTest.php b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/DatabaseStorageTest.php new file mode 100644 index 0000000..c81f8e9 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/DatabaseStorageTest.php @@ -0,0 +1,26 @@ + 'Database Storage', + 'description' => 'Test the key-value database storage.', + 'group' => 'Key-value store' + ); + } + + protected function setUp() { + parent::setUp(); + module_load_install('system'); + $schema = system_schema(); + db_create_table('keyvalue', $schema['keyvalue']); + } + + protected function tearDown() { + db_drop_table('keyvalue'); + parent::tearDown(); + } +} diff --git a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/MemoryStorageTest.php b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/MemoryStorageTest.php new file mode 100644 index 0000000..d3db3bc --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/MemoryStorageTest.php @@ -0,0 +1,15 @@ + 'Memory Storage', + 'description' => 'Test the key-value memory storage.', + 'group' => 'Key-value store' + ); + } + +} diff --git a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php new file mode 100644 index 0000000..8fb38a0 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php @@ -0,0 +1,41 @@ +collection = end($class); + $storageClass = '\\Drupal\\Core\\KeyValueStore\\' . substr($this->collection, 0, -4); + $this->kv = new $storageClass($this->collection); + } + + public function testSetGet() { + $this->assertEqual($this->kv->getCollectionName(), $this->collection); + // get-set test. + $this->kv->set('foo', 'bar'); + $this->assertEqual('bar', $this->kv->get('foo')); + $this->kv->delete('foo'); + $this->assertFalse($this->kv->get('foo')); + + // multiple test. + $values = array('foo' => 'bar', 'baz' => 'qux'); + $this->kv->setMultiple($values); + $result = $this->kv->getMultiple(array('foo', 'baz')); + $this->assertEqual($values, $result); + $result = $this->kv->getAll(); + // $result might not equal to $values, the order is not defined for + // getAll(). + $this->assertEqual(count($result), count($values)); + foreach ($result as $key => $value) { + $this->assertEqual($values[$key], $value); + } + $this->kv->deleteMultiple(array_keys($values)); + $this->assertFalse($this->kv->get('foo')); + $this->assertFalse($this->kv->get('bar')); + $this->assertFalse($this->kv->getMultiple(array('foo', 'baz'))); + } +} diff --git a/core/modules/system/lib/Drupal/system/Tests/Menu/RebuildTest.php b/core/modules/system/lib/Drupal/system/Tests/Menu/RebuildTest.php index b6e0ec2..9cf1080 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Menu/RebuildTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Menu/RebuildTest.php @@ -38,7 +38,7 @@ class RebuildTest extends WebTestBase { // Now we enable the rebuild variable and send a request to rebuild the menu // item. Now 'admin' should exist. - variable_set('menu_rebuild_needed', TRUE); + state()->set('menu_rebuild_needed', TRUE); // The request should trigger the rebuild. $this->drupalGet(''); $admin_exists = db_query('SELECT path from {menu_router} WHERE path = :path', array(':path' => 'admin'))->fetchField(); diff --git a/core/modules/system/system.install b/core/modules/system/system.install index ac983aa..0cca136 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -549,6 +549,60 @@ function system_schema() { ), 'primary key' => array('name'), ); + $schema['keyvalue'] = array( + 'description' => 'Generic key-value storage table.', + 'fields' => array( + 'name' => array( + 'description' => 'The key.', + 'type' => 'varchar', + 'length' => 128, + 'not null' => TRUE, + 'default' => '', + ), + 'collection' => array( + 'description' => 'The collection of the variable.', + 'type' => 'varchar', + 'length' => 128, + 'not null' => TRUE, + 'default' => '', + ), + 'value' => array( + 'description' => 'The value.', + 'type' => 'blob', + 'not null' => TRUE, + 'size' => 'big', + 'translatable' => TRUE, + ), + ), + 'primary key' => array('collection', 'name'), + ); + $schema['keyvalue'] = array( + 'description' => 'Generic key-value storage table.', + 'fields' => array( + 'name' => array( + 'description' => 'The key.', + 'type' => 'varchar', + 'length' => 128, + 'not null' => TRUE, + 'default' => '', + ), + 'collection' => array( + 'description' => 'The collection of the variable.', + 'type' => 'varchar', + 'length' => 128, + 'not null' => TRUE, + 'default' => '', + ), + 'value' => array( + 'description' => 'The value.', + 'type' => 'blob', + 'not null' => TRUE, + 'size' => 'big', + 'translatable' => TRUE, + ), + ), + 'primary key' => array('collection', 'name'), + ); $schema['actions'] = array( 'description' => 'Stores action information.',