1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Rarst\Hugo\wprss2hugo; |
5
|
|
|
|
6
|
|
|
use Prewk\XmlStringStreamer; |
7
|
|
|
use Prewk\XmlStringStreamer\Parser\StringWalker; |
8
|
|
|
use Prewk\XmlStringStreamer\Stream\File; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Incremental wrapper for the XML export file. |
12
|
|
|
*/ |
13
|
|
|
class Export implements \Iterator |
14
|
|
|
{ |
15
|
|
|
/** @var XmlStringStreamer */ |
16
|
|
|
private $streamer; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
private $currentNode; |
20
|
|
|
|
21
|
|
|
/** @var int */ |
22
|
|
|
private $size; |
23
|
|
|
|
24
|
|
|
/** @var int */ |
25
|
|
|
private $bytes = 0; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set up for a given export file path. |
29
|
|
|
*/ |
30
|
|
|
public function __construct(string $file) |
31
|
|
|
{ |
32
|
|
|
$this->size = filesize($file); |
33
|
|
|
$stream = new File($file, 16384, function (string $chunk, int $readBytes) { |
34
|
|
|
$this->bytes = $readBytes; |
35
|
|
|
}); |
36
|
|
|
$parser = new StringWalker([ |
37
|
|
|
'captureDepth' => 3, |
38
|
|
|
'expectGT' => true, |
39
|
|
|
]); |
40
|
|
|
$this->streamer = new XmlStringStreamer($parser, $stream); |
41
|
|
|
|
42
|
|
|
$this->next(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* File read progress in percent. |
47
|
|
|
*/ |
48
|
|
|
public function progress(): int |
49
|
|
|
{ |
50
|
|
|
return (int)round($this->bytes / $this->size * 100); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Current XML node. |
55
|
|
|
*/ |
56
|
|
|
public function current() |
57
|
|
|
{ |
58
|
|
|
return $this->transform($this->currentNode); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Convert XML string to an element object. |
63
|
|
|
*/ |
64
|
|
|
private function transform(string $xml): \SimpleXMLElement |
65
|
|
|
{ |
66
|
|
|
return simplexml_load_string($this->removeNamespaces($xml)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Remove namespaces from partial XML string for simplicity of processing. |
71
|
|
|
*/ |
72
|
|
|
private function removeNamespaces(string $xml): string |
73
|
|
|
{ |
74
|
|
|
$xml = strtr($xml, [ |
75
|
|
|
'<content:encoded>' => '<content>', |
76
|
|
|
'</content:encoded>' => '</content>', |
77
|
|
|
'<excerpt:encoded>' => '<excerpt>', |
78
|
|
|
'</excerpt:encoded>' => '</excerpt>', |
79
|
|
|
]); |
80
|
|
|
|
81
|
|
|
$xml = preg_replace('~<([/]?)[[:alpha:]_]+?:([[:alpha:]_]+?)([/]?)>~', '<$1$2$3>', $xml); |
82
|
|
|
|
83
|
|
|
return $xml; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function next() |
87
|
|
|
{ |
88
|
|
|
$this->currentNode = (string)$this->streamer->getNode(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Iterator key (not applicable). |
93
|
|
|
*/ |
94
|
|
|
public function key() |
95
|
|
|
{ |
96
|
|
|
return null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* If got valid node iteration. |
101
|
|
|
*/ |
102
|
|
|
public function valid() |
103
|
|
|
{ |
104
|
|
|
return '' !== $this->currentNode; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function rewind() |
108
|
|
|
{ |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|