-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathextension.driver.php
491 lines (408 loc) · 17.7 KB
/
extension.driver.php
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<?php
class Extension_CacheableDatasource extends Extension
{
private $_sectionsToFlush = array();
public function install()
{
if (!General::realiseDirectory(
CACHE . '/cacheabledatasource',
Symphony::Configuration()->get('write_mode', 'directory')
)) {
throw new Exception(__(
'Cacheable Datasource was not installed: cache directory could not be created at %s.',
array('<code>/manifest/cache/cacheabledatasource</code>')
));
}
return true;
}
public function uninstall()
{
if (is_dir(CACHE . '/cacheabledatasource')) {
return rmdir(CACHE . '/cacheabledatasource');
}
return true;
}
public function getSubscribedDelegates()
{
return array(
array(
'page' => '/frontend/',
'delegate' => 'DataSourcePreExecute',
'callback' => 'dataSourcePreExecute'
),
array(
'page' => '/blueprints/datasources/',
'delegate' => 'DatasourcePreCreate',
'callback' => 'dataSourceSave'
),
array(
'page' => '/blueprints/datasources/',
'delegate' => 'DatasourcePreEdit',
'callback' => 'dataSourceSave'
),
array(
'page' => '/backend/',
'delegate' => 'InitaliseAdminPageHead',
'callback' => 'initaliseAdminPageHead'
),
array(
'page' => '/publish/new/',
'delegate' => 'EntryPostCreate',
'callback' => 'flushCache'
),
array(
'page' => '/publish/edit/',
'delegate' => 'EntryPostEdit',
'callback' => 'flushCache'
),
array(
'page' => '/publish/',
'delegate' => 'EntryPreDelete',
'callback' => 'flushCache'
),
array(
'page' => '/publish/',
'delegate' => 'EntriesPostOrder',
'callback' => 'flushCache'
),
array(
'page' => '/frontend/',
'delegate' => 'EventFinalSaveFilter',
'callback' => 'eventFinalSaveFilter'
),
array(
'page' => '/blueprints/events/new/',
'delegate' => 'AppendEventFilter',
'callback' => 'appendEventFilter'
),
array(
'page' => '/blueprints/events/edit/',
'delegate' => 'AppendEventFilter',
'callback' => 'appendEventFilter'
),
);
}
public function flushCache($context)
{
$this->__fetchSectionsFromContext($context);
$cacheDir = CACHE . '/cacheabledatasource/';
try {
foreach (DatasourceManager::listAll() as $ds) {
if (!in_array($ds['source'], $this->_sectionsToFlush)) {
continue;
}
$cache = glob($cacheDir.$ds['handle'].'_*.xml');
if (empty($cache)) {
continue;
}
foreach ($cache as $file) {
unlink($file);
}
}
} catch (Exception $e) {
Symphony::Log()->writeToLog(date('d.m.y H:i:s') . ' > CacheableDatasource: '. $e->getMessage(), true);
}
}
private function __fetchSectionsFromContext($context)
{
// Determine what the affected section is from the entry_id
if (in_array($context['delegate'], array('EntryPreDelete', 'EntriesPostOrder'))) {
$affectedSection = EntryManager::fetchEntrySectionID($context['entry_id'][0]);
// Or from the entry object
} elseif ($context['delegate'] === 'EventFinalSaveFilter') {
$affectedSection = EntryManager::fetchEntrySectionID($context['entry']->get('id'));
// Or finally, default from the section id
} else {
$affectedSection = $context['section']->get('id');
}
// Find any associated sections for the affected section
$associatedSections = Symphony::Database()->fetch(sprintf('
SELECT DISTINCT `child_section_id`, `parent_section_id`
FROM `tbl_sections_association`
WHERE `parent_section_id` = %1$d OR `child_section_id` = %1$d',
$affectedSection
));
General::flattenArray($associatedSections);
$associatedSections = array_values($associatedSections);
$sectionsToFlush = array_unique(array_merge(
array($affectedSection),
$associatedSections
));
if (!empty($sectionsToFlush)) {
$this->_sectionsToFlush = array_merge($this->_sectionsToFlush, $sectionsToFlush);
}
}
public function eventFinalSaveFilter(array $context)
{
if (!in_array('cacheable-datasource', $context['event']->eParamFILTERS)) {
return;
}
$this->flushCache($context);
}
public function appendEventFilter(array $context)
{
$context['options'][] = array(
'cacheable-datasource',
is_array($context['selected']) ? in_array('cacheable-datasource', $context['selected']) : false,
'Flush DS Cache'
);
}
protected function dataSourceClassCanBeCached($ds)
{
// don't double cache any Dynamic XML or Remote datasources
return !is_subclass_of($ds, 'DynamicXMLDatasource') && !is_subclass_of($ds, 'RemoteDatasource');
}
protected function dataSourceContentCanBeCached($ds)
{
// don't double cache any Dynamic XML or Remote datasources
return strpos($ds, 'DynamicXMLDatasource') === false && strpos($ds, 'RemoteDatasource') === false;
}
/**
* `DatasourcePreCreate` delegate callback function
* Checks whether a data source should be cached or not: builds a filename based on
* the hashed object parameters. If a cache file exists and is not stale, the XML is
* read from the file and returned. If the cache file does not exist, or exists but is
* stale, the data source grab() is executed and the XML cached.
*
* @param mixed $context
* Delegate context including the data source object, output XML and param pool array
*/
public function dataSourcePreExecute($context)
{
if (!isset($context['datasource'])) {
return;
}
$ds = $context['datasource'];
$param_pool = $context['param_pool'];
// don't cache if no cache TTL is set at all
if (!isset($ds->dsParamCACHE)) {
return;
}
// don't cache when the TTL is zero
if ((int)$ds->dsParamCACHE === 0) {
return;
}
// don't cache if not needed
if (!$this->dataSourceClassCanBeCached($ds)) {
return;
}
$filename = null;
$file_age = 0;
if ($this->__buildCacheFilename($ds, $filename, $file_age)) {
// HACK: peek at the first line of XML to see if it's a serialised array
// which contains cached output parameters
$xml = file_get_contents($filename);
// split XML into an array of each line
$xml_lines = explode("\n", $xml);
// output params are a serialised array on line 1
$output_params = @unserialize(trim($xml_lines[0]));
// there are cached output parameters
if (is_array($output_params)) {
// remove line 1 and join XML into a string again
unset($xml_lines[0]);
$xml = implode("\n", $xml_lines);
// add cached output params back into the pool
foreach ($output_params as $key => $value) {
$param_pool[$key] = $value;
}
}
// set cache age in the XML result
$xml = preg_replace('/(cache-age=[\'\"])fresh([\'\"])/', '${1}'.$file_age.'s$2', $xml);
} else {
// Backup the param pool
$old_param_pool = $param_pool;
// Fetch the contents
$xml = $this->__executeDatasource($ds, $param_pool);
// See what has been added to the param pool
$new_params = array_diff_key($param_pool, $old_param_pool);
// Create output params string (null or one line)
$output_params = null;
if (!empty($new_params)) {
$output_params = sprintf("%s\n", serialize($new_params));
}
// Add an attribute to preg_replace later
$xml->setAttribute("cache-age", "fresh");
// Add an attribute cache-expiration
$xml->setAttribute("cache-expiration", $ds->dsParamCACHE);
// Write the cached XML to disk
file_put_contents($filename, $output_params . $xml->generate(true, 1));
}
$context['xml'] = $xml;
$context['param_pool'] = $param_pool;
}
/**
* Serialises the data source object properties into a checksum hash to see
* whether the data source is currently cached, and whether it has expired.
* Returns boolean of whether the data source is stale or not.
*
* @param Datasource $datasource
* The current data source object
* @param string $filename
* Cache filename, passed by reference
* @param int $file_age
* Cache file age (in seconds), passed by reference
*/
private function __buildCacheFilename($datasource, &$filename, &$file_age)
{
$filename = null;
// Checks if cacheabledatasource directory exists. If not, try to restore.
if (!file_exists(CACHE . '/cacheabledatasource')) {
if (!General::realiseDirectory(CACHE . '/cacheabledatasource', Symphony::Configuration()->get('write_mode', 'directory'))) {
throw new Exception(__('Cacheable Datasource: Cache directory could not be restored at %s.', array('<code>/manifest/cache/cacheabledatasource</code>')));
}
}
// get resolved values of each public property of this DS
// (sort, filters, included elements etc.)
foreach (get_class_vars(get_class($datasource)) as $key => $value) {
if (substr($key, 0, 2) == 'ds') {
$value = $datasource->{$key};
$filename .= $key . (is_array($value) ? http_build_query($value) : $value);
}
}
// Check for multilingual settings
if (class_exists('FLang') && is_callable(array('FLang', 'getLangCode'))) {
$filename .= FLang::getLangCode();
}
$filename = sprintf(
"%s/cacheabledatasource/%s_%s.xml",
CACHE,
preg_replace("/^datasource/", '', get_class($datasource)),
md5($filename)
);
if (!file_exists($filename)) {
return false;
}
// Check if cache is infinite
if ($datasource->dsParamCACHE === -1) {
return true;
}
$file_age = (int)(floor(time() - filemtime($filename)));
return ($file_age < ($datasource->dsParamCACHE));
}
/**
* Executes a data source. Invalid XML is escaped (CDATA) but still
* cached. Prevents persistent cached XML from breaking pages.
*
* @param Datasource $datasource
* The current data source object
* @param int $file_age
* Cache file age (in seconds), passed by reference
*/
private function __executeDatasource($datasource, &$param_pool=array())
{
$result = $datasource->execute($param_pool);
$xml = is_object($result) ? $result->generate(true, 1) : $result;
// Parse DS XML to check for errors. If contains malformed XML such as
// an unescaped database error, the error is escaped in CDATA
$doc = new DOMDocument('1.0', 'utf-8');
libxml_use_internal_errors(true);
$doc->loadXML($xml);
$errors = libxml_get_errors();
libxml_clear_errors();
libxml_use_internal_errors(false);
// No error, just return the result
if (empty($errors)) {
return $result;
}
// There's an error, so $doc will be empty
// Use regex to get the root node
// If something's wrong, just push back the broken XML
if (!preg_match('/<([^ \/>]+)/', $xml, $matches)) {
return $result;
}
$ret = new XMLElement($matches[1]);
// Set the invalid flag
$ret->setAttribute("xml-invalid", "true");
$errornode = new XMLElement("errors");
// Store the errors
foreach ($errors as $error) {
$item = new XMLElement("error", trim($error->message));
$item->setAttribute('line', $error->line);
$item->setAttribute('column', $error->column);
$errornode->appendChild($item);
}
$ret->appendChild($errornode);
// Return the XML
$ret->appendChild(new XMLElement('broken-xml', "<![CDATA[" . $xml . "]]>"));
return $ret;
}
/**
* `DatasourcePreCreate` and `DatasourcePreEdit` delegates callback function
* Adds the dsParamCACHE property to the data source class file.
*
* @param mixed $context
* Delegate context including string contents of the data souce PHP file
*/
public function dataSourceSave($context)
{
if (!isset($context['contents'])) {
return;
}
$contents = $context['contents'];
if (empty($_POST['fields']['cache'])) {
return;
}
$cache = $_POST['fields']['cache'];
// don't edit if not needed
if (!$this->dataSourceContentCanBeCached($contents)) {
return;
}
$cache = General::intval($cache);
// Check if the user submitted -1 literally
if ($cache === -1 && (int)$_POST['fields']['cache'] === -1) {
$cache = -1;
// Failed to parse to a int, disable cache
} elseif ($cache < 0) {
$cache = 0;
}
$contents = preg_replace(
"/<!-- VAR LIST -->/",
"public \$dsParamCACHE = $cache;\n<!-- VAR LIST -->",
$contents
);
$context['contents'] = $contents;
}
/**
* `InitaliseAdminPageHead` delegate callback function
* Appends script assets and context to page head
*
* @param mixed $context
* Delegate context including page object
*/
public function initaliseAdminPageHead($context)
{
$page = Administration::instance()->Page;
if (!$page instanceof contentBlueprintsDatasources) {
return;
}
$url_context = $page->getContext();
if (!in_array($url_context[0], array('new', 'edit'))) {
return;
}
$cache = 0;
// if editing an existing data source, instantiate the DS object
// to retrieve the dsParamCACHE property if it exists
if ($url_context[0] == 'edit') {
$ds = $url_context[1];
$dsm = new DatasourceManager(Symphony::Engine());
$datasource = $dsm->create($ds, null, false);
$cache = $datasource->dsParamCACHE;
}
if (is_null($cache)) {
$cache = 0;
}
Administration::instance()->Page->addElementToHead(
new XMLElement(
'script',
"Symphony.Context.add('cacheabledatasource', " . json_encode(array(
'cache' => (isset($_POST['fields']['cache']) ? $_POST['fields']['cache'] : $cache)
)) . ");",
array('type' => 'text/javascript')
), time()
);
Administration::instance()->Page->addScriptToHead(
URL . '/extensions/cacheabledatasource/assets/cacheabledatasource.blueprintsdatasources.js',
time()
);
}
}