serial.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 content type us renamed.
  41. *
  42. * @param string $old_type
  43. * An old node type machine name.
  44. * @param string $new_type
  45. * A new node type machine name.
  46. */
  47. function _serial_rename_tables($old_type, $new_type) {
  48. // Build the query to find all affected tables.
  49. $query = db_select('field_config', 'f')
  50. ->fields('f', array('field_name'));
  51. $query->join(
  52. 'field_config_instance',
  53. 'i',
  54. "f.field_name = i.field_name AND f.type = 'serial' AND i.bundle = '$new_type'"
  55. );
  56. // Rename each affected table.
  57. foreach ($query->addTag('node_access')->execute() as $record) {
  58. db_rename_table(
  59. _serial_get_table_name($old_type, $record->field_name),
  60. _serial_get_table_name($new_type, $record->field_name)
  61. );
  62. }
  63. }
  64. /**
  65. * Gets the name of the assistant table for a specific field.
  66. *
  67. * @param array $field
  68. * Serial field.
  69. * @param array $instance
  70. * An instance of that serial field.
  71. *
  72. * @return string
  73. * The name of the assistant table of the specified field instance.
  74. */
  75. function _serial_get_field_table_name(array $field, array $instance) {
  76. return _serial_get_table_name($instance['bundle'], $field['field_name']);
  77. }
  78. /**
  79. * Gets the name of the assistant table for a specific field.
  80. *
  81. * @param string $bundle
  82. * The name of the entity type that contains the field.
  83. * @param string $field_name
  84. * The name of the field.
  85. *
  86. * @return string
  87. * the name of the assistant table of the specified field.
  88. */
  89. function _serial_get_table_name($bundle, $field_name) {
  90. return db_escape_table('serial_' . $bundle . '_' . $field_name);
  91. }
  92. /**
  93. * Gets the schema of the assistant tables for generating serial values.
  94. *
  95. * @return array
  96. * Assistant table schema.
  97. */
  98. function _serial_get_table_schema() {
  99. return array(
  100. 'fields' => array(
  101. 'sid' => array(
  102. 'type' => 'serial',
  103. 'unsigned' => TRUE,
  104. 'not null' => TRUE,
  105. 'description' => 'The atomic serial field.',
  106. ),
  107. 'uniqid' => array(
  108. 'description' => 'Unique temporary allocation Id.',
  109. 'type' => 'varchar',
  110. 'length' => 23,
  111. 'not null' => TRUE,
  112. 'default' => ''),
  113. ),
  114. 'primary key' => array('sid'),
  115. 'unique keys' => array(
  116. 'uniqid' => array('uniqid'),
  117. ),
  118. );
  119. }
  120. /**
  121. * Generates a unique serial value (unique per node type).
  122. *
  123. * @param string $bundle
  124. * Containing bundle (e.g. content type).
  125. * @param string $field_name
  126. * The field name.
  127. * @param bool $delete
  128. * Indicates if temporary records should be deleted.
  129. *
  130. * @return int
  131. * the unique serial value number.
  132. */
  133. function _serial_generate_value($bundle, $field_name, $delete = TRUE) {
  134. // Get the name of the relevant table.
  135. $table = _serial_get_table_name($bundle, $field_name);
  136. // Insert a temporary record to get a new unique serial value.
  137. $uniqid = uniqid('', TRUE);
  138. $sid = db_insert($table)
  139. ->fields(array('uniqid' => $uniqid))
  140. ->execute();
  141. // If there's a reason why it's come back undefined, reset it.
  142. $sid = isset($sid) ? $sid : 0;
  143. // Delete the temporary record.
  144. if ($delete && ($sid % 10) == 0) {
  145. db_delete($table)
  146. ->condition('uniqid', $uniqid)
  147. ->execute();
  148. }
  149. // Return the new unique serial value.
  150. return $sid;
  151. }
  152. /**
  153. * Initializes the value of a new serial field in existing nodes.
  154. *
  155. * @todo Currently works only for nodes - should support comments and users.
  156. *
  157. * @param string $bundle
  158. * Containing bundle (e.g. content type).
  159. * @param string $field_name
  160. * The field name.
  161. *
  162. * @return int
  163. * Number of existing nodes that have been initialized.
  164. */
  165. function _serial_init_old_nodes($bundle, $field_name) {
  166. $nodes = node_load_multiple(array(), array('type' => $bundle));
  167. // Allocate a serial number for every old node.
  168. foreach ($nodes as $node) {
  169. $node->{$field_name} = array(
  170. LANGUAGE_NONE => array(
  171. array(
  172. 'value' => _serial_generate_value($bundle, $field_name, FALSE),
  173. ),
  174. ),
  175. );
  176. node_save($node);
  177. }
  178. // Return the number of existing nodes that have been initialized.
  179. return count($nodes);
  180. }
  181. /**
  182. * Retrieves all the managed serial fields.
  183. *
  184. * @return array
  185. * Pairs of node type name, field name.
  186. */
  187. function _serial_get_all_fields() {
  188. $query = db_select('field_config', 'f');
  189. $query->join('field_config_instance', 'i', 'i.field_name = f.field_name');
  190. return $query->fields('i', array('bundle', 'field_name'))
  191. ->condition('f.type', 'serial')
  192. ->condition('i.deleted', 0)
  193. ->execute()
  194. ->fetchAll();
  195. }