diff --git a/h5p/classes/file_storage.php b/h5p/classes/file_storage.php index 675645c40a0..c1e24ade3c7 100644 --- a/h5p/classes/file_storage.php +++ b/h5p/classes/file_storage.php @@ -561,13 +561,23 @@ class file_storage implements \H5PFileStorage { * @param array $library Library details */ public function delete_library(array $library): void { + global $DB; // A library ID of false would result in all library files being deleted, which we don't want. Return instead. if ($library['libraryId'] === false) { return; } + $areafiles = $this->fs->get_area_files($this->context->id, self::COMPONENT, self::LIBRARY_FILEAREA, $library['libraryId']); $this->delete_directory($this->context->id, self::COMPONENT, self::LIBRARY_FILEAREA, $library['libraryId']); + $librarycache = \cache::make('core', 'h5p_library_files'); + foreach ($areafiles as $file) { + if (!$DB->record_exists('files', array('contenthash' => $file->get_contenthash(), + 'component' => self::COMPONENT, + 'filearea' => self::LIBRARY_FILEAREA))) { + $librarycache->delete($file->get_contenthash()); + } + } } /** @@ -590,6 +600,7 @@ class file_storage implements \H5PFileStorage { * @param array $options File system information. */ private function copy_directory(string $source, array $options): void { + $librarycache = \cache::make('core', 'h5p_library_files'); $it = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST); @@ -607,7 +618,13 @@ class file_storage implements \H5PFileStorage { $options['filepath'] = $root; } - $this->fs->create_file_from_pathname($options, $item->getPathName()); + $file = $this->fs->create_file_from_pathname($options, $item->getPathName()); + + if ($options['filearea'] == self::LIBRARY_FILEAREA) { + if (!$librarycache->has($file->get_contenthash())) { + $librarycache->set($file->get_contenthash(), file_get_contents($item->getPathName())); + } + } } $it->next(); } @@ -629,12 +646,24 @@ class file_storage implements \H5PFileStorage { // Read source files. $files = $this->fs->get_directory_files($contextid, self::COMPONENT, $filearea, $itemid, $filepath, true); + $librarycache = \cache::make('core', 'h5p_library_files'); + foreach ($files as $file) { $path = $target . str_replace($filepath, DIRECTORY_SEPARATOR, $file->get_filepath()); if ($file->is_directory()) { check_dir_exists(rtrim($path)); } else { - $file->copy_content_to($path . $file->get_filename()); + if ($filearea == self::LIBRARY_FILEAREA) { + $cachedfile = $librarycache->get($file->get_contenthash()); + if (empty($cachedfile)) { + $file->copy_content_to($path . $file->get_filename()); + $librarycache->set($file->get_contenthash(), file_get_contents($path . $file->get_filename())); + } else { + file_put_contents($path . $file->get_filename(), $cachedfile); + } + } else { + $file->copy_content_to($path . $file->get_filename()); + } } } } diff --git a/h5p/upgrade.txt b/h5p/upgrade.txt index 0031d7d30dd..59eb0cba4dc 100644 --- a/h5p/upgrade.txt +++ b/h5p/upgrade.txt @@ -1,9 +1,12 @@ This files describes API changes in core libraries and APIs, information provided here is intended especially for developers. +=== 4.0 === +* Added a new cache for h5p_library_files (MDL-69207) + === 3.9 === * A new plugintype has been created, h5plib, for having installed more than one H5P library version. * H5P third-party libraries have been moved from /lib/h5p to h5p/h5plib/v124, as an h5plib plugintype. -* H5P Editor PHP library added to h5plib v124 plugin. \ No newline at end of file +* H5P Editor PHP library added to h5plib v124 plugin. diff --git a/lang/en/cache.php b/lang/en/cache.php index 8ca427a1d8e..5a374dd3760 100644 --- a/lang/en/cache.php +++ b/lang/en/cache.php @@ -58,6 +58,7 @@ $string['cachedef_fontawesomeiconmapping'] = 'Mapping of icons for font awesome' $string['cachedef_suspended_userids'] = 'List of suspended users per course'; $string['cachedef_groupdata'] = 'Course group information'; $string['cachedef_h5p_content_type_translations'] = 'H5P content-type libraries translations'; +$string['cachedef_h5p_library_files'] = 'H5P library files'; $string['cachedef_htmlpurifier'] = 'HTML Purifier - cleaned content'; $string['cachedef_langmenu'] = 'List of available languages'; $string['cachedef_license'] = 'List of licences'; diff --git a/lib/db/caches.php b/lib/db/caches.php index 14dca042857..66dbe762a64 100644 --- a/lib/db/caches.php +++ b/lib/db/caches.php @@ -455,6 +455,13 @@ $definitions = array( 'simpledata' => true, ], + // File cache for H5P Library files. + 'h5p_library_files' => [ + 'mode' => cache_store::MODE_APPLICATION, + 'simplekeys' => true, + 'canuselocalstore' => true + ], + // Cache the grade letters for faster retrival. 'grade_letters' => [ 'mode' => cache_store::MODE_REQUEST, diff --git a/version.php b/version.php index 5ea5e1f2a3c..7e9b1e0a597 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2020073000.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2020073000.01; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. $release = '4.0dev (Build: 20200730)'; // Human-friendly version name