Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH #2944: CA Admin: Fix slow check if content type/library can be deleted #2958

Merged
merged 4 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions sourcecode/apis/contentauthor/app/H5PLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,14 @@ public function getIconUrl(): string
return $icon ?? url('/graphical/h5p_logo.svg');
}

public static function canBeDeleted(int $libraryId): bool
public static function canBeDeleted(int $libraryId, int|null $usageCount = 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 ($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
$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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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['libraries']),
];

if ($library->runnable) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('h5p_contents_libraries', function (Blueprint $table) {
$table->index('library_id');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('h5p_contents_libraries', function (Blueprint $table) {
$table->dropIndex(['library_id']);
});
}
};