infoparser.php 2.26 KB
Newer Older
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
<?php
 /**
 * @author Thomas Müller
 * @copyright 2014 Thomas Müller deepdiver@owncloud.com
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

namespace OC\App;

use OCP\IURLGenerator;
use SimpleXMLElement;

class InfoParser {

	/**
	 * @param \OC\HTTPHelper $httpHelper
	 * @param IURLGenerator $urlGenerator
	 */
	public function __construct(\OC\HTTPHelper $httpHelper, IURLGenerator $urlGenerator) {
		$this->httpHelper = $httpHelper;
		$this->urlGenerator = $urlGenerator;
	}

	/**
	 * @param string $file
	 * @return null|string
	 */
	public function parse($file) {
		if (!file_exists($file)) {
			return null;
		}

		$content = @file_get_contents($file);
		if (!$content) {
			return null;
		}
		$xml = new SimpleXMLElement($content);
		$data['info'] = array();
		$data['remote'] = array();
		$data['public'] = array();
		foreach ($xml->children() as $child) {
			/**
			 * @var $child SimpleXMLElement
			 */
			if ($child->getName() == 'remote') {
				foreach ($child->children() as $remote) {
					/**
					 * @var $remote SimpleXMLElement
					 */
					$data['remote'][$remote->getName()] = (string)$remote;
				}
			} elseif ($child->getName() == 'public') {
				foreach ($child->children() as $public) {
					/**
					 * @var $public SimpleXMLElement
					 */
					$data['public'][$public->getName()] = (string)$public;
				}
			} elseif ($child->getName() == 'types') {
				$data['types'] = array();
				foreach ($child->children() as $type) {
					/**
					 * @var $type SimpleXMLElement
					 */
					$data['types'][] = $type->getName();
				}
			} elseif ($child->getName() == 'description') {
				$xml = (string)$child->asXML();
				$data[$child->getName()] = substr($xml, 13, -14); //script <description> tags
			} elseif ($child->getName() == 'documentation') {
				foreach ($child as $subChild) {
					$url = (string)$subChild;

					// If it is not an absolute URL we assume it is a key
					// i.e. admin-ldap will get converted to go.php?to=admin-ldap
					if (!$this->httpHelper->isHTTPURL($url)) {
						$url = $this->urlGenerator->linkToDocs($url);
					}

					$data["documentation"][$subChild->getName()] = $url;
				}
			} else {
				$data[$child->getName()] = (string)$child;
			}
		}

		return $data;
	}
}