Validate the main h5p.json file

namespaces
Svein-Tore Griff With 2012-11-29 08:51:36 +01:00
parent 9a1cabff9d
commit 8737c75ea3
1 changed files with 66 additions and 26 deletions

View File

@ -44,8 +44,17 @@ class h5pValidator {
'metaDescription' => '/^.{1,}$/k', 'metaDescription' => '/^.{1,}$/k',
); );
// These are the same as above, except the preloadedDependencies are optional. Created in the constructor.
private $h5pLibraryRequired;
private $h5pLibraryOptional;
public function __construct($h5pFramework) { public function __construct($h5pFramework) {
$this->h5pF = $h5pFramework; $this->h5pF = $h5pFramework;
$this->h5pLibraryRequired = $this->arrayCopy($this->h5pLibraryRequired);
$requiredDependencies = $this->h5pLibraryRequired['requiredDependencies'];
unset($this->h5pLibraryRequired['requiredDependencies']);
$this->h5pLibraryOptional = $this->arrayCopy($this->h5pLibraryRequired);
$this->h5pLibraryOptional['requiredDependencies'] = $requiredDependencies;
} }
public function validatePackage() { public function validatePackage() {
@ -79,28 +88,37 @@ class h5pValidator {
$file_path = $tmp_dir . DIRECTORY_SEPARATOR . $file; $file_path = $tmp_dir . DIRECTORY_SEPARATOR . $file;
if (strtolower($file) == 'h5p.json') { if (strtolower($file) == 'h5p.json') {
$json_exists = TRUE; $json_exists = TRUE;
// TODO: Validate this main h5p.json file $h5pData = $this->getJsonData($file_path);
// TODO: Make sure preloadedDependencies isn't required in the libraries if ($h5pData === FALSE) {
$valid = FALSE;
$this->h5pF->setErrorMessage($this->t('Could not find or parse the main h5p.json file'));
}
else {
$validH5p = $this->isValidH5pData($h5pData, $file, $this->h5pRequired, $this->h5pOptional);
if ($validH5p) {
$mainH5pExists = TRUE;
}
else {
$valid = FALSE;
$this->h5pF->setErrorMessage($this->t('Could not find or parse the main h5p.json file'));
}
}
} }
elseif (strtolower($file) == 'h5p.jpg') { elseif (strtolower($file) == 'h5p.jpg') {
$image_exists = TRUE; $image_exists = TRUE;
} }
elseif ($file == 'content') { elseif ($file == 'content') {
$json = file_get_contents($file_path . DIRECTORY_SEPARATOR . 'content.json'); $jsonData = $this->getJsonData($file_path . DIRECTORY_SEPARATOR . 'content.json');
if (!$json) { if ($jsonData === FALSE) {
$this->h5pF->setErrorMessage($this->t('Could not find content.json file')); $this->h5pF->setErrorMessage($this->t('Could not find or parse the content.json file'));
$valid = FALSE; $valid = FALSE;
continue; continue;
} }
$contentData = json_decode($json); else {
if (!$contentData) { $content_exists = TRUE;
$this->h5pF->setErrorMessage('Invalid content.json file format. Json is required'); // In the future we might let the librarys provide validation functions for content.json
$valid = FALSE;
continue;
} }
$content_exists = TRUE;
// In the future we might let the librarys provide validation functions for content.json
} }
elseif (strpos($file, '.') !== FALSE) { elseif (strpos($file, '.') !== FALSE) {
@ -114,19 +132,14 @@ class h5pValidator {
$valid = FALSE; $valid = FALSE;
continue; continue;
} }
$json = file_get_contents($file_path . DIRECTORY_SEPARATOR . 'h5p.json'); $h5pData = $this->getJsonData($file_path . DIRECTORY_SEPARATOR . 'h5p.json');
if (!$json) { if ($h5pData === FALSE) {
$this->h5pF->setErrorMessage($this->t('Could not find h5p.json file: %name', array('%name' => $file))); $this->h5pF->setErrorMessage($this->t('Could not find h5p.json file with valid json format for library %name', array('%name' => $file)));
$valid = FALSE; $valid = FALSE;
continue; continue;
} }
$h5pData = json_decode($json);
if (!$h5pData) { $validLibrary = $this->isValidH5pData($h5pData, $library_name, $this->h5pLibraryRequired, $this->h5pLibraryOptional);
$this->h5pF->setErrorMessage($this->t('Invalid h5p.json file format: %name', array('%name' => $file)));
$valid = FALSE;
continue;
}
$validLibrary = $this->isValidH5pData($h5pData, $file) && $valid;
if (isset($h5pData->preloadedJs)) { if (isset($h5pData->preloadedJs)) {
$validLibrary = $this->isExcistingFiles($h5pData->preloadedJs, $tmp_dir, $file) && $validLibrary; $validLibrary = $this->isExcistingFiles($h5pData->preloadedJs, $tmp_dir, $file) && $validLibrary;
@ -141,7 +154,6 @@ class h5pValidator {
} }
} }
if ($valid) { if ($valid) {
// TODO: Also validate the main h5p.json file
$missingLibraries = $this->getMissingLibraries($libraries); $missingLibraries = $this->getMissingLibraries($libraries);
foreach ($missingLibraries as $missing) { foreach ($missingLibraries as $missing) {
if ($this->h5pF->isStoredLibrary($missing['machineName'], $missing['minimumVersion'])) { if ($this->h5pF->isStoredLibrary($missing['machineName'], $missing['minimumVersion'])) {
@ -195,9 +207,9 @@ class h5pValidator {
return TRUE; return TRUE;
} }
private function isValidH5pData($h5pData, $library_name) { private function isValidH5pData($h5pData, $library_name, $required, $optional) {
$valid = $this->isValidRequiredH5pData($h5pData, $this->h5pRequired, $library_name); $valid = $this->isValidRequiredH5pData($h5pData, $required, $library_name);
$valid = $this->isValidOptionalH5pData($h5pData, $this->h5pOptional, $library_name) && $valid; $valid = $this->isValidOptionalH5pData($h5pData, $optional, $library_name) && $valid;
return $valid; return $valid;
} }
@ -277,6 +289,34 @@ class h5pValidator {
return $valid; return $valid;
} }
private function getJsonData($file_path) {
$json = file_get_contents($file_path);
if (!$json) {
return FALSE;
}
$jsonData = json_decode($json);
if (!$jsonData) {
return FALSE;
}
return $jsonData;
}
private function arrayCopy(array $array) {
$result = array();
foreach ($array as $key => $val) {
if (is_array($val)) {
$result[$key] = arrayCopy($val);
}
elseif (is_object($val)) {
$result[$key] = clone $val;
}
else {
$result[$key] = $val;
}
}
return $result;
}
/** /**
* Recursive function for removing directories. * Recursive function for removing directories.
* *