-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreferences_integrity.inc
258 lines (225 loc) · 8.83 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
<?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, '');
}
/**
* 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 += 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['type'] == $field_type) {
$fields[] = 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['type']) || !in_array($field['type'], references_integrity_references())) {
return FALSE;
}
$counters_array = array('count' => 0, 'orphan' => 0, 'status' => 'ok');
if ($counters) {
$id = references_integrity_get_id_key($field['type']);
$query = new EntityFieldQuery;
$query->entityCondition('entity_type', $field['entity_type']);
if ($field['entity_type'] != 'user'){
$query->entityCondition('bundle', $field['bundle']);
}
$query->fieldCondition($field['field_name'], $id, 0, '>');
$result = $query->execute();
$total_count = $orphan_count = 0;
while (list($type, $referring_entities) = each($result)) {
list($table, $field_info) = each($field['storage']['details']['sql'][FIELD_LOAD_CURRENT]);
list($column, $columnname) = each($field_info);
$references = array();
foreach (entity_load($field['entity_type'], array_keys($referring_entities)) as $entity_id => $entity) {
$name = $field['field_name'];
foreach ($entity->{$name} as $language_code => $delta_group) {
foreach ($delta_group as $delta => $delta_item) {
$references[] = $delta_item[$column];
}
}
}
// 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_id($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]);
}
}
// dpm('REFERENCES');
// dpm($references);
// dpm('REFERENCED IDS');
// dpm($valid_referenced_ids);
$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['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_id
*
* - 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_id($field);
*/
function references_integrity_get_target_entity_type_and_id($field){
if ($field['type'] == 'user_reference'){
$target_entity_type = 'user';
$target_entity_id = 'uid';
} else if ($field['type'] == 'node_reference' || $field['entity_type'] == 'node'){
$target_entity_type = 'node';
$target_entity_id = 'nid';
} elseif ($field['type'] == 'entityreference'){
if (isset($field['field_info']['settings']['target_type'])){
$target_entity_type = $field['field_info']['settings']['target_type'];
} else {
list($target_entity_type, $garbage) = each($field['foreign keys']);
$target_entity_type = str_replace('_data', '', $target_entity_type);
}
$target_entity_id = NULL;
}
return array($target_entity_type, $target_entity_id);
}
/**
* 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 $field_value
* A node id (nid), user id (uid), or entity_id.
* @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, $object, $apply = TRUE) {
$id = references_integrity_get_id_key($field_type);
$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) {
if ($field['entity_type'] == 'node' && $id == 'target_id'){
$object_id = 'nid';
} else {
$object_id = $id;
}
$query = new EntityFieldQuery;
$query->entityCondition('entity_type', $field['entity_type']);
$query->entityCondition('bundle', $field['bundle']);
$query->fieldCondition($field['field_name'], $id, $object->{$object_id}, '=');
$result = $query->execute();
if (isset($result[$field['entity_type']])) {
foreach (entity_load($field['entity_type'], array_keys($result[$field['entity_type']])) as $entity_id => $entity) {
$item = array(
'entity_type' => $field['entity_type'],
'entity_id' => $entity_id,
'field_name' => $field['field_name'],
'column' => $id,
'references' => array($object->{$object_id}),
'op' => $field['ri_behavior']
);
if ($apply){
// Queue the item for cron processing.
$queue->createItem($item);
} else {
$items[] = $item;
}
}
}
}
}
if (! $apply){
return $items;
}
}