-
Notifications
You must be signed in to change notification settings - Fork 871
/
Export.php
443 lines (332 loc) · 12.9 KB
/
Export.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
<?php
namespace MicroweberPackages\Backup;
use MicroweberPackages\Backup\Exporters\JsonExport;
use MicroweberPackages\Backup\Exporters\CsvExport;
use MicroweberPackages\Backup\Exporters\XmlExport;
use MicroweberPackages\Backup\Exporters\ZipExport;
use MicroweberPackages\Backup\Loggers\BackupExportLogger;
use Microweber\App\Providers\Illuminate\Support\Facades\Cache;
use MicroweberPackages\Backup\Exporters\XlsxExport;
use MicroweberPackages\Backup\Traits\ExportGetSet;
class Export
{
use ExportGetSet;
public $exportData;
public $type = 'json';
public $exportAllData = false;
public function setType($type)
{
$this->type = $type;
}
public function setExportData($data)
{
$this->exportData = $data;
}
public function setExportAllData($exportAllData)
{
$this->exportAllData = $exportAllData;
}
public function exportAsType($data)
{
$backupManager = new BackupManager();
$exportCacheLocation = $backupManager->getBackupCacheLocation();
$exportWithZip = false;
$exportMediaUserFiles = false;
if (array_key_exists('media', $data)) {
$exportMediaUserFiles = true;
}
$export = $this->_getExporter($data);
if (isset($export['files']) && count($export['files']) > 1) {
$exportWithZip = true;
}
if ($this->exportModules) {
$exportWithZip = true;
}
if ($this->exportTemplates) {
$exportWithZip = true;
}
if ($this->exportOnlyTemplate) {
$exportWithZip = true;
unset($export['files']);
}
if ($exportWithZip || $exportMediaUserFiles) {
// Make Zip
$zipExport = new ZipExport();
// Move files to zip temp
if (isset($export['files'])) {
foreach ($export['files'] as $file) {
$newFilePath = $exportCacheLocation . $file['filename'];
rename($file['filepath'], $newFilePath);
// Add exported files
$zipExport->addFile(array('filepath' => $newFilePath, 'filename' => $file['filename']));
}
}
if ($exportMediaUserFiles) {
$zipExport->setExportMedia(true);
}
if ($this->exportMedia == false) {
$zipExport->setExportMedia(false);
}
if ($this->exportModules) {
$zipExport->setExportModules($this->exportModules);
}
if ($this->exportTemplates) {
$zipExport->setExportTemplates($this->exportTemplates);
}
if ($this->exportOnlyTemplate) {
$zipExport->setExportOnlyTemplate($this->exportOnlyTemplate);
}
$zipExportReady = $zipExport->start();
if (isset($zipExportReady['download']) && !empty($zipExportReady['download'])) {
// Delete unused ziped files
array_map('unlink', glob("$exportCacheLocation/*.*"));
rmdir($exportCacheLocation);
return array(
'success' => 'Items are exported',
'export_type' => $this->type,
'data' => $zipExportReady
);
} else {
return $zipExportReady;
}
}
if (isset($export['files'])) {
$exportSingleFile = false;
if (count($export['files']) == 1) {
$exportSingleFile = true;
}
if ($exportSingleFile && isset($export['files']) && !empty($export['files'])) {
return array(
'success' => 'Items are exported',
'export_type' => $this->type,
'data' => $export['files'][0]
);
} else {
return $export;
}
}
}
public function start()
{
$readyData = $this->_getReadyDataCached();
if (empty($readyData)) {
return array("error" => "Empty content data.");
}
return $this->exportAsType($readyData);
}
private function _getExportDataHash()
{
return md5(json_encode($this->exportData));
}
private function _getReadyDataCached()
{
return $this->_getReadyData();
/* $exportDataHash = $this->_getExportDataHash();
$zipExport = new ZipExport();
$currentStep = $zipExport->getCurrentStep();
if ($currentStep == 0) {
// This is frist step
Cache::forget($exportDataHash);
return Cache::rememberForever($exportDataHash, function () {
return $this->_getReadyData();
});
} else {
BackupExportLogger::setLogInfo('Start exporting selected data...');
// This is for the next steps from wizard
return Cache::get($exportDataHash);
} */
}
private function _getReadyData()
{
$exportTables = new ExportTables();
$tablesStructures = array();
foreach ($this->_getTablesForExport() as $table) {
BackupExportLogger::setLogInfo('Exporting table: <b>' . $table . '</b>');
$tableFields = app()->database_manager->get_fields($table);
if ($tableFields) {
$tableFieldsStructure = array();
foreach ($tableFields as $tableField) {
$tableFieldType = \DB::getSchemaBuilder()->getColumnType($table, $tableField);
$tableFieldsStructure[$tableField] = $tableFieldType;
}
$tablesStructures[$table] = $tableFieldsStructure;
}
if ($this->exportAllData) {
$tableContent = $this->_getTableContent($table);
if (!empty($tableContent)) {
$exportTables->addItemsToTable($table, $tableContent);
}
continue;
}
$ids = array();
if ($table == 'categories') {
if (!empty($this->exportData['categoryIds'])) {
$ids = $this->exportData['categoryIds'];
}
// Get all posts for this category
$contentForCategories = get_content(array(
"categories" => $ids,
"no_limit" => true,
"do_not_replace_site_url" => 1
));
if (is_array($contentForCategories) && !empty($contentForCategories)) {
$exportTables->addItemsToTable('content', $contentForCategories);
}
}
if ($table == 'content') {
if (!empty($this->exportData['contentIds'])) {
$ids = $this->exportData['contentIds'];
}
}
$tableContent = $this->_getTableContent($table, $ids);
if (!empty($tableContent)) {
$exportTables->addItemsToTable($table, $tableContent);
$relations = array();
foreach ($tableContent as $content) {
if (isset($content['rel_type']) && isset($content['rel_id'])) {
$relations[$content['rel_type']][$content['rel_id']] = $content['rel_id'];
}
}
if (!empty($relations)) {
BackupExportLogger::setLogInfo('Get relations from table: <b>' . $table . '</b>');
foreach ($relations as $relationTable => $relationIds) {
BackupExportLogger::setLogInfo('Get data from relations table: <b>' . $relationTable . '</b>');
$relationTableContent = $this->_getTableContent($relationTable, $relationIds);
$exportTables->addItemsToTable($relationTable, $relationTableContent);
}
}
}
}
$exportTablesReady = $exportTables->getAllTableItems();
$exportTablesReady['__table_structures'] = $tablesStructures;
return $exportTablesReady;
}
private function _getTableContent($table, $ids = array())
{
$exportFilter = array();
$exportFilter['no_limit'] = 1;
$exportFilter['do_not_replace_site_url'] = 1;
if (!empty($ids)) {
$exportFilter['ids'] = implode(',', $ids);
}
$tableExists = mw()->database_manager->table_exists($table);
if (!$tableExists) {
return;
}
if ($table == 'media') {
$exportFilter['media_type_without_media_tn_temp'] = function ($query_filter) {
$query_filter->where('media_type', '!=', 'media_tn_temp');
return $query_filter;
};
}
$dbGet = db_get($table, $exportFilter);
return $dbGet;
}
private function _skipTables()
{
$this->skipTables[] = '__table_structures';
$this->skipTables[] = 'modules';
$this->skipTables[] = 'elements';
$this->skipTables[] = 'users';
$this->skipTables[] = 'login_attempts';
$this->skipTables[] = 'log';
$this->skipTables[] = 'notifications';
$this->skipTables[] = 'countries';
$this->skipTables[] = 'content_revisions_history';
$this->skipTables[] = 'content_fields_drafts';
$this->skipTables[] = 'module_templates';
$this->skipTables[] = 'stats_users_online';
$this->skipTables[] = 'stats_browser_agents';
$this->skipTables[] = 'stats_referrers_paths';
$this->skipTables[] = 'stats_referrers_domains';
$this->skipTables[] = 'stats_referrers';
$this->skipTables[] = 'stats_visits_log';
$this->skipTables[] = 'stats_urls';
$this->skipTables[] = 'stats_sessions';
$this->skipTables[] = 'stats_geoip';
$this->skipTables[] = 'system_licenses';
$this->skipTables[] = 'users_oauth';
$this->skipTables[] = 'sessions';
$this->skipTables[] = 'global';
$this->skipTables[] = 'migrations';
$this->skipTables[] = 'translation_keys';
$this->skipTables[] = 'translation_texts';
$this->skipTables[] = 'media_thumbnails';
$this->skipTables[] = 'personal_access_tokens';
return $this->skipTables;
}
private function _prepareSkipTables()
{
$skipTables = $this->_skipTables();
// Add table categories if we have category ids
if (!empty($this->exportData['categoryIds'])) {
if (!in_array('categories', $this->exportData['tables'])) {
$this->exportData['tables'][] = 'categories';
}
}
// Add table categories if we have content ids
if (!empty($this->exportData['contentIds'])) {
if (!in_array('content', $this->exportData['tables'])) {
$this->exportData['tables'][] = 'content';
}
}
if (!empty($this->exportData['tables'])) {
if (in_array('users', $this->exportData['tables'])) {
$keyOfSkipTable = array_search('users', $skipTables);
if ($keyOfSkipTable) {
unset($skipTables[$keyOfSkipTable]);
}
}
}
return $skipTables;
}
private function _getTablesForExport()
{
$skipTables = $this->_prepareSkipTables();
$tablesList = mw()->database_manager->get_tables_list(true);
$tablePrefix = mw()->database_manager->get_prefix();
$readyTableList = array();
foreach ($tablesList as $tableName) {
if ($tablePrefix) {
$tableName = str_replace_first($tablePrefix, '', $tableName);
}
if (in_array($tableName, $skipTables)) {
continue;
}
if (!empty($this->exportData) && $this->exportAllData == false) {
if (isset($this->exportData['tables'])) {
if (!in_array($tableName, $this->exportData['tables'])) {
continue;
}
}
}
$readyTableList[] = $tableName;
}
return $readyTableList;
}
private function _getExporter($data)
{
$export = false;
switch ($this->type) {
case 'json':
$export = new JsonExport($data);
break;
case 'csv':
$export = new CsvExport($data);
break;
case 'xml':
$export = new XmlExport($data);
break;
case 'xlsx':
$export = new XlsxExport($data);
break;
/* case 'zip':
$export = new ZipExport($data);
break; */
default:
throw new \Exception('Format not supported for exporting.');
}
$export->setType($this->type);
return $export->start();
}
}