Removing old updates, fixing saving of global settings.
[project/filefield.git] / filefield.install
1 <?php
2 // $Id$
3 /**
4 * @file
5 * FileField: Defines a CCK file field type.
6 *
7 * Uses content.module to store the fid and field specific metadata,
8 * and Drupal's {files} table to store the actual file data.
9 */
10
11 include_once('field_file.inc');
12
13 /**
14 * Implementation of hook_install().
15 */
16 function filefield_install() {
17 drupal_load('module', 'content');
18 content_notify('install', 'filefield');
19 }
20
21 /**
22 * Implementation of hook_uninstall().
23 */
24 function filefield_uninstall() {
25 drupal_load('module', 'content');
26 content_notify('uninstall', 'filefield');
27 }
28
29 /**
30 * Implementation of hook_enable().
31 */
32 function filefield_enable() {
33 drupal_load('module', 'content');
34 content_notify('enable', 'filefield');
35 }
36
37 /**
38 * Implementation of hook_disable().
39 */
40 function filefield_disable() {
41 drupal_load('module', 'content');
42 content_notify('disable', 'filefield');
43 }
44
45 /**
46 * Implementation of hook_update_last_removed().
47 */
48 function filefield_update_last_removed() {
49 return 3;
50 }
51
52 /**
53 * Upgrade FileField to Drupal 6.
54 */
55 function filefield_update_6001() {
56 if ($abort = content_check_update('filefield')) {
57 return $abort;
58 }
59
60 $ret = array();
61 include_once(drupal_get_path('module','content') .'/includes/content.admin.inc');
62
63 // rename the field type from file to filefield. adhere to module namespace.
64 $ret[] = update_sql("UPDATE {content_node_field} SET type='filefield' WHERE type='file'");
65 // rename default widget to filefield_widget. adhere to module namespace.
66 $ret[] = update_sql("UPDATE {content_node_field_instance} SET widget_type='filefield_widget' WHERE widget_type='file'");
67
68 // update list default value and force list settings.
69 $result = db_query("SELECT * FROM {content_node_field} WHERE type = 'filefield'");
70 while ($field = db_fetch_object($result)) {
71 $updated = false;
72 $field_settings = unserialize($field->global_settings);
73 if (!isset($field_settings['list_default']) || !is_numeric($field_settings['list_default'])) {
74 $field_settings['list_default'] = 1;
75 $updated = true;
76
77 }
78 // set behavior to match old force_list behavior.
79 if (!empty($field_settings['force_list'])) {
80 $field_settings['list_default'] = 1;
81 $field_settings['force_list_default'] = 1;
82 $updated = true;
83 }
84 if ($updated) {
85 db_query("UPDATE {content_node_field} SET global_settings = '%s' WHERE field_name = '%s'", serialize($field_settings), $field->field_name);
86 }
87 }
88
89
90 // add data column to filefields.
91 $fields = content_fields();
92 foreach($fields as $field) {
93 if ($field['type'] != 'filefield') continue;
94 $new_field = $field;
95 $new_field['columns']['data'] = array('type' => 'text');
96 content_alter_db($field, $new_field);
97 }
98
99 // Build a batch that migrates the data in each filefield
100 $batch = array(
101 'title' => t('Migrating filefield values'),
102 'operations' => array(),
103 'file' => drupal_get_path('module', 'filefield') .'/filefield.install',
104 );
105 $content_info = _content_type_info();
106 foreach ($content_info['content types'] as $node_type => $node_info) {
107 foreach ($node_info['fields'] as $field_name => $field) {
108 if ($field['type'] == 'filefield') {
109 $batch['operations'][] = array('_filefield_update_6001_move_operation', array($field));
110 $batch['operations'][] = array('_filefield_update_6001_drop_operation', array($field));
111 }
112 }
113 }
114 batch_set($batch);
115
116
117 // clear them caches.
118 cache_clear_all('*', content_cache_tablename(), true);
119 cache_clear_all('*', 'cache', true);
120 return $ret;
121 }
122
123 /**
124 * Migrate field settings from 'force_list_default' and 'show_description'.
125 */
126 function filefield_update_6100() {
127 $ret = array();
128
129 module_load_include('inc', 'content', 'includes/content.crud');
130
131 $fields = content_fields();
132 foreach ($fields as $field) {
133 if ($field['type'] == 'filefield') {
134 $field['list_field'] = empty($field['force_list_default']);
135 $field['description_field'] = $field['show_description'];
136 _content_field_write($field);
137 $ret[] = array('success' => TRUE, 'query' => t('The File field %field has been updated with new settings.', array('%field' => $field['field_name'])));
138 }
139 }
140
141 return $ret;
142 }
143
144
145 /**
146 * Move the list and descriptions column into the serialized data column.
147 */
148 function _filefield_update_6001_move_operation($field, &$context) {
149 // Setup the first through
150 if (!isset($context['sandbox']['progress'])) {
151 $db_info = content_database_info($field);
152 $context['sandbox']['db_info'] = $db_info;
153 $context['sandbox']['table'] = $db_info['table'];
154 $context['sandbox']['col_data'] = $db_info['columns']['data']['column'];
155 $context['sandbox']['col_desc'] = $db_info['columns']['description']['column'];
156 $context['sandbox']['max'] = db_result(db_query("SELECT COUNT(*) FROM {". $db_info['table'] ."}"));
157 $context['sandbox']['progress'] = 0;
158 $context['sandbox']['current_node'] = 0;
159 }
160
161 // Work our way through the field values 50 rows at a time.
162 $limit = 50;
163 $result = db_query_range("SELECT * FROM {{$context['sandbox']['table']}} WHERE vid > %d ORDER BY nid ASC", $context['sandbox']['current_node'], 0, $limit);
164 while ($row = db_fetch_array($result)) {
165 // Try to unserialize the data column.
166 if (!empty($row[$context['sandbox']['col_data']])) {
167 $data = unserialize($row[$context['sandbox']['col_data']]);
168 }
169 if (empty($data)) {
170 $data = array();
171 }
172
173 // Copy move the values from the columns into the array...
174 $data['description'] = $row[$context['sandbox']['col_desc']];
175
176 // ...serialize it and store it back to the db.
177 db_query("UPDATE {{$context['sandbox']['table']}} SET {$context['sandbox']['col_data']} = '%s' WHERE vid = %d", serialize($data), $row['vid']);
178
179 // Update our progress information.
180 $context['sandbox']['progress']++;
181 $context['sandbox']['current_node'] = $row['vid'];
182 }
183
184 // Inform the batch engine that we are not finished,
185 // and provide an estimation of the completion level we reached.
186 if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
187 $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
188 }
189 }
190
191 /**
192 * Drop the list and description columns.
193 */
194 function _filefield_update_6001_drop_operation($field, &$context) {
195 $ret = array();
196 $db_info = content_database_info($field);
197 // TODO: Now that the data has been migrated we can drop the columns.
198 db_drop_field($ret, $db_info['table'], $db_info['columns']['description']['column']);
199 $context['finished'] = 1;
200 }