forked from fmann/references_integrity
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreferences_integrity.inc
273 lines (236 loc) · 9.63 KB
/
references_integrity.inc
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
<?php
/**
* @file
* Common functions for the References Integrity.
*/
/**
* Get the list of referential integrity behaviors.
*
* @param $field_entities
* You can currently use either t('nodes') or t('users').
*/
function references_integrity_get_behavior_options($field_entities) {
return array(
REFERENCES_INTEGRITY_BEHAVIOR_NONE => t('Do nothing.'),
REFERENCES_INTEGRITY_BEHAVIOR_REMOVE_REFERENCE => t('Delete references to @entities when referenced @entities are deleted.', array('@entities' => $field_entities)),
REFERENCES_INTEGRITY_BEHAVIOR_REMOVE_ENTITY => t('Delete this entity when referenced @entities are deleted.', array('@entities' => $field_entities)),
);
}
/**
* Get the RI behavior defined for the given field name.
*/
function references_integrity_get_field_behavior($field_name) {
return variable_get('references_integrity_behavior_' . $field_name, '');
}
/**
* Get the RI message defined for the given field name.
*/
function references_integrity_get_delete_message($field_name) {
$op = references_integrity_get_field_behavior($field_name);
if ($op == REFERENCES_INTEGRITY_BEHAVIOR_REMOVE_REFERENCE){
$default_message = t('References to this entity will be deleted too.');
} elseif ($op == REFERENCES_INTEGRITY_BEHAVIOR_REMOVE_ENTITY){
$default_message = t('Entities referencing this entity will be deleted too.');
}
return variable_get('references_integrity_delete_message_' . $field_name, $default_message);
}
/**
* Get information about reference fields of the given type.
*
* @param $field_type
* Field types supported are 'node_reference', 'user_reference', and 'entityreference'
*/
function references_integrity_get_reference_fields($field_type, $counters = TRUE) {
static $entity_info = array();
$fields = array();
foreach (field_info_instances() as $entity_type => $entity) {
if (!isset($entity_info[$entity_type])) {
$entity_info = entity_get_info($entity_type);
}
foreach ($entity as $bundle_name => $bundle) {
foreach ($bundle as $field_name => $field) {
$field['info'] = field_info_field($field_name);
$field += array(
'entity_name' => $entity_info['label'],
'bundle_name' => isset($entity_info['bundles'][$bundle_name]['label']) ? $entity_info['bundles'][$bundle_name]['label'] : NULL,
);
if ($field['info']['type'] == $field_type) {
$fields[$field['field_name']] = references_integrity_get_field_info($field, $counters);
}
}
}
}
return $fields;
}
/**
* Get orphans related information about a single field.
*
* @param $field
* The field to retrieve information from.
*/
function references_integrity_get_field_info($field, $counters = TRUE) {
if (empty($field) || empty($field['field_name']) || empty($field['info']['type']) || !in_array($field['info']['type'], references_integrity_references())) {
return FALSE;
}
$counters_array = array('count' => 0, 'orphan' => 0, 'status' => 'ok');
if ($counters) {
$value_column = references_integrity_get_value_column($field['info']['type']);
$query = new EntityFieldQuery;
$query->entityCondition('entity_type', $field['entity_type']);
// If entity_type and bundle match, there is no bundle for this entity_type.
if ($field['entity_type'] !== $field['bundle']){
$query->entityCondition('bundle', $field['bundle']);
}
$query->fieldCondition($field['field_name'], $value_column, 0, '>');
$result = $query->execute();
$total_count = $orphan_count = 0;
while (list($type, $referring_entities) = each($result)) {
list($table, $field_info) = each($field['info']['storage']['details']['sql'][FIELD_LOAD_CURRENT]);
list($property, $columnname) = each($field_info);
$references = array();
foreach (entity_load($field['entity_type'], array_keys($referring_entities)) as $entity) {
foreach ($entity->{$field['field_name']} as $delta_group) {
foreach ($delta_group as $delta_item) {
$references[] = $delta_item[$property];
}
}
}
// Total count is the count of references, not of referring entities.
$total_count = count($references);
list($target_entity_type, $target_entity_id) = references_integrity_get_target_entity_type_and_property($field);
$valid_referenced_ids = array();
// The ids of entities that actually exist
if (!isset($target_entity_id)) {
$valid_referenced_ids = array_keys(entity_load($target_entity_type, $references));
}
else {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', $target_entity_type)
->propertyCondition($target_entity_id, array_unique($references), 'IN');
$efq_result = $query->execute();
if (isset($efq_result[$target_entity_type])) {
$valid_referenced_ids = array_keys($efq_result[$target_entity_type]);
}
}
$orphan_count = 0;
foreach ($references as $i => $reference) {
if (!in_array($reference, $valid_referenced_ids)) {
$orphan_count++;
}
}
$counters_array = array(
'count' => $total_count,
'orphan' => $orphan_count,
'status' => $orphan_count > 0 ? 'warning' : 'ok',
);
}
}
return array(
'entity_type' => $field['entity_type'],
'entity_name' => $field['entity_name'],
'bundle' => $field['bundle'],
'bundle_name' => $field['bundle_name'],
'field_name' => $field['field_name'],
'field_type' => $field['info']['type'],
'has_delta' => (!empty($field['multiple']) ? TRUE : FALSE),
'label' => $field['label'],
'ri_behavior' => references_integrity_get_field_behavior($field['field_name']),
) + $counters_array;
}
/**
* function references_integrity_get_target_entity_type_and_property
*
* - Return the foreign key information needed for the EFQ
*
* @param $field
*
* list($target_entity_type, $target_entity_id) = references_integrity_get_target_entity_type_and_property($field);
*/
function references_integrity_get_target_entity_type_and_property($field) {
// This is a bit of a hack, not sure if the index actually changed from
// 'info' to 'field_info'
if (empty($field['info']) && !empty($field['field_info'])) {
$field['info'] = $field['field_info'];
}
switch ($field['info']['type']) {
case 'node_reference':
return array('node', 'nid');
break;
case 'user_reference':
return array('user', 'uid');
break;
case 'entityreference':
$entity_info = entity_get_info($field['info']['settings']['target_type']);
return array($field['info']['settings']['target_type'], $entity_info['entity keys']['id']);
break;
}
if (isset($field['info']['foreign keys'])) {
$message = 'References Integrity could not handle the field type !field_type with foreign keys !foreign_keys';
$variables = array(
'!field_type' => $field['info']['type'],
'!foreign_keys' => serialize($field['info']['foreign keys']),
);
watchdog('references_integrity', $message, $variables);
drupal_set_message(t($message, $variables));
// Return node by default in order to not crash the page
return array('node', 'nid');
}
$message = 'References Integrity could not handle the field !field';
$variables = array(
'!field' => serialize($field),
);
watchdog('references_integrity', $message, $variables);
drupal_set_message(t($message, $variables));
// Return node by default in order to not crash the page
return array('node', 'nid');
}
/**
* Apply referential integrity rule to the given object id.
* Called prior to deleting an entity.
*
* @param $field_type
* Field types supported are 'node_reference', 'user_reference', and 'entityreference'
* @param $value_entity_type
* String name for entity type. (user, node, etc)
* @param $value_entity
* A fully built entity that is a field value.
* @param $apply (boolean)
* If false items will not be queued. Used to determined how many items will
* be modified.
*/
function references_integrity_apply($field_type, $value_entity_type, $value_entity, $apply = TRUE) {
$value_column = references_integrity_get_value_column($field_type);
list($value_entity_id) = entity_extract_ids($value_entity_type, $value_entity);
$queue = DrupalQueue::get('references_integrity');
$queue->createQueue();
$items = array();
// Get all references field instances.
foreach (references_integrity_get_reference_fields($field_type, FALSE) as $field) {
// Process only if the admin enforced referential integrity.
if ($field['ri_behavior'] == REFERENCES_INTEGRITY_BEHAVIOR_REMOVE_REFERENCE || $field['ri_behavior'] == REFERENCES_INTEGRITY_BEHAVIOR_REMOVE_ENTITY) {
$query = new EntityFieldQuery;
$query->entityCondition('entity_type', $field['entity_type']);
$query->entityCondition('bundle', $field['bundle']);
$query->fieldCondition($field['field_name'], $value_column, $value_entity_id, '=');
$result = $query->execute();
if (isset($result[$field['entity_type']])) {
foreach (entity_load($field['entity_type'], array_keys($result[$field['entity_type']])) as $field_entity_id => $field_entity) {
$item = array(
'entity_type' => $field['entity_type'],
'entity_id' => $field_entity_id,
'field_name' => $field['field_name'],
'column' => $value_column,
'references' => array($value_entity_id),
'op' => $field['ri_behavior']
);
// Queue the item for cron processing.
if ($apply){
$queue->createItem($item);
}
$items[] = $item;
}
}
}
}
return $items;
}