MDL-80544 core_h5p: Add core lib changes after upgrading it

This commit is contained in:
Sara Arjona 2024-01-16 15:01:03 +01:00
parent 25f1712602
commit cda7b09ae1
No known key found for this signature in database
9 changed files with 53 additions and 27 deletions

View File

@ -1,5 +1,7 @@
<?php
namespace Moodle;
/**
* File info?
*/
@ -9,13 +11,13 @@
* operations using PHP's standard file operation functions.
*
* Some implementations of H5P that doesn't use the standard file system will
* want to create their own implementation of the \H5P\FileStorage interface.
* want to create their own implementation of the H5P\FileStorage interface.
*
* @package H5P
* @copyright 2016 Joubel AS
* @license MIT
*/
class H5PDefaultStorage implements \H5PFileStorage {
class H5PDefaultStorage implements H5PFileStorage {
private $path, $alteditorpath;
/**
@ -39,10 +41,10 @@ class H5PDefaultStorage implements \H5PFileStorage {
* Library properties
*/
public function saveLibrary($library) {
$dest = $this->path . '/libraries/' . \H5PCore::libraryToFolderName($library);
$dest = $this->path . '/libraries/' . H5PCore::libraryToFolderName($library);
// Make sure destination dir doesn't exist
\H5PCore::deleteFileTree($dest);
H5PCore::deleteFileTree($dest);
// Move library folder
self::copyFileTree($library['uploadDirectory'], $dest);
@ -64,7 +66,7 @@ class H5PDefaultStorage implements \H5PFileStorage {
$dest = "{$this->path}/content/{$content['id']}";
// Remove any old content
\H5PCore::deleteFileTree($dest);
H5PCore::deleteFileTree($dest);
self::copyFileTree($source, $dest);
}
@ -76,7 +78,7 @@ class H5PDefaultStorage implements \H5PFileStorage {
* Content properties
*/
public function deleteContent($content) {
\H5PCore::deleteFileTree("{$this->path}/content/{$content['id']}");
H5PCore::deleteFileTree("{$this->path}/content/{$content['id']}");
}
/**
@ -137,7 +139,7 @@ class H5PDefaultStorage implements \H5PFileStorage {
* Folder that library resides in
*/
public function exportLibrary($library, $target, $developmentPath=NULL) {
$folder = \H5PCore::libraryToFolderName($library);
$folder = H5PCore::libraryToFolderName($library);
$srcPath = ($developmentPath === NULL ? "/libraries/{$folder}" : $developmentPath);
self::copyFileTree("{$this->path}{$srcPath}", "{$target}/{$folder}");

View File

@ -1,5 +1,7 @@
<?php
namespace Moodle;
/**
* This is a data layer which uses the file system so it isn't specific to any framework.
*/

View File

@ -1,5 +1,7 @@
<?php
namespace Moodle;
/**
* The base class for H5P events. Extend to track H5P events in your system.
*

View File

@ -1,5 +1,7 @@
<?php
namespace Moodle;
/**
* File info?
*/

View File

@ -1,4 +1,7 @@
<?php
namespace Moodle;
/**
* Utility class for handling metadata
*/

View File

@ -1,4 +1,9 @@
<?php
namespace Moodle;
use ZipArchive;
/**
* Interface defining functions the h5p library needs the framework to implement
*/
@ -1650,12 +1655,14 @@ class H5PStorage {
H5PMetadata::boolifyAndEncodeSettings($library['metadataSettings']) :
NULL;
// Save library folder
$this->h5pC->fs->saveLibrary($library);
// MOODLE PATCH: The library needs to be saved in database first before creating the files, because the libraryid is used
// as itemid for the files.
// Update our DB
$this->h5pF->saveLibraryData($library, $new);
// Save library folder
$this->h5pC->fs->saveLibrary($library);
// Remove cached assets that uses this library
if ($this->h5pC->aggregateAssets && isset($library['libraryId'])) {
$removedKeys = $this->h5pF->deleteCachedAssets($library['libraryId']);
@ -2148,7 +2155,7 @@ class H5PCore {
*
* @param H5PFrameworkInterface $H5PFramework
* The frameworks implementation of the H5PFrameworkInterface
* @param string|\H5PFileStorage $path H5P file storage directory or class.
* @param string|H5PFileStorage $path H5P file storage directory or class.
* @param string $url To file storage directory.
* @param string $language code. Defaults to english.
* @param boolean $export enabled?
@ -2156,7 +2163,7 @@ class H5PCore {
public function __construct(H5PFrameworkInterface $H5PFramework, $path, $url, $language = 'en', $export = FALSE) {
$this->h5pF = $H5PFramework;
$this->fs = ($path instanceof \H5PFileStorage ? $path : new \H5PDefaultStorage($path));
$this->fs = ($path instanceof H5PFileStorage ? $path : new H5PDefaultStorage($path));
$this->url = $url;
$this->exportEnabled = $export;
@ -3323,21 +3330,22 @@ class H5PCore {
* @return string
*/
private static function hashToken($action, $time_factor) {
if (!isset($_SESSION['h5p_token'])) {
global $SESSION;
if (!isset($SESSION->h5p_token)) {
// Create an unique key which is used to create action tokens for this session.
if (function_exists('random_bytes')) {
$_SESSION['h5p_token'] = base64_encode(random_bytes(15));
$SESSION->h5p_token = base64_encode(random_bytes(15));
}
else if (function_exists('openssl_random_pseudo_bytes')) {
$_SESSION['h5p_token'] = base64_encode(openssl_random_pseudo_bytes(15));
$SESSION->h5p_token = base64_encode(openssl_random_pseudo_bytes(15));
}
else {
$_SESSION['h5p_token'] = uniqid('', TRUE);
$SESSION->h5p_token = uniqid('', TRUE);
}
}
// Create hash and return
return substr(hash('md5', $action . $time_factor . $_SESSION['h5p_token']), -16, 13);
return substr(hash('md5', $action . $time_factor . $SESSION->h5p_token), -16, 13);
}
/**

View File

@ -1040,7 +1040,7 @@ H5P.t = function (key, vars, ns) {
* @param {H5P.jQuery} $element
* Which DOM element the dialog should be inserted after.
* @param {H5P.jQuery} $returnElement
* Which DOM element the focus should be moved to on close
* Which DOM element the focus should be moved to on close
*/
H5P.Dialog = function (name, title, content, $element, $returnElement) {
/** @alias H5P.Dialog# */
@ -1894,7 +1894,7 @@ H5P.MediaCopyright = function (copyright, labels, order, extraFields) {
* @param {string} source
* @param {number} width
* @param {number} height
* @param {string} alt
* @param {string} alt
* alternative text for the thumbnail
*/
H5P.Thumbnail = function (source, width, height, alt) {
@ -2373,6 +2373,11 @@ H5P.createTitle = function (rawTitle, maxLength) {
done('Not signed in.');
return;
}
// Moodle patch to let override this method.
if (H5P.contentUserDataAjax !== undefined) {
return H5P.contentUserDataAjax(contentId, dataType, subContentId, done, data, preload, invalidate, async);
}
// End of Moodle patch.
var options = {
url: H5PIntegration.ajax.contentUserData.replace(':contentId', contentId).replace(':dataType', dataType).replace(':subContentId', subContentId ? subContentId : 0),
@ -2716,7 +2721,7 @@ H5P.createTitle = function (rawTitle, maxLength) {
}
return path.substr(0, prefix.length) === prefix ? path : prefix + path;
}
return path; // Will automatically be looked for in tmp folder
});

View File

@ -3,6 +3,9 @@ H5P PHP library
Downloaded last release from: https://github.com/h5p/h5p-php-library/tags
When no new tags are released, a specific commit can also be used. In that case, the version number used in the thirdpartylibs.xml
will be <branch name>-<commit hash>. For instance, master-f3579c0.
Import procedure:
* Remove the content in this folder (but the readme_moodle.txt)
* Copy all the files from the folder repository in this directory.
@ -13,6 +16,11 @@ Removed:
* .travis.yml
Changed:
0. Open the new version of joubel/core/h5p.classes.php and at the beginning of the H5PCore class (around line 2082), check the
value of the coreApi minor and major versions. If they are different from the values in the current Moodle version, instead of
upgrading the library in the current h5plib_vxxx, a new h5plib_vX.Y should be released (as it was done in MDL-80544). The new
h5plib module should be named taking into account that X is the coreApi.majorVersion and Y is the coreApi.minorVersion.
1. Replace the $_SESSION references with $SESSION. That implies that the information is saved to backends, so only the Moodle one
should be used by core (core should be free from $_SESSION and always use $SESSION).
More specifically, in h5p.classes.php file, into hashToken() method:
@ -50,9 +58,3 @@ instance, if a new method is added to h5p-file-storage.interface.php, it should
// End of Moodle patch.
var options = {
Notes:
* 2023-05-10 To avoid PHP 8.2 deprecations, please apply below patches:
- https://github.com/h5p/h5p-php-library/pull/146
- https://github.com/h5p/h5p-php-library/pull/148
See MDL-78147 for more details.

View File

@ -4,7 +4,7 @@
<location>joubel/core</location>
<name>h5p-php-library</name>
<description>The general H5P library.</description>
<version>moodle-1.23</version>
<version>1.26</version>
<license>GPL</license>
<licenseversion>3.0+</licenseversion>
<repository>https://github.com/h5p/h5p-php-library/</repository>