Recreate .h5p when content cache is rebuilt. Avoid loading content dependencies multiple times.

namespaces
Frode Petterson 2014-05-27 14:05:20 +02:00
parent 43d239c6f1
commit 2053b43024
1 changed files with 78 additions and 79 deletions

View File

@ -1152,22 +1152,14 @@ Class H5PExport {
* *
* Creates package if not already created * Creates package if not already created
* *
* @param int/string $contentId * @param array $content
* Identifier for this H5P
* @param String $title
* Title of H5P
* @param string $language
* Language code for H5P
* @return string * @return string
* Path to .h5p file
*/ */
public function getExportPath($content) { public function createExportFile($content) {
$h5pDir = $this->h5pF->getH5pPath() . DIRECTORY_SEPARATOR; $h5pDir = $this->h5pF->getH5pPath() . DIRECTORY_SEPARATOR;
$tempPath = $h5pDir . 'temp' . DIRECTORY_SEPARATOR . $content['id']; $tempPath = $h5pDir . 'temp' . DIRECTORY_SEPARATOR . $content['id'];
$zipPath = $h5pDir . 'exports' . DIRECTORY_SEPARATOR . $content['id'] . '.h5p'; $zipPath = $h5pDir . 'exports' . DIRECTORY_SEPARATOR . $content['id'] . '.h5p';
// Check if h5p-package already exists.
if (!file_exists($zipPath)) {
// Temp dir to put the h5p files in // Temp dir to put the h5p files in
@mkdir($tempPath, 0777, TRUE); @mkdir($tempPath, 0777, TRUE);
@mkdir($h5pDir . 'exports', 0777, TRUE); @mkdir($h5pDir . 'exports', 0777, TRUE);
@ -1190,22 +1182,23 @@ Class H5PExport {
); );
// Add dependencies to h5p // Add dependencies to h5p
$dependencies = $this->h5pC->loadContentDependencies($content['id']); foreach ($content['dependencies'] as $dependency) {
foreach ($dependencies as $dependency) { $library = $dependency['library'];
// Copy library to h5p // Copy library to h5p
$source = isset($dependency['path']) ? $dependency['path'] : $h5pDir . 'libraries' . DIRECTORY_SEPARATOR . H5PCore::libraryToString($dependency, TRUE); $source = isset($library['path']) ? $library['path'] : $h5pDir . 'libraries' . DIRECTORY_SEPARATOR . H5PCore::libraryToString($library, TRUE);
$destination = $tempPath . DIRECTORY_SEPARATOR . $dependency['machineName']; $destination = $tempPath . DIRECTORY_SEPARATOR . $library['machineName'];
$this->h5pC->copyFileTree($source, $destination); $this->h5pC->copyFileTree($source, $destination);
// Do not add editor dependencies to h5p json. // Do not add editor dependencies to h5p json.
if ($dependency['dependencyType'] === 'editor') { if ($dependency['type'] === 'editor') {
continue; continue;
} }
$h5pJson[$dependency['dependencyType'] . 'Dependencies'][] = array( $h5pJson[$dependency['type'] . 'Dependencies'][] = array(
'machineName' => $dependency['machineName'], 'machineName' => $library['machineName'],
'majorVersion' => $dependency['majorVersion'], 'majorVersion' => $library['majorVersion'],
'minorVersion' => $dependency['minorVersion'] 'minorVersion' => $library['minorVersion']
); );
} }
@ -1220,7 +1213,7 @@ Class H5PExport {
// Get all files and folders in $tempPath // Get all files and folders in $tempPath
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tempPath . DIRECTORY_SEPARATOR)); $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tempPath . DIRECTORY_SEPARATOR));
// Add files to zip // Add files to zip
foreach ($iterator as $key=>$value) { foreach ($iterator as $key => $value) {
$test = '.'; $test = '.';
// Do not add the folders '.' and '..' to the zip. This will make zip invalid. // Do not add the folders '.' and '..' to the zip. This will make zip invalid.
if (substr_compare($key, $test, -strlen($test), strlen($test)) !== 0) { if (substr_compare($key, $test, -strlen($test), strlen($test)) !== 0) {
@ -1235,9 +1228,6 @@ Class H5PExport {
H5PCore::deleteFileTree($tempPath); H5PCore::deleteFileTree($tempPath);
} }
return str_replace(DIRECTORY_SEPARATOR, '/', $zipPath);
}
/** /**
* Delete .h5p file * Delete .h5p file
* *
@ -1297,6 +1287,7 @@ class H5PCore {
public static $defaultLibraryWhitelistExtras = 'js css'; public static $defaultLibraryWhitelistExtras = 'js css';
public $librariesJsonData, $contentJsonData, $mainJsonData, $h5pF, $path, $development_mode, $h5pD; public $librariesJsonData, $contentJsonData, $mainJsonData, $h5pF, $path, $development_mode, $h5pD;
private $exportEnabled;
/** /**
* Constructor for the H5PCore * Constructor for the H5PCore
@ -1305,13 +1296,15 @@ class H5PCore {
* The frameworks implementation of the H5PFrameworkInterface * The frameworks implementation of the H5PFrameworkInterface
* @param string $path H5P file storage directory. * @param string $path H5P file storage directory.
* @param string $language code. Defaults to english. * @param string $language code. Defaults to english.
* @param boolean $export enabled?
* @param int $development_mode mode. * @param int $development_mode mode.
*/ */
public function __construct($H5PFramework, $path, $language = 'en', $development_mode = H5PDevelopment::MODE_NONE) { public function __construct($H5PFramework, $path, $language = 'en', $export = FALSE, $development_mode = H5PDevelopment::MODE_NONE) {
$this->h5pF = $H5PFramework; $this->h5pF = $H5PFramework;
$this->h5pF = $H5PFramework; $this->h5pF = $H5PFramework;
$this->path = $path; $this->path = $path;
$this->exportEnabled = $export;
$this->development_mode = $development_mode; $this->development_mode = $development_mode;
if ($development_mode & H5PDevelopment::MODE_LIBRARY) { if ($development_mode & H5PDevelopment::MODE_LIBRARY) {
@ -1398,11 +1391,17 @@ class H5PCore {
$params = json_encode($params->params); $params = json_encode($params->params);
// Update content dependencies. // Update content dependencies.
$dependencies = $validator->getDependencies(); $content['dependencies'] = $validator->getDependencies();
$this->h5pF->deleteLibraryUsage($content['id']); $this->h5pF->deleteLibraryUsage($content['id']);
$this->h5pF->saveLibraryUsage($content['id'], $dependencies); $this->h5pF->saveLibraryUsage($content['id'], $content['dependencies']);
// TODO: Create new export if enabled. if ($this->exportEnabled) {
// Recreate export file
$exporter = new H5PExport($this->h5pF, $this);
$exporter->createExportFile($content);
// TODO: Should we rather create the file once first accessed, like imagecache?
}
// Cache. // Cache.
$this->h5pF->cacheSet('parameters', $content['id'], $params); $this->h5pF->cacheSet('parameters', $content['id'], $params);