schema.inc 6.5 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 = array();
  28. foreach (\Drupal::moduleHandler()->getModuleList() as $loaded_module => $filename) {
  29. $updates[$loaded_module] = array();
  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. static $versions = array();
  72. if ($reset) {
  73. $versions = array();
  74. }
  75. if (!$versions) {
  76. if (!$versions = \Drupal::keyValue('system.schema')->getAll()) {
  77. $versions = array();
  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. db_create_table($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. $schema = drupal_get_module_schema($module);
  121. _drupal_schema_initialize($schema, $module, FALSE);
  122. foreach ($schema as $table) {
  123. if (db_table_exists($table['name'])) {
  124. db_drop_table($table['name']);
  125. }
  126. }
  127. }
  128. /**
  129. * Returns a module's schema.
  130. *
  131. * This function can be used to retrieve a schema specification in
  132. * hook_schema(), so it allows you to derive your tables from existing
  133. * specifications.
  134. *
  135. * @param string $module
  136. * The module to which the table belongs.
  137. * @param string $table
  138. * The name of the table. If not given, the module's complete schema
  139. * is returned.
  140. */
  141. function drupal_get_module_schema($module, $table = NULL) {
  142. // Load the .install file to get hook_schema.
  143. module_load_install($module);
  144. $schema = \Drupal::moduleHandler()->invoke($module, 'schema');
  145. if (isset($table)) {
  146. if (isset($schema[$table])) {
  147. return $schema[$table];
  148. }
  149. return array();
  150. }
  151. elseif (!empty($schema)) {
  152. return $schema;
  153. }
  154. return array();
  155. }
  156. /**
  157. * Fills in required default values for table definitions from hook_schema().
  158. *
  159. * @param array $schema
  160. * The schema definition array as it was returned by the module's
  161. * hook_schema().
  162. * @param string $module
  163. * The module for which hook_schema() was invoked.
  164. * @param bool $remove_descriptions
  165. * (optional) Whether to additionally remove 'description' keys of all tables
  166. * and fields to improve performance of serialize() and unserialize().
  167. * Defaults to TRUE.
  168. */
  169. function _drupal_schema_initialize(&$schema, $module, $remove_descriptions = TRUE) {
  170. // Set the name and module key for all tables.
  171. foreach ($schema as $name => &$table) {
  172. if (empty($table['module'])) {
  173. $table['module'] = $module;
  174. }
  175. if (!isset($table['name'])) {
  176. $table['name'] = $name;
  177. }
  178. if ($remove_descriptions) {
  179. unset($table['description']);
  180. foreach ($table['fields'] as &$field) {
  181. unset($field['description']);
  182. }
  183. }
  184. }
  185. }
  186. /**
  187. * Typecasts values to proper datatypes.
  188. *
  189. * MySQL PDO silently casts, e.g. FALSE and '' to 0, when inserting the value
  190. * into an integer column, but PostgreSQL PDO does not. Look up the schema
  191. * information and use that to correctly typecast the value.
  192. *
  193. * @param array $info
  194. * An array describing the schema field info.
  195. * @param mixed $value
  196. * The value to be converted.
  197. *
  198. * @return mixed
  199. * The converted value.
  200. */
  201. function drupal_schema_get_field_value(array $info, $value) {
  202. // Preserve legal NULL values.
  203. if (isset($value) || !empty($info['not null'])) {
  204. if ($info['type'] == 'int' || $info['type'] == 'serial') {
  205. $value = (int) $value;
  206. }
  207. elseif ($info['type'] == 'float') {
  208. $value = (float) $value;
  209. }
  210. elseif (!is_array($value)) {
  211. $value = (string) $value;
  212. }
  213. }
  214. return $value;
  215. }
  216. /**
  217. * @} End of "addtogroup schemaapi".
  218. */