1 | <?php |
---|
2 | /** |
---|
3 | * @brief Repository modules XML feed parser |
---|
4 | * |
---|
5 | * Provides an object to parse XML feed of modules from a repository. |
---|
6 | * |
---|
7 | * @package Dotclear |
---|
8 | * @subpackage Core |
---|
9 | * |
---|
10 | * @copyright Olivier Meunier & Association Dotclear |
---|
11 | * @copyright GPL-2.0-only |
---|
12 | * |
---|
13 | * @since 2.6 |
---|
14 | */ |
---|
15 | |
---|
16 | if (!defined('DC_RC_PATH')) {return;} |
---|
17 | |
---|
18 | class dcStoreParser |
---|
19 | { |
---|
20 | /** @var object XML object of feed contents */ |
---|
21 | protected $xml; |
---|
22 | /** @var array Array of feed contents */ |
---|
23 | protected $items; |
---|
24 | /** @var string XML bloc tag */ |
---|
25 | protected static $bloc = 'http://dotaddict.org/da/'; |
---|
26 | |
---|
27 | /** |
---|
28 | * Constructor. |
---|
29 | * |
---|
30 | * @param string $data Feed content |
---|
31 | */ |
---|
32 | public function __construct($data) |
---|
33 | { |
---|
34 | if (!is_string($data)) { |
---|
35 | throw new Exception(__('Failed to read data feed')); |
---|
36 | } |
---|
37 | |
---|
38 | $this->xml = simplexml_load_string($data); |
---|
39 | $this->items = array(); |
---|
40 | |
---|
41 | if ($this->xml === false) { |
---|
42 | throw new Exception(__('Wrong data feed')); |
---|
43 | } |
---|
44 | |
---|
45 | $this->_parse(); |
---|
46 | |
---|
47 | unset($data); |
---|
48 | unset($this->xml); |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * Parse XML into array |
---|
53 | */ |
---|
54 | protected function _parse() |
---|
55 | { |
---|
56 | if (empty($this->xml->module)) { |
---|
57 | return; |
---|
58 | } |
---|
59 | |
---|
60 | foreach ($this->xml->module as $i) { |
---|
61 | $attrs = $i->attributes(); |
---|
62 | |
---|
63 | $item = array(); |
---|
64 | |
---|
65 | # DC/DA shared markers |
---|
66 | $item['id'] = (string) $attrs['id']; |
---|
67 | $item['file'] = (string) $i->file; |
---|
68 | $item['label'] = (string) $i->name; // deprecated |
---|
69 | $item['name'] = (string) $i->name; |
---|
70 | $item['version'] = (string) $i->version; |
---|
71 | $item['author'] = (string) $i->author; |
---|
72 | $item['desc'] = (string) $i->desc; |
---|
73 | |
---|
74 | # DA specific markers |
---|
75 | $item['dc_min'] = (string) $i->children(self::$bloc)->dcmin; |
---|
76 | $item['details'] = (string) $i->children(self::$bloc)->details; |
---|
77 | $item['section'] = (string) $i->children(self::$bloc)->section; |
---|
78 | $item['support'] = (string) $i->children(self::$bloc)->support; |
---|
79 | $item['sshot'] = (string) $i->children(self::$bloc)->sshot; |
---|
80 | |
---|
81 | $tags = array(); |
---|
82 | foreach ($i->children(self::$bloc)->tags as $t) { |
---|
83 | $tags[] = (string) $t->tag; |
---|
84 | } |
---|
85 | $item['tags'] = implode(', ', $tags); |
---|
86 | |
---|
87 | # First filter right now. If DC_DEV is set all modules are parse |
---|
88 | if (defined('DC_DEV') && DC_DEV === true || dcUtils::versionsCompare(DC_VERSION, $item['dc_min'], '>=', false)) { |
---|
89 | $this->items[$item['id']] = $item; |
---|
90 | } |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * Get modules. |
---|
96 | * |
---|
97 | * @return array Modules list |
---|
98 | */ |
---|
99 | public function getModules() |
---|
100 | { |
---|
101 | return $this->items; |
---|
102 | } |
---|
103 | } |
---|