Chris@0: getModuleList() as $loaded_module => $filename) { Chris@0: $updates[$loaded_module] = []; Chris@0: } Chris@0: Chris@0: // Prepare regular expression to match all possible defined hook_update_N(). Chris@0: $regexp = '/^(?.+)_update_(?\d+)$/'; Chris@0: $functions = get_defined_functions(); Chris@0: // Narrow this down to functions ending with an integer, since all Chris@0: // hook_update_N() functions end this way, and there are other Chris@0: // possible functions which match '_update_'. We use preg_grep() here Chris@0: // instead of foreaching through all defined functions, since the loop Chris@0: // through all PHP functions can take significant page execution time Chris@0: // and this function is called on every administrative page via Chris@0: // system_requirements(). Chris@0: foreach (preg_grep('/_\d+$/', $functions['user']) as $function) { Chris@0: // If this function is a module update function, add it to the list of Chris@0: // module updates. Chris@0: if (preg_match($regexp, $function, $matches)) { Chris@0: $updates[$matches['module']][] = $matches['version']; Chris@0: } Chris@0: } Chris@0: // Ensure that updates are applied in numerical order. Chris@0: foreach ($updates as &$module_updates) { Chris@0: sort($module_updates, SORT_NUMERIC); Chris@0: } Chris@0: } Chris@0: return empty($updates[$module]) ? FALSE : $updates[$module]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the currently installed schema version for a module. Chris@0: * Chris@0: * @param string $module Chris@0: * A module name. Chris@0: * @param bool $reset Chris@0: * Set to TRUE after installing or uninstalling an extension. Chris@0: * @param bool $array Chris@0: * Set to TRUE if you want to get information about all modules in the Chris@0: * system. Chris@0: * Chris@0: * @return string|int Chris@0: * The currently installed schema version, or SCHEMA_UNINSTALLED if the Chris@0: * module is not installed. Chris@0: */ Chris@0: function drupal_get_installed_schema_version($module, $reset = FALSE, $array = FALSE) { Chris@0: $versions = &drupal_static(__FUNCTION__, []); Chris@0: Chris@0: if ($reset) { Chris@0: $versions = []; Chris@0: } Chris@0: Chris@0: if (!$versions) { Chris@0: if (!$versions = \Drupal::keyValue('system.schema')->getAll()) { Chris@0: $versions = []; Chris@0: } Chris@0: } Chris@0: Chris@0: if ($array) { Chris@0: return $versions; Chris@0: } Chris@0: else { Chris@0: return isset($versions[$module]) ? $versions[$module] : SCHEMA_UNINSTALLED; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Updates the installed version information for a module. Chris@0: * Chris@0: * @param string $module Chris@0: * A module name. Chris@0: * @param string $version Chris@0: * The new schema version. Chris@0: */ Chris@0: function drupal_set_installed_schema_version($module, $version) { Chris@0: \Drupal::keyValue('system.schema')->set($module, $version); Chris@0: // Reset the static cache of module schema versions. Chris@0: drupal_get_installed_schema_version(NULL, TRUE); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates all tables defined in a module's hook_schema(). Chris@0: * Chris@0: * @param string $module Chris@0: * The module for which the tables will be created. Chris@0: */ Chris@0: function drupal_install_schema($module) { Chris@0: $schema = drupal_get_module_schema($module); Chris@0: _drupal_schema_initialize($schema, $module, FALSE); Chris@0: Chris@0: foreach ($schema as $name => $table) { Chris@0: \Drupal::database()->schema()->createTable($name, $table); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Removes all tables defined in a module's hook_schema(). Chris@0: * Chris@0: * @param string $module Chris@0: * The module for which the tables will be removed. Chris@0: */ Chris@0: function drupal_uninstall_schema($module) { Chris@17: $tables = drupal_get_module_schema($module); Chris@17: _drupal_schema_initialize($tables, $module, FALSE); Chris@17: $schema = \Drupal::database()->schema(); Chris@17: foreach ($tables as $table) { Chris@17: if ($schema->tableExists($table['name'])) { Chris@17: $schema->dropTable($table['name']); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a module's schema. Chris@0: * Chris@0: * This function can be used to retrieve a schema specification in Chris@0: * hook_schema(), so it allows you to derive your tables from existing Chris@0: * specifications. Chris@0: * Chris@0: * @param string $module Chris@0: * The module to which the table belongs. Chris@0: * @param string $table Chris@0: * The name of the table. If not given, the module's complete schema Chris@0: * is returned. Chris@0: */ Chris@0: function drupal_get_module_schema($module, $table = NULL) { Chris@0: // Load the .install file to get hook_schema. Chris@0: module_load_install($module); Chris@0: $schema = \Drupal::moduleHandler()->invoke($module, 'schema'); Chris@0: Chris@0: if (isset($table)) { Chris@0: if (isset($schema[$table])) { Chris@0: return $schema[$table]; Chris@0: } Chris@0: return []; Chris@0: } Chris@0: elseif (!empty($schema)) { Chris@0: return $schema; Chris@0: } Chris@0: return []; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Fills in required default values for table definitions from hook_schema(). Chris@0: * Chris@0: * @param array $schema Chris@0: * The schema definition array as it was returned by the module's Chris@0: * hook_schema(). Chris@0: * @param string $module Chris@0: * The module for which hook_schema() was invoked. Chris@0: * @param bool $remove_descriptions Chris@0: * (optional) Whether to additionally remove 'description' keys of all tables Chris@0: * and fields to improve performance of serialize() and unserialize(). Chris@0: * Defaults to TRUE. Chris@0: */ Chris@0: function _drupal_schema_initialize(&$schema, $module, $remove_descriptions = TRUE) { Chris@0: // Set the name and module key for all tables. Chris@0: foreach ($schema as $name => &$table) { Chris@0: if (empty($table['module'])) { Chris@0: $table['module'] = $module; Chris@0: } Chris@0: if (!isset($table['name'])) { Chris@0: $table['name'] = $name; Chris@0: } Chris@0: if ($remove_descriptions) { Chris@0: unset($table['description']); Chris@0: foreach ($table['fields'] as &$field) { Chris@0: unset($field['description']); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@16: * Typecasts values to proper data types. Chris@0: * Chris@0: * MySQL PDO silently casts, e.g. FALSE and '' to 0, when inserting the value Chris@0: * into an integer column, but PostgreSQL PDO does not. Look up the schema Chris@0: * information and use that to correctly typecast the value. Chris@0: * Chris@0: * @param array $info Chris@0: * An array describing the schema field info. Chris@0: * @param mixed $value Chris@0: * The value to be converted. Chris@0: * Chris@0: * @return mixed Chris@0: * The converted value. Chris@0: */ Chris@0: function drupal_schema_get_field_value(array $info, $value) { Chris@0: // Preserve legal NULL values. Chris@0: if (isset($value) || !empty($info['not null'])) { Chris@0: if ($info['type'] == 'int' || $info['type'] == 'serial') { Chris@0: $value = (int) $value; Chris@0: } Chris@0: elseif ($info['type'] == 'float') { Chris@0: $value = (float) $value; Chris@0: } Chris@0: elseif (!is_array($value)) { Chris@0: $value = (string) $value; Chris@0: } Chris@0: } Chris@0: return $value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @} End of "addtogroup schemaapi". Chris@0: */