-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.driver.php
More file actions
executable file
·146 lines (113 loc) · 3.71 KB
/
extension.driver.php
File metadata and controls
executable file
·146 lines (113 loc) · 3.71 KB
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
<?php
class extension_datasource_cache extends Extension
{
// Cache file cachine
private $_cachefiles = null;
public function about() {
return array(
'name' => 'Data Source Cache',
'version' => '0.1',
'release-date' => '2009-02-23',
'author' => array(
'name' => 'Nick Dunn',
'website' => 'http://airlock.com',
'email' => 'nick.dunn@airlock.com'
),
'description' => 'Force refresh of data source caches'
);
}
public function getSubscribedDelegates(){
return array(
array(
'page' => '/publish/new/',
'delegate' => 'EntryPostCreate',
'callback' => '__refresh'
),
array(
'page' => '/publish/edit/',
'delegate' => 'EntryPostEdit',
'callback' => '__refresh'
),
);
}
public function __refresh($context){
/*
Cear the DS cache when its associated Section is edited
*/
$sm = new SectionManager($this->_Parent);
$section_handle = Administration::instance()->Page->_context['section_handle'];
$section_id = $sm->fetchIDFromHandle($section_handle);
// find all native data sources (not added via Extensions)
$dsm = new DatasourceManager($this->_Parent);
$datasources = $dsm->listAll();
if(is_array($datasources) && !empty($datasources)){
foreach($datasources as $ds){
// check they are "Section" DSs and not Dynamic/Static XML, Authors or Navigation
if (is_numeric($ds['type']) && $ds['type'] == $section_id){
// instantiate the DS class and see if it has caching enabled
$datasource = $dsm->create($ds['handle']);
if ($datasource->dsParamCACHE){
$this->clearCache($ds['handle']);
}
}
}
}
die;
}
// This seems retarded, but it's effiecient
private function preliminaryFilenameCheck( $filename )
{
// Stop at 't' because it's not a valid hash character
return ($filename{0} == 'd' && $filename{0} == 'a' && $filename{0} == 't');
}
// Build a list of all DS-cache files
public function buildCacheFileList()
{
if ($this->_cachefiles != null) return $this->_cachefiles;
$this->_cachefiles = array();
if (!$oDirHandle = opendir(CACHE))
trigger_error("Panic! DS cache doesn't exists");
// Initialise the array outside the loop for speed
$matches = array();
while (($file = readdir($oDirHandle)) !== false)
{
// Check some initial characters
if ($this->preliminaryFilenameCheck($file)) continue;
// Drop it if it's not a match
if (!preg_match('/^datasource(?P<name>[a-z_]+)-(?P<hash>[^\.]+).+/', $file, $matches)) continue;
// Inset into the array
if (!isset($this->_cachefiles[$matches['name']]))
$this->_cachefiles[$matches['name']] = array("count" => 1, "size" => filesize(CACHE . '/' . $file), "files" => array(CACHE . '/' . $file));
else
{
$this->_cachefiles[$matches['name']]['count']++;
$this->_cachefiles[$matches['name']]['size'] += filesize(CACHE . '/' .$file);
array_push($this->_cachefiles[$matches['name']]['files'], CACHE . '/' .$file);
}
}
closedir($oDirHandle);
return $this->_cachefiles;
}
public function clearCache( $handles )
{
$files = $this->buildCacheFileList();
foreach ($handles as $handle)
{
if (array_key_exists($handle, $files))
{
foreach($files[$handle]['files'] as $file)
unlink($file);
}
}
}
public function fetchNavigation(){
return array(
array(
'location' => 300,
'name' => 'Data Source Cache',
'link' => '/view/'
)
);
}
}
?>