1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * Provides the ability to export to specific |
---|
6 | */ |
---|
7 | |
---|
8 | define('VIEWS_DATA_EXPORT_HEADER', 'header'); |
---|
9 | define('VIEWS_DATA_EXPORT_BODY', 'body'); |
---|
10 | define('VIEWS_DATA_EXPORT_FOOTER', 'footer'); |
---|
11 | define('VIEWS_DATA_EXPORT_FINISHED', 'finished'); |
---|
12 | |
---|
13 | define('VIEWS_DATA_EXPORT_INDEX_TABLE_PREFIX', 'views_data_export_index_'); |
---|
14 | |
---|
15 | /** |
---|
16 | * Implements hook_init(). |
---|
17 | */ |
---|
18 | function views_data_export_init() { |
---|
19 | // We have to include our theme preprocessors here until: |
---|
20 | // http://drupal.org/node/1096770 is fixed. |
---|
21 | module_load_include('inc', 'views_data_export', 'theme/views_data_export.theme'); |
---|
22 | } |
---|
23 | |
---|
24 | /** |
---|
25 | * Implementation of hook_views_api(). |
---|
26 | */ |
---|
27 | function views_data_export_views_api() { |
---|
28 | return array( |
---|
29 | 'api' => 2, |
---|
30 | ); |
---|
31 | } |
---|
32 | |
---|
33 | /** |
---|
34 | * Implementation of hook_theme(). |
---|
35 | */ |
---|
36 | function views_data_export_theme() { |
---|
37 | // Make sure that views picks up the preprocess functions. |
---|
38 | module_load_include('inc', 'views_data_export', 'theme/views_data_export.theme'); |
---|
39 | $hooks = array(); |
---|
40 | $hooks['views_data_export_feed_icon'] = array( |
---|
41 | 'pattern' => 'views_data_export_feed_icon__', |
---|
42 | 'arguments' => array( |
---|
43 | 'image_path' => NULL, |
---|
44 | 'url' => NULL, |
---|
45 | 'query' => '', |
---|
46 | 'text' => '', |
---|
47 | ), |
---|
48 | 'file' => 'theme/views_data_export.theme.inc', |
---|
49 | ); |
---|
50 | |
---|
51 | $hooks['views_data_export_complete_page'] = array ( |
---|
52 | 'arguments' => array( |
---|
53 | 'file' => '', |
---|
54 | 'errors' => array(), |
---|
55 | 'return_url'=> '', |
---|
56 | ), |
---|
57 | 'file' => 'theme/views_data_export.theme.inc', |
---|
58 | ); |
---|
59 | |
---|
60 | $hooks['views_data_export_message'] = array ( |
---|
61 | 'arguments' => array( |
---|
62 | 'message' => '', |
---|
63 | 'type' => 'info', |
---|
64 | ), |
---|
65 | 'file' => 'theme/views_data_export.theme.inc', |
---|
66 | ); |
---|
67 | |
---|
68 | return $hooks; |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | /** |
---|
73 | * Implementation of hook_cron(). |
---|
74 | */ |
---|
75 | function views_data_export_cron() { |
---|
76 | views_data_export_garbage_collect(); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Removes any temporary index tables that have been left |
---|
81 | * behind. This is caused by batch processes which are |
---|
82 | * started but never finished. |
---|
83 | * |
---|
84 | * Removes all trace of exports from the database that |
---|
85 | * were created more than $expires seconds ago |
---|
86 | * |
---|
87 | * @param $expires |
---|
88 | * Seconds ago. Defaults to that given in the settings. |
---|
89 | * @param $chunk |
---|
90 | * The number of tables to test for and delete. |
---|
91 | * Defaults to that given in the settings. Pass -1 |
---|
92 | * for this setting to remove any restriction and to |
---|
93 | * garbage collect all exports. |
---|
94 | */ |
---|
95 | function views_data_export_garbage_collect($expires = NULL, $chunk = NULL) { |
---|
96 | if (lock_acquire('views_data_export_gc')) { |
---|
97 | if (!isset($expires)) { |
---|
98 | $expires = variable_get('views_data_export_gc_expires', 604800); // one week |
---|
99 | } |
---|
100 | if (!isset($chunk)) { |
---|
101 | $chunk = variable_get('views_data_export_gc_chunk', 30); |
---|
102 | } |
---|
103 | |
---|
104 | if ($chunk == -1) { |
---|
105 | $qry = db_query("SELECT eid FROM {views_data_export} WHERE time_stamp <= %d ORDER BY time_stamp ASC", time() - $expires); |
---|
106 | } |
---|
107 | else { |
---|
108 | $qry = db_query_range("SELECT eid FROM {views_data_export} WHERE time_stamp <= %d ORDER BY time_stamp ASC", time() - $expires, 0, $chunk); |
---|
109 | } |
---|
110 | |
---|
111 | $eids_to_clear = array(); |
---|
112 | while ($row = db_fetch_array($qry)) { |
---|
113 | $eids_to_clear[] = $row['eid']; |
---|
114 | } |
---|
115 | |
---|
116 | // We do two things to exports we want to garbage collect |
---|
117 | // 1. Delete the index table for it, if it is still around |
---|
118 | // 2. Delete the row from the exports table |
---|
119 | // 3. Delete the view from the object_cache |
---|
120 | if (count($eids_to_clear)) { |
---|
121 | $ret = array(); |
---|
122 | foreach ($eids_to_clear as $eid) { |
---|
123 | // 1. Delete index table, if it is still around for some reason |
---|
124 | $table = VIEWS_DATA_EXPORT_INDEX_TABLE_PREFIX . $eid; |
---|
125 | if (db_table_exists($table)) { |
---|
126 | db_drop_table($ret, $table); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | // 2. Delete the entries in the exports table. |
---|
131 | db_query("DELETE FROM {views_data_export} WHERE eid IN (" . db_placeholders($eids_to_clear) . ")", $eids_to_clear); |
---|
132 | |
---|
133 | // 3. Clear the cached views |
---|
134 | views_data_export_view_clear($eids_to_clear); |
---|
135 | |
---|
136 | } |
---|
137 | lock_release('views_data_export_gc'); |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | /** |
---|
143 | * Batch API callback. |
---|
144 | * Handles all batching operations by executing the appropriate view. |
---|
145 | */ |
---|
146 | function _views_data_export_batch_process($export_id, $display_id, $exposed_input, &$context) { |
---|
147 | // Don't show the admin menu on batch page, some people don't like it. |
---|
148 | if (module_exists('admin_menu')) { |
---|
149 | module_invoke('admin_menu', 'suppress'); |
---|
150 | } |
---|
151 | |
---|
152 | // Fetch the view in question from our cache |
---|
153 | $view = views_data_export_view_retrieve($export_id); |
---|
154 | $view->set_display($display_id); |
---|
155 | if (!empty($exposed_input)) { |
---|
156 | $view->set_exposed_input($exposed_input); |
---|
157 | } |
---|
158 | // Inform the data_export display which export it corresponds to and execute |
---|
159 | $view->display_handler->batched_execution_state->eid = $export_id; |
---|
160 | $view->execute_display($display_id); |
---|
161 | |
---|
162 | // Update batch api progress information |
---|
163 | $sandbox = $view->display_handler->batched_execution_state->sandbox; |
---|
164 | $context['finished'] = $sandbox['finished']; |
---|
165 | $context['message'] = $sandbox['message']; |
---|
166 | |
---|
167 | views_data_export_view_store($export_id, $view); |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | |
---|
172 | /**********/ |
---|
173 | /** CRUD **/ |
---|
174 | /**********/ |
---|
175 | |
---|
176 | /** |
---|
177 | * Save a new export into the database. |
---|
178 | */ |
---|
179 | function views_data_export_new($view_name, $view_display_id, $file) { |
---|
180 | // Insert new row into exports table |
---|
181 | $record = (object) array( |
---|
182 | 'view_name' => $view_name, |
---|
183 | 'view_display_id' => $view_display_id, |
---|
184 | 'time_stamp' => time(), |
---|
185 | 'fid' => $file, |
---|
186 | 'batch_state' => VIEWS_DATA_EXPORT_HEADER, |
---|
187 | 'sandbox' => array(), |
---|
188 | ); |
---|
189 | drupal_write_record('views_data_export', $record); |
---|
190 | return $record; |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | /** |
---|
195 | * Update an export row in the database |
---|
196 | */ |
---|
197 | function views_data_export_update($state) { |
---|
198 | // Note, drupal_write_record handles serializing |
---|
199 | // the sandbox field as per our schema definition |
---|
200 | drupal_write_record('views_data_export', $state, 'eid'); |
---|
201 | } |
---|
202 | |
---|
203 | |
---|
204 | |
---|
205 | /** |
---|
206 | * Get the information about a previous export. |
---|
207 | */ |
---|
208 | function views_data_export_get($export_id) { |
---|
209 | $qry = db_query("SELECT * FROM {views_data_export} WHERE eid = %d", (int)$export_id); |
---|
210 | $object = db_fetch_object($qry); |
---|
211 | if ($object) { |
---|
212 | $object->sandbox = unserialize($object->sandbox); |
---|
213 | } |
---|
214 | return $object; |
---|
215 | } |
---|
216 | |
---|
217 | /** |
---|
218 | * Remove the information about an export. |
---|
219 | */ |
---|
220 | function views_data_export_clear($export_id) { |
---|
221 | db_query("DELETE FROM {views_data_export} WHERE eid = %d", $export_id); |
---|
222 | views_data_export_view_clear($export_id); |
---|
223 | } |
---|
224 | |
---|
225 | |
---|
226 | /** |
---|
227 | * Store a view in the object cache. |
---|
228 | */ |
---|
229 | function views_data_export_view_store($export_id, $view) { |
---|
230 | // Store a clean copy of the view. |
---|
231 | $_view = $view->clone_view(); |
---|
232 | |
---|
233 | views_data_export_view_clear($export_id); |
---|
234 | $record = array( |
---|
235 | 'eid' => $export_id, |
---|
236 | 'data' => $_view, |
---|
237 | 'updated' => time(), |
---|
238 | ); |
---|
239 | drupal_write_record('views_data_export_object_cache', $record); |
---|
240 | } |
---|
241 | |
---|
242 | /** |
---|
243 | * Retrieve a view from the object cache. |
---|
244 | */ |
---|
245 | function views_data_export_view_retrieve($export_id) { |
---|
246 | views_include('view'); |
---|
247 | $data = db_fetch_object(db_query("SELECT * FROM {views_data_export_object_cache} WHERE eid = '%s'", $export_id)); |
---|
248 | if ($data) { |
---|
249 | $view = unserialize($data->data); |
---|
250 | } |
---|
251 | return $view; |
---|
252 | } |
---|
253 | |
---|
254 | /** |
---|
255 | * Clear a view from the object cache. |
---|
256 | * |
---|
257 | * @param $export_id |
---|
258 | * An export ID or an array of export IDs to clear from the object cache. |
---|
259 | */ |
---|
260 | function views_data_export_view_clear($export_id) { |
---|
261 | if (is_array($export_id)) { |
---|
262 | db_query("DELETE FROM {views_data_export_object_cache} WHERE eid IN (" . db_placeholders($export_id) . ")", $export_id); |
---|
263 | } |
---|
264 | else { |
---|
265 | db_query("DELETE FROM {views_data_export_object_cache} WHERE eid = '%s'", $export_id); |
---|
266 | } |
---|
267 | } |
---|