|
| 1 | +<?php |
| 2 | + |
| 3 | +// This command fetches extension metadata on a schedule, to then provide administrators |
| 4 | +// with stuff like latest version info, and maybe more in the future. |
| 5 | +// |
| 6 | +// 1. make a request to blueprint.zip/api/extensions/latest |
| 7 | +// 2. figure out which extensions it should write metadata for |
| 8 | +// 3. build a json object for those extensions' metadata |
| 9 | +// 4. flush metadata table |
| 10 | +// 5. write metadata table |
| 11 | + |
| 12 | +namespace Pterodactyl\Console\Commands\BlueprintFramework; |
| 13 | + |
| 14 | +use Illuminate\Console\Command; |
| 15 | +use Illuminate\Support\Facades\DB; |
| 16 | +use Pterodactyl\Models\ExtensionCachedMetadata; |
| 17 | +use Pterodactyl\BlueprintFramework\Services\PlaceholderService\BlueprintPlaceholderService; |
| 18 | +use Pterodactyl\BlueprintFramework\Libraries\ExtensionLibrary\Console\BlueprintConsoleLibrary as BlueprintExtensionLibrary; |
| 19 | + |
| 20 | +class MetadataCacheCommand extends Command |
| 21 | +{ |
| 22 | + protected $description = 'Refreshes extension metadata'; |
| 23 | + protected $signature = 'bp:meta'; |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + private BlueprintPlaceholderService $PlaceholderService, |
| 27 | + private BlueprintExtensionLibrary $blueprint, |
| 28 | + ) { |
| 29 | + parent::__construct(); |
| 30 | + } |
| 31 | + |
| 32 | + public function handle() |
| 33 | + { |
| 34 | + if(! $this->blueprint->dbGet("blueprint", "flags:remote_metadata")) { |
| 35 | + $this->error('remote_metadata flag set to false'); |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + $now = now(); |
| 40 | + $rows = []; |
| 41 | + $installedExtensions = $this->blueprint->extensions(); |
| 42 | + |
| 43 | + // get version info |
| 44 | + $context = stream_context_create(['http' => ['method' => 'GET', 'header' => 'User-Agent: BlueprintFramework']]); |
| 45 | + $remoteVersions = @file_get_contents( |
| 46 | + $this->PlaceholderService->api_url() . '/api/extensions/latest', |
| 47 | + false, |
| 48 | + $context |
| 49 | + ); |
| 50 | + |
| 51 | + if($remoteVersions) { |
| 52 | + $remoteVersionsData = json_decode($remoteVersions, true); |
| 53 | + } |
| 54 | + |
| 55 | + if(! isset($remoteVersionsData)) { |
| 56 | + $this->error('failed to fetch extension versions'); |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + foreach ($installedExtensions as $identifier) { |
| 61 | + if(! isset($remoteVersionsData[$identifier]) || ! is_scalar($remoteVersionsData[$identifier])) continue; |
| 62 | + |
| 63 | + $local_extension = $this->blueprint->extensionConfig($identifier); |
| 64 | + |
| 65 | + $rows[] = [ |
| 66 | + 'identifier' => $identifier, |
| 67 | + 'metadata' => json_encode([ |
| 68 | + 'latest_version' => (string) $remoteVersionsData[$identifier], |
| 69 | + 'local_version' => (string) $local_extension['info']['version'] ?? '', |
| 70 | + ]), |
| 71 | + 'fetched_at' => $now, |
| 72 | + ]; |
| 73 | + } |
| 74 | + |
| 75 | + if (empty($rows)) { |
| 76 | + $this->info('no relevant data available, do you have any extensions installed?'); |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + $table = (new ExtensionCachedMetadata())->getTable(); |
| 81 | + |
| 82 | + DB::transaction(function () use ($rows, $table) { |
| 83 | + DB::table($table)->delete(); |
| 84 | + DB::table($table)->insert($rows); |
| 85 | + }); |
| 86 | + |
| 87 | + $this->info('updated extension cached metadata'); |
| 88 | + } |
| 89 | +} |
0 commit comments