Skip to content

Commit

Permalink
Update h5p packages (#2291)
Browse files Browse the repository at this point in the history
* Update h5p/h5p-core and h5p/h5p-editor Composer packages, use patch version in library folder name
  • Loading branch information
chrieinv authored Sep 28, 2023
1 parent ed5bb39 commit 6281b75
Show file tree
Hide file tree
Showing 49 changed files with 3,372 additions and 116 deletions.
2 changes: 2 additions & 0 deletions sourcecode/apis/contentauthor/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ Homestead.json
!/public/js/videos/brightcove.js
!/public/js/videos/streamps.js
!/public/js/h5p/ndlah5p-youtube.js
!public/js/h5p/core-override
!/public/js/h5p/h5peditor-pre-save.js
!public/css
/public/css/*
!/public/css/ndlah5p-youtube.css

mix-manifest.json
app/Libraries/oauth-php/example/server/cache/
ca1.sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function handle(): void

private static function getDestination(H5PLibrary $library): string
{
$directory = $library->getLibraryString(true);
$directory = $library->getFolderName();

return "libraries/$directory/presave.js";
}
Expand Down
9 changes: 1 addition & 8 deletions sourcecode/apis/contentauthor/app/H5PContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,7 @@ public static function getContentTypeInfo(string $contentType): ?ContentTypeData

if ($library->has_icon) {
$h5pFramework = app(H5PFrameworkInterface::class);

$library_folder = H5PCore::libraryToString([
'machineName' => $library->machine_name,
'majorVersion' => $library->major_version,
'minorVersion' => $library->minor_version
], true);


$library_folder = $library->getFolderName();
$icon_path = $h5pFramework->getLibraryFileUrl($library_folder, 'icon.svg');

if (!empty($icon_path)) {
Expand Down
16 changes: 11 additions & 5 deletions sourcecode/apis/contentauthor/app/H5PLibrariesHubCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,16 @@ public function libraries(): HasMany

public function getLibraryString($folderName = false)
{
return \H5PCore::libraryToString([
'machineName' => $this->name,
'majorVersion' => $this->major_version,
'minorVersion' => $this->minor_version,
], $folderName);
return $folderName ?
\H5PCore::libraryToFolderName([
'machineName' => $this->name,
'majorVersion' => $this->major_version,
'minorVersion' => $this->minor_version,
]) :
\H5PCore::libraryToString([
'machineName' => $this->name,
'majorVersion' => $this->major_version,
'minorVersion' => $this->minor_version,
]);
}
}
77 changes: 72 additions & 5 deletions sourcecode/apis/contentauthor/app/H5PLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class H5PLibrary extends Model

protected $guarded = ['id'];

protected $casts = [
'patch_version_in_folder_name' => 'bool',
];

protected static function boot(): void
{
parent::boot();
Expand Down Expand Up @@ -144,23 +148,86 @@ public function getVersions($asModels = false)
return $asModels !== true ? $versions : $this->hydrate($versions->toArray());
}

public function getLibraryString($folderName = false)
/**
* @param bool|null $withPatchVersion Null to use patchVersionInFolderName value to decide or true/false to force
*/
public function getLibraryString(?bool $withPatchVersion = null): string
{
return self::getLibraryName($this->getLibraryH5PFriendly(), false, $withPatchVersion);
}

/**
* @param bool|null $withPatchVersion Null to use patchVersionInFolderName value to decide or true/false to force
*/
public function getFolderName(?bool $withPatchVersion = null): string
{
return \H5PCore::libraryToString($this->getLibraryH5PFriendly(), $folderName);
return self::getLibraryName($this->getLibraryH5PFriendly(), true, $withPatchVersion);
}

public function getLibraryH5PFriendly($machineName = 'name')
/**
* @param array{machineName?: string, name?: string, majorVersion: int, minorVersion: int, patchVersion: int, patchVersionInFolderName: bool} $libraryData
* @param bool|null $withPatchVersion Null to use patchVersionInFolderName value to decide or true/false to force
* @throws \InvalidArgumentException If requesting full version without patchVersion present in data
*/
public static function libraryToFolderName(array $libraryData, ?bool $withPatchVersion = null): string
{
return self::getLibraryName($libraryData, true, $withPatchVersion);
}

/**
* @param array{machineName?: string, name?: string, majorVersion: int, minorVersion: int, patchVersion: int, patchVersionInFolderName: bool} $libraryData
* @param bool|null $withPatchVersion Null to use patchVersionInFolderName value to decide or true/false to force
* @throws \InvalidArgumentException If requesting full version without patchVersion present in data
*/
public static function libraryToString(array $libraryData, ?bool $withPatchVersion = null): string
{
return self::getLibraryName($libraryData, false, $withPatchVersion);
}

/**
* @throws \InvalidArgumentException If requesting full version without patchVersion present in data
*/
private static function getLibraryName(array $libraryData, bool $asFolder, ?bool $withPatchVersion): string
{
$usePatch = $withPatchVersion === true || ($withPatchVersion === null && array_key_exists('patchVersionInFolderName', $libraryData) && $libraryData['patchVersionInFolderName']);
if ($usePatch && !isset($libraryData['patchVersion'])) {
throw new \InvalidArgumentException('Full version name requested but patch version missing');
}

if ($usePatch) {
$format = $asFolder ? '%s-%d.%d.%d' : '%s %d.%d.%d';
} else {
$format = $asFolder ? '%s-%d.%d' : '%s %d.%d';
}

return sprintf(
$format,
$libraryData['machineName'] ?? $libraryData['name'],
$libraryData['majorVersion'],
$libraryData['minorVersion'],
$libraryData['patchVersion'] ?? ''
);
}

public function getLibraryH5PFriendly($machineName = 'name'): array
{
return [
'machineName' => $this->$machineName,
'majorVersion' => $this->major_version,
'minorVersion' => $this->minor_version,
'patchVersion' => $this->patch_version,
'patchVersionInFolderName' => $this->patch_version_in_folder_name,
];
}

public function getTitleAndVersionString()
{
return \H5PCore::libraryToString($this->getLibraryH5PFriendly('title'));
return self::getLibraryName([
'machineName' => $this->title,
'majorVersion' => $this->major_version,
'minorVersion' => $this->minor_version,
'patchVersion' => $this->patch_version,
], false, true);
}

/**
Expand Down Expand Up @@ -249,7 +316,7 @@ public function getAddons()

public function supportsMaxScore(): bool
{
$libraryLocation = sprintf('libraries/%s/presave.js', self::getLibraryString(true));
$libraryLocation = sprintf('libraries/%s/presave.js', self::getFolderName());
if (Storage::disk()->exists($libraryLocation)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(

public function checkLibrary(H5PLibrary $library): View
{
$h5pDataFolderName = $library->getLibraryString(true);
$h5pDataFolderName = $library->getFolderName();
$tmpLibrariesRelative = 'libraries';
$tmpLibraryRelative = 'libraries/' . $h5pDataFolderName;
// Download files from bucket to tmp folder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public function edit(Request $request, int $id): View

$state = H5PStateDataObject::create($displayOptions + [
'id' => $h5pContent->id,
'library' => $library->getLibraryString(),
'library' => $library->getLibraryString(false),
'libraryid' => $h5pContent->library_id,
'parameters' => $params,
'language_iso_639_3' => $contentLanguage,
Expand Down
14 changes: 7 additions & 7 deletions sourcecode/apis/contentauthor/app/Libraries/H5P/AjaxRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ private function libraryRebuild(Request $request): array

$libraries = collect();
$this->getLibraryDetails($H5PLibrary, $libraries);
if ($libraries->has($H5PLibrary->getLibraryString())) {
$libraryData = $libraries->get($H5PLibrary->getLibraryString());
if ($libraries->has($H5PLibrary->getLibraryString(false))) {
$libraryData = $libraries->get($H5PLibrary->getLibraryString(false));
if (array_key_exists('semantics', $libraryData)) {
$H5PLibrary->semantics = $libraryData['semantics'];
$H5PLibrary->save();
Expand Down Expand Up @@ -292,7 +292,7 @@ private function getLibraryDetails(H5PLibrary $H5PLibrary, Collection $affectedL
{
/** @var H5PValidator $validator */
$validator = resolve(H5PValidator::class);
$h5pDataFolderName = $H5PLibrary->getLibraryString(true);
$h5pDataFolderName = $H5PLibrary->getFolderName();
$tmpLibrariesRelative = 'libraries';
$tmpLibraryRelative = 'libraries/' . $h5pDataFolderName;
// Download files from bucket to tmp folder
Expand All @@ -304,18 +304,18 @@ private function getLibraryDetails(H5PLibrary $H5PLibrary, Collection $affectedL
);
$tmpLibraries = $this->core->h5pF->getH5pPath($tmpLibrariesRelative);
$tmpLibraryFolder = $this->core->h5pF->getH5pPath($tmpLibraryRelative);
$libraryData = $validator->getLibraryData($H5PLibrary->getLibraryString(true), $tmpLibraryFolder, $tmpLibraries);
$libraryData = $validator->getLibraryData($H5PLibrary->getFolderName(), $tmpLibraryFolder, $tmpLibraries);
$libraryData['libraryId'] = $H5PLibrary->id;

if (!$affectedLibraries->has($H5PLibrary->getLibraryString())) {
$affectedLibraries->put($H5PLibrary->getLibraryString(), $libraryData);
if (!$affectedLibraries->has($H5PLibrary->getLibraryString(false))) {
$affectedLibraries->put($H5PLibrary->getLibraryString(false), $libraryData);
}
foreach (['preloadedDependencies', 'dynamicDependencies', 'editorDependencies'] as $value) {
if (!empty($libraryData[$value])) {
foreach ($libraryData[$value] as $library) {
/** @var H5PLibrary $dependentLibrary */
$dependentLibrary = H5PLibrary::fromLibrary($library)->first();
if (!$affectedLibraries->has($dependentLibrary->getLibraryString())) {
if (!$affectedLibraries->has($dependentLibrary->getLibraryString(false))) {
$affectedLibraries = $this->getLibraryDetails($dependentLibrary, $affectedLibraries);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function getTranslations($libraries, $language_code)
})
->get()
->mapWithKeys(function ($library) {
return [$library->library->getLibraryString() => $library->translation];
return [$library->library->getLibraryString(false) => $library->translation];
})
->toArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function getLibraries($libraries = null)
->map(function ($h5pLibrary) {
/** @var H5PLibrary $h5pLibrary */
$library = [
'uberName' => $h5pLibrary->getLibraryString(),
'uberName' => $h5pLibrary->getLibraryString(false),
'name' => $h5pLibrary->name,
'majorVersion' => $h5pLibrary->major_version,
'minorVersion' => $h5pLibrary->minor_version,
Expand Down Expand Up @@ -101,7 +101,7 @@ public function getLibraries($libraries = null)
$library->minorVersion = $library->minor_version;

// Add new library
$library->uberName = $library->getLibraryString();
$library->uberName = $library->getLibraryString(false);
if ($index > 0) {
$library->isOld = true;
}
Expand Down
60 changes: 55 additions & 5 deletions sourcecode/apis/contentauthor/app/Libraries/H5P/Framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Stringable;
use InvalidArgumentException;
use PDO;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -132,19 +133,35 @@ public function getPlatformInfo()
];
}

public function fetchExternalData($url, $data = null, $blocking = true, $stream = null): string|null
{
public function fetchExternalData(
$url,
$data = null,
$blocking = true,
$stream = null,
$fullData = false,
$headers = [],
$files = [],
$method = 'POST'
): string|array|null {
$method = $data ? 'POST' : 'GET';
$options = [RequestOptions::FORM_PARAMS => $data];
if ($stream !== null) {
$options[RequestOptions::SINK] = $stream;
}
$options[RequestOptions::HEADERS] = $headers;

return $this->httpClient->requestAsync($method, $url, $options)
->then(static function (ResponseInterface $response) use ($blocking) {
->then(static function (ResponseInterface $response) use ($blocking, $fullData) {
if (!$blocking) {
return null;
}
if ($fullData) {
return [
'status' => $response->getStatusCode(),
'headers' => $response->getHeaders(),
'data' => $response->getBody()->getContents(),
];
}

return $response->getBody()->getContents();
})
Expand Down Expand Up @@ -490,7 +507,8 @@ public function saveLibraryData(&$libraryData, $new = true)
'metadata_settings' => $libraryData['metadataSettings'],
'add_to' => $libraryData['addTo'],
'has_icon' => $libraryData['hasIcon'] ?? 0,
'tutorial_url' => ''
'tutorial_url' => '',
'patch_version_in_folder_name' => true,
]);
$libraryData['libraryId'] = $h5pLibrary->id;

Expand Down Expand Up @@ -809,13 +827,16 @@ public function loadLibrary($machineName, $majorVersion, $minorVersion): array|f
'preloadedCss' => $h5pLibrary->preloaded_css,
'dropLibraryCss' => $h5pLibrary->drop_library_css,
'semantics' => $h5pLibrary->semantics,
'patchVersionInFolderName' => $h5pLibrary->patch_version_in_folder_name,
];

foreach ($h5pLibrary->libraries as $dependency) {
$library[$dependency->dependency_type . 'Dependencies'][] = [
'machineName' => $dependency->requiredLibrary->name,
'majorVersion' => $dependency->requiredLibrary->major_version,
'minorVersion' => $dependency->requiredLibrary->minor_version,
'patchVersion' => $dependency->requiredLibrary->patch_version,
'patchVersionInFolderName' => $dependency->requiredLibrary->patch_version_in_folder_name,
];
}

Expand Down Expand Up @@ -897,10 +918,11 @@ public function deleteLibrary($library): void
throw new TypeError(sprintf('Expected object, %s given', get_debug_type($library)));
}

/** @var H5PLibrary $libraryModel */
$libraryModel = H5PLibrary::findOrFail($library->id);
$libraryModel->deleteOrFail();

app(CerpusStorageInterface::class)->deleteLibrary($libraryModel);
app(\H5PFileStorage::class)->deleteLibrary($libraryModel->getLibraryH5PFriendly());
}

/**
Expand Down Expand Up @@ -943,6 +965,8 @@ public function loadContent($id)
'libraryName' => $h5pcontent->library->name,
'libraryMajorVersion' => $h5pcontent->library->major_version,
'libraryMinorVersion' => $h5pcontent->library->minor_version,
'libraryPatchVersion' => $h5pcontent->library->patch_version,
'libraryFullVersionName' => $h5pcontent->library->getLibraryString(),
'libraryEmbedTypes' => $h5pcontent->library->embed_types,
'libraryFullscreen' => $h5pcontent->library->fullscreen,
'language' => $h5pcontent->metadata->default_language ?? null,
Expand Down Expand Up @@ -976,6 +1000,8 @@ public function loadContent($id)
* - preloadedJs(optional): comma separated string with js file paths
* - preloadedCss(optional): comma separated sting with css file paths
* - dropCss(optional): csv of machine names
* - dependencyType: editor or preloaded
* - patchVersionInFolderName: Is patch version a part of the folder name
*/
public function loadContentDependencies($id, $type = null)
{
Expand All @@ -993,6 +1019,7 @@ public function loadContentDependencies($id, $type = null)
, hl.preloaded_js AS preloadedJs
, hcl.drop_css AS dropCss
, hcl.dependency_type AS dependencyType
, hl.patch_version_in_folder_name AS patchVersionInFolderName
FROM h5p_contents_libraries hcl
JOIN h5p_libraries hl ON hcl.library_id = hl.id
WHERE hcl.content_id = ?";
Expand Down Expand Up @@ -1324,4 +1351,27 @@ public function libraryHasUpgrade($library)
$h5pLibrary = H5PLibrary::fromLibrary($library)->first();
return !is_null($h5pLibrary) && $h5pLibrary->isUpgradable();
}

public function replaceContentHubMetadataCache($metadata, $lang)
{
// H5P Content Hub is not in use
}

public function getContentHubMetadataCache($lang = 'en')
{
// H5P Content Hub is not in use
return new Stringable();
}

public function getContentHubMetadataChecked($lang = 'en')
{
// H5P Content Hub is not in use
return now()->toRfc7231String();
}

public function setContentHubMetadataChecked($time, $lang = 'en')
{
// H5P Content Hub is not in use
return true;
}
}
Loading

0 comments on commit 6281b75

Please sign in to comment.