From cd4ff2dadd829f191b65d83785ac1420307f7cd8 Mon Sep 17 00:00:00 2001 From: Christian Einvik Date: Fri, 7 Feb 2025 13:57:48 +0100 Subject: [PATCH 1/4] GH #2944: CA Admin: Fix slow check if content type/library can be deleted --- .../apis/contentauthor/app/H5PLibrary.php | 10 ++++--- .../Admin/LibraryUpgradeController.php | 3 +- ...5p_contents_libraries_library_id_index.php | 28 +++++++++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 sourcecode/apis/contentauthor/database/migrations/2025_02_07_124500_create_h5p_contents_libraries_library_id_index.php diff --git a/sourcecode/apis/contentauthor/app/H5PLibrary.php b/sourcecode/apis/contentauthor/app/H5PLibrary.php index 62249f939..e299a4d1e 100644 --- a/sourcecode/apis/contentauthor/app/H5PLibrary.php +++ b/sourcecode/apis/contentauthor/app/H5PLibrary.php @@ -370,11 +370,13 @@ public function getIconUrl(): string return $icon ?? url('/graphical/h5p_logo.svg'); } - public static function canBeDeleted(int $libraryId): bool + public static function canBeDeleted(int $libraryId, array|null $usage = null): bool { - $h5pFramework = app(H5PFrameworkInterface::class); - // Number of references by other content types/libraries. Only counts content using library as main content type, so we skip that - $usage = $h5pFramework->getLibraryUsage($libraryId, skipContent: true); + if ($usage === null || array_key_exists('libraries', $usage) === false) { + $h5pFramework = app(H5PFrameworkInterface::class); + // Number of references by other content types/libraries. Only counts content using library as main content type, so we skip that + $usage = $h5pFramework->getLibraryUsage($libraryId, skipContent: true); + } return $usage['libraries'] === 0 && H5PContentLibrary::where('library_id', $libraryId)->doesntExist(); } diff --git a/sourcecode/apis/contentauthor/app/Http/Controllers/Admin/LibraryUpgradeController.php b/sourcecode/apis/contentauthor/app/Http/Controllers/Admin/LibraryUpgradeController.php index 4dcce6edc..e5fd599cb 100644 --- a/sourcecode/apis/contentauthor/app/Http/Controllers/Admin/LibraryUpgradeController.php +++ b/sourcecode/apis/contentauthor/app/Http/Controllers/Admin/LibraryUpgradeController.php @@ -61,6 +61,7 @@ public function index(): View reset($versions); foreach ($versions as $library) { $usage = $this->h5pFramework->getLibraryUsage($library->id); + $item = [ 'machineName' => $library->name, 'majorVersion' => $library->major_version, @@ -71,7 +72,7 @@ public function index(): View 'hubUpgrade' => null, 'isLast' => $library->id === $lastVersion->id, 'libraryId' => $library->id, - 'canDelete' => H5PLibrary::canBeDeleted($library->id), + 'canDelete' => H5PLibrary::canBeDeleted($library->id, $usage), ]; if ($library->runnable) { diff --git a/sourcecode/apis/contentauthor/database/migrations/2025_02_07_124500_create_h5p_contents_libraries_library_id_index.php b/sourcecode/apis/contentauthor/database/migrations/2025_02_07_124500_create_h5p_contents_libraries_library_id_index.php new file mode 100644 index 000000000..4f2b6fd38 --- /dev/null +++ b/sourcecode/apis/contentauthor/database/migrations/2025_02_07_124500_create_h5p_contents_libraries_library_id_index.php @@ -0,0 +1,28 @@ +index('library_id'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('h5p_contents_libraries', function (Blueprint $table) { + $table->dropIndex(['library_id']); + }); + } +}; From 10e5a510c60f11de2caf982a90aa6469fa2a7141 Mon Sep 17 00:00:00 2001 From: chrieinv <84850107+chrieinv@users.noreply.github.com> Date: Fri, 7 Feb 2025 13:03:31 +0000 Subject: [PATCH 2/4] Run php-cs-fixer --- ...7_124500_create_h5p_contents_libraries_library_id_index.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sourcecode/apis/contentauthor/database/migrations/2025_02_07_124500_create_h5p_contents_libraries_library_id_index.php b/sourcecode/apis/contentauthor/database/migrations/2025_02_07_124500_create_h5p_contents_libraries_library_id_index.php index 4f2b6fd38..03a0f0a1b 100644 --- a/sourcecode/apis/contentauthor/database/migrations/2025_02_07_124500_create_h5p_contents_libraries_library_id_index.php +++ b/sourcecode/apis/contentauthor/database/migrations/2025_02_07_124500_create_h5p_contents_libraries_library_id_index.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { /** * Run the migrations. */ From 871df9e0088b7ebc64009fcbd87c820067b6cc91 Mon Sep 17 00:00:00 2001 From: Christian Einvik Date: Mon, 10 Feb 2025 13:35:54 +0100 Subject: [PATCH 3/4] CR fix --- sourcecode/apis/contentauthor/app/H5PLibrary.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sourcecode/apis/contentauthor/app/H5PLibrary.php b/sourcecode/apis/contentauthor/app/H5PLibrary.php index e299a4d1e..a6b316f18 100644 --- a/sourcecode/apis/contentauthor/app/H5PLibrary.php +++ b/sourcecode/apis/contentauthor/app/H5PLibrary.php @@ -370,14 +370,14 @@ public function getIconUrl(): string return $icon ?? url('/graphical/h5p_logo.svg'); } - public static function canBeDeleted(int $libraryId, array|null $usage = null): bool + public static function canBeDeleted(int $libraryId, int|null $usageCount = null): bool { - if ($usage === null || array_key_exists('libraries', $usage) === false) { + if ($usageCount === null) { $h5pFramework = app(H5PFrameworkInterface::class); // Number of references by other content types/libraries. Only counts content using library as main content type, so we skip that - $usage = $h5pFramework->getLibraryUsage($libraryId, skipContent: true); + $usageCount = $h5pFramework->getLibraryUsage($libraryId, skipContent: true)['libraries']; } - return $usage['libraries'] === 0 && H5PContentLibrary::where('library_id', $libraryId)->doesntExist(); + return $usageCount === 0 && H5PContentLibrary::where('library_id', $libraryId)->doesntExist(); } } From 082ad49653bca2ca0ea1ff445e9e4aa47c9bf133 Mon Sep 17 00:00:00 2001 From: Christian Einvik Date: Mon, 10 Feb 2025 13:37:59 +0100 Subject: [PATCH 4/4] CR fix --- .../app/Http/Controllers/Admin/LibraryUpgradeController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sourcecode/apis/contentauthor/app/Http/Controllers/Admin/LibraryUpgradeController.php b/sourcecode/apis/contentauthor/app/Http/Controllers/Admin/LibraryUpgradeController.php index e5fd599cb..18a2fbd18 100644 --- a/sourcecode/apis/contentauthor/app/Http/Controllers/Admin/LibraryUpgradeController.php +++ b/sourcecode/apis/contentauthor/app/Http/Controllers/Admin/LibraryUpgradeController.php @@ -72,7 +72,7 @@ public function index(): View 'hubUpgrade' => null, 'isLast' => $library->id === $lastVersion->id, 'libraryId' => $library->id, - 'canDelete' => H5PLibrary::canBeDeleted($library->id, $usage), + 'canDelete' => H5PLibrary::canBeDeleted($library->id, $usage['libraries']), ]; if ($library->runnable) {