Introduce app info xml parser including basic unit test - necessary for #10777

This commit is contained in:
Thomas Müller 2014-11-24 16:24:26 +01:00
parent 6fb2477fb7
commit c503ecd544
7 changed files with 193 additions and 60 deletions

View File

@ -635,63 +635,10 @@ class OC_App {
}
$file = self::getAppPath($appId) . '/appinfo/info.xml';
}
$data = array();
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(!\OC::$server->getHTTPHelper()->isHTTPURL($url)) {
$url = OC_Helper::linkToDocs($url);
}
$parser = new \OC\App\InfoParser(\OC::$server->getHTTPHelper(), \OC::$server->getURLGenerator());
$data = $parser->parse($file);
$data["documentation"][$subChild->getName()] = $url;
}
} else {
$data[$child->getName()] = (string)$child;
}
}
self::$appInfo[$appId] = $data;
return $data;

View File

@ -0,0 +1,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;
}
}

View File

@ -58,12 +58,11 @@ class OC_Helper {
}
/**
* @param $key
* @param string $key
* @return string url to the online documentation
*/
public static function linkToDocs($key) {
$theme = new OC_Defaults();
return $theme->buildDocLinkToKey($key);
return OC::$server->getURLGenerator()->linkToDocs($key);
}
/**

View File

@ -8,6 +8,7 @@
*/
namespace OC;
use OC_Defaults;
use OCP\IURLGenerator;
use RuntimeException;
@ -156,7 +157,7 @@ class URLGenerator implements IURLGenerator {
/**
* Makes an URL absolute
* @param string $url the url in the owncloud host
* @param string $url the url in the ownCloud host
* @return string the absolute version of the url
*/
public function getAbsoluteURL($url) {
@ -173,4 +174,13 @@ class URLGenerator implements IURLGenerator {
return \OC_Request::serverProtocol() . '://' . \OC_Request::serverHost(). $webRoot . $separator . $url;
}
/**
* @param string $key
* @return string url to the online documentation
*/
public function linkToDocs($key) {
$theme = new OC_Defaults();
return $theme->buildDocLinkToKey($key);
}
}

View File

@ -69,8 +69,14 @@ interface IURLGenerator {
/**
* Makes an URL absolute
* @param string $url the url in the owncloud host
* @param string $url the url in the ownCloud host
* @return string the absolute version of the url
*/
public function getAbsoluteURL($url);
/**
* @param string $key
* @return string url to the online documentation
*/
public function linkToDocs($key);
}

View File

@ -0,0 +1,22 @@
<?xml version="1.0"?>
<info>
<id>files_encryption</id>
<name>Server-side Encryption</name>
<description>
This application encrypts all files accessed by ownCloud at rest, wherever they are stored. As an example, with this application enabled, external cloud based Amazon S3 storage will be encrypted, protecting this data on storage outside of the control of the Admin. When this application is enabled for the first time, all files are encrypted as users log in and are prompted for their password. The recommended recovery key option enables recovery of files in case the key is lost.
Note that this app encrypts all files that are touched by ownCloud, so external storage providers and applications such as SharePoint will see new files encrypted when they are accessed. Encryption is based on AES 128 or 256 bit keys. More information is available in the Encryption documentation
</description>
<licence>AGPL</licence>
<author>Sam Tuke, Bjoern Schiessle, Florin Peter</author>
<requiremin>4</requiremin>
<shipped>true</shipped>
<documentation>
<user>user-encryption</user>
<admin>admin-encryption</admin>
</documentation>
<rememberlogin>false</rememberlogin>
<types>
<filesystem/>
</types>
<ocsid>166047</ocsid>
</info>

View File

@ -0,0 +1,57 @@
<?php
/**
* @author Thomas Müller
* @copyright 2014 Thomas Müller deepdiver@owncloud.com
* later.
* See the COPYING-README file.
*/
namespace Test\App;
use OC;
class InfoParser extends \PHPUnit_Framework_TestCase {
/**
* @var \OC\App\InfoParser
*/
private $parser;
public function setUp() {
$httpHelper = $this->getMockBuilder('\OC\HTTPHelper')
->disableOriginalConstructor()
->getMock();
$httpHelper->expects($this->any())
->method('isHTTPURL')
->will($this->returnCallback(function ($url) {
return stripos($url, 'https://') === 0 || stripos($url, 'http://') === 0;
}));
$urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
->disableOriginalConstructor()
->getMock();
//linkToDocs
$httpHelper->expects($this->any())
->method('linkToDocs')
->will($this->returnCallback(function ($url) {
return $url;
}));
$this->parser = new \OC\App\InfoParser($httpHelper, $urlGenerator);
}
public function testParsingValidXml() {
$data = $this->parser->parse(OC::$SERVERROOT.'/tests/data/app/valid-info.xml');
$expectedKeys = array(
'id', 'info', 'remote', 'public', 'name', 'description', 'licence', 'author', 'requiremin', 'shipped',
'documentation', 'rememberlogin', 'types', 'ocsid'
);
foreach($expectedKeys as $expectedKey) {
$this->assertArrayHasKey($expectedKey, $data, "ExpectedKey($expectedKey) was missing.");
}
}
}