Adapted and simplified export, makes it easier to implement on new systems and improves performance.

namespaces
Frode Petterson 2014-05-07 16:43:48 +02:00
parent 34d65b9601
commit 48b07c46f5
1 changed files with 32 additions and 76 deletions

View File

@ -247,33 +247,6 @@ interface H5PFrameworkInterface {
*/ */
public function deleteLibrary($libraryId); public function deleteLibrary($libraryId);
/**
* Get all the data we need to export H5P
*
* @param int $contentId
* ContentID of the node we are going to export
* @return array
* An array with all the data needed to export the h5p in the following format:
* 'contentId' => string/int,
* 'mainLibrary' => string (machine name for main library),
* 'embedType' => string,
* 'libraries' => array(
* 'machineName' => string,
* 'majorVersion' => int,
* 'minorVersion' => int,
* 'preloaded' => int(0|1),
* 'editorLibraries' => array(
* 'machineName' => string,
* 'majorVersion' => int,
* 'minorVersion' => int,
* 'preloaded' => int(0|1),
*/
public function getExportData($contentId);
/**
* Check if export is enabled.
*/
public function isExportEnabled();
/** /**
* Load content. * Load content.
* *
@ -1147,76 +1120,58 @@ Class H5PExport {
* @return string * @return string
* Path to .h5p file * Path to .h5p file
*/ */
public function getExportPath($contentId, $title, $language) { public function getExportPath($content) {
$h5pDir = $this->h5pF->getH5pPath() . DIRECTORY_SEPARATOR; $h5pDir = $this->h5pF->getH5pPath() . DIRECTORY_SEPARATOR;
$tempPath = $h5pDir . 'temp' . DIRECTORY_SEPARATOR . $contentId; $tempPath = $h5pDir . 'tmp' . DIRECTORY_SEPARATOR . $content['id'];
$zipPath = $h5pDir . 'exports' . DIRECTORY_SEPARATOR . $contentId . '.h5p'; $zipPath = $h5pDir . 'exports' . DIRECTORY_SEPARATOR . $content['id'] . '.h5p';
// Check if h5p-package already exists. // Check if h5p-package already exists.
if (!file_exists($zipPath)) { if (!file_exists($zipPath)) {
// Temp dir to put the h5p files in // Temp dir to put the h5p files in
@mkdir($tempPath); @mkdir($tempPath, 0777, TRUE);
$exportData = $this->h5pF->getExportData($contentId); @mkdir($h5pDir . 'exports', 0777, TRUE);
// Create content folder // Create content folder
if ($this->h5pC->copyFileTree($h5pDir . 'content' . DIRECTORY_SEPARATOR . $contentId, $tempPath . DIRECTORY_SEPARATOR . 'content') === FALSE) { if ($this->h5pC->copyFileTree($h5pDir . 'content' . DIRECTORY_SEPARATOR . $content['id'], $tempPath . DIRECTORY_SEPARATOR . 'content') === FALSE) {
return FALSE; return FALSE;
} }
file_put_contents($tempPath . DIRECTORY_SEPARATOR . 'content' . DIRECTORY_SEPARATOR . 'content.json', $exportData['jsonContent']); file_put_contents($tempPath . DIRECTORY_SEPARATOR . 'content' . DIRECTORY_SEPARATOR . 'content.json', $content['params']);
// Make embedTypes into an array // Make embedTypes into an array
$embedTypes = explode(', ', $exportData['embedType']); $embedTypes = explode(', ', $content['embedType']); // Won't content always be embedded in one way?
// Build h5p.json // Build h5p.json
$h5pJson = array ( $h5pJson = array (
'title' => $title, 'title' => $content['title'],
'language' => $language ? $language : 'und', 'language' => $content['language'],
'mainLibrary' => $exportData['mainLibrary'], 'mainLibrary' => $content['library']['name'],
'embedTypes' => $embedTypes, 'embedTypes' => $embedTypes,
); );
// Copies libraries to temp dir and create mention in h5p.json // Add dependencies to h5p
foreach($exportData['libraries'] as $library) { $dependencies = $this->h5pC->loadContentDependencies($content['id']);
// Set preloaded and dynamic dependencies foreach ($dependencies as $dependency) {
if ($library['dependencyType'] == 'preloaded') { // Copy library to h5p
$preloadedDependencies[] = array( $source = isset($dependency['path']) ? $dependency['path'] : $h5pDir . 'libraries' . DIRECTORY_SEPARATOR . H5PCore::libraryToString($dependency, TRUE);
'machineName' => $library['machineName'], $destination = $tempPath . DIRECTORY_SEPARATOR . $dependency['machineName'];
'majorVersion' => $library['majorVersion'], $this->h5pC->copyFileTree($source, $destination);
'minorVersion' => $library['minorVersion'],
);
} elseif ($library['dependencyType'] == 'dynamic') {
$dynamicDependencies[] = array(
'machineName' => $library['machineName'],
'majorVersion' => $library['majorVersion'],
'minorVersion' => $library['minorVersion'],
);
}
}
// Add preloaded and dynamic dependencies if they exist // Do not add editor dependencies to h5p json.
if (isset($preloadedDependencies)) { $h5pJson['preloadedDependencies'] = $preloadedDependencies; } if ($dependency['dependencyType'] === 'editor') {
if (isset($dynamicDependencies)) { $h5pJson['dynamicDependencies'] = $dynamicDependencies; } continue;
}
$h5pJson[$dependency['dependencyType'] . 'Dependencies'][] = array(
'machineName' => $dependency['machineName'],
'majorVersion' => $dependency['majorVersion'],
'minorVersion' => $dependency['minorVersion']
);
}
// Save h5p.json // Save h5p.json
$results = print_r(json_encode($h5pJson), true); $results = print_r(json_encode($h5pJson), true);
file_put_contents($tempPath . DIRECTORY_SEPARATOR . 'h5p.json', $results); file_put_contents($tempPath . DIRECTORY_SEPARATOR . 'h5p.json', $results);
// Copies libraries to temp dir and create mention in h5p.json
foreach($exportData['libraries'] as $library) {
$source = NULL;
if ($this->h5pC->development_mode & H5PDevelopment::MODE_LIBRARY) {
$devlib = $this->h5pC->h5pD->getLibrary($library['machineName'], $library['majorVersion'], $library['minorVersion']);
if ($devlib !== NULL) {
$source = $devlib['path'];
}
}
if ($source === NULL) {
$source = $h5pDir . 'libraries' . DIRECTORY_SEPARATOR . $library['machineName'] . '-' . $library['majorVersion'] . '.' . $library['minorVersion'];
}
$destination = $tempPath . DIRECTORY_SEPARATOR . $library['machineName'];
$this->h5pC->copyFileTree($source, $destination);
}
// Create new zip instance. // Create new zip instance.
$zip = new ZipArchive(); $zip = new ZipArchive();
$zip->open($zipPath, ZIPARCHIVE::CREATE); $zip->open($zipPath, ZIPARCHIVE::CREATE);
@ -1374,12 +1329,13 @@ class H5PCore {
foreach ($dependencies as $key => $dependency) { foreach ($dependencies as $key => $dependency) {
$libraryString = H5PCore::libraryToString($dependency); $libraryString = H5PCore::libraryToString($dependency);
if (isset($developmentLibraries[$libraryString])) { if (isset($developmentLibraries[$libraryString])) {
$developmentLibraries[$libraryString]['dependencyType'] = $dependencies[$key]['dependencyType'];
$dependencies[$key] = $developmentLibraries[$libraryString]; $dependencies[$key] = $developmentLibraries[$libraryString];
} }
} }
} }
return $this->getDependenciesFiles($dependencies); return $dependencies;
} }
/** /**