filefield_paths.install 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the FileField Paths module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function filefield_paths_schema() {
  10. $schema['filefield_paths'] = array(
  11. 'fields' => array(
  12. 'type' => array(
  13. 'type' => 'varchar',
  14. 'length' => 128,
  15. 'not null' => TRUE,
  16. 'default' => ''
  17. ),
  18. 'field' => array(
  19. 'type' => 'varchar',
  20. 'length' => 128,
  21. 'not null' => TRUE,
  22. 'default' => ''
  23. ),
  24. 'filename' => array(
  25. 'type' => 'text',
  26. 'size' => 'medium',
  27. 'not null' => TRUE,
  28. 'serialize' => TRUE
  29. ),
  30. 'filepath' => array(
  31. 'type' => 'text',
  32. 'size' => 'medium',
  33. 'not null' => TRUE,
  34. 'serialize' => TRUE
  35. ),
  36. 'active_updating' => array(
  37. 'type' => 'int',
  38. 'size' => 'tiny',
  39. 'not null' => TRUE,
  40. 'default' => 0
  41. ),
  42. ),
  43. 'unique keys' => array(
  44. 'type_field' => array('type', 'field'),
  45. ),
  46. );
  47. return $schema;
  48. }
  49. /**
  50. * Implements hook_schema_alter().
  51. *
  52. * @param $schema
  53. * The system-wide schema
  54. */
  55. function filefield_paths_schema_alter(&$schema) {
  56. $schema['file_managed']['fields']['origname'] = array(
  57. 'description' => 'Original name of the file.',
  58. 'type' => 'varchar',
  59. 'length' => 255,
  60. 'not null' => TRUE,
  61. 'default' => '',
  62. );
  63. }
  64. /**
  65. * Implements hook_install().
  66. */
  67. function filefield_paths_install() {
  68. // Add origname field to {file_managed}, and populate with the current
  69. // filenames.
  70. db_add_field('file_managed', 'origname', array(
  71. 'description' => 'Original name of the file with no path components. Used by the filefield_paths module.',
  72. 'type' => 'varchar',
  73. 'length' => 255,
  74. 'not null' => TRUE,
  75. 'default' => '',
  76. ));
  77. db_update('file_managed')
  78. ->expression('origname', 'filename')
  79. ->execute();
  80. }
  81. /**
  82. * Implements hook_uninstall().
  83. */
  84. function filefield_paths_uninstall() {
  85. db_drop_field('file_managed', 'origname');
  86. }
  87. /**
  88. * Implements hook_update_last_removed().
  89. */
  90. function hook_update_last_removed() {
  91. return 6103;
  92. }
  93. /**
  94. * Implements hook_update_dependencies().
  95. */
  96. function filefield_paths_dependencies() {
  97. // Update 7103 uses the {file_managed} table, so make sure it is available.
  98. $dependencies['filefield_paths'][7103] = array(
  99. 'system' => 7034,
  100. );
  101. return $dependencies;
  102. }
  103. /**
  104. * Add origname field to {file_managed}.
  105. */
  106. function filefield_paths_update_7103() {
  107. // Clean-up an unused variable.
  108. variable_del('filefield_paths_schema_version');
  109. // Add origname field to {file_managed}, and populate with the current
  110. // filenames.
  111. if (!db_field_exists('file_managed', 'origname')) {
  112. db_add_field('file_managed', 'origname', array(
  113. 'description' => 'Original name of the file with no path components. Used by the filefield_paths module.',
  114. 'type' => 'varchar',
  115. 'length' => 255,
  116. 'not null' => TRUE,
  117. 'default' => '',
  118. ));
  119. }
  120. db_update('file_managed')
  121. ->expression('origname', 'filename')
  122. ->condition('origname', '')
  123. ->execute();
  124. }
  125. /**
  126. * Add active updating flag to {filefield_paths}
  127. */
  128. function filefield_paths_update_7104() {
  129. db_add_field('filefield_paths', 'active_updating', array(
  130. 'type' => 'int',
  131. 'size' => 'tiny',
  132. 'not null' => TRUE,
  133. 'default' => '0'
  134. ));
  135. // migrate variable to filefield_paths table
  136. $result = db_query("SELECT name, value FROM {variable} WHERE name LIKE 'ffp_%%_field_%%'");
  137. foreach ($result as $row) {
  138. if (preg_match('/ffp_(.+)_field_(.+)$/', $row->name, $match)) {
  139. $active_updating = unserialize($row->value);
  140. if ($active_updating) {
  141. db_update('filefield_paths')
  142. ->fields(array(
  143. 'active_updating' => $active_updating
  144. ))
  145. ->condition('type', $match[1])
  146. ->condition('field', $match[2])
  147. ->execute();
  148. }
  149. variable_del($row->name);
  150. }
  151. }
  152. }
  153. /**
  154. * Correct the default value for {filefield_paths}.active_updating field.
  155. */
  156. function filefield_paths_update_7105() {
  157. db_change_field('filefield_paths', 'active_updating', 'active_updating', array(
  158. 'type' => 'int',
  159. 'size' => 'tiny',
  160. 'not null' => TRUE,
  161. 'default' => 0
  162. ));
  163. }
  164. /**
  165. * Increase length of 'type' and 'field' columns.
  166. */
  167. function filefield_paths_update_7106() {
  168. db_change_field('filefield_paths', 'type', 'type', array(
  169. 'type' => 'varchar',
  170. 'length' => 128,
  171. 'not null' => TRUE,
  172. 'default' => ''
  173. ));
  174. db_change_field('filefield_paths', 'field', 'field', array(
  175. 'type' => 'varchar',
  176. 'length' => 128,
  177. 'not null' => TRUE,
  178. 'default' => ''
  179. ));
  180. }