1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
4
|
|
|
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
5
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use midgard\portable\api\dbobject; |
9
|
|
|
use midgard\portable\api\attachment; |
10
|
|
|
use midgard\portable\storage\connection; |
11
|
|
|
use \midgard_datetime as midgard_datetime; |
|
|
|
|
12
|
|
|
use \SimpleXMLElement as SimpleXMLElement; |
|
|
|
|
13
|
|
|
use midgard\portable\storage\subscriber; |
14
|
|
|
use Doctrine\Persistence\Mapping\ClassMetadata; |
15
|
|
|
use midgard\portable\api\error\exception; |
|
|
|
|
16
|
|
|
use midgard\portable\api\blob; |
17
|
|
|
use Doctrine\ORM\NoResultException; |
18
|
|
|
|
19
|
|
|
class midgard_replicator |
20
|
|
|
{ |
21
|
|
|
public static function export(dbobject $object) : bool |
22
|
|
|
{ |
23
|
|
|
if (!mgd_is_guid($object->guid)) { |
|
|
|
|
24
|
|
|
return false; |
25
|
|
|
} |
26
|
|
|
throw new Exception('not implemented'); |
27
|
|
|
return true; |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
1 |
|
public static function export_by_guid(string $guid) : bool |
31
|
|
|
{ |
32
|
1 |
|
if (!mgd_is_guid($guid)) { |
33
|
1 |
|
exception::invalid_property_value(); |
34
|
1 |
|
return false; |
35
|
|
|
} |
36
|
1 |
|
$result = connection::get_em() |
37
|
1 |
|
->createQueryBuilder() |
38
|
1 |
|
->from(connection::get_fqcn('midgard_repligard'), 'c') |
39
|
1 |
|
->select('c.typename', 'c.object_action') |
40
|
1 |
|
->where('c.guid = ?0') |
41
|
1 |
|
->setParameter(0, $guid) |
42
|
1 |
|
->getQuery() |
43
|
1 |
|
->getSingleResult(); |
44
|
|
|
|
45
|
1 |
|
if ($result['object_action'] === subscriber::ACTION_PURGE) { |
46
|
1 |
|
exception::object_purged(); |
47
|
1 |
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
$result = connection::get_em() |
51
|
1 |
|
->createQueryBuilder() |
52
|
1 |
|
->update(connection::get_fqcn($result['typename']), 'c') |
53
|
1 |
|
->set('c.metadata_exported', '?0') |
54
|
1 |
|
->setParameter(0, new midgard_datetime) |
55
|
1 |
|
->where('c.guid = ?1') |
56
|
1 |
|
->setParameter(1, $guid) |
57
|
1 |
|
->getQuery() |
58
|
1 |
|
->execute(); |
59
|
|
|
|
60
|
1 |
|
exception::ok(); |
61
|
1 |
|
return $result > 0; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string XML document containing purged object information |
66
|
|
|
*/ |
67
|
|
|
public static function export_purged(string $class, $startdate = null, $enddate = null) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
throw new Exception('not implemented'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string XML representation of the object |
74
|
|
|
*/ |
75
|
5 |
|
public static function serialize(dbobject $object) : string |
76
|
|
|
{ |
77
|
5 |
|
$xml = new SimpleXMLElement('<midgard_object xmlns="http://www.midgard-project.org/midgard_object/1.8"/>'); |
78
|
|
|
|
79
|
5 |
|
$cm = connection::get_em()->getClassMetadata(get_class($object)); |
80
|
5 |
|
$node = $xml->addChild($cm->getReflectionClass()->getShortName()); |
81
|
5 |
|
$node->addAttribute('guid', $object->guid); |
|
|
|
|
82
|
5 |
|
$node->addAttribute('purge', 'no'); |
83
|
|
|
|
84
|
5 |
|
if (mgd_is_guid($object->guid)) { |
85
|
4 |
|
$node->addAttribute('action', self::get_object_action($object->guid)); |
86
|
|
|
} |
87
|
|
|
|
88
|
5 |
|
$metadata = []; |
89
|
|
|
|
90
|
5 |
|
foreach ($cm->getAssociationNames() as $name) { |
91
|
5 |
|
$node->addChild($name, self::resolve_link_id($cm, $object, $name)); |
92
|
|
|
} |
93
|
5 |
|
foreach ($cm->getFieldNames() as $name) { |
94
|
5 |
|
if ($name == 'guid') { |
95
|
5 |
|
continue; |
96
|
|
|
} |
97
|
5 |
|
if (str_starts_with($name, 'metadata_')) { |
98
|
5 |
|
$metadata[substr($name, 9)] = $object->$name; |
99
|
|
|
} else { |
100
|
5 |
|
$node->addChild($name, self::convert_value($object->$name)); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
5 |
|
if (!empty($metadata)) { |
105
|
5 |
|
$mnode = $node->addChild('metadata'); |
106
|
5 |
|
foreach ($metadata as $name => $value) { |
107
|
5 |
|
$mnode->addChild($name, self::convert_value($value)); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
5 |
|
return $xml->asXML(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string XML representation of the blob (content is base64 encoded) |
116
|
|
|
*/ |
117
|
2 |
|
public static function serialize_blob(attachment $object) : string |
118
|
|
|
{ |
119
|
2 |
|
$blob = new blob($object); |
120
|
2 |
|
$xml = new SimpleXMLElement('<midgard_object xmlns="http://www.midgard-project.org/midgard_object/1.8"/>'); |
121
|
2 |
|
$node = $xml->addChild('midgard_blob', base64_encode($blob->read_content())); |
|
|
|
|
122
|
2 |
|
$node->addAttribute('guid', $object->guid); |
123
|
|
|
|
124
|
2 |
|
return $xml->asXML(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return dbobject[] Array of objects read from input XML |
129
|
|
|
*/ |
130
|
7 |
|
public static function unserialize(string $xml, bool $force = false) : array |
131
|
|
|
{ |
132
|
7 |
|
$ret = []; |
133
|
|
|
|
134
|
7 |
|
$xml = new SimpleXMLElement($xml); |
135
|
7 |
|
foreach ($xml as $node) { |
136
|
|
|
try { |
137
|
7 |
|
if ($node->getName() == 'midgard_blob') { |
138
|
2 |
|
$ret[] = self::blob_from_xml($node, $force); |
139
|
|
|
} else { |
140
|
7 |
|
$ret[] = self::object_from_xml($node, $force); |
141
|
|
|
} |
142
|
2 |
|
} catch (\Exception $e) { |
143
|
2 |
|
connection::log()->warning($e->getMessage()); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
7 |
|
return $ret; |
148
|
|
|
} |
149
|
|
|
|
150
|
5 |
|
public static function import_object(dbobject $object, bool $force = false) : bool |
151
|
|
|
{ |
152
|
5 |
|
if (!mgd_is_guid($object->guid)) { |
|
|
|
|
153
|
1 |
|
exception::invalid_property_value(); |
154
|
1 |
|
return false; |
155
|
|
|
} |
156
|
|
|
|
157
|
5 |
|
$classname = get_class($object); |
158
|
|
|
|
159
|
5 |
|
switch (self::get_object_action($object->guid)) { |
160
|
5 |
|
case 'created': |
161
|
5 |
|
case 'updated': |
162
|
1 |
|
$dbobject = new $classname($object->guid); |
163
|
1 |
|
break; |
164
|
|
|
|
165
|
5 |
|
case 'deleted': |
166
|
1 |
|
connection::get_em()->getFilters()->disable('softdelete'); |
167
|
1 |
|
$dbobject = new $classname($object->guid); |
168
|
1 |
|
connection::get_em()->getFilters()->enable('softdelete'); |
169
|
1 |
|
break; |
170
|
|
|
|
171
|
4 |
|
case 'purged': |
172
|
1 |
|
if (!$force) { |
173
|
1 |
|
return false; |
174
|
|
|
} |
175
|
1 |
|
if ($repligard_entry = connection::get_em()->getRepository(connection::get_fqcn('midgard_repligard'))->findOneBy(['guid' => $object->guid])) { |
176
|
1 |
|
connection::get_em()->remove($repligard_entry); |
177
|
1 |
|
connection::get_em()->flush($repligard_entry); |
178
|
|
|
} else { |
179
|
|
|
return false; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
//fall-through |
183
|
|
|
|
184
|
|
|
default: |
185
|
4 |
|
$dbobject = new $classname; |
186
|
4 |
|
$dbobject->set_guid($object->guid); |
187
|
4 |
|
break; |
188
|
|
|
} |
189
|
|
|
|
190
|
5 |
|
if ( $dbobject->id > 0 |
191
|
5 |
|
&& $dbobject->metadata->revised->format('U') >= $object->metadata->revised->format('U')) { |
|
|
|
|
192
|
1 |
|
exception::object_imported(); |
193
|
1 |
|
return false; |
194
|
|
|
} |
195
|
|
|
|
196
|
5 |
|
if ( $dbobject->metadata->deleted |
197
|
5 |
|
&& !$object->metadata->deleted) { |
198
|
1 |
|
if (!midgard_object_class::undelete($dbobject->guid)) { |
199
|
|
|
return false; |
200
|
|
|
} |
201
|
1 |
|
$dbobject->metadata_deleted = false; |
202
|
5 |
|
} elseif ( !$dbobject->metadata->deleted |
203
|
5 |
|
&& $object->metadata->deleted) { |
204
|
1 |
|
return $dbobject->delete(); |
205
|
|
|
} |
206
|
|
|
|
207
|
5 |
|
$cm = connection::get_em()->getClassMetadata(get_class($object)); |
208
|
|
|
|
209
|
5 |
|
foreach ($cm->getAssociationNames() as $name) { |
210
|
5 |
|
$dbobject->$name = self::resolve_link_guid($cm, $name, $object->$name); |
211
|
|
|
} |
212
|
5 |
|
foreach ($cm->getFieldNames() as $name) { |
213
|
5 |
|
if ($name == 'id') { |
214
|
5 |
|
continue; |
215
|
|
|
} |
216
|
5 |
|
if (!str_contains($name, 'metadata_')) { |
217
|
5 |
|
$dbobject->$name = $object->$name; |
218
|
|
|
} |
219
|
|
|
} |
220
|
5 |
|
$dbobject->metadata->imported = new \midgard_datetime(); |
221
|
5 |
|
if ($dbobject->id > 0) { |
222
|
1 |
|
return $dbobject->update(); |
223
|
|
|
} |
224
|
|
|
|
225
|
4 |
|
return $dbobject->create(); |
226
|
|
|
} |
227
|
|
|
|
228
|
3 |
|
public static function import_from_xml(string $xml, bool $force = false) |
229
|
|
|
{ |
230
|
3 |
|
$objects = self::unserialize($xml, $force); |
231
|
3 |
|
foreach ($objects as $object) { |
232
|
3 |
|
if ($object instanceof blob) { |
233
|
1 |
|
self::import_blob($object, $force); |
234
|
|
|
} else { |
235
|
3 |
|
self::import_object($object, $force); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
1 |
|
private static function import_blob(blob $blob, bool $force) |
|
|
|
|
241
|
|
|
{ |
242
|
1 |
|
$blob->write_content($blob->content); |
243
|
|
|
} |
244
|
|
|
|
245
|
5 |
|
private static function resolve_link_id(ClassMetadata $cm, dbobject $object, string $name) : string |
246
|
|
|
{ |
247
|
5 |
|
if ($object->$name == 0) { |
248
|
5 |
|
return '0'; |
249
|
|
|
} |
250
|
2 |
|
$target_class = $cm->getAssociationTargetClass($name); |
251
|
2 |
|
return connection::get_em() |
252
|
2 |
|
->createQueryBuilder() |
253
|
2 |
|
->from($target_class, 'c') |
254
|
2 |
|
->select('c.guid') |
255
|
2 |
|
->where('c.id = ?0') |
256
|
2 |
|
->setParameter(0, $object->$name) |
257
|
2 |
|
->getQuery() |
258
|
2 |
|
->getSingleScalarResult(); |
259
|
|
|
} |
260
|
|
|
|
261
|
8 |
|
private static function resolve_link_guid(ClassMetadata $cm, string $name, string $value) : int |
262
|
|
|
{ |
263
|
8 |
|
if (!mgd_is_guid($value)) { |
264
|
8 |
|
return 0; |
265
|
|
|
} |
266
|
2 |
|
$target_class = $cm->getAssociationTargetClass($name); |
267
|
2 |
|
return connection::get_em() |
268
|
2 |
|
->createQueryBuilder() |
269
|
2 |
|
->from($target_class, 'c') |
270
|
2 |
|
->select('c.id') |
271
|
2 |
|
->where('c.guid = ?0') |
272
|
2 |
|
->setParameter(0, $value) |
273
|
2 |
|
->getQuery() |
274
|
2 |
|
->getSingleScalarResult(); |
275
|
|
|
} |
276
|
|
|
|
277
|
6 |
|
private static function object_from_xml(SimpleXMLElement $node, bool $force) : dbobject |
278
|
|
|
{ |
279
|
6 |
|
$cm = connection::get_em()->getClassMetadata(connection::get_fqcn($node->getName())); |
280
|
6 |
|
$classname = $cm->getName(); |
281
|
6 |
|
$object = new $classname; |
282
|
6 |
|
$object->set_guid($node['guid']); |
283
|
6 |
|
$object->action = $node['action']; |
284
|
6 |
|
foreach ($node as $child) { |
285
|
6 |
|
$field = $child->getName(); |
286
|
6 |
|
if ($field == 'metadata') { |
287
|
6 |
|
foreach ($child as $mchild) { |
288
|
6 |
|
$field = 'metadata_' . $mchild->getName(); |
289
|
6 |
|
$object->$field = (string) $mchild; |
290
|
|
|
} |
291
|
6 |
|
continue; |
292
|
|
|
} |
293
|
6 |
|
$value = (string) $child; |
294
|
6 |
|
if ($cm->isSingleValuedAssociation($field)) { |
295
|
|
|
try { |
296
|
6 |
|
$value = self::resolve_link_guid($cm, $field, $value); |
297
|
1 |
|
} catch (NoResultException $e) { |
298
|
1 |
|
if (!$force) { |
299
|
1 |
|
throw $e; |
300
|
|
|
} |
301
|
1 |
|
$value = 0; |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
305
|
6 |
|
$object->$field = $value; |
306
|
|
|
} |
307
|
6 |
|
return $object; |
308
|
|
|
} |
309
|
|
|
|
310
|
2 |
|
private static function blob_from_xml(SimpleXMLElement $node, bool $force) : blob |
|
|
|
|
311
|
|
|
{ |
312
|
2 |
|
$attachment = midgard_object_class::get_object_by_guid((string) $node['guid']); |
313
|
|
|
|
314
|
2 |
|
$blob = new blob($attachment); |
315
|
2 |
|
$blob->content = base64_decode($node); |
316
|
2 |
|
return $blob; |
317
|
|
|
} |
318
|
|
|
|
319
|
9 |
|
private static function get_object_action(string $guid) : string |
320
|
|
|
{ |
321
|
9 |
|
$result = connection::get_em() |
322
|
9 |
|
->createQueryBuilder() |
323
|
9 |
|
->from(connection::get_fqcn('midgard_repligard'), 'c') |
324
|
9 |
|
->select('c.object_action') |
325
|
9 |
|
->where('c.guid = ?0') |
326
|
9 |
|
->setParameter(0, $guid) |
327
|
9 |
|
->getQuery() |
328
|
9 |
|
->getScalarResult(); |
329
|
9 |
|
$action = (empty($result)) ? 0 : (int) $result[0]['object_action']; |
330
|
|
|
|
331
|
9 |
|
return match ($action) { |
332
|
5 |
|
subscriber::ACTION_CREATE => 'created', |
333
|
3 |
|
subscriber::ACTION_UPDATE => 'updated', |
334
|
3 |
|
subscriber::ACTION_DELETE => 'deleted', |
335
|
3 |
|
subscriber::ACTION_PURGE => 'purged', |
336
|
9 |
|
default => 'none', |
337
|
9 |
|
}; |
338
|
|
|
} |
339
|
|
|
|
340
|
5 |
|
private static function convert_value($value) |
341
|
|
|
{ |
342
|
5 |
|
if ($value instanceof midgard_datetime) { |
343
|
5 |
|
return $value->format('Y-m-d H:i:sO'); |
344
|
|
|
} |
345
|
5 |
|
return $value; |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths