|
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 \midgard_connection as midgard_connection; |
|
13
|
|
|
use \SimpleXMLElement as SimpleXMLElement; |
|
14
|
|
|
use midgard\portable\storage\subscriber; |
|
15
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
|
16
|
|
|
use midgard\portable\api\error\exception; |
|
|
|
|
|
|
17
|
|
|
use midgard\portable\api\blob; |
|
18
|
|
|
use Doctrine\ORM\NoResultException; |
|
19
|
|
|
|
|
20
|
|
|
class midgard_replicator |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @return boolean Indicating success |
|
24
|
|
|
*/ |
|
25
|
|
|
public static function export(dbobject $object) |
|
26
|
|
|
{ |
|
27
|
|
|
if (!mgd_is_guid($object->guid)) { |
|
|
|
|
|
|
28
|
|
|
return false; |
|
29
|
|
|
} |
|
30
|
|
|
throw new Exception('not implemented'); |
|
31
|
|
|
return true; |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return boolean Indicating success |
|
36
|
|
|
*/ |
|
37
|
4 |
|
public static function export_by_guid($guid) |
|
38
|
|
|
{ |
|
39
|
1 |
|
if (!mgd_is_guid($guid)) { |
|
40
|
1 |
|
midgard_connection::get_instance()->set_error(exception::INVALID_PROPERTY_VALUE); |
|
41
|
1 |
|
return false; |
|
42
|
|
|
} |
|
43
|
1 |
|
$result = connection::get_em() |
|
44
|
1 |
|
->createQueryBuilder() |
|
45
|
1 |
|
->from('midgard:midgard_repligard', 'c') |
|
46
|
1 |
|
->select('c.typename', 'c.object_action') |
|
47
|
4 |
|
->where('c.guid = ?0') |
|
48
|
1 |
|
->setParameter(0, $guid) |
|
49
|
1 |
|
->getQuery() |
|
50
|
1 |
|
->getSingleResult(); |
|
51
|
|
|
|
|
52
|
1 |
|
if ($result['object_action'] === subscriber::ACTION_PURGE) { |
|
53
|
4 |
|
midgard_connection::get_instance()->set_error(exception::OBJECT_PURGED); |
|
54
|
1 |
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
$result = connection::get_em() |
|
58
|
1 |
|
->createQueryBuilder() |
|
59
|
1 |
|
->update('midgard:' . $result['typename'], 'c') |
|
60
|
1 |
|
->set('c.metadata_exported', '?0') |
|
61
|
1 |
|
->setParameter(0, new midgard_datetime) |
|
62
|
1 |
|
->where('c.guid = ?1') |
|
63
|
1 |
|
->setParameter(1, $guid) |
|
64
|
1 |
|
->getQuery() |
|
65
|
1 |
|
->execute(); |
|
66
|
|
|
|
|
67
|
1 |
|
midgard_connection::get_instance()->set_error(exception::OK); |
|
68
|
1 |
|
return ($result > 0); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return string XML document containing purged object information |
|
73
|
|
|
*/ |
|
74
|
|
|
public static function export_purged($class, $startdate = null, $enddate = null) |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
throw new Exception('not implemented'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return string XML representation of the object |
|
81
|
|
|
*/ |
|
82
|
5 |
|
public static function serialize(dbobject $object) |
|
83
|
|
|
{ |
|
84
|
5 |
|
$xml = new SimpleXMLElement('<midgard_object xmlns="http://www.midgard-project.org/midgard_object/1.8"/>'); |
|
85
|
|
|
|
|
86
|
5 |
|
$cm = connection::get_em()->getClassMetadata(get_class($object)); |
|
87
|
5 |
|
$node = $xml->addChild($cm->getReflectionClass()->getShortName()); |
|
88
|
5 |
|
$node->addAttribute('guid', $object->guid); |
|
|
|
|
|
|
89
|
5 |
|
$node->addAttribute('purge', 'no'); |
|
90
|
|
|
|
|
91
|
5 |
|
if (mgd_is_guid($object->guid)) { |
|
92
|
4 |
|
$node->addAttribute('action', self::get_object_action($object->guid)); |
|
93
|
4 |
|
} |
|
94
|
|
|
|
|
95
|
5 |
|
$metadata = []; |
|
96
|
|
|
|
|
97
|
5 |
|
foreach ($cm->getAssociationNames() as $name) { |
|
98
|
5 |
|
$node->addChild($name, self::resolve_link_id($cm, $object, $name)); |
|
99
|
5 |
|
} |
|
100
|
5 |
|
foreach ($cm->getFieldNames() as $name) { |
|
101
|
5 |
|
if ($name == 'guid') { |
|
102
|
5 |
|
continue; |
|
103
|
|
|
} |
|
104
|
5 |
|
if (strpos($name, 'metadata_') === 0) { |
|
105
|
5 |
|
$metadata[substr($name, 9)] = $object->$name; |
|
106
|
5 |
|
} else { |
|
107
|
5 |
|
$node->addChild($name, self::convert_value($object->$name)); |
|
108
|
|
|
} |
|
109
|
5 |
|
} |
|
110
|
|
|
|
|
111
|
5 |
|
if (!empty($metadata)) { |
|
112
|
5 |
|
$mnode = $node->addChild('metadata'); |
|
113
|
5 |
|
foreach ($metadata as $name => $value) { |
|
114
|
5 |
|
$mnode->addChild($name, self::convert_value($value)); |
|
115
|
5 |
|
} |
|
116
|
5 |
|
} |
|
117
|
|
|
|
|
118
|
5 |
|
return $xml->asXML(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return string XML representation of the blob (content is base64 encoded) |
|
123
|
|
|
*/ |
|
124
|
2 |
|
public static function serialize_blob(attachment $object) |
|
125
|
|
|
{ |
|
126
|
2 |
|
$blob = new blob($object); |
|
127
|
2 |
|
$xml = new SimpleXMLElement('<midgard_object xmlns="http://www.midgard-project.org/midgard_object/1.8"/>'); |
|
128
|
2 |
|
$node = $xml->addChild('midgard_blob', base64_encode($blob->read_content())); |
|
129
|
2 |
|
$node->addAttribute('guid', $object->guid); |
|
|
|
|
|
|
130
|
|
|
|
|
131
|
2 |
|
return $xml->asXML(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return dbobject[] Array of objects read from input XML |
|
136
|
|
|
*/ |
|
137
|
7 |
|
public static function unserialize($xml, $force = false) |
|
138
|
|
|
{ |
|
139
|
7 |
|
$ret = []; |
|
140
|
|
|
|
|
141
|
7 |
|
$xml = new SimpleXMLElement($xml); |
|
142
|
7 |
|
foreach ($xml as $node) { |
|
143
|
|
|
try { |
|
144
|
7 |
|
if ($node->getName() == 'midgard_blob') { |
|
145
|
2 |
|
$ret[] = self::blob_from_xml($node, $force); |
|
146
|
2 |
|
} else { |
|
147
|
6 |
|
$ret[] = self::object_from_xml($node, $force); |
|
148
|
|
|
} |
|
149
|
7 |
|
} catch (\Exception $e) { |
|
150
|
2 |
|
connection::log()->warning($e->getMessage()); |
|
151
|
|
|
} |
|
152
|
7 |
|
} |
|
153
|
|
|
|
|
154
|
7 |
|
return $ret; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @return boolean Indicating success |
|
159
|
|
|
*/ |
|
160
|
5 |
|
public static function import_object(dbobject $object, $force = false) |
|
161
|
|
|
{ |
|
162
|
5 |
|
if (!mgd_is_guid($object->guid)) { |
|
|
|
|
|
|
163
|
1 |
|
midgard_connection::get_instance()->set_error(exception::INVALID_PROPERTY_VALUE); |
|
164
|
1 |
|
return false; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
5 |
|
$classname = get_class($object); |
|
168
|
|
|
|
|
169
|
5 |
|
switch (self::get_object_action($object->guid)) { |
|
170
|
5 |
|
case 'created': |
|
171
|
5 |
|
case 'updated': |
|
172
|
1 |
|
$dbobject = new $classname($object->guid); |
|
173
|
1 |
|
break; |
|
174
|
|
|
|
|
175
|
5 |
|
case 'deleted': |
|
176
|
1 |
|
connection::get_em()->getFilters()->disable('softdelete'); |
|
177
|
1 |
|
$dbobject = new $classname($object->guid); |
|
178
|
1 |
|
connection::get_em()->getFilters()->enable('softdelete'); |
|
179
|
1 |
|
break; |
|
180
|
|
|
|
|
181
|
4 |
|
case 'purged': |
|
182
|
1 |
|
if (!$force) { |
|
183
|
1 |
|
return false; |
|
184
|
|
|
} |
|
185
|
1 |
|
$result = connection::get_em() |
|
186
|
1 |
|
->createQueryBuilder() |
|
187
|
1 |
|
->delete('midgard:midgard_repligard', 'c') |
|
188
|
1 |
|
->where('c.guid = ?0') |
|
189
|
1 |
|
->setParameter(0, $object->guid) |
|
190
|
1 |
|
->getQuery() |
|
191
|
1 |
|
->execute(); |
|
192
|
|
|
|
|
193
|
1 |
|
if ($result == 0) { |
|
194
|
|
|
return false; |
|
195
|
|
|
} |
|
196
|
|
|
//fall-through |
|
197
|
|
|
|
|
198
|
4 |
|
default: |
|
199
|
4 |
|
$dbobject = new $classname; |
|
200
|
4 |
|
$dbobject->set_guid($object->guid); |
|
201
|
4 |
|
break; |
|
202
|
5 |
|
} |
|
203
|
|
|
|
|
204
|
5 |
|
if ( $dbobject->id > 0 |
|
205
|
5 |
|
&& $dbobject->metadata->revised->format('U') >= $object->metadata->revised->format('U')) { |
|
|
|
|
|
|
206
|
1 |
|
midgard_connection::get_instance()->set_error(exception::OBJECT_IMPORTED); |
|
207
|
1 |
|
return false; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
5 |
|
if ( $dbobject->metadata->deleted |
|
211
|
5 |
|
&& !$object->metadata->deleted) { |
|
212
|
1 |
|
if (!midgard_object_class::undelete($dbobject->guid)) { |
|
213
|
|
|
return false; |
|
214
|
|
|
} |
|
215
|
1 |
|
$dbobject->metadata_deleted = false; |
|
216
|
5 |
|
} elseif ( !$dbobject->metadata->deleted |
|
217
|
5 |
|
&& $object->metadata->deleted) { |
|
218
|
1 |
|
return $dbobject->delete(); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
5 |
|
$cm = connection::get_em()->getClassMetadata(get_class($object)); |
|
222
|
|
|
|
|
223
|
5 |
|
foreach ($cm->getAssociationNames() as $name) { |
|
224
|
5 |
|
$dbobject->$name = self::resolve_link_guid($cm, $name, $object->$name); |
|
225
|
5 |
|
} |
|
226
|
5 |
|
foreach ($cm->getFieldNames() as $name) { |
|
227
|
5 |
|
if ($name == 'id') { |
|
228
|
5 |
|
continue; |
|
229
|
|
|
} |
|
230
|
5 |
|
if (strpos($name, 'metadata_') === false) { |
|
231
|
5 |
|
$dbobject->$name = $object->$name; |
|
232
|
5 |
|
} |
|
233
|
5 |
|
} |
|
234
|
5 |
|
$dbobject->metadata->imported = new \midgard_datetime(); |
|
235
|
5 |
|
if ($dbobject->id > 0) { |
|
236
|
1 |
|
return $dbobject->update(); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
4 |
|
return $dbobject->create(); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @return boolean Indicating success |
|
244
|
|
|
*/ |
|
245
|
3 |
|
public static function import_from_xml($xml, $force = false) |
|
246
|
|
|
{ |
|
247
|
3 |
|
$objects = self::unserialize($xml, $force); |
|
248
|
3 |
|
foreach ($objects as $object) { |
|
249
|
3 |
|
if ($object instanceof blob) { |
|
250
|
1 |
|
self::import_blob($object, $force); |
|
251
|
1 |
|
} else { |
|
252
|
3 |
|
self::import_object($object, $force); |
|
253
|
|
|
} |
|
254
|
3 |
|
} |
|
255
|
3 |
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* |
|
259
|
|
|
* @param blob $blob |
|
260
|
|
|
* @param boolean $force |
|
261
|
|
|
*/ |
|
262
|
1 |
|
private static function import_blob(blob $blob, $force) |
|
|
|
|
|
|
263
|
|
|
{ |
|
264
|
1 |
|
$blob->write_content($blob->content); |
|
265
|
1 |
|
} |
|
266
|
|
|
|
|
267
|
5 |
|
private static function resolve_link_id(ClassMetadata $cm, dbobject $object, $name) |
|
268
|
|
|
{ |
|
269
|
5 |
|
if ($object->$name == 0) { |
|
270
|
5 |
|
return '0'; |
|
271
|
|
|
} |
|
272
|
2 |
|
$target_class = $cm->getAssociationTargetClass($name); |
|
273
|
2 |
|
return connection::get_em() |
|
274
|
2 |
|
->createQueryBuilder() |
|
275
|
2 |
|
->from($target_class, 'c') |
|
276
|
2 |
|
->select('c.guid') |
|
277
|
2 |
|
->where('c.id = ?0') |
|
278
|
2 |
|
->setParameter(0, $object->$name) |
|
279
|
2 |
|
->getQuery() |
|
280
|
2 |
|
->getSingleScalarResult(); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
8 |
|
private static function resolve_link_guid(ClassMetadata $cm, $name, $value) |
|
284
|
|
|
{ |
|
285
|
8 |
|
if (!mgd_is_guid($value)) { |
|
286
|
8 |
|
return 0; |
|
287
|
|
|
} |
|
288
|
2 |
|
$target_class = $cm->getAssociationTargetClass($name); |
|
289
|
2 |
|
return connection::get_em() |
|
290
|
2 |
|
->createQueryBuilder() |
|
291
|
2 |
|
->from($target_class, 'c') |
|
292
|
2 |
|
->select('c.id') |
|
293
|
2 |
|
->where('c.guid = ?0') |
|
294
|
2 |
|
->setParameter(0, $value) |
|
295
|
2 |
|
->getQuery() |
|
296
|
2 |
|
->getSingleScalarResult(); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* |
|
301
|
|
|
* @param SimpleXMLElement $node |
|
302
|
|
|
* @param boolean $force |
|
303
|
|
|
* @return dbobject |
|
304
|
|
|
*/ |
|
305
|
6 |
|
private static function object_from_xml(SimpleXMLElement $node, $force) |
|
306
|
|
|
{ |
|
307
|
6 |
|
$cm = connection::get_em()->getClassMetadata('midgard:' . $node->getName()); |
|
308
|
6 |
|
$classname = $cm->getName(); |
|
309
|
6 |
|
$object = new $classname; |
|
310
|
6 |
|
$object->set_guid($node['guid']); |
|
311
|
6 |
|
$object->action = $node['action']; |
|
312
|
6 |
|
foreach ($node as $child) { |
|
313
|
6 |
|
$field = $child->getName(); |
|
314
|
6 |
|
if ($field == 'metadata') { |
|
315
|
6 |
|
foreach ($child as $mchild) { |
|
316
|
6 |
|
$field = 'metadata_' . $mchild->getName(); |
|
317
|
6 |
|
$object->$field = (string) $mchild; |
|
318
|
6 |
|
} |
|
319
|
6 |
|
continue; |
|
320
|
|
|
} |
|
321
|
6 |
|
$value = (string) $child; |
|
322
|
6 |
|
if ($cm->isSingleValuedAssociation($field)) { |
|
323
|
|
|
try { |
|
324
|
6 |
|
$value = self::resolve_link_guid($cm, $field, $value); |
|
325
|
6 |
|
} catch (NoResultException $e) { |
|
326
|
1 |
|
if (!$force) { |
|
327
|
1 |
|
throw $e; |
|
328
|
|
|
} |
|
329
|
1 |
|
$value = 0; |
|
330
|
|
|
} |
|
331
|
6 |
|
} |
|
332
|
|
|
|
|
333
|
6 |
|
$object->$field = $value; |
|
334
|
6 |
|
} |
|
335
|
6 |
|
return $object; |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* |
|
340
|
|
|
* @param SimpleXMLElement $node |
|
341
|
|
|
* @param boolean $force |
|
342
|
|
|
* @return dbobject |
|
343
|
|
|
*/ |
|
344
|
2 |
|
private static function blob_from_xml(SimpleXMLElement $node, $force) |
|
|
|
|
|
|
345
|
|
|
{ |
|
346
|
2 |
|
$attachment = midgard_object_class::get_object_by_guid((string) $node['guid']); |
|
347
|
|
|
|
|
348
|
2 |
|
$blob = new blob($attachment); |
|
349
|
2 |
|
$blob->content = base64_decode($node); |
|
350
|
2 |
|
return $blob; |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
9 |
|
private static function get_object_action($guid) |
|
354
|
|
|
{ |
|
355
|
9 |
|
$result = connection::get_em() |
|
356
|
9 |
|
->createQueryBuilder() |
|
357
|
9 |
|
->from('midgard:midgard_repligard', 'c') |
|
358
|
9 |
|
->select('c.object_action') |
|
359
|
9 |
|
->where('c.guid = ?0') |
|
360
|
9 |
|
->setParameter(0, $guid) |
|
361
|
9 |
|
->getQuery() |
|
362
|
9 |
|
->getScalarResult(); |
|
363
|
9 |
|
$action = (empty($result)) ? 0 : (int) $result[0]['object_action']; |
|
364
|
|
|
|
|
365
|
|
|
switch ($action) { |
|
366
|
9 |
|
case subscriber::ACTION_CREATE: |
|
367
|
5 |
|
return 'created'; |
|
368
|
7 |
|
case subscriber::ACTION_UPDATE: |
|
369
|
3 |
|
return 'updated'; |
|
370
|
7 |
|
case subscriber::ACTION_DELETE: |
|
371
|
3 |
|
return 'deleted'; |
|
372
|
6 |
|
case subscriber::ACTION_PURGE: |
|
373
|
3 |
|
return 'purged'; |
|
374
|
3 |
|
default: |
|
375
|
3 |
|
return 'none'; |
|
376
|
3 |
|
} |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
5 |
|
private static function convert_value($value) |
|
380
|
|
|
{ |
|
381
|
5 |
|
if ($value instanceof midgard_datetime) { |
|
382
|
5 |
|
return $value->format('Y-m-d H:i:sO'); |
|
383
|
|
|
} |
|
384
|
5 |
|
return $value; |
|
385
|
|
|
} |
|
386
|
|
|
} |
|
387
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: