serial.inc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * @file
  4. * Internal functions for the Serial module.
  5. *
  6. * Note: This module uses php in SQL to support dynamic table names.
  7. * (required because each serial field needs a dedicated dynamic table).
  8. * However, all table names are safe (passed through db_escape_table).
  9. *
  10. * It seems that this is better than using table names as arguments, e.g.
  11. * $query = 'INSERT INTO %s (nid) VALUES(%d)';
  12. * db_query($query, db_prefix_tables('{'. $table .'}'), $nid);
  13. */
  14. /**
  15. * Creates an assistant serial table for a new created field.
  16. *
  17. * @param array $field
  18. * Serial field.
  19. * @param array $instance
  20. * New instance of that serial field.
  21. */
  22. function _serial_create_table(array $field, array $instance) {
  23. $table = _serial_get_field_table_name($field, $instance);
  24. if (!db_table_exists($table)) {
  25. db_create_table($table, _serial_get_table_schema());
  26. }
  27. }
  28. /**
  29. * Drops an assistant serial table for a deleted field.
  30. *
  31. * @param array $field
  32. * Serial field.
  33. * @param array $instance
  34. * Deleted instance of that serial field.
  35. */
  36. function _serial_drop_table(array $field, array $instance) {
  37. db_drop_table(_serial_get_field_table_name($field, $instance));
  38. }
  39. /**
  40. * Renames serial table(s) when a entity bundle us renamed.
  41. *
  42. * @param string $entity_type
  43. * Type of entity.
  44. * @param string $bundle_old
  45. * An old entity bundle machine name.
  46. * @param string $bundle_new
  47. * A new entity bundle machine name.
  48. */
  49. function _serial_rename_tables($entity_type, $bundle_old, $bundle_new) {
  50. // Build the query to find all affected tables.
  51. $query = db_select('field_config', 'f')
  52. ->fields('f', array('field_name'));
  53. $query->join('field_config_instance', 'i', '(f.field_name = i.field_name)');
  54. $query->condition('f.type', SERIAL_FIELD_TYPE);
  55. $query->condition('i.entity_type', $entity_type);
  56. $query->condition('i.bundle', $bundle_new);
  57. // Rename each affected table.
  58. foreach ($query->addTag('node_access')->execute() as $record) {
  59. db_rename_table(
  60. _serial_get_table_name($entity_type, $bundle_old, $record->field_name),
  61. _serial_get_table_name($entity_type, $bundle_new, $record->field_name)
  62. );
  63. }
  64. }
  65. /**
  66. * Gets the name of the assistant table for a specific field.
  67. *
  68. * @param array $field
  69. * Serial field.
  70. * @param array $instance
  71. * An instance of that serial field.
  72. *
  73. * @return string
  74. * The name of the assistant table of the specified field instance.
  75. */
  76. function _serial_get_field_table_name(array $field, array $instance) {
  77. return _serial_get_table_name($instance['entity_type'], $instance['bundle'], $field['field_name']);
  78. }
  79. /**
  80. * Gets the name of the assistant table for a specific field.
  81. *
  82. * @param string $entity_type
  83. * Type of entity (e.g. node)
  84. * @param string $bundle
  85. * The name of the entity type that contains the field (e.g. content type)
  86. * @param string $field_name
  87. * The name of the field.
  88. *
  89. * @return string
  90. * The name of the assistant table of the specified field.
  91. */
  92. function _serial_get_table_name($entity_type, $bundle, $field_name) {
  93. // Remember about max length of MySQL tables - 64 symbols.
  94. // @todo Think about improvement for this.
  95. return db_escape_table('serial_' . md5("{$entity_type}_{$bundle}_{$field_name}"));
  96. }
  97. /**
  98. * Gets the schema of the assistant tables for generating serial values.
  99. *
  100. * @return array
  101. * Assistant table schema.
  102. */
  103. function _serial_get_table_schema() {
  104. return array(
  105. 'fields' => array(
  106. 'sid' => array(
  107. 'type' => SERIAL_FIELD_TYPE,
  108. 'not null' => TRUE,
  109. 'unsigned' => TRUE,
  110. 'description' => 'The atomic serial field.',
  111. ),
  112. 'uniqid' => array(
  113. 'type' => 'varchar',
  114. 'length' => 23,
  115. 'default' => '',
  116. 'not null' => TRUE,
  117. 'description' => 'Unique temporary allocation Id.',
  118. ),
  119. ),
  120. 'primary key' => array('sid'),
  121. 'unique keys' => array(
  122. 'uniqid' => array('uniqid'),
  123. ),
  124. );
  125. }
  126. /**
  127. * Generates a unique serial value (unique per entity bundle).
  128. *
  129. * @param string $entity_type
  130. * Type of entity (e.g. node)
  131. * @param string $bundle
  132. * Containing bundle (e.g. content type).
  133. * @param string $field_name
  134. * The field name.
  135. * @param bool $delete
  136. * Indicates if temporary records should be deleted.
  137. *
  138. * @return int
  139. * the unique serial value number.
  140. *
  141. * @throws \Exception
  142. */
  143. function _serial_generate_value($entity_type, $bundle, $field_name, $delete = TRUE) {
  144. $transaction = db_transaction();
  145. try {
  146. // Get the name of the relevant table.
  147. $table = _serial_get_table_name($entity_type, $bundle, $field_name);
  148. // Insert a temporary record to get a new unique serial value.
  149. $uniqid = uniqid('', TRUE);
  150. $sid = db_insert($table)
  151. ->fields(array('uniqid' => $uniqid))
  152. ->execute();
  153. // If there's a reason why it's come back undefined, reset it.
  154. $sid = isset($sid) ? $sid : 0;
  155. // Delete the temporary record.
  156. if ($delete && $sid && ($sid % 10) == 0) {
  157. db_delete($table)
  158. ->condition('sid', $sid, '<')
  159. ->execute();
  160. }
  161. // Return the new unique serial value.
  162. return $sid;
  163. }
  164. catch (Exception $e) {
  165. $transaction->rollback();
  166. watchdog_exception('serial', $e);
  167. throw $e;
  168. }
  169. }
  170. /**
  171. * Initializes the value of a new serial field in existing entities.
  172. *
  173. * @param string $entity_type
  174. * Type of entity (e.g. node)
  175. * @param string $bundle
  176. * Containing bundle (e.g. content type).
  177. * @param string $field_name
  178. * The field name.
  179. *
  180. * @return int
  181. * Number of existing entities that have been initialized.
  182. */
  183. function _serial_init_old_entities($entity_type, $bundle, $field_name) {
  184. $query = new EntityFieldQuery();
  185. $query->entityCondition('entity_type', $entity_type)
  186. ->fieldCondition($field_name);
  187. // The "comment" entity type does not support bundle conditions.
  188. // @see https://api.drupal.org/api/drupal/includes!entity.inc/function/EntityFieldQuery%3A%3AentityCondition/7
  189. if ('comment' !== $entity_type) {
  190. $query->entityCondition('bundle', $bundle);
  191. }
  192. $results = $query->execute();
  193. if (!empty($results[$entity_type])) {
  194. foreach ($results[$entity_type] as $entity) {
  195. list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
  196. $entity = entity_load_unchanged($entity_type, $id);
  197. $entity->{$field_name} = array(
  198. LANGUAGE_NONE => array(
  199. array(
  200. 'value' => _serial_generate_value($entity_type, $bundle, $field_name, FALSE),
  201. ),
  202. ),
  203. );
  204. field_attach_insert($entity_type, $entity);
  205. }
  206. return count($results[$entity_type]);
  207. }
  208. return 0;
  209. }
  210. /**
  211. * Retrieves all the managed serial fields.
  212. *
  213. * @return string[]
  214. * Result set containing entity type, entity bundle, field name.
  215. */
  216. function _serial_get_all_fields() {
  217. $query = db_select('field_config', 'f');
  218. $query->join('field_config_instance', 'i', 'i.field_name = f.field_name');
  219. return $query
  220. ->fields('i', array('entity_type', 'bundle', 'field_name'))
  221. ->condition('f.type', SERIAL_FIELD_TYPE)
  222. ->condition('i.deleted', 0)
  223. ->execute()
  224. ->fetchAll();
  225. }