This repository was archived by the owner on May 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinstall.php
209 lines (190 loc) · 7.01 KB
/
install.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
<?php
/**
* @file
* Implements CLI Command for installing addon data.
*/
if (!COCKPIT_CLI) {
return;
}
$name = $app->param('name', FALSE);
$force = $app->param('force', FALSE);
$nodata = $app->param('nodata', FALSE);
CLI::writeln('');
CLI::writeln('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
CLI::writeln(' Cockpit CMS Addon installer');
CLI::writeln('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
CLI::writeln('');
if (!$name) {
return CLI::writeln('--name parameter is missing', FALSE);
}
// Check that addon exists.
if (!$module = $app->module(strtolower($name))) {
return CLI::writeln("Invalid addon name: {$name}", FALSE);
}
// Check that info file exists.
if (!file_exists($module->_dir . '/info.yaml')) {
return CLI::writeln("Cannot find addon info file at {$module->_dir} /info.yaml", FALSE);
}
// Load info file.
if (!$info = Spyc::YAMLLoad($module->_dir . '/info.yaml')) {
return CLI::writeln("Error loading info file at {$module->_dir}/info.yaml", FALSE);
}
if (empty($info['install'])) {
return CLI::writeln("No installation steps found at {$module->_dir}/info.yaml", FALSE);
}
if ($nodata) {
CLI::writeln('nodata flag is active, processing only data structures, no contents will be imported');
}
$install = $info['install'];
// Handle Singletons installation.
if (!empty($install['singletons'])) {
CLI::writeln('Processing singletons...');
foreach ($install['singletons'] as $singleton) {
if (empty($singleton['name']) || (empty($singleton['source']) && empty($singleton['data']))) {
CLI::writeln("Invalid format for singletons in addon info file.", FALSE);
continue;
}
$name = $singleton['name'];
$exists = FALSE;
if (!empty($singleton['source'])) {
$source = $module->_dir . '/' . $singleton['source'];
CLI::writeln(" Installing singleton {$name} from {$source}");
if (!file_exists($source)) {
CLI::writeln("Singleton '{$name}' source file {$source} missing.", FALSE);
continue;
}
$target = $app->path('#storage:singleton/') . "{$name}.singleton.php";
if (file_exists($target) && !$force) {
CLI::writeln("Singleton '{$name}' already exists.", FALSE);
$exists = TRUE;
}
else {
$app->helper('fs')->copy($source, $target);
CLI::writeln("* Singleton '{$name}' created.", TRUE);
}
}
if (!empty($singleton['data']) && !$nodata) {
// Import data.
$source = $module->_dir . '/' . $singleton['data'];
if (!$data = $app->helper('fs')->read($source)) {
CLI::writeln("Cannot read json data from {$source}", FALSE);
continue;
}
$data = json_decode($data, TRUE);
if (!$exists || $force || empty($singleton['source'])) {
$res = $app->module('singletons')->saveData($name, $data, ['revision' => TRUE]);
if ($res) {
CLI::writeln("* Singleton '{$name}' data imported.", TRUE);
}
else {
CLI::writeln("Singleton '{$name}' data install failed.", FALSE);
}
}
}
}
}
// Handle Collections installation.
if (!empty($install['collections'])) {
CLI::writeln('Processing collections...');
foreach ($install['collections'] as $idx => $collection) {
if (empty($collection['name']) || empty($collection['source'])) {
CLI::writeln("Invalid format for collections in addon info file.", FALSE);
continue;
}
$name = $collection['name'];
$source = $module->_dir . '/' . $collection['source'];
CLI::writeln(" Installing collection {$name} from {$source}");
$target = $app->path('#storage:collections/') . "{$name}.collection.php";
$exists = FALSE;
if (file_exists($target) && !$force) {
$exists = TRUE;
CLI::writeln("Collection '{$name}' already exists.", FALSE);
}
else {
$app->helper('fs')->copy($source, $target);
CLI::writeln("* Collection '{$name}' created.", TRUE);
if (!empty($install['collections'][$idx]['rules'])) {
CLI::writeln(" Installing collection {$name} rules");
// Import collection rules if any.
foreach ($install['collections'][$idx]['rules'] as $rule) {
$type = key($rule);
$source = $module->_dir . '/' . reset($rule);
if (!file_exists($source)) {
continue;
}
$target = $app->path('#storage:collections/rules/') . "{$name}.{$type}.php";
if (file_exists($target) && !$force) {
CLI::writeln("Collection '{$name}' rule '{$type}' already exists.", FALSE);
}
else {
if ($app->helper('fs')->copy($source, $target)) {
CLI::writeln("* Collection '{$name}' rule '{$type}' created.", TRUE);
}
}
}
}
if ((!$exists || $force) && !empty($install['collections'][$idx]['data']) && !$nodata) {
$source = $module->_dir . '/' . $install['collections'][$idx]['data'];
if (!file_exists($source)) {
CLI::writeln("Collection data source {$source} doesn't exists!", FALSE);
continue;
}
if (!$_collection = $app->module('collections')->collection($name)) {
CLI::writeln("Collection {$name} doesn't exists!", FALSE);
continue;
}
if (!$data = $app->helper('fs')->read($source)) {
CLI::writeln("Cannot read from {$source}", FALSE);
continue;
}
$entries = json_decode($data, TRUE);
CLI::writeln(" Importing collection {$name} (" . count($entries) . " entries)");
$count = 0;
$cid = $_collection['_id'];
foreach ($entries as $entry) {
$mode = "insert";
// Check if exists.
if ($app->storage->count("collections/{$cid}", ['_id' => $entry['_id']])) {
$res = $app->module('collections')->save($name, $entry, ['revision' => TRUE]);
$mode = "update";
}
else {
$res = $app->storage->insert("collections/{$cid}", $entry);
}
if ($res) {
$count++;
CLI::writeln("* Imported collection '{$name}' entry -> _id:{$entry['_id']} ({$mode})", TRUE);
}
else {
CLI::writeln("Failed importing collection entry {$entry['_id']}", FALSE);
}
}
CLI::writeln("Collection {$name} import done. Imported {$count} entries", TRUE);
}
}
}
}
// Handle Custom storage installation.
if (!empty($install['customStorage'])) {
CLI::writeln('Processing custom storage...');
foreach ($install['customStorage'] as $item) {
$source = $module->_dir . '/' . $item['source'];
if (!file_exists($source)) {
continue;
}
$target = $app->path('#storage:') . $item['target'];
CLI::writeln(" Importing file into {$target}");
if (file_exists($target) && !$force) {
CLI::writeln("File '{$target}' already exists.", FALSE);
}
else {
$folder = dirname($target);
if (!is_dir($folder)) {
$app->helper('fs')->mkdir($folder);
}
if ($app->helper('fs')->copy($source, $target)) {
CLI::writeln("* File '{$target}' created.", TRUE);
}
}
}
}