schema.inc 6.6 KB

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