From e0e1c1e58833c48fa36d90252042f7623181b224 Mon Sep 17 00:00:00 2001
From: Franz Glauber Vanderlinde <franz.glauber@gmail.com>
Date: Fri, 26 Aug 2011 19:21:06 -0300
Subject: [PATCH] 1180112-move_schema_functions

---
 includes/bootstrap.inc    |  120 ------------------
 includes/common.inc       |   83 +------------
 includes/install.core.inc |    1 +
 includes/install.inc      |   98 --------------
 includes/schema.inc       |  307 +++++++++++++++++++++++++++++++++++++++++++++
 update.php                |    1 +
 6 files changed, 310 insertions(+), 300 deletions(-)
 create mode 100644 includes/schema.inc

diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index 7c6ca09..9fc4086 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -2764,126 +2764,6 @@ function ip_address() {
   return $ip_address;
 }
 
-/**
- * @ingroup schemaapi
- * @{
- */
-
-/**
- * Get the schema definition of a table, or the whole database schema.
- *
- * The returned schema will include any modifications made by any
- * module that implements hook_schema_alter().
- *
- * @param $table
- *   The name of the table. If not given, the schema of all tables is returned.
- * @param $rebuild
- *   If true, the schema will be rebuilt instead of retrieved from the cache.
- */
-function drupal_get_schema($table = NULL, $rebuild = FALSE) {
-  static $schema;
-
-  if ($rebuild || !isset($table)) {
-    $schema = drupal_get_complete_schema($rebuild);
-  }
-  elseif (!isset($schema)) {
-    $schema = new SchemaCache();
-  }
-
-  if (!isset($table)) {
-    return $schema;
-  }
-  if (isset($schema[$table])) {
-    return $schema[$table];
-  }
-  else {
-    return FALSE;
-  }
-}
-
-/**
- * Extends DrupalCacheArray to allow for dynamic building of the schema cache.
- */
-class SchemaCache extends DrupalCacheArray {
-
-  public function __construct() {
-    // Cache by request method.
-    parent::__construct('schema:runtime:' . $_SERVER['REQUEST_METHOD'] == 'GET', 'cache');
-  }
-
-  protected function resolveCacheMiss($offset) {
-    $complete_schema = drupal_get_complete_schema();
-    $value = isset($complete_schema[$offset]) ? $complete_schema[$offset] :  NULL;
-    $this->storage[$offset] = $value;
-    $this->persist($offset);
-    return $value;
-  }
-}
-
-/**
- * Get the whole database schema.
- *
- * The returned schema will include any modifications made by any
- * module that implements hook_schema_alter().
- *
- * @param $rebuild
- *   If true, the schema will be rebuilt instead of retrieved from the cache.
- */
-function drupal_get_complete_schema($rebuild = FALSE) {
-  static $schema = array();
-
-  if (empty($schema) || $rebuild) {
-    // Try to load the schema from cache.
-    if (!$rebuild && $cached = cache()->get('schema')) {
-      $schema = $cached->data;
-    }
-    // Otherwise, rebuild the schema cache.
-    else {
-      $schema = array();
-      // Load the .install files to get hook_schema.
-      // On some databases this function may be called before bootstrap has
-      // been completed, so we force the functions we need to load just in case.
-      if (function_exists('module_load_all_includes')) {
-        // This function can be called very early in the bootstrap process, so
-        // we force the module_list() cache to be refreshed to ensure that it
-        // contains the complete list of modules before we go on to call
-        // module_load_all_includes().
-        module_list(TRUE);
-        module_load_all_includes('install');
-      }
-
-      require_once DRUPAL_ROOT . '/includes/common.inc';
-      // Invoke hook_schema for all modules.
-      foreach (module_implements('schema') as $module) {
-        // Cast the result of hook_schema() to an array, as a NULL return value
-        // would cause array_merge() to set the $schema variable to NULL as well.
-        // That would break modules which use $schema further down the line.
-        $current = (array) module_invoke($module, 'schema');
-        // Set 'module' and 'name' keys for each table, and remove descriptions,
-        // as they needlessly slow down cache()->get() for every single request.
-        _drupal_schema_initialize($current, $module);
-        $schema = array_merge($schema, $current);
-      }
-
-      drupal_alter('schema', $schema);
-      // If the schema is empty, avoid saving it: some database engines require
-      // the schema to perform queries, and this could lead to infinite loops.
-      if (!empty($schema) && (drupal_get_bootstrap_phase() == DRUPAL_BOOTSTRAP_FULL)) {
-        cache()->set('schema', $schema);
-      }
-      if ($rebuild) {
-        cache()->deletePrefix('schema:');
-      }
-    }
-  }
-
-  return $schema;
-}
-
-/**
- * @} End of "ingroup schemaapi".
- */
-
 
 /**
  * @ingroup registry
diff --git a/includes/common.inc b/includes/common.inc
index 268e36b..4b15cec 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -4914,6 +4914,7 @@ function _drupal_bootstrap_full() {
   require_once DRUPAL_ROOT . '/includes/ajax.inc';
   require_once DRUPAL_ROOT . '/includes/token.inc';
   require_once DRUPAL_ROOT . '/includes/errors.inc';
+  require_once DRUPAL_ROOT . '/includes/schema.inc';
 
   // Detect string handling method
   unicode_check();
@@ -6612,88 +6613,6 @@ function drupal_common_theme() {
  */
 
 /**
- * Creates all tables in a module's hook_schema() implementation.
- *
- * Note: This function does not pass the module's schema through
- * hook_schema_alter(). The module's tables will be created exactly as the
- * module defines them.
- *
- * @param $module
- *   The module for which the tables will be created.
- */
-function drupal_install_schema($module) {
-  $schema = drupal_get_schema_unprocessed($module);
-  _drupal_schema_initialize($schema, $module, FALSE);
-
-  foreach ($schema as $name => $table) {
-    db_create_table($name, $table);
-  }
-}
-
-/**
- * Remove all tables that a module defines in its hook_schema().
- *
- * Note: This function does not pass the module's schema through
- * hook_schema_alter(). The module's tables will be created exactly as the
- * module defines them.
- *
- * @param $module
- *   The module for which the tables will be removed.
- * @return
- *   An array of arrays with the following key/value pairs:
- *    - success: a boolean indicating whether the query succeeded.
- *    - query: the SQL query(s) executed, passed through check_plain().
- */
-function drupal_uninstall_schema($module) {
-  $schema = drupal_get_schema_unprocessed($module);
-  _drupal_schema_initialize($schema, $module, FALSE);
-
-  foreach ($schema as $table) {
-    if (db_table_exists($table['name'])) {
-      db_drop_table($table['name']);
-    }
-  }
-}
-
-/**
- * Returns the unprocessed and unaltered version of a module's schema.
- *
- * Use this function only if you explicitly need the original
- * specification of a schema, as it was defined in a module's
- * hook_schema(). No additional default values will be set,
- * hook_schema_alter() is not invoked and these unprocessed
- * definitions won't be cached.
- *
- * This function can be used to retrieve a schema specification in
- * hook_schema(), so it allows you to derive your tables from existing
- * specifications.
- *
- * It is also used by drupal_install_schema() and
- * drupal_uninstall_schema() to ensure that a module's tables are
- * created exactly as specified without any changes introduced by a
- * module that implements hook_schema_alter().
- *
- * @param $module
- *   The module to which the table belongs.
- * @param $table
- *   The name of the table. If not given, the module's complete schema
- *   is returned.
- */
-function drupal_get_schema_unprocessed($module, $table = NULL) {
-  // Load the .install file to get hook_schema.
-  module_load_install($module);
-  $schema = module_invoke($module, 'schema');
-
-  if (isset($table) && isset($schema[$table])) {
-    return $schema[$table];
-  }
-  elseif (!empty($schema)) {
-    return $schema;
-  }
-  return array();
-}
-
-/**
  * Fill in required default values for table definitions returned by hook_schema().
  *
  * @param $schema
diff --git a/includes/install.core.inc b/includes/install.core.inc
index 3791d71..4958d6b 100644
--- a/includes/install.core.inc
+++ b/includes/install.core.inc
@@ -244,6 +244,7 @@ function install_begin_request(&$install_state) {
   require_once DRUPAL_ROOT . '/includes/common.inc';
   require_once DRUPAL_ROOT . '/includes/file.inc';
   require_once DRUPAL_ROOT . '/includes/install.inc';
+  require_once DRUPAL_ROOT . '/includes/schema.inc';
   require_once DRUPAL_ROOT . '/' . variable_get('path_inc', 'includes/path.inc');
 
   // Load module basics (needed for hook invokes).
diff --git a/includes/install.inc b/includes/install.inc
index 089cdee..0aff552 100644
--- a/includes/install.inc
+++ b/includes/install.inc
@@ -82,104 +82,6 @@ function drupal_load_updates() {
 }
 
 /**
- * Returns an array of available schema versions for a module.
- *
- * @param $module
- *   A module name.
- * @return
- *   If the module has updates, an array of available updates sorted by version.
- *   Otherwise, FALSE.
- */
-function drupal_get_schema_versions($module) {
-  $updates = &drupal_static(__FUNCTION__, NULL);
-  if (!isset($updates[$module])) {
-    $updates = array();
-
-    foreach (module_list() as $loaded_module) {
-      $updates[$loaded_module] = array();
-    }
-
-    // Prepare regular expression to match all possible defined hook_update_N().
-    $regexp = '/^(?P<module>.+)_update_(?P<version>\d+)$/';
-    $functions = get_defined_functions();
-    // Narrow this down to functions ending with an integer, since all
-    // hook_update_N() functions end this way, and there are other
-    // possible functions which match '_update_'. We use preg_grep() here
-    // instead of foreaching through all defined functions, since the loop
-    // through all PHP functions can take significant page execution time
-    // and this function is called on every administrative page via
-    // system_requirements().
-    foreach (preg_grep('/_\d+$/', $functions['user']) as $function) {
-      // If this function is a module update function, add it to the list of
-      // module updates.
-      if (preg_match($regexp, $function, $matches)) {
-        $updates[$matches['module']][] = $matches['version'];
-      }
-    }
-    // Ensure that updates are applied in numerical order.
-    foreach ($updates as &$module_updates) {
-      sort($module_updates, SORT_NUMERIC);
-    }
-  }
-  return empty($updates[$module]) ? FALSE : $updates[$module];
-}
-
-/**
- * Returns the currently installed schema version for a module.
- *
- * @param $module
- *   A module name.
- * @param $reset
- *   Set to TRUE after modifying the system table.
- * @param $array
- *   Set to TRUE if you want to get information about all modules in the
- *   system.
- * @return
- *   The currently installed schema version, or SCHEMA_UNINSTALLED if the
- *   module is not installed.
- */
-function drupal_get_installed_schema_version($module, $reset = FALSE, $array = FALSE) {
-  static $versions = array();
-
-  if ($reset) {
-    $versions = array();
-  }
-
-  if (!$versions) {
-    $versions = array();
-    $result = db_query("SELECT name, schema_version FROM {system} WHERE type = :type", array(':type' => 'module'));
-    foreach ($result as $row) {
-      $versions[$row->name] = $row->schema_version;
-    }
-  }
-
-  if ($array) {
-    return $versions;
-  }
-  else {
-    return isset($versions[$module]) ? $versions[$module] : SCHEMA_UNINSTALLED;
-  }
-}
-
-/**
- * Update the installed version information for a module.
- *
- * @param $module
- *   A module name.
- * @param $version
- *   The new schema version.
- */
-function drupal_set_installed_schema_version($module, $version) {
-  db_update('system')
-    ->fields(array('schema_version' => $version))
-    ->condition('name', $module)
-    ->execute();
-
-  // Reset the static cache of module schema versions.
-  drupal_get_installed_schema_version(NULL, TRUE);
-}
-
-/**
  * Loads the install profile, extracting its defined distribution name.
  *
  * @return
diff --git a/includes/schema.inc b/includes/schema.inc
new file mode 100644
index 0000000..510d166
--- /dev/null
+++ b/includes/schema.inc
@@ -0,0 +1,307 @@
+<?php
+
+/**
+ * @file
+ * Functions that operate SchemaAPI.
+ */
+
+/**
+ * @ingroup schemaapi
+ * @{
+ */
+
+/**
+ * Get the schema definition of a table, or the whole database schema.
+ *
+ * The returned schema will include any modifications made by any
+ * module that implements hook_schema_alter().
+ *
+ * @param $table
+ *   The name of the table. If not given, the schema of all tables is returned.
+ * @param $rebuild
+ *   If true, the schema will be rebuilt instead of retrieved from the cache.
+ */
+function drupal_get_schema($table = NULL, $rebuild = FALSE) {
+  static $schema;
+
+  if ($rebuild || !isset($table)) {
+    $schema = drupal_get_complete_schema($rebuild);
+  }
+  elseif (!isset($schema)) {
+    $schema = new SchemaCache();
+  }
+
+  if (!isset($table)) {
+    return $schema;
+  }
+  if (isset($schema[$table])) {
+    return $schema[$table];
+  }
+  else {
+    return FALSE;
+  }
+}
+/**
+ * Get the whole database schema.
+ *
+ * The returned schema will include any modifications made by any
+ * module that implements hook_schema_alter().
+ *
+ * @param $rebuild
+ *   If true, the schema will be rebuilt instead of retrieved from the cache.
+ */
+function drupal_get_complete_schema($rebuild = FALSE) {
+  static $schema = array();
+
+  if (empty($schema) || $rebuild) {
+    // Try to load the schema from cache.
+    if (!$rebuild && $cached = cache()->get('schema')) {
+      $schema = $cached->data;
+    }
+    // Otherwise, rebuild the schema cache.
+    else {
+      $schema = array();
+      // Load the .install files to get hook_schema.
+      // On some databases this function may be called before bootstrap has
+      // been completed, so we force the functions we need to load just in case.
+      if (function_exists('module_load_all_includes')) {
+        // This function can be called very early in the bootstrap process, so
+        // we force the module_list() cache to be refreshed to ensure that it
+        // contains the complete list of modules before we go on to call
+        // module_load_all_includes().
+        module_list(TRUE);
+        module_load_all_includes('install');
+      }
+
+      require_once DRUPAL_ROOT . '/includes/common.inc';
+      // Invoke hook_schema for all modules.
+      foreach (module_implements('schema') as $module) {
+        // Cast the result of hook_schema() to an array, as a NULL return value
+        // would cause array_merge() to set the $schema variable to NULL as well.
+        // That would break modules which use $schema further down the line.
+        $current = (array) module_invoke($module, 'schema');
+        // Set 'module' and 'name' keys for each table, and remove descriptions,
+        // as they needlessly slow down cache()->get() for every single request.
+        _drupal_schema_initialize($current, $module);
+        $schema = array_merge($schema, $current);
+      }
+
+      drupal_alter('schema', $schema);
+      // If the schema is empty, avoid saving it: some database engines require
+      // the schema to perform queries, and this could lead to infinite loops.
+      if (!empty($schema) && (drupal_get_bootstrap_phase() == DRUPAL_BOOTSTRAP_FULL)) {
+        cache()->set('schema', $schema);
+      }
+      if ($rebuild) {
+        cache()->deletePrefix('schema:');
+      }
+    }
+  }
+
+  return $schema;
+}
+
+/**
+ * Returns an array of available schema versions for a module.
+ *
+ * @param $module
+ *   A module name.
+ * @return
+ *   If the module has updates, an array of available updates sorted by version.
+ *   Otherwise, FALSE.
+ */
+function drupal_get_schema_versions($module) {
+  $updates = &drupal_static(__FUNCTION__, NULL);
+  if (!isset($updates[$module])) {
+    $updates = array();
+
+    foreach (module_list() as $loaded_module) {
+      $updates[$loaded_module] = array();
+    }
+
+    // Prepare regular expression to match all possible defined hook_update_N().
+    $regexp = '/^(?P<module>.+)_update_(?P<version>\d+)$/';
+    $functions = get_defined_functions();
+    // Narrow this down to functions ending with an integer, since all
+    // hook_update_N() functions end this way, and there are other
+    // possible functions which match '_update_'. We use preg_grep() here
+    // instead of foreaching through all defined functions, since the loop
+    // through all PHP functions can take significant page execution time
+    // and this function is called on every administrative page via
+    // system_requirements().
+    foreach (preg_grep('/_\d+$/', $functions['user']) as $function) {
+      // If this function is a module update function, add it to the list of
+      // module updates.
+      if (preg_match($regexp, $function, $matches)) {
+        $updates[$matches['module']][] = $matches['version'];
+      }
+    }
+    // Ensure that updates are applied in numerical order.
+    foreach ($updates as &$module_updates) {
+      sort($module_updates, SORT_NUMERIC);
+    }
+  }
+  return empty($updates[$module]) ? FALSE : $updates[$module];
+}
+
+/**
+ * Returns the currently installed schema version for a module.
+ *
+ * @param $module
+ *   A module name.
+ * @param $reset
+ *   Set to TRUE after modifying the system table.
+ * @param $array
+ *   Set to TRUE if you want to get information about all modules in the
+ *   system.
+ * @return
+ *   The currently installed schema version, or SCHEMA_UNINSTALLED if the
+ *   module is not installed.
+ */
+function drupal_get_installed_schema_version($module, $reset = FALSE, $array = FALSE) {
+  static $versions = array();
+
+  if ($reset) {
+    $versions = array();
+  }
+
+  if (!$versions) {
+    $versions = array();
+    $result = db_query("SELECT name, schema_version FROM {system} WHERE type = :type", array(':type' => 'module'));
+    foreach ($result as $row) {
+      $versions[$row->name] = $row->schema_version;
+    }
+  }
+
+  if ($array) {
+    return $versions;
+  }
+  else {
+    return isset($versions[$module]) ? $versions[$module] : SCHEMA_UNINSTALLED;
+  }
+}
+
+/**
+ * Update the installed version information for a module.
+ *
+ * @param $module
+ *   A module name.
+ * @param $version
+ *   The new schema version.
+ */
+function drupal_set_installed_schema_version($module, $version) {
+  db_update('system')
+    ->fields(array('schema_version' => $version))
+    ->condition('name', $module)
+    ->execute();
+
+  // Reset the static cache of module schema versions.
+  drupal_get_installed_schema_version(NULL, TRUE);
+}
+
+/**
+ * Creates all tables in a module's hook_schema() implementation.
+ *
+ * Note: This function does not pass the module's schema through
+ * hook_schema_alter(). The module's tables will be created exactly as the
+ * module defines them.
+ *
+ * @param $module
+ *   The module for which the tables will be created.
+ */
+function drupal_install_schema($module) {
+  $schema = drupal_get_schema_unprocessed($module);
+  _drupal_schema_initialize($schema, $module, FALSE);
+
+  foreach ($schema as $name => $table) {
+    db_create_table($name, $table);
+  }
+}
+
+/**
+ * Remove all tables that a module defines in its hook_schema().
+ *
+ * Note: This function does not pass the module's schema through
+ * hook_schema_alter(). The module's tables will be created exactly as the
+ * module defines them.
+ *
+ * @param $module
+ *   The module for which the tables will be removed.
+ * @return
+ *   An array of arrays with the following key/value pairs:
+ *    - success: a boolean indicating whether the query succeeded.
+ *    - query: the SQL query(s) executed, passed through check_plain().
+ */
+function drupal_uninstall_schema($module) {
+  $schema = drupal_get_schema_unprocessed($module);
+  _drupal_schema_initialize($schema, $module, FALSE);
+
+  foreach ($schema as $table) {
+    if (db_table_exists($table['name'])) {
+      db_drop_table($table['name']);
+    }
+  }
+}
+
+/**
+ * Returns the unprocessed and unaltered version of a module's schema.
+ *
+ * Use this function only if you explicitly need the original
+ * specification of a schema, as it was defined in a module's
+ * hook_schema(). No additional default values will be set,
+ * hook_schema_alter() is not invoked and these unprocessed
+ * definitions won't be cached.
+ *
+ * This function can be used to retrieve a schema specification in
+ * hook_schema(), so it allows you to derive your tables from existing
+ * specifications.
+ *
+ * It is also used by drupal_install_schema() and
+ * drupal_uninstall_schema() to ensure that a module's tables are
+ * created exactly as specified without any changes introduced by a
+ * module that implements hook_schema_alter().
+ *
+ * @param $module
+ *   The module to which the table belongs.
+ * @param $table
+ *   The name of the table. If not given, the module's complete schema
+ *   is returned.
+ */
+function drupal_get_schema_unprocessed($module, $table = NULL) {
+  // Load the .install file to get hook_schema.
+  module_load_install($module);
+  $schema = module_invoke($module, 'schema');
+
+  if (isset($table) && isset($schema[$table])) {
+    return $schema[$table];
+  }
+  elseif (!empty($schema)) {
+    return $schema;
+  }
+  return array();
+}
+
+/**
+ * Extends DrupalCacheArray to allow for dynamic building of the schema cache.
+ */
+class SchemaCache extends DrupalCacheArray {
+
+  public function __construct() {
+    // Cache by request method.
+    parent::__construct('schema:runtime:' . $_SERVER['REQUEST_METHOD'] == 'GET', 'cache');
+  }
+
+  protected function resolveCacheMiss($offset) {
+    $complete_schema = drupal_get_complete_schema();
+    $value = isset($complete_schema[$offset]) ? $complete_schema[$offset] :  NULL;
+    $this->storage[$offset] = $value;
+    $this->persist($offset);
+    return $value;
+  }
+}
+
+
+/**
+ * @} End of "ingroup schemaapi".
+ */
+
diff --git a/update.php b/update.php
index e1ad8c0..0229645 100644
--- a/update.php
+++ b/update.php
@@ -346,6 +346,7 @@ require_once DRUPAL_ROOT . '/includes/update.inc';
 require_once DRUPAL_ROOT . '/includes/common.inc';
 require_once DRUPAL_ROOT . '/includes/file.inc';
 require_once DRUPAL_ROOT . '/includes/unicode.inc';
+require_once DRUPAL_ROOT . '/includes/schema.inc';
 update_prepare_d8_bootstrap();
 
 // Determine if the current user has access to run update.php.
-- 
1.7.3.4

