2014-03-26 08:43:29 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a data layer which uses the file system so it isn't specific to any framework.
|
|
|
|
*/
|
|
|
|
class H5PDevelopment {
|
|
|
|
|
|
|
|
const MODE_NONE = 0;
|
|
|
|
const MODE_CONTENT = 1;
|
|
|
|
const MODE_LIBRARY = 2;
|
|
|
|
|
2015-04-07 19:30:46 +02:00
|
|
|
private $h5pF, $libraries, $language, $filesPath;
|
2014-03-26 08:43:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2016-04-13 06:01:15 +02:00
|
|
|
* @param H5PFrameworkInterface|object $H5PFramework
|
2014-03-26 08:43:29 +01:00
|
|
|
* The frameworks implementation of the H5PFrameworkInterface
|
|
|
|
* @param string $filesPath
|
|
|
|
* Path to where H5P should store its files
|
2016-04-13 06:01:15 +02:00
|
|
|
* @param $language
|
2014-03-26 08:43:29 +01:00
|
|
|
* @param array $libraries Optional cache input.
|
|
|
|
*/
|
2016-04-13 06:01:15 +02:00
|
|
|
public function __construct(H5PFrameworkInterface $H5PFramework, $filesPath, $language, $libraries = NULL) {
|
2014-03-26 08:43:29 +01:00
|
|
|
$this->h5pF = $H5PFramework;
|
|
|
|
$this->language = $language;
|
2015-04-07 19:30:46 +02:00
|
|
|
$this->filesPath = $filesPath;
|
2014-03-26 08:43:29 +01:00
|
|
|
if ($libraries !== NULL) {
|
|
|
|
$this->libraries = $libraries;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->findLibraries($filesPath . '/development');
|
|
|
|
}
|
|
|
|
}
|
2014-10-07 14:46:35 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
/**
|
|
|
|
* Get contents of file.
|
|
|
|
*
|
2016-04-13 06:01:15 +02:00
|
|
|
* @param string $file File path.
|
2014-03-26 08:43:29 +01:00
|
|
|
* @return mixed String on success or NULL on failure.
|
|
|
|
*/
|
|
|
|
private function getFileContents($file) {
|
|
|
|
if (file_exists($file) === FALSE) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-10-07 14:46:35 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
$contents = file_get_contents($file);
|
|
|
|
if ($contents === FALSE) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-10-07 14:46:35 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
return $contents;
|
|
|
|
}
|
2014-10-07 14:46:35 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
/**
|
|
|
|
* Scans development directory and find all libraries.
|
|
|
|
*
|
|
|
|
* @param string $path Libraries development folder
|
|
|
|
*/
|
|
|
|
private function findLibraries($path) {
|
|
|
|
$this->libraries = array();
|
2014-10-07 14:46:35 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
if (is_dir($path) === FALSE) {
|
2014-10-07 14:46:35 +02:00
|
|
|
return;
|
2014-03-26 08:43:29 +01:00
|
|
|
}
|
2014-10-07 14:46:35 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
$contents = scandir($path);
|
2014-10-07 14:46:35 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
for ($i = 0, $s = count($contents); $i < $s; $i++) {
|
2019-10-30 11:28:54 +01:00
|
|
|
if ($contents[$i][0] === '.') {
|
2014-03-26 08:43:29 +01:00
|
|
|
continue; // Skip hidden stuff.
|
|
|
|
}
|
2014-10-07 14:46:35 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
$libraryPath = $path . '/' . $contents[$i];
|
|
|
|
$libraryJSON = $this->getFileContents($libraryPath . '/library.json');
|
|
|
|
if ($libraryJSON === NULL) {
|
|
|
|
continue; // No JSON file, skip.
|
|
|
|
}
|
2014-10-07 14:46:35 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
$library = json_decode($libraryJSON, TRUE);
|
2016-07-08 11:08:47 +02:00
|
|
|
if ($library === NULL) {
|
2014-03-26 08:43:29 +01:00
|
|
|
continue; // Invalid JSON.
|
|
|
|
}
|
2014-10-07 14:46:35 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
// TODO: Validate props? Not really needed, is it? this is a dev site.
|
2014-10-07 14:46:35 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
$library['libraryId'] = $this->h5pF->getLibraryId($library['machineName'], $library['majorVersion'], $library['minorVersion']);
|
2018-09-21 15:09:31 +02:00
|
|
|
|
|
|
|
// Convert metadataSettings values to boolean & json_encode it before saving
|
|
|
|
$library['metadataSettings'] = isset($library['metadataSettings']) ?
|
|
|
|
H5PMetadata::boolifyAndEncodeSettings($library['metadataSettings']) :
|
|
|
|
NULL;
|
|
|
|
|
|
|
|
// Save/update library.
|
2014-03-26 08:43:29 +01:00
|
|
|
$this->h5pF->saveLibraryData($library, $library['libraryId'] === FALSE);
|
2014-10-07 14:46:35 +02:00
|
|
|
|
2018-09-26 10:30:50 +02:00
|
|
|
// Need to decode it again, since it is served from here.
|
|
|
|
$library['metadataSettings'] = json_decode($library['metadataSettings']);
|
|
|
|
|
2015-04-07 19:30:46 +02:00
|
|
|
$library['path'] = 'development/' . $contents[$i];
|
2014-03-26 08:43:29 +01:00
|
|
|
$this->libraries[H5PDevelopment::libraryToString($library['machineName'], $library['majorVersion'], $library['minorVersion'])] = $library;
|
|
|
|
}
|
2014-10-07 14:46:35 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
// TODO: Should we remove libraries without files? Not really needed, but must be cleaned up some time, right?
|
|
|
|
|
|
|
|
// Go trough libraries and insert dependencies. Missing deps. will just be ignored and not available. (I guess?!)
|
2014-11-27 23:14:15 +01:00
|
|
|
$this->h5pF->lockDependencyStorage();
|
2014-03-26 08:43:29 +01:00
|
|
|
foreach ($this->libraries as $library) {
|
|
|
|
$this->h5pF->deleteLibraryDependencies($library['libraryId']);
|
|
|
|
// This isn't optimal, but without it we would get duplicate warnings.
|
2014-04-14 10:50:37 +02:00
|
|
|
// TODO: You might get PDOExceptions if two or more requests does this at the same time!!
|
2014-03-26 08:43:29 +01:00
|
|
|
$types = array('preloaded', 'dynamic', 'editor');
|
|
|
|
foreach ($types as $type) {
|
|
|
|
if (isset($library[$type . 'Dependencies'])) {
|
|
|
|
$this->h5pF->saveLibraryDependencies($library['libraryId'], $library[$type . 'Dependencies'], $type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-27 23:14:15 +01:00
|
|
|
$this->h5pF->unlockDependencyStorage();
|
2014-03-26 08:43:29 +01:00
|
|
|
// TODO: Deps must be inserted into h5p_nodes_libraries as well... ? But only if they are used?!
|
|
|
|
}
|
2014-10-07 14:46:35 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
/**
|
2016-04-13 06:01:15 +02:00
|
|
|
* @return array Libraries in development folder.
|
2014-03-26 08:43:29 +01:00
|
|
|
*/
|
|
|
|
public function getLibraries() {
|
|
|
|
return $this->libraries;
|
|
|
|
}
|
2014-10-07 14:46:35 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
/**
|
|
|
|
* Get library
|
2014-10-07 14:46:35 +02:00
|
|
|
*
|
2014-03-26 08:43:29 +01:00
|
|
|
* @param string $name of the library.
|
|
|
|
* @param int $majorVersion of the library.
|
|
|
|
* @param int $minorVersion of the library.
|
|
|
|
* @return array library.
|
|
|
|
*/
|
|
|
|
public function getLibrary($name, $majorVersion, $minorVersion) {
|
|
|
|
$library = H5PDevelopment::libraryToString($name, $majorVersion, $minorVersion);
|
|
|
|
return isset($this->libraries[$library]) === TRUE ? $this->libraries[$library] : NULL;
|
|
|
|
}
|
2014-10-07 14:46:35 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
/**
|
|
|
|
* Get semantics for the given library.
|
2014-10-07 14:46:35 +02:00
|
|
|
*
|
2014-03-26 08:43:29 +01:00
|
|
|
* @param string $name of the library.
|
|
|
|
* @param int $majorVersion of the library.
|
|
|
|
* @param int $minorVersion of the library.
|
|
|
|
* @return string Semantics
|
|
|
|
*/
|
|
|
|
public function getSemantics($name, $majorVersion, $minorVersion) {
|
|
|
|
$library = H5PDevelopment::libraryToString($name, $majorVersion, $minorVersion);
|
|
|
|
if (isset($this->libraries[$library]) === FALSE) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-04-07 19:30:46 +02:00
|
|
|
return $this->getFileContents($this->filesPath . $this->libraries[$library]['path'] . '/semantics.json');
|
2014-03-26 08:43:29 +01:00
|
|
|
}
|
2014-10-07 14:46:35 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
/**
|
|
|
|
* Get translations for the given library.
|
2014-10-07 14:46:35 +02:00
|
|
|
*
|
2014-03-26 08:43:29 +01:00
|
|
|
* @param string $name of the library.
|
|
|
|
* @param int $majorVersion of the library.
|
|
|
|
* @param int $minorVersion of the library.
|
2016-04-13 06:01:15 +02:00
|
|
|
* @param $language
|
2014-03-26 08:43:29 +01:00
|
|
|
* @return string Translation
|
|
|
|
*/
|
2014-10-07 14:46:35 +02:00
|
|
|
public function getLanguage($name, $majorVersion, $minorVersion, $language) {
|
2014-03-26 08:43:29 +01:00
|
|
|
$library = H5PDevelopment::libraryToString($name, $majorVersion, $minorVersion);
|
2014-10-07 14:46:35 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
if (isset($this->libraries[$library]) === FALSE) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-10-07 14:46:35 +02:00
|
|
|
|
2015-04-07 19:30:46 +02:00
|
|
|
return $this->getFileContents($this->filesPath . $this->libraries[$library]['path'] . '/language/' . $language . '.json');
|
2014-03-26 08:43:29 +01:00
|
|
|
}
|
2014-10-07 14:46:35 +02:00
|
|
|
|
2014-03-26 08:43:29 +01:00
|
|
|
/**
|
|
|
|
* Writes library as string on the form "name majorVersion.minorVersion"
|
|
|
|
*
|
|
|
|
* @param string $name Machine readable library name
|
|
|
|
* @param integer $majorVersion
|
2016-04-13 06:01:15 +02:00
|
|
|
* @param $minorVersion
|
2014-03-26 08:43:29 +01:00
|
|
|
* @return string Library identifier.
|
|
|
|
*/
|
|
|
|
public static function libraryToString($name, $majorVersion, $minorVersion) {
|
|
|
|
return $name . ' ' . $majorVersion . '.' . $minorVersion;
|
|
|
|
}
|
|
|
|
}
|