1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Functions related to the tracking the file usage of embedded media.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_field_attach_insert().
|
10
|
*
|
11
|
* Track file usage for media files included in formatted text. Note that this
|
12
|
* is heavy-handed, and should be replaced when Drupal's filter system is
|
13
|
* context-aware.
|
14
|
*/
|
15
|
function media_wysiwyg_field_attach_insert($entity_type, $entity) {
|
16
|
_media_wysiwyg_filter_add_file_usage_from_fields($entity_type, $entity);
|
17
|
}
|
18
|
|
19
|
/**
|
20
|
* Implements hook_field_attach_update().
|
21
|
*
|
22
|
* @see media_field_attach_insert().
|
23
|
*/
|
24
|
function media_wysiwyg_field_attach_update($entity_type, $entity) {
|
25
|
_media_wysiwyg_filter_add_file_usage_from_fields($entity_type, $entity);
|
26
|
}
|
27
|
|
28
|
/**
|
29
|
* Add file usage from file references in an entity's text fields.
|
30
|
*/
|
31
|
function _media_wysiwyg_filter_add_file_usage_from_fields($entity_type, $entity) {
|
32
|
// Track the total usage for files from all fields combined.
|
33
|
$entity_files = media_wysiwyg_entity_field_count_files($entity_type, $entity);
|
34
|
|
35
|
list($entity_id, $entity_vid, $entity_bundle) = entity_extract_ids($entity_type, $entity);
|
36
|
|
37
|
// When an entity has revisions and then is saved again NOT as new version the
|
38
|
// previous revision of the entity has be loaded to get the last known good
|
39
|
// count of files. The saved data is compared against the last version
|
40
|
// so that a correct file count can be created for that (the current) version
|
41
|
// id. This code may assume some things about entities that are only true for
|
42
|
// node objects. This should be reviewed.
|
43
|
// @TODO this conditional can probably be condensed
|
44
|
if (empty($entity->revision) && empty($entity->old_vid) && empty($entity->is_new) && ! empty($entity->original)) {
|
45
|
$old_files = media_wysiwyg_entity_field_count_files($entity_type, $entity->original);
|
46
|
foreach ($old_files as $fid => $old_file_count) {
|
47
|
// Were there more files on the node just prior to saving?
|
48
|
if (empty($entity_files[$fid])) {
|
49
|
$entity_files[$fid] = 0;
|
50
|
}
|
51
|
if ($old_file_count > $entity_files[$fid]) {
|
52
|
$deprecate = $old_file_count - $entity_files[$fid];
|
53
|
// Now deprecate this usage
|
54
|
$file = file_load($fid);
|
55
|
if ($file) {
|
56
|
file_usage_delete($file, 'media', $entity_type, $entity_id, $deprecate);
|
57
|
}
|
58
|
// Usage is deleted, nothing more to do with this file
|
59
|
unset($entity_files[$fid]);
|
60
|
}
|
61
|
// There are the same number of files, nothing to do
|
62
|
elseif ($entity_files[$fid] == $old_file_count) {
|
63
|
unset($entity_files[$fid]);
|
64
|
}
|
65
|
// There are more files now, adjust the difference for the greater number.
|
66
|
// file_usage incrementing will happen below.
|
67
|
else {
|
68
|
// We just need to adjust what the file count will account for the new
|
69
|
// images that have been added since the increment process below will
|
70
|
// just add these additional ones in
|
71
|
$entity_files[$fid] = $entity_files[$fid] - $old_file_count;
|
72
|
}
|
73
|
}
|
74
|
}
|
75
|
|
76
|
// Each entity revision counts for file usage. If versions are not enabled
|
77
|
// the file_usage table will have no entries for this because of the delete
|
78
|
// query above.
|
79
|
foreach ($entity_files as $fid => $entity_count) {
|
80
|
if ($file = file_load($fid)) {
|
81
|
file_usage_add($file, 'media', $entity_type, $entity_id, $entity_count);
|
82
|
}
|
83
|
}
|
84
|
}
|
85
|
|
86
|
/**
|
87
|
* Parse file references from an entity's text fields and return them as an array.
|
88
|
*/
|
89
|
function media_wysiwyg_filter_parse_from_fields($entity_type, $entity) {
|
90
|
$file_references = array();
|
91
|
|
92
|
foreach (media_wysiwyg_filter_fields_with_text_filtering($entity_type, $entity) as $field_name) {
|
93
|
if ($field_items = field_get_items($entity_type, $entity, $field_name)) {
|
94
|
foreach ($field_items as $field_item) {
|
95
|
preg_match_all(MEDIA_WYSIWYG_TOKEN_REGEX, $field_item['value'], $matches);
|
96
|
foreach ($matches[0] as $tag) {
|
97
|
$tag = str_replace(array('[[', ']]'), '', $tag);
|
98
|
$tag_info = drupal_json_decode($tag);
|
99
|
if (isset($tag_info['fid']) && $tag_info['type'] == 'media') {
|
100
|
$file_references[] = $tag_info;
|
101
|
}
|
102
|
}
|
103
|
|
104
|
preg_match_all(MEDIA_WYSIWYG_TOKEN_REGEX, $field_item['value'], $matches_alt);
|
105
|
foreach ($matches_alt[0] as $tag) {
|
106
|
$tag = urldecode($tag);
|
107
|
$tag_info = drupal_json_decode($tag);
|
108
|
if (isset($tag_info['fid']) && $tag_info['type'] == 'media') {
|
109
|
$file_references[] = $tag_info;
|
110
|
}
|
111
|
}
|
112
|
}
|
113
|
}
|
114
|
}
|
115
|
|
116
|
return $file_references;
|
117
|
}
|
118
|
|
119
|
/**
|
120
|
* Utility function to get the file count in this entity
|
121
|
*
|
122
|
* @param type $entity
|
123
|
* @param type $entity_type
|
124
|
* @return int
|
125
|
*/
|
126
|
function media_wysiwyg_entity_field_count_files($entity_type, $entity) {
|
127
|
$entity_files = array();
|
128
|
foreach (media_wysiwyg_filter_parse_from_fields($entity_type, $entity) as $file_reference) {
|
129
|
if (empty($entity_files[$file_reference['fid']])) {
|
130
|
$entity_files[$file_reference['fid']] = 1;
|
131
|
}
|
132
|
else {
|
133
|
$entity_files[$file_reference['fid']]++;
|
134
|
}
|
135
|
}
|
136
|
return $entity_files;
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* Implements hook_entity_delete().
|
141
|
*/
|
142
|
function media_wysiwyg_entity_delete($entity, $type) {
|
143
|
list($entity_id) = entity_extract_ids($type, $entity);
|
144
|
|
145
|
db_delete('file_usage')
|
146
|
->condition('module', 'media')
|
147
|
->condition('type', $type)
|
148
|
->condition('id', $entity_id)
|
149
|
->execute();
|
150
|
}
|
151
|
|
152
|
/**
|
153
|
* Implements hook_field_attach_delete_revision().
|
154
|
*
|
155
|
* @param type $entity_type
|
156
|
* @param type $entity
|
157
|
*/
|
158
|
function media_wysiwyg_field_attach_delete_revision($entity_type, $entity) {
|
159
|
list($entity_id) = entity_extract_ids($entity_type, $entity);
|
160
|
$files = media_wysiwyg_entity_field_count_files($entity_type, $entity);
|
161
|
foreach ($files as $fid => $count) {
|
162
|
if ($file = file_load($fid)) {
|
163
|
file_usage_delete($file, 'media', $entity_type , $entity_id, $count);
|
164
|
}
|
165
|
}
|
166
|
}
|
167
|
|
168
|
/**
|
169
|
* Implements hook_entity_dependencies().
|
170
|
*/
|
171
|
function media_wysiwyg_entity_dependencies($entity, $entity_type) {
|
172
|
// Go through all the entity's text fields and add a dependency on any files
|
173
|
// that are referenced there.
|
174
|
$dependencies = array();
|
175
|
foreach (media_wysiwyg_filter_parse_from_fields($entity_type, $entity) as $file_reference) {
|
176
|
$dependencies[] = array('type' => 'file', 'id' => $file_reference['fid']);
|
177
|
}
|
178
|
return $dependencies;
|
179
|
}
|