schema.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * @file
  4. * Schema API handling functions.
  5. */
  6. use Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema;
  7. /**
  8. * @addtogroup schemaapi
  9. * @{
  10. */
  11. /**
  12. * Indicates that a module has not been installed yet.
  13. */
  14. const SCHEMA_UNINSTALLED = -1;
  15. /**
  16. * Returns an array of available schema versions for a module.
  17. *
  18. * @param string $module
  19. * A module name.
  20. *
  21. * @return array|bool
  22. * If the module has updates, an array of available updates sorted by
  23. * version. Otherwise, FALSE.
  24. */
  25. function drupal_get_schema_versions($module) {
  26. $updates = &drupal_static(__FUNCTION__, NULL);
  27. if (!isset($updates[$module])) {
  28. $updates = [];
  29. foreach (\Drupal::moduleHandler()->getModuleList() as $loaded_module => $filename) {
  30. $updates[$loaded_module] = [];
  31. }
  32. // Prepare regular expression to match all possible defined hook_update_N().
  33. $regexp = '/^(?<module>.+)_update_(?<version>\d+)$/';
  34. $functions = get_defined_functions();
  35. // Narrow this down to functions ending with an integer, since all
  36. // hook_update_N() functions end this way, and there are other
  37. // possible functions which match '_update_'. We use preg_grep() here
  38. // instead of foreaching through all defined functions, since the loop
  39. // through all PHP functions can take significant page execution time
  40. // and this function is called on every administrative page via
  41. // system_requirements().
  42. foreach (preg_grep('/_\d+$/', $functions['user']) as $function) {
  43. // If this function is a module update function, add it to the list of
  44. // module updates.
  45. if (preg_match($regexp, $function, $matches)) {
  46. $updates[$matches['module']][] = $matches['version'];
  47. }
  48. }
  49. // Ensure that updates are applied in numerical order.
  50. foreach ($updates as &$module_updates) {
  51. sort($module_updates, SORT_NUMERIC);
  52. }
  53. }
  54. return empty($updates[$module]) ? FALSE : $updates[$module];
  55. }
  56. /**
  57. * Returns the currently installed schema version for a module.
  58. *
  59. * @param string $module
  60. * A module name.
  61. * @param bool $reset
  62. * Set to TRUE after installing or uninstalling an extension.
  63. * @param bool $array
  64. * Set to TRUE if you want to get information about all modules in the
  65. * system.
  66. *
  67. * @return string|int
  68. * The currently installed schema version, or SCHEMA_UNINSTALLED if the
  69. * module is not installed.
  70. */
  71. function drupal_get_installed_schema_version($module, $reset = FALSE, $array = FALSE) {
  72. $versions = &drupal_static(__FUNCTION__, []);
  73. if ($reset) {
  74. $versions = [];
  75. }
  76. if (!$versions) {
  77. if (!$versions = \Drupal::keyValue('system.schema')->getAll()) {
  78. $versions = [];
  79. }
  80. }
  81. if ($array) {
  82. return $versions;
  83. }
  84. else {
  85. return isset($versions[$module]) ? $versions[$module] : SCHEMA_UNINSTALLED;
  86. }
  87. }
  88. /**
  89. * Updates the installed version information for a module.
  90. *
  91. * @param string $module
  92. * A module name.
  93. * @param string $version
  94. * The new schema version.
  95. */
  96. function drupal_set_installed_schema_version($module, $version) {
  97. \Drupal::keyValue('system.schema')->set($module, $version);
  98. // Reset the static cache of module schema versions.
  99. drupal_get_installed_schema_version(NULL, TRUE);
  100. }
  101. /**
  102. * Creates all tables defined in a module's hook_schema().
  103. *
  104. * @param string $module
  105. * The module for which the tables will be created.
  106. */
  107. function drupal_install_schema($module) {
  108. $schema = drupal_get_module_schema($module);
  109. _drupal_schema_initialize($schema, $module, FALSE);
  110. foreach ($schema as $name => $table) {
  111. \Drupal::database()->schema()->createTable($name, $table);
  112. }
  113. }
  114. /**
  115. * Removes all tables defined in a module's hook_schema().
  116. *
  117. * @param string $module
  118. * The module for which the tables will be removed.
  119. */
  120. function drupal_uninstall_schema($module) {
  121. $tables = drupal_get_module_schema($module);
  122. _drupal_schema_initialize($tables, $module, FALSE);
  123. $schema = \Drupal::database()->schema();
  124. foreach ($tables as $table) {
  125. if ($schema->tableExists($table['name'])) {
  126. $schema->dropTable($table['name']);
  127. }
  128. }
  129. }
  130. /**
  131. * Returns a module's schema.
  132. *
  133. * This function can be used to retrieve a schema specification in
  134. * hook_schema(), so it allows you to derive your tables from existing
  135. * specifications.
  136. *
  137. * @param string $module
  138. * The module to which the table belongs.
  139. * @param string $table
  140. * The name of the table. If not given, the module's complete schema
  141. * is returned.
  142. */
  143. function drupal_get_module_schema($module, $table = NULL) {
  144. // Load the .install file to get hook_schema.
  145. module_load_install($module);
  146. $schema = \Drupal::moduleHandler()->invoke($module, 'schema');
  147. if (isset($table)) {
  148. if (isset($schema[$table])) {
  149. return $schema[$table];
  150. }
  151. return [];
  152. }
  153. elseif (!empty($schema)) {
  154. return $schema;
  155. }
  156. return [];
  157. }
  158. /**
  159. * Fills in required default values for table definitions from hook_schema().
  160. *
  161. * @param array $schema
  162. * The schema definition array as it was returned by the module's
  163. * hook_schema().
  164. * @param string $module
  165. * The module for which hook_schema() was invoked.
  166. * @param bool $remove_descriptions
  167. * (optional) Whether to additionally remove 'description' keys of all tables
  168. * and fields to improve performance of serialize() and unserialize().
  169. * Defaults to TRUE.
  170. */
  171. function _drupal_schema_initialize(&$schema, $module, $remove_descriptions = TRUE) {
  172. // Set the name and module key for all tables.
  173. foreach ($schema as $name => &$table) {
  174. if (empty($table['module'])) {
  175. $table['module'] = $module;
  176. }
  177. if (!isset($table['name'])) {
  178. $table['name'] = $name;
  179. }
  180. if ($remove_descriptions) {
  181. unset($table['description']);
  182. foreach ($table['fields'] as &$field) {
  183. unset($field['description']);
  184. }
  185. }
  186. }
  187. }
  188. /**
  189. * Typecasts values to proper data types.
  190. *
  191. * MySQL PDO silently casts, e.g. FALSE and '' to 0, when inserting the value
  192. * into an integer column, but PostgreSQL PDO does not. Look up the schema
  193. * information and use that to correctly typecast the value.
  194. *
  195. * @param array $info
  196. * An array describing the schema field info.
  197. * @param mixed $value
  198. * The value to be converted.
  199. *
  200. * @return mixed
  201. * The converted value.
  202. *
  203. * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use
  204. * \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::castValue() instead.
  205. *
  206. * @see https://www.drupal.org/node/3051983
  207. */
  208. function drupal_schema_get_field_value(array $info, $value) {
  209. @trigger_error('drupal_schema_get_field_value() is deprecated in drupal:8.8.0. It will be removed from drupal:9.0.0. Use \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::castValue($info, $value) instead. See https://www.drupal.org/node/3051983', E_USER_DEPRECATED);
  210. return SqlContentEntityStorageSchema::castValue($info, $value);
  211. }
  212. /**
  213. * @} End of "addtogroup schemaapi".
  214. */