Skip to content

Commit

Permalink
MDL-69207 core_h5p: Add muc cache support for h5p library files
Browse files Browse the repository at this point in the history
  • Loading branch information
durzo authored and vmdef committed Aug 6, 2020
1 parent e77dc82 commit ef8dff0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
33 changes: 31 additions & 2 deletions h5p/classes/file_storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,13 +561,23 @@ public function saveFileFromZip($path, $file, $stream) {
* @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());
}
}
}

/**
Expand All @@ -590,6 +600,7 @@ private function delete_directory(int $contextid, string $component, string $fil
* @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);

Expand All @@ -607,7 +618,13 @@ private function copy_directory(string $source, array $options): void {
$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();
}
Expand All @@ -629,12 +646,24 @@ private function export_file_tree(string $target, int $contextid, string $filear
// 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());
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion h5p/upgrade.txt
Original file line number Diff line number Diff line change
@@ -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.
* H5P Editor PHP library added to h5plib v124 plugin.
1 change: 1 addition & 0 deletions lang/en/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
$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';
Expand Down
7 changes: 7 additions & 0 deletions lib/db/caches.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,13 @@
'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,
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ef8dff0

Please sign in to comment.