-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathopen_data_schema_map.file_cache.inc
178 lines (155 loc) · 5.72 KB
/
open_data_schema_map.file_cache.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
<?php
/**
* @file
* Performance for open_data_schema_map.
*/
$module_path = drupal_get_path('module', 'open_data_schema_map');
require_once $module_path . '/open_data_schema_map.output.inc';
/**
* Helper to build odsm file cache file names.
*/
function open_data_schema_map_file_cache_name_helper($api, $arguments = array()) {
$name = 'public://odsm_cache_' . $api->machine_name;
foreach ($arguments as $argument) {
$name .= '_' . $argument;
}
return $name;
}
/**
* Generate a link to an api endpoint file cache admin page.
*/
function open_data_schema_map_file_cache_admin_link($api) {
$label = open_data_schema_map_file_cache_admin_link_label($api);
return l($label, OPEN_DATA_SCHEMA_MAP_ADMIN_PATH . '/cache/' . $api->machine_name);
}
/**
* Get cache age in hours or false if none exists.
*/
function open_data_schema_map_file_cache_age($api) {
if (open_data_schema_map_file_cache_exits($api)) {
$filename = open_data_schema_map_file_cache_name_helper($api);
$stat = stat($filename);
$seconds_in_hour = 3600;
$time = time();
$elapsed_time = $time > $stat['ctime'] ? $time - $stat['ctime'] : 0;
return floor($elapsed_time / $seconds_in_hour);
}
return FALSE;
}
/**
* Generate a label for api endpoint file cache admin page link.
*/
function open_data_schema_map_file_cache_admin_link_label($api) {
$label = 'none';
$age = open_data_schema_map_file_cache_age($api);
if (is_numeric($age)) {
$label = "$age " . t('hrs');
}
return $label;
}
/**
* Generate a status for api endpoint file cache admin page.
*/
function open_data_schema_map_file_cache_status($api) {
$status = t('Not cached');
$age = open_data_schema_map_file_cache_age($api);
if (is_numeric($age)) {
$status = "$age " . t('hours ago');
}
return $status;
}
/**
* Check existance of a file_cache for an api endpoint.
*/
function open_data_schema_map_file_cache_exits($api, $arguments = array()) {
return file_exists(open_data_schema_map_file_cache_name_helper($api, $arguments));
}
/**
* File caches a specific odsm endpoint.
*/
function open_data_schema_map_file_cache_endpoint($machine_name = FALSE) {
if ($machine_name) {
$api = open_data_schema_map_api_load($machine_name);
if (isset($api)) {
watchdog('open_data_schema_map', 'api present and loaded', [], WATCHDOG_INFO);
$filename = open_data_schema_map_file_cache_name_helper($api);
// This will only work for data.json v1.1 right now.
watchdog('open_data_schema_map', 'Beginning processing for endpoint (this could take a while)', [], WATCHDOG_INFO);
$render = open_data_schema_map_render_api($api);
$result = $render['result'];
// Load the correct output format, render results, and write to a file.
if ($output_format = open_data_schema_map_output_format_load($api->outputformat)) {
$response = $output_format['callback']($api, $result);
watchdog('open_data_schema_map', "Saving $filename", [], WATCHDOG_INFO);
file_save_data($response, $filename, FILE_EXISTS_REPLACE);
watchdog('open_data_schema_map', "$filename saved", [], WATCHDOG_INFO);
}
}
else {
drush_print(dt('@value is not a valid argument', array('@value' => $machine_name)));
}
}
else {
drush_print(t('odsm-filecache requires an argument. (e.g. data_json_1_1)'));
}
}
/**
* API wrapper for open_data_schema_map_file_cache_endpoint.
*/
function open_data_schema_map_file_cache_create($api) {
open_data_schema_map_file_cache_endpoint($api->machine_name);
}
/**
* Delete a specific api endpoint file cache.
*/
function open_data_schema_map_file_cache_delete($api) {
$filename = open_data_schema_map_file_cache_name_helper($api);
if (file_exists($filename)) {
file_unmanaged_delete($filename);
}
}
/**
* Callback for the api endpoint file cache admin page.
*/
function open_data_schema_map_cache_manage(array $form, array &$form_state, $api = NULL) {
$name = $api->name;
$title = "$name Cache";
drupal_set_title($title, FALSE);
$form['help'] = array(
'#type' => 'item',
'#markup' => "The Open Data Schema Map filecache writes an open data endpoint such as data.json to disk, so that it does not need to be generated upon every request. See the <a href='http://docs.getdkan.com/en/latest/components/open-data-schema.html#the-odsm-file-cache'>documentation</a> for instructions on automating this process. Use the controls on this page to manually generate or remove this API's filecache.",
);
$status_label = t("Filecache status");
$status = open_data_schema_map_file_cache_status($api);
$form['status'] = array(
'#type' => 'item',
'#markup' => "<strong>$status_label:</strong> $status",
);
if (open_data_schema_map_file_cache_exits($api)) {
$form['rebuild'] = array('#type' => 'submit', '#value' => t('Rebuild'));
$form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
}
else {
$form['create'] = array('#type' => 'submit', '#value' => t('Create'));
}
$form['#api'] = $api;
return $form;
}
/**
* Submit function for managing the api endpoint file caches.
*/
function open_data_schema_map_cache_manage_submit(array $form, array &$form_state) {
$api = $form['#api'];
$op = $form_state['input']['op'];
if ($op == t('Create') || $op == t('Rebuild')) {
open_data_schema_map_file_cache_create($api);
drupal_set_message(t('A cache file for the %name api endpoint has been created.', array('%name' => $api->name)));
}
elseif ($op == t('Delete')) {
drupal_set_message(t('A cache file for the %name api endpoint has been deleted.', array('%name' => $api->name)));
open_data_schema_map_file_cache_delete($api);
}
// Rebuild menu so the api is not available anymore.
menu_rebuild();
drupal_goto('admin/config/services/odsm');
}