serial.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 $field
  18. * a serial field
  19. * @param $instance
  20. * a new instance of that serial field
  21. */
  22. function _serial_create_table($field, $instance) {
  23. $table = _serial_get_field_table_name($field, $instance);
  24. $schema = _serial_get_table_schema();
  25. db_create_table($table, $schema);
  26. }
  27. /**
  28. * Drops an assistant serial table for a deleted field.
  29. *
  30. * @param $field
  31. * a serial field
  32. * @param $instance
  33. * a deleted instance of that serial field
  34. */
  35. function _serial_drop_table($field, $instance) {
  36. $table = _serial_get_field_table_name($field, $instance);
  37. db_drop_table($table);
  38. }
  39. /**
  40. * Renames serial table(s) when a content type us renamed.
  41. *
  42. * @param $old_type
  43. * an old node type machine name
  44. * @param $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')->fields('f', array('field_name'));
  50. $table_joined_alias = $query->join(
  51. 'field_config_instance', 'i',
  52. '(f.field_name = i.field_name) AND ' .
  53. '(f.type = :field_type) AND (i.bundle = :bundle_type)',
  54. array(':field_type' => 'serial', ':bundle_type' => $new_type)
  55. );
  56. // Add an access check and execute it.
  57. $result = $query->addTag('node_access')->execute();
  58. // Rename each affected table.
  59. foreach ($result as $record) {
  60. $old_table = _serial_get_table_name($old_type, $record->field_name);
  61. $new_table = _serial_get_table_name($new_type, $record->field_name);
  62. db_rename_table($old_table, $new_table);
  63. }
  64. }
  65. /**
  66. * Gets the name of the assistant table for a specific field.
  67. *
  68. * @param $field
  69. * a serial field
  70. * @param $instance
  71. * an instance of that serial field
  72. * @return
  73. * the name of the assistant table of the specified field instance.
  74. */
  75. function _serial_get_field_table_name($field, $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 $bundle
  82. * the name of the entity type that contains the field
  83. * @param $field_name
  84. * the name of the field
  85. * @return
  86. * the name of the assistant table of the specified field.
  87. */
  88. function _serial_get_table_name($bundle, $field_name) {
  89. return db_escape_table( // be on the safe side
  90. 'serial_' . $bundle . '_' . $field_name);
  91. }
  92. /**
  93. * Gets the schema of the assistant tables for generating serial values.
  94. *
  95. * @return
  96. * the 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. 'nid' => array(
  108. 'type' => 'int',
  109. 'unsigned' => TRUE,
  110. 'not null' => TRUE,
  111. 'description' => 'Id of the owner node.',
  112. ),
  113. ),
  114. 'primary key' => array('sid'),
  115. 'unique keys' => array(
  116. 'nid' => array('nid'),
  117. ),
  118. );
  119. }
  120. /**
  121. * Generates a unique serial value (unique per node type).
  122. *
  123. * @param $nid
  124. * id of the node for which to generate a serial value
  125. * @param $bundle
  126. * a containing bundle (e.g. content type)
  127. * @param $field_name
  128. * the field name
  129. * @param $delete
  130. * indicates if temporary records should be deleted
  131. * @return
  132. * the unique serial value number.
  133. */
  134. function _serial_generate_value($nid, $bundle, $field_name, $delete = TRUE) {
  135. // Get the name of the relevant table.
  136. $table = _serial_get_table_name($bundle, $field_name);
  137. // Create a temporary record for this node and retrieve the serial value.
  138. $sid = db_insert($table)
  139. ->fields(array(
  140. 'nid' => $nid,
  141. ))
  142. ->execute();
  143. // If there's a reason why it's come back undefined, reset it.
  144. $sid = isset($sid) ? $sid : 0;
  145. // Delete old temporary records:
  146. if ($delete && ($sid % 10) == 0) {
  147. db_delete($table)
  148. ->condition('nid', $nid, '<')
  149. ->execute();
  150. }
  151. // Return the new unique serial value:
  152. return $sid;
  153. }
  154. /**
  155. * Initializes the value of a new serial field in existing nodes.
  156. *
  157. * @param $bundle
  158. * a containing bundle (e.g. content type)
  159. * @param $field_name
  160. * the field name
  161. * @return
  162. * the number of existing nodes that have been initialized.
  163. */
  164. function _serial_init_old_nodes($bundle, $field_name) {
  165. // Retrieve all the node ids of that type:
  166. $query = "SELECT nid FROM {node} WHERE type = :type ORDER BY nid";
  167. // TODO: Currently works only for nodes - should support comments and users.
  168. $result = db_query($query, array('type' => $bundle));
  169. // Allocate a serial number for every old node:
  170. $count = 0;
  171. foreach ($result as $node) {
  172. $nid = $node->nid;
  173. $node = node_load($nid);
  174. $sid = _serial_generate_value($nid, $bundle, $field_name, FALSE);
  175. $node->{$field_name} = array('und' => array(array('value' => $sid)));
  176. node_save($node);
  177. $last_nid = $nid;
  178. $count++;
  179. }
  180. // Delete temporary records (except the last):
  181. if (isset($last_nid)) {
  182. $serial_table = _serial_get_table_name($bundle, $field_name);
  183. db_delete($serial_table)
  184. ->condition('nid', $last_nid, '<')
  185. ->execute();
  186. }
  187. // Return the number of existing nodes that have been initialized:
  188. return $count;
  189. }