Skip to content

Commit

Permalink
GH #2944: CA Admin: Fix slow check if content type/library can be del…
Browse files Browse the repository at this point in the history
…eted (#2958)
  • Loading branch information
chrieinv authored Feb 12, 2025
1 parent 9290bb9 commit a8fbe83
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
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']);
});
}
};

0 comments on commit a8fbe83

Please sign in to comment.