1 |
ca0757b9
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Common pages for the Media Migrate File Types module.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* File type migration page.
|
10 |
|
|
*
|
11 |
|
|
* Allows site administrator to execute migration of old/disabled/deleted
|
12 |
|
|
* file types to new ones.
|
13 |
|
|
*/
|
14 |
|
|
function media_migrate_file_types_upgrade_file_types($form, &$form_state) {
|
15 |
|
|
$migratable_types = _media_migrate_file_types_get_migratable_file_types();
|
16 |
|
|
|
17 |
|
|
// Silently return if there are no file types that need migration.
|
18 |
|
|
if (empty($migratable_types)) {
|
19 |
|
|
return array(
|
20 |
|
|
'message' => array(
|
21 |
|
|
'#markup' => t('There are no file types that need migration. The Media Medigrate File Types module can now be safely <a href="@modules">disabled</a>.', array('@modules' => url('admin/modules'))),
|
22 |
|
|
),
|
23 |
|
|
);
|
24 |
|
|
}
|
25 |
|
|
|
26 |
|
|
$form['message'] = array(
|
27 |
|
|
'message' => array(
|
28 |
|
|
'#markup' => t('This page allows you to migrate deprecated and/or disabled file types to new ones. It will migrate files from old type to new one and optionally migrate fields and delete old type.'),
|
29 |
|
|
),
|
30 |
|
|
);
|
31 |
|
|
|
32 |
|
|
$form['migrate_fields'] = array(
|
33 |
|
|
'#type' => 'checkbox',
|
34 |
|
|
'#title' => t('Migrate fields'),
|
35 |
|
|
'#default_value' => TRUE,
|
36 |
|
|
'#description' => t('Migrate fields and their values from old file types to new ones.'),
|
37 |
|
|
);
|
38 |
|
|
$form['delete_old_type'] = array(
|
39 |
|
|
'#type' => 'checkbox',
|
40 |
|
|
'#title' => t('Delete old type'),
|
41 |
|
|
'#default_value' => FALSE,
|
42 |
|
|
'#description' => t('Delete old file type if migration was successful and delete operation is possible (type is not exported in code).'),
|
43 |
|
|
);
|
44 |
|
|
$form['migrate_mimes'] = array(
|
45 |
|
|
'#type' => 'checkbox',
|
46 |
|
|
'#title' => t('Migrate type mime-type'),
|
47 |
|
|
'#default_value' => TRUE,
|
48 |
|
|
'#description' => t('Move mime-type from old type to new one.'),
|
49 |
|
|
);
|
50 |
|
|
|
51 |
|
|
$form['upgradable_types'] = array(
|
52 |
|
|
'#type' => 'fieldset',
|
53 |
|
|
'#title' => t('Upgradable file types'),
|
54 |
|
|
);
|
55 |
|
|
|
56 |
|
|
$options = array('- ' . t('Do not upgrade') . ' -');
|
57 |
|
|
foreach (file_type_get_enabled_types() as $type) {
|
58 |
|
|
$options[$type->type] = $type->label;
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
foreach ($migratable_types as $machine_name) {
|
62 |
|
|
$type = file_type_load($machine_name);
|
63 |
|
|
if (!$type) {
|
64 |
|
|
$type = new stdClass;
|
65 |
|
|
$type->label = $type->type = $machine_name;
|
66 |
|
|
}
|
67 |
|
|
$form['upgradable_types'][$machine_name] = array(
|
68 |
|
|
'#type' => 'select',
|
69 |
|
|
'#title' => $type->label,
|
70 |
|
|
'#options' => $options,
|
71 |
|
|
'#description' => t(
|
72 |
|
|
'Select file type which you want to migrate @type to. Select %no_upgrade if type should stay as it is.',
|
73 |
|
|
array('@type' => $type->label, '%no_upgrade' => '- ' . t('Do not upgrade') . ' -')),
|
74 |
|
|
);
|
75 |
|
|
}
|
76 |
|
|
|
77 |
|
|
$form['submit'] = array(
|
78 |
|
|
'#type' => 'submit',
|
79 |
|
|
'#value' => t('Start migration'),
|
80 |
|
|
);
|
81 |
|
|
|
82 |
|
|
return $form;
|
83 |
|
|
}
|
84 |
|
|
|
85 |
|
|
/**
|
86 |
|
|
* File type migration page submit handler.
|
87 |
|
|
*/
|
88 |
|
|
function media_migrate_file_types_upgrade_file_types_submit($form, &$form_state) {
|
89 |
|
|
$migratable_types = _media_migrate_file_types_get_migratable_file_types();
|
90 |
|
|
$migrate = FALSE;
|
91 |
|
|
foreach ($migratable_types as $type) {
|
92 |
|
|
if ($form_state['values'][$type]) {
|
93 |
|
|
$migrate = TRUE;
|
94 |
|
|
break;
|
95 |
|
|
}
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
// Return silently if no types were selected for migration.
|
99 |
|
|
if (!$migrate) {
|
100 |
|
|
return;
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
// Use confirmation page/form.
|
104 |
|
|
$query = $form_state['values'];
|
105 |
|
|
unset($query['op']);
|
106 |
|
|
unset($query['submit']);
|
107 |
|
|
unset($query['form_id']);
|
108 |
|
|
unset($query['form_token']);
|
109 |
|
|
unset($query['form_build_id']);
|
110 |
|
|
|
111 |
|
|
$form_state['redirect'] = array(
|
112 |
|
|
'admin/structure/file-types/upgrade/confirm',
|
113 |
|
|
array('query' => $query),
|
114 |
|
|
);
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
/**
|
118 |
|
|
* File types migration confirmation page.
|
119 |
|
|
*/
|
120 |
|
|
function media_migrate_file_types_upgrade_file_types_confirm($form, &$form_state) {
|
121 |
|
|
return confirm_form(
|
122 |
|
|
$form,
|
123 |
|
|
t('Do you really want to migrate selected file types?'),
|
124 |
|
|
'admin/structure/file-types/upgrade',
|
125 |
|
|
NULL,
|
126 |
|
|
t('Migrate')
|
127 |
|
|
);
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
/**
|
131 |
|
|
* File types migration confirmation page submit. Executes actual migration.
|
132 |
|
|
*/
|
133 |
|
|
function media_migrate_file_types_upgrade_file_types_confirm_submit($form, &$form_state) {
|
134 |
|
|
$migratable_types = _media_migrate_file_types_get_migratable_file_types();
|
135 |
|
|
foreach ($migratable_types as $type) {
|
136 |
|
|
if ($_GET[$type] && $bundle_new = file_type_load($_GET[$type])) {
|
137 |
|
|
// Old bundle might be deleted so let's fake some values.
|
138 |
|
|
$bundle_old = file_type_load($type);
|
139 |
|
|
if (empty($bundle_old)) {
|
140 |
|
|
$bundle_old = new stdClass;
|
141 |
|
|
$bundle_old->type = $type;
|
142 |
|
|
$bundle_old->mimetypes = array();
|
143 |
|
|
$bundle_old->export_type = 2;
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
// Migrate fields to new bundle.
|
147 |
|
|
if ($_GET['migrate_fields']) {
|
148 |
|
|
$old_fields = db_select('field_config_instance', 'fc')->fields('fc', array('field_name'))->condition('entity_type', 'file')->condition('bundle', $bundle_old->type)->execute()->fetchCol();
|
149 |
|
|
$new_fields = db_select('field_config_instance', 'fc')->fields('fc', array('field_name'))->condition('entity_type', 'file')->condition('bundle', $bundle_new->type)->execute()->fetchCol();
|
150 |
|
|
$fields_to_move = array_diff($old_fields, $new_fields);
|
151 |
|
|
$fields_to_drop = array_diff($old_fields, $fields_to_move);
|
152 |
|
|
|
153 |
|
|
db_update('field_config_instance')
|
154 |
|
|
->fields(array('bundle' => $bundle_new->type))
|
155 |
|
|
->condition('entity_type', 'file')
|
156 |
|
|
->condition('bundle', $bundle_old->type)
|
157 |
|
|
->condition('field_name', $fields_to_move, 'IN')
|
158 |
|
|
->execute();
|
159 |
|
|
|
160 |
|
|
db_delete('field_config_instance')
|
161 |
|
|
->condition('entity_type', 'file')
|
162 |
|
|
->condition('bundle', $bundle_old->type)
|
163 |
|
|
->condition('field_name', $fields_to_drop, 'IN')
|
164 |
|
|
->execute();
|
165 |
|
|
|
166 |
|
|
field_cache_clear();
|
167 |
|
|
module_invoke_all('field_attach_rename_bundle', 'file', $bundle_old->type, $bundle_new->type);
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
// Migrate mimetypes to new bundle.
|
171 |
|
|
if ($_GET['migrate_mimes']) {
|
172 |
|
|
$changed = FALSE;
|
173 |
|
|
foreach ($bundle_old->mimetypes as $mime) {
|
174 |
|
|
if (!file_entity_match_mimetypes($bundle_new->mimetypes, $mime)) {
|
175 |
|
|
$bundle_new->mimetypes[] = $mime;
|
176 |
|
|
$changed = TRUE;
|
177 |
|
|
}
|
178 |
|
|
}
|
179 |
|
|
|
180 |
|
|
if ($changed) {
|
181 |
|
|
file_type_save($bundle_new);
|
182 |
|
|
}
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
// Delete old bundle.
|
186 |
|
|
if ($_GET['delete_old_type'] && $bundle_old->export_type == 1) {
|
187 |
|
|
file_type_delete($bundle_old);
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
// Migrate files.
|
191 |
|
|
db_update('file_managed')
|
192 |
|
|
->fields(array('type' => $bundle_new->type))
|
193 |
|
|
->condition('type', $bundle_old->type)
|
194 |
|
|
->execute();
|
195 |
|
|
}
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
$form_state['redirect'] = 'admin/structure/file-types';
|
199 |
|
|
} |