-
Notifications
You must be signed in to change notification settings - Fork 11
/
class.xmlimporter.php
501 lines (414 loc) · 13.5 KB
/
class.xmlimporter.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
492
493
494
495
496
497
498
499
500
501
<?php
require_once(TOOLKIT . '/class.gateway.php');
require_once(TOOLKIT . '/class.fieldmanager.php');
require_once(TOOLKIT . '/class.entrymanager.php');
require_once(TOOLKIT . '/class.sectionmanager.php');
// Attempt to load XMLImporter Helper functions from the workspace rather
// than the extension. If that file doesn't exist, then just load what
// is provided.
// @see https://github.com/symphonists/xmlimporter/issues/16
if(@file_exists(WORKSPACE . '/xml-importers/class.xmlimporterhelpers.php') === true) {
require_once(WORKSPACE . '/xml-importers/class.xmlimporterhelpers.php');
}
else if(@file_exists(EXTENSIONS . '/xmlimporter/lib/class.xmlimporterhelpers.php') === true) {
require_once(EXTENSIONS . '/xmlimporter/lib/class.xmlimporterhelpers.php');
}
class XMLImporter {
const __OK__ = 100;
const __PARTIAL_OK__ = 110;
const __ERROR_PREPARING__ = 200;
const __ERROR_VALIDATING__ = 210;
const __ERROR_CREATING__ = 220;
protected $_entries = array();
protected $_errors = array();
public $context = array();
public function about() {
return array();
}
public function options() {
return array();
}
public function getEntries() {
return $this->_entries;
}
public function getErrors() {
return $this->_errors;
}
public function setContext(array $context)
{
$this->context = $context;
}
protected function getExpressionValue($xml, $entry, $xpath, $expression) {
$matches = $xpath->evaluate($expression, $entry);
if ($matches instanceof DOMNodeList) {
$values = array();
foreach ($matches as $match) {
if ($match instanceof DOMAttr or $match instanceof DOMText) {
$values[] = $match->nodeValue;
}
else {
$values[] = $xml->saveXML($match);
}
}
return $values;
}
else if (!is_null($matches)) {
return array(strval($matches));
}
return null;
}
public function validate($source = null, $remote = true) {
if (!function_exists('handleXMLError')) {
function handleXMLError($errno, $errstr, $errfile, $errline, $context) {
$context['self']->_errors[] = $errstr;
}
}
set_time_limit(900);
set_error_handler('handleXMLError');
$self = $this; // Fucking PHP...
$options = $this->options();
$passed = true;
// If $remote, override the source of the XMLImporter with the given $source
if ($remote) {
if (!is_null($source)) {
$options['source'] = $source;
}
// Support {$root}
$options['source'] = str_replace('{$root}', URL, $options['source']);
// Parse timeout, default is 60
$timeout = isset($options['timeout']) ? (int)$options['timeout'] : 60;
// Fetch document:
$gateway = new Gateway();
$gateway->init();
$gateway->setopt('URL', $options['source']);
$gateway->setopt('TIMEOUT', $timeout);
$data = $gateway->exec();
$info = $gateway->getInfoLast();
if (empty($data) || $info['http_code'] >= 400) {
$this->_errors[] = __('No data to import. URL returned HTTP code %d', array($info['http_code']));
$passed = false;
}
}
else if (isset($options['source'])) {
$ds = DatasourceManager::create($options['source'], $this->context, true);
// Not a DataSource (legacy)
if(!($ds instanceof Datasource)) {
$data = $source;
}
// DataSource output
else {
$xml = $ds->execute($param_pool);
if(isset($ds->dsParamNAMESPACES)) {
foreach($ds->dsParamNAMESPACES as $name => $uri) {
$options['namespaces'][] = array(
'name' => $name,
'uri' => $uri
);
}
}
if($xml->getAttribute('valid') == 'false') {
$this->_errors[] = __('Failed to retrieve data from source: %s', array($xml->generate()));
$passed = false;
}
else {
$data = '<data>' . $xml->generate(true) . '</data>';
}
}
}
else {
$this->_errors[] = __('No data to import.');
$passed = false;
}
if(!is_array($options['fields'])) {
$this->_errors[] = __('No field mappings have been set for this XML Importer.');
$passed = false;
}
if (!$passed) return self::__ERROR_PREPARING__;
// Load document:
$xml = new DOMDocument();
$xml->loadXML($data);
restore_error_handler();
$xpath = new DOMXPath($xml);
$passed = true;
// Register namespaces:
if (is_array($options['namespaces'])) {
foreach ($options['namespaces'] as $namespace) {
$xpath->registerNamespace($namespace['name'], $namespace['uri']);
}
}
// Invalid Markup:
if (empty($xml)) {
$passed = false;
}
// Invalid Expression:
else if (($entries = $xpath->query($options['included-elements'])) === false) {
$this->_errors[] = __(
'Root expression <code>%s</code> is invalid.', array(
General::sanitize($options['included-elements'])
)
);
$passed = false;
}
// No Entries:
else if (is_null($entries) or $entries->length == 0) {
$this->_errors[] = __('No entries to import.');
$passed = false;
}
// Test expressions:
else foreach ($options['fields'] as $mapping) {
if ($xpath->evaluate(stripslashes($mapping['xpath'])) !== false) continue;
$field = FieldManager::fetch($mapping['field']);
$this->_errors[] = __(
'\'%s\' expression <code>%s</code> is invalid.', array(
$field->get('label'),
General::sanitize($mapping['xpath'])
)
);
$passed = false;
}
if (!$passed) return self::__ERROR_PREPARING__;
// Gather data:
foreach ($entries as $index => $entry) {
$this->_entries[$index] = array(
'element' => $entry,
'values' => array(),
'errors' => array(),
'entry' => null
);
foreach ($options['fields'] as $mapping) {
$values = $this->getExpressionValue($xml, $entry, $xpath, $mapping['xpath'], $debug);
if (isset($mapping['php']) && $mapping['php'] != '') {
$php = stripslashes($mapping['php']);
// static helper
if (preg_match('/::/', $php)) {
foreach($values as $id => $value) {
$values[$id] = call_user_func_array($php, array($value));
}
}
// basic function
else {
foreach($values as $id => $value) {
$function = preg_replace('/\$value/', "'" . $value . "'", $php);
if (!preg_match('/^return/', $function)) $function = 'return ' . $function;
if (!preg_match('/;$/', $function)) $function .= ';';
$values[$id] = @eval($function);
}
}
}
$this->_entries[$index]['values'][$mapping['field']] = $values;
}
}
// Validate:
$passed = true;
foreach ($this->_entries as $index => &$current) {
$entry = EntryManager::create();
$entry->set('section_id', $options['section']);
$entry->set('author_id', is_null(Symphony::Engine()->Author()) ? '1' : Symphony::Engine()->Author()->get('id'));
$entry->set('modification_date_gmt', DateTimeObj::getGMT('Y-m-d H:i:s'));
$entry->set('modification_date', DateTimeObj::get('Y-m-d H:i:s'));
$values = array();
// Map values:
foreach ($current['values'] as $field_id => $value) {
$field = FieldManager::fetch($field_id);
if(is_array($value)) {
if(count($value) === 1) {
$value = current($value);
}
if(count($value) === 0) {
$value = '';
}
}
// Adjust value?
if (method_exists($field, 'prepareImportValue') && method_exists($field, 'getImportModes')) {
$modes = $field->getImportModes();
if(is_array($modes) && !empty($modes)) {
$mode = current($modes);
}
if ($field->get('type') != 'selectbox_link') {
$value = $field->prepareImportValue($value, $mode, $entry->get('id'));
}
}
// Handle different field types
else {
$type = $field->get('type');
if ($type == 'author') {
if ($field->get('allow_multiple_selection') == 'no') {
if(is_array($value)){
$value = array(implode('', $value));
}
}
}
else if ($type == 'datetime') {
$value = $value[0];
}
else if (is_array($value)) {
$value = implode('', $value);
}
}
$values[$field->get('element_name')] = $value;
}
// Validate:
try {
if (__ENTRY_FIELD_ERROR__ == $entry->checkPostData($values, $current['errors'])) {
$passed = false;
}
else if (__ENTRY_OK__ != $entry->setDataFromPost($values, $current['errors'], true, true)) {
$passed = false;
}
}
catch (Exception $ex) {
$passed = false;
$current['errors'] = array($ex->getMessage());
Symphony::Log()->pushToLog(sprintf('XMLImporter: Failed to set values for entry in position %d, %s', $index, $ex->getMessage()), E_NOTICE, true);
}
$current['entry'] = $entry;
$current['values'] = $values;
}
if (!$passed) return self::__ERROR_VALIDATING__;
return self::__OK__;
}
public function commit($status) {
$options = $this->options();
$section = SectionManager::fetch($options['section']);
$existing = array();
// if $status = PARTIAL_OK
if($status == self::__PARTIAL_OK__) {
$entries = $this->_entries;
foreach($entries as $index => $current) {
if(!empty($current['errors'])) {
$this->_entries[$index]['entry']->set('importer_status', 'failed');
unset($entries[$index]);
}
}
}
else {
$entries = $this->_entries;
}
// Check uniqueness
if ((integer)$options['unique-field'] > 0) {
$field = FieldManager::fetch($options['unique-field']);
}
foreach ($entries as $index => $current) {
$entry = $current['entry'];
$values = $current['values'];
$date = DateTimeObj::get('Y-m-d H:i:s');
$dateGMT = DateTimeObj::getGMT('Y-m-d H:i:s');
// Uniqueness check (if required)
if(!empty($field)) {
$this->checkExisting($field, $entry, $index, $existing);
};
// Matches an existing entry
if (!is_null($existing[$index])) {
// Update
if ($options['can-update'] == 'yes') {
$entry->set('id', $existing[$index]);
$entry->set('modification_date', $date);
$entry->set('modification_date_gmt', $dateGMT);
###
# Delegate: XMLImporterEntryPreEdit
# Description: Just prior to editing of an Entry.
Symphony::ExtensionManager()->notifyMembers(
'XMLImporterEntryPreEdit', '/xmlimporter/importers/run/',
array(
'section' => $section,
'fields' => &$values,
'entry' => &$entry
)
);
EntryManager::edit($entry);
$entry->set('importer_status', 'updated');
###
# Delegate: XMLImporterEntryPostEdit
# Description: Editing an entry. Entry object is provided.
Symphony::ExtensionManager()->notifyMembers(
'XMLImporterEntryPostEdit', '/xmlimporter/importers/run/',
array(
'section' => $section,
'entry' => $entry,
'fields' => $values
)
);
}
// Skip
else {
$entry->set('importer_status', 'skipped');
###
# Delegate: XMLImporterEntryPostSkip
# Description: Skipping an entry. Entry object is provided.
Symphony::ExtensionManager()->notifyMembers(
'XMLImporterEntryPostSkip', '/xmlimporter/importers/run/',
array(
'section' => $section,
'entry' => $entry,
'fields' => $values
)
);
continue;
}
}
// Create entry
else {
$entry->set('creation_date', $date);
$entry->set('creation_date_gmt', $dateGMT);
$entry->set('modification_date', $date);
$entry->set('modification_date_gmt', $dateGMT);
$entry->set('modification_author_id', !Symphony::Author() ? '1' : Symphony::Author()->get('id'));
###
# Delegate: XMLImporterEntryPreCreate
# Description: Just prior to creation of an Entry. Entry object provided
Symphony::ExtensionManager()->notifyMembers(
'XMLImporterEntryPreCreate', '/xmlimporter/importers/run/',
array(
'section' => $section,
'fields' => &$values,
'entry' => &$entry
)
);
EntryManager::add($entry);
$entry->set('importer_status', 'created');
###
# Delegate: XMLImporterEntryPostCreate
# Description: Creation of an Entry. New Entry object is provided.
Symphony::ExtensionManager()->notifyMembers(
'XMLImporterEntryPostCreate', '/xmlimporter/importers/run/',
array(
'section' => $section,
'entry' => $entry,
'fields' => $values
)
);
}
}
}
/**
* Given the `$field`, and the `$entry`, this function
* will take the value that is about to be imported and
* check to see if it's already in the system.
* If it is, the `entry_id` of `$entry` will be added
* to the `$existing` array.
*
* @param Field $field
* The unique field
* @param Entry $entry
* The current entry that is about to be imported
* @param integer $index
* The current position of the Entry in the import
* @param array $existing
* An associative array, by reference. The key is the position of
* the entry in the import, and the value is the `entry_id` if
* a match was found, otherwise null.
*/
private function checkExisting(Field $field, Entry $entry, $index, array &$existing) {
$data = $entry->getData($field->get('id'));
$where = $joins = $group = null;
$field->buildDSRetrievalSQL($data, $joins, $where);
$group = $field->requiresSQLGrouping();
$existing_entries = EntryManager::fetch(null, $field->get('parent_section'), 1, null, $where, $joins, $group, false, null, false);
if (is_array($existing_entries) && !empty($existing_entries)) {
$existing[$index] = $existing_entries[0]['id'];
}
else {
$existing[$index] = null;
}
}
}