-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeatures_cleanup.module
executable file
·380 lines (354 loc) · 14.1 KB
/
features_cleanup.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
/**
* @file
* Module file for Features Cleanup.
*/
/**
* Implements hook_menu().
*/
function features_cleanup_menu() {
$items['admin/structure/features/%feature/resolve_overrides'] = array(
'title' => 'Resolve Overrides',
'description' => 'Resolve pesky overrides by deleting default components based on current feature.',
'page callback' => 'features_cleanup_diff',
'page arguments' => array(3, 5),
'load arguments' => array(3, TRUE),
'access callback' => 'features_access_override_actions',
'access arguments' => array(3),
'type' => MENU_LOCAL_TASK,
);
$items['admin/structure/features/%feature/resolve_overrides/perform'] = array(
'title' => 'Performing Resolution of all Overrides',
'description' => 'Resolve pesky overrides by deleting default components based on current feature.',
'page callback' => 'features_cleanup_override_perform',
'page arguments' => array(3, 5, TRUE),
'load arguments' => array(3, TRUE),
'access callback' => 'features_access_override_actions',
'access arguments' => array(3),
'type' => MENU_LOCAL_TASK,
);
$items['admin/structure/features/sync-features'] = array(
'title' => 'Cleanup Features',
'description' => 'Resolve content type, panels, vocabularies, and image styles by deleting default based on all feature.',
'page callback' => 'drupal_get_form',
'page arguments' => array('features_cleanup_sync_features_form'),
'access arguments' => array('Administer content'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_form().
*/
function features_cleanup_sync_features_form($form, $form_state) {
$form['info'] = array(
'#markup' =>
'<p>' . t('WARNING: You are about to sync all the features on your site. Doing this will delete any content types, panels, vocabularies, and image styles not contained within any ACTIVE feature. Please activate all desired features before running this operation. Additionally, please backup your code and database.') . '</p>' .
'<p>' . t('Are you sure you wish to continue?') . '</p>',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Sync all features'),
);
$form['cancel'] = array();
return $form;
}
/**
* Callback of form submit.
*/
function features_cleanup_sync_features_form_submit($form, &$form_state) {
features_cleanup_perform();
drupal_set_message(t('All features have now been synced. All content types, panels, vocabularies, and image styles not controlled via features have been deleted.'));
drupal_goto('admin/structure/features');
}
/**
* Perform a cleanup of content types, page manager pages (node panels), mini panels, taxonomy vocabs, image styles.
*/
function features_cleanup_perform() {
$features = features_get_features();
$msgs = array();
$content_types = array();
$entityform_types = array();
$mini_panels = array();
$fpages = array();
$vocabs = array();
$menus = array();
foreach ($features as $i => $feature) {
if ($feature->status) {
// Delete all fields and then prepare additional deletions.
features_cleanup_override_perform($feature);
if (isset($feature->info['features']['node'])) {
foreach ($feature->info['features']['node'] as $j => $content_type) {
$content_types[] = $content_type;
}
}
if (isset($feature->info['features']['entityform_type'])) {
foreach ($feature->info['features']['entityform_type'] as $j => $entityform_type) {
$entityform_types[] = $entityform_type;
}
}
if (isset($feature->info['features']['page_manager_pages'])) {
foreach ($feature->info['features']['page_manager_pages'] as $j => $page) {
$fpages[] = $page;
}
}
if (isset($feature->info['features']['panels_mini'])) {
foreach ($feature->info['features']['panels_mini'] as $j => $panel) {
$mini_panels[] = $panel;
}
}
if (isset($feature->info['features']['taxonomy'])) {
foreach ($feature->info['features']['taxonomy'] as $j => $vocab) {
$vocabs[] = $vocab;
}
}
if (isset($feature->info['features']['image'])) {
foreach ($feature->info['features']['image'] as $j => $style) {
$styles[] = $style;
}
}
if (isset($feature->info['features']['menu_custom'])) {
foreach ($feature->info['features']['menu_custom'] as $j => $menu) {
$menus[] = $menu;
}
}
}
}
// Sync Content Types.
$all_content_types_in_db = node_type_get_types();
foreach ($all_content_types_in_db as $id => $obj) {
if ((array_search($id, $content_types) === FALSE) && ($obj->custom == TRUE)) {
node_type_delete($id);
features_cleanup_add_msg(t('Features Cleanup deleted content type !ct-name (id: !ct-id) from the database.', array('!ct-name' => $obj->name, '!ct-id' => $id)));
}
}
// Sync Entityform types.
// @todo
// Sync Panel Pages.
if (module_exists('page_manager')) {
module_load_include('inc', 'page_manager', 'page_manager.admin');
$tasks = page_manager_get_tasks_by_type('page');
$pages = array('operations' => array(), 'tasks' => array());
page_manager_get_pages($tasks, $pages);
$dpages = array();
foreach ($pages['rows'] as $i => $v) {
if ($v['data']['type']['data'] == 'Custom') {
$dpages[] = $i;
}
}
foreach ($fpages as $i => $v) {
$fpages[$i] = 'page-' . $v;
}
foreach ($dpages as $id => $page) {
if (array_search($page, $fpages) === FALSE) {
$page = str_replace('page-', '', $page);
$p = page_manager_page_load($page);
page_manager_page_delete($p);
features_cleanup_add_msg(t('Features Cleanup deleted page !p-name (id: !p-id) at !p-path from the database.', array('!p-name' => $p->admin_title, '!p-id' => $p->pid, '!p-path' => $p->path)));
}
}
}
if (module_exists('panels_mini')) {
// Sync Mini panels.
$dpanels = panels_mini_load_all();
foreach ($dpanels as $id => $panel) {
if (array_search($id, $mini_panels) === FALSE) {
$p = panels_mini_load($id);
panels_mini_delete($p);
features_cleanup_add_msg(t('Features Cleanup deleted mini panel !p-name (id: !p-id) from the database.', array('!p-name' => $p->admin_title, '!p-id' => $p->pid)));
}
}
}
if (module_exists('taxonomy')) {
// Sync Vocabs.
$dvocabs = taxonomy_vocabulary_get_names();
foreach ($dvocabs as $id => $vocab) {
if (array_search($id, $vocabs) === FALSE) {
taxonomy_vocabulary_delete($vocab->vid);
features_cleanup_add_msg(t('Features Cleanup deleted taxonomy !t-name (id: !t-id) from the database.', array('!t-name' => $vocab->name, '!t-id' => $vocab->vid)));
}
}
}
if (module_exists('image')) {
// Sync Image Styles.
$dstyles = image_styles();
foreach ($dstyles as $id => $style) {
if (isset($style['isid']) && array_search($id, $styles) === FALSE) {
features_cleanup_add_msg(t('Features Cleanup deleted image style !is-name (id: !is-id) from the database.', array('!is-name' => $style['name'], '!is-id' => $style['isid'])));
image_style_delete($style);
}
}
}
if (module_exists('menu')) {
// Sync Custom Menus.
$dmenus = menu_get_menus();
foreach ($dmenus as $id => $menu) {
if (array_search($id, $menus) === FALSE) {
$menu_obj = menu_load($id);
menu_delete($menu_obj);
features_cleanup_add_msg(t('Features Cleanup deleted custom menu !is-name (id: !is-id) from the database.', array('!is-name' => $menu, '!is-id' => $id)));
}
}
}
$msgs = features_cleanup_add_msg();
if (!empty($msgs)) {
watchdog('Features Cleanup', theme('item_list', array('title' => t('The following Features Cleanup actions were performed:'), 'items' => $msgs)));
}
return TRUE;
}
/**
* Returns a diff object for a feature in the code. Pass in a component to run a diff only on that component.
*
* @param $feature
* A loaded feature object to display differences for.
* @param $component
* Optional: specific component to display differences for. If excluded, all components are used.
*/
function features_cleanup_get_diff($feature, $component = NULL) {
module_load_include('inc', 'features', 'features.export');
$overrides = features_detect_overrides($feature);
$output = '';
if (!empty($overrides)) {
// Filter overrides down to specified component.
if (isset($component) && isset($overrides[$component])) {
$overrides = array($component => $overrides[$component]);
}
module_load_include('inc', 'diff', 'diff.engine');
foreach ($overrides as $component => $items) {
$diff[$component] = new Diff(explode("\n", $items['default']), explode("\n", $items['normal']));
}
}
else {
return array();
}
return $diff;
}
/**
* Page callback to display the differences between what's in code and
* what is in the db.
*
* @param $feature
* A loaded feature object to display differences for.
* @param $component
* Optional: specific component to display differences for. If excluded, all components are used.
*
* @return Themed display of what is different.
*/
function features_cleanup_diff($feature, $component = NULL) {
drupal_add_css(drupal_get_path('module', 'features') . '/features.css');
$output = '';
module_load_include('inc', 'diff', 'diff.engine');
// - temporarily broken.
$formatter = new DrupalDiffFormatter();
// $formatter = new DiffFormatter();
$formatter->leading_context_lines = 2;
$formatter->trailing_context_lines = 2;
// $formatter->show_header = $show_header.
$rows = array();
$all_diffs = features_cleanup_get_diff($feature, $component);
if ($all_diffs != array()) {
foreach ($all_diffs as $component => $diff) {
$rows[] = array(array(array('data' => $component, 'colspan' => 4, 'header' => TRUE)));
// dpm($formatter->format($diff));
$rows = array_merge($rows, $formatter->format($diff));
}
$header = array(
array('data' => t('Default'), 'colspan' => 2),
array('data' => t('Overrides'), 'colspan' => 2),
);
$output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('class' => array('diff', 'features-diff'))));
$output .= l('Resolve All Overrides', 'admin/structure/features/' . $feature->name . '/resolve_overrides/perform');
}
else {
$output = "<div class='features-empty'>" . t('No changes have been made to this feature.') . "</div>";
}
$output = array('page' => array('#markup' => "<div class='features-comparison'>{$output}</div>"));
return $output;
}
/**
* Perform override resolution on a particular feature, component.
*
* @param $feature
* A loaded feature object to display differences for.
* @param $component
* Optional: specific component to display differences for. If excluded, all components are used.
* @param $drush
* Optional: Whether drush is being used or not.
*/
function features_cleanup_override_perform($feature, $component = NULL, $redirect = FALSE) {
$feature_name = $feature->name;
$all_diffs = features_cleanup_get_diff($feature, $component);
// Clean up field instances.
foreach ($all_diffs as $component => $feature) {
if ($component == 'info') {
foreach ($feature->edits as $i => $diff_obj) {
if ($diff_obj->type == 'add' && empty($diff_obj->orig)) {
// This is invoked, when the field was removed from the features, but still exists in the database.
$component = drupal_parse_info_format(current($diff_obj->closing));
if (!empty($component['features']['field_instance'])) {
$instance = str_replace('"', '', current($component['features']['field_instance']));
list($entity_type, $bundle, $field_name) = explode('-', $instance);
_features_cleanup_delete_field($bundle, $field_name);
}
}
/*
if ($diff_obj->type == 'delete') {
// This is invoked, when the field was removed from the database, but it's still in the features.
// This scenario is probably not needed.
$component = drupal_parse_info_format(current($diff_obj->orig));
if (!empty($component['features']['field_instance'])) {
$instance = str_replace('"', '', current($component['features']['field_instance']));
list($entity_type, $bundle, $field_name) = explode('-', $instance);
_features_cleanup_delete_field($bundle, $field_name);
}
}
*/
}
}
}
// Revert after override resolution is performed.
features_revert(array($feature_name => array(0 => 'field')));
features_cleanup_add_msg(t('Features Cleanup has resolved all field overrides and reverted to this feature.'));
if ($redirect) {
drupal_goto('admin/structure/features/' . $feature_name);
}
}
/**
* Delete given field from bundle.
*
* @param string $content_type
* The content type (bundle) associated with the instance.
* @param string $field
* The field name for the instance.
*/
function _features_cleanup_delete_field($content_type, $field) {
$field_info = field_info_field($field);
$count = 0;
$deleted = FALSE;
foreach ($field_info['bundles'] as $entity_type => $bundles) {
foreach ($bundles as $bundle) {
$count++;
if ($bundle == $content_type) {
$instance = field_info_instance($entity_type, $field, $bundle);
field_delete_instance($instance, FALSE);
features_cleanup_add_msg(t('Features Cleanup deleted field !f-name (id: !f-id) from the "!content-type" content type.', array('!f-name' => $field_info['field_name'], '!f-id' => $field_info['id'], '!content-type' => $bundle)));
$deleted = TRUE;
}
}
}
if ($count == 1 && $deleted) {
field_delete_field($field);
features_cleanup_add_msg(t('Features Cleanup deleted field !f-name (id: !f-id) entirely as the reference in the "!content-type" content type was the last remaining reference.', array('!f-name' => $field_info['field_name'], '!f-id' => $field_info['id'], '!content-type' => $bundle)));
}
}
/**
* Shows message to the user.
*/
function features_cleanup_add_msg($msg = NULL) {
static $msgs = array();
if ($msg) {
$msgs[] = $msg;
drupal_set_message(filter_xss($msg));
}
return $msgs;
}