This repository was archived by the owner on Nov 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Partially implement core/version-check endpoint.
#252
Draft
costdev
wants to merge
8
commits into
aspirepress:main
Choose a base branch
from
costdev:handle_core_updates_internally
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
94e6d91
Add initial controller implementation.
costdev 7e7fa2f
Update API routes to use new controller.
costdev 2bb041d
Resolve PHPStan errors.
costdev e866480
Nit: Remove space between `)` and `:` in return type.
costdev dff31d9
reformat
chuckadams 4f7aa4b
get core version from Assets
chuckadams 17481ff
minor code cleanups
chuckadams a6ed3ff
comment invoke method with all known request args
chuckadams File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
332 changes: 332 additions & 0 deletions
332
app/Http/Controllers/API/WpOrg/Core/VersionCheckController.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,332 @@ | ||
| <?php | ||
|
|
||
| namespace App\Http\Controllers\API\WpOrg\Core; | ||
|
|
||
| use App\Http\Controllers\Controller; | ||
| use App\Models\WpOrg\Asset; | ||
| use Illuminate\Cache\CacheManager; | ||
| use Illuminate\Http\JsonResponse; | ||
| use Illuminate\Http\Request; | ||
|
|
||
| class VersionCheckController extends Controller | ||
| { | ||
| public const int CACHE_TTL = 60 * 60 * 6; | ||
|
|
||
| // TODO remove these, pass request around instead | ||
| private string $currentVersion; | ||
| private string $locale; | ||
|
|
||
| public function __construct(private readonly CacheManager $cache) {} | ||
|
|
||
| public function __invoke(Request $request): JsonResponse | ||
| { | ||
| // query args | ||
| // * version: string | ||
| // * php: string | ||
| // * locale: string | ||
| // * mysql: string | ||
| // * local_package: string | ||
| // * blogs: int | ||
| // * users: int | ||
| // * multisite_enabled: bool | ||
| // * initial_db_version: string | ||
| // * extensions: array (???) | ||
| // * platform_flags: {os: string, bits: 32|64} | ||
| // * image_support: {gd?: list<webp|avif|heic|jxl>, imagick?: list<webp|avif|heic|jxl>} | ||
| // * channel (optional): beta|rc|development|branch-development | ||
|
|
||
| // body args (all optional). Only translations is set from normal requests, others are sent during rollback. | ||
| // * translations: json of translations, format tbd | ||
| // * success: false (only ever set when false as result of an update failure) | ||
| // * error_code: string|int | ||
| // * error_data: mixed | ||
| // * rollback: bool | ||
| // * rollback_code: string|int | ||
| // * rollback_data: mixed | ||
|
|
||
| $this->currentVersion = $request->query('version') ?? (string)PHP_INT_MAX; | ||
| $this->locale = $request->query('locale') ?? ''; | ||
|
|
||
| $response = new \stdClass(); | ||
| $response->offers = $this->buildOffers(); | ||
| $response->translations = $this->buildTranslations(); | ||
|
|
||
| return response()->json($response); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<int, \stdClass> | ||
| */ | ||
| private function buildOffers(): array | ||
| { | ||
| $offers = []; | ||
| $latestVersion = $this->getLatestCoreVersion(); | ||
|
|
||
| if ($this->locale) { | ||
| $offers[] = $this->buildTranslatedUpgradeOffer($latestVersion); | ||
|
|
||
| // When a translated offer is needed, the API only adds a 'latest' en_US offer | ||
| // if the currently installed version is not the latest version. | ||
| if ($this->currentVersion !== $latestVersion) { | ||
| $offers[] = $this->buildUpgradeOffer($latestVersion); | ||
| } | ||
| } else { | ||
| $offers[] = $this->buildUpgradeOffer($latestVersion); | ||
| } | ||
|
|
||
| // Versions older than 5.1.19 get an extra upgrade offer. | ||
| if (version_compare($this->currentVersion, '5.1.19', '<')) { | ||
| $offers[] = $this->buildAutoupdateOffer('5.1.19'); | ||
| } | ||
|
|
||
| $olderVersions = $this->getOlderVersions(); | ||
| foreach ($olderVersions as $olderVersion) { | ||
| $offers[] = $this->buildAutoupdateOffer($olderVersion); | ||
| } | ||
|
|
||
| return $offers; | ||
| } | ||
|
|
||
| private function buildTranslatedUpgradeOffer(string $version): \stdClass | ||
| { | ||
| $offer = $this->buildUpgradeOffer($version); | ||
| $base = config('app.aspirecloud.download.base'); | ||
| $offer->download = "{$base}release/$this->locale/wordpress-$version.zip"; | ||
| $offer->locale = $this->locale; | ||
|
|
||
| $offer->packages->full = "{$base}release/$this->locale/wordpress-$version.zip"; | ||
| $offer->packages->no_content = false; | ||
| $offer->packages->new_bundled = false; | ||
| $offer->packages->partial = false; | ||
| $offer->packages->rollback = false; | ||
|
|
||
| return $offer; | ||
| } | ||
|
|
||
| private function buildAutoupdateOffer(string $version): \stdClass | ||
| { | ||
| $offer = $this->buildUpgradeOffer($version); | ||
|
|
||
| $offer->response = 'autoupdate'; | ||
| $offer->new_files = $this->getHasNewFiles($version); | ||
|
|
||
| return $offer; | ||
| } | ||
|
|
||
| private function buildUpgradeOffer(string $version): \stdClass | ||
| { | ||
| $offer = new \stdClass(); | ||
|
|
||
| $offer->response = $this->currentVersion === $version ? 'latest' : 'upgrade'; | ||
| $base = config('app.aspirecloud.download.base'); | ||
| $offer->download = $base . 'release/wordpress-' . $version . '.zip'; | ||
| $offer->locale = $offer->response === 'latest' && !$this->locale ? false : 'en_US'; | ||
|
|
||
| $offer->packages = new \stdClass(); | ||
| $offer->packages->full = "{$base}release/wordpress-$version.zip"; | ||
| $offer->packages->no_content = "{$base}release/wordpress-$version-no-content.zip"; | ||
| $offer->packages->new_bundled = "{$base}release/wordpress-$version-new-bundled.zip"; | ||
|
|
||
| // Disabling these for now, pending more research from the team. | ||
| $offer->packages->partial = false; | ||
| $offer->packages->rollback = false; | ||
|
|
||
| $offer->current = $version; | ||
| $offer->version = $version; | ||
| $offer->php_version = $this->getPhpVersion($version); | ||
| $offer->mysql_version = $this->getMySqlVersion($version); | ||
| $offer->new_bundled = $this->getNewBundled(); | ||
| $offer->partial_version = false; | ||
|
|
||
| return $offer; | ||
| } | ||
|
|
||
| /** | ||
| * @return array<int, \stdClass> | ||
| */ | ||
| private function buildTranslations(): array | ||
| { | ||
| if (!$this->locale) { | ||
| return []; | ||
| } | ||
|
|
||
| // There are other potential translations, possibly based on the slug. | ||
| // For now, this just generates a single translation package. | ||
| $base = config('app.aspirecloud.download.base'); | ||
|
|
||
| $translation = new \stdClass(); | ||
| $translation->type = 'core'; | ||
| $translation->slug = 'default'; | ||
| $translation->language = $this->locale; | ||
| $translation->version = $this->currentVersion; | ||
| $translation->updated = '2023-10-01T00:00:00Z'; // Store in DB from the translations file. Pull from DB for here. | ||
| $translation->package = "{$base}translation/core/$this->currentVersion/$this->locale.zip"; | ||
| $translation->autoupdate = true; | ||
|
|
||
| return [$translation]; | ||
| } | ||
|
|
||
| private function getLatestCoreVersion(): string | ||
| { | ||
| // technically semver comparison should be used, but core versions are always sortable lexicographically | ||
| $getLatest = fn() | ||
| => Asset::query() | ||
| ->where('type', 'core') | ||
| ->orderBy('version', 'desc') | ||
| ->first() | ||
| ->version; | ||
|
|
||
| return $this->cache->remember('wporg.core.latest', self::CACHE_TTL, $getLatest); | ||
| } | ||
|
|
||
| private function getLatestCoreMajorVersion(): string | ||
| { | ||
| // Wordpress considers x.y to be a major version (it predates the semver standard) | ||
| [$x, $y] = explode('.', $this->getLatestCoreVersion()); | ||
| return "$x.$y"; | ||
| } | ||
|
|
||
| /** | ||
| * @return array<int, string> | ||
| */ | ||
| private function getOlderVersions(): array | ||
| { | ||
| // Probably pull from the database instead of hardcoding these. | ||
| // Should have the latest minor from every branch. | ||
| // If not going for completeness, can probably remove the versions | ||
| // below AspireUpdate's minimum WP version (currently 5.3). | ||
| $map = [ | ||
| '6.8.1', | ||
| '6.8', | ||
| '6.7.2', | ||
| '6.6.2', | ||
| '6.6.5', | ||
| '6.4.5', | ||
| '6.3.5', | ||
| '6.2.6', | ||
| '6.1.7', | ||
| '6.0.9', | ||
| '5.9.10', | ||
| '5.8.10', | ||
| '5.7.12', | ||
| '5.6.14', | ||
| '5.5.15', | ||
| '5.4.16', | ||
| '5.3.18', | ||
| '5.2.21', | ||
| '5.1.19', | ||
| '5.0.22', | ||
| '4.9.26', | ||
| '4.8.25', | ||
| '4.7.29', | ||
| '4.6.29', | ||
| '4.5.32', | ||
| '4.4.33', | ||
| '4.3.34', | ||
| '4.2.38', | ||
| '4.1.41', | ||
| '4.0.38', | ||
| '3.9.40', | ||
| '3.8.41', | ||
| '3.7.41', | ||
| '3.6.1', | ||
| '3.5.2', | ||
| '3.4.2', | ||
| '3.3.3', | ||
| '3.2.1', | ||
| '3.1.4', | ||
| '3.0.6', | ||
| '2.9.2', | ||
| '2.8.6', | ||
| '2.7.1', | ||
| '2.6.5', | ||
| '2.5.1', | ||
| '2.3.3', | ||
| '2.2.3', | ||
| '2.1.3', | ||
| '2.0.11', | ||
| '1.5.2', | ||
| '1.2.2', | ||
| '1.0.2', | ||
| '0.72', | ||
| ]; | ||
|
|
||
| // If no version has been provided, all versions are considered older. | ||
| if ($this->currentVersion === (string)PHP_INT_MAX) { | ||
| return $map; | ||
| } | ||
|
|
||
| $versions = []; | ||
| foreach ($map as $version) { | ||
| if (version_compare($this->currentVersion, $version, '<')) { | ||
| $versions[] = $version; | ||
| } | ||
| } | ||
|
|
||
| return $versions; | ||
| } | ||
|
|
||
| private function getPhpVersion(string $version): string | ||
| { | ||
| // The WordPress versions that started a new minimum PHP version requirement. | ||
| $map = [ | ||
| '6.6' => '7.2.24', | ||
| '6.3' => '7.0.0', | ||
| '5.2' => '5.6.20', | ||
| '4.1' => '5.2.4', | ||
| ]; | ||
|
|
||
| foreach ($map as $wpMajor => $php) { | ||
| if (version_compare($version, $wpMajor, '>=')) { | ||
| return $php; | ||
| } | ||
| } | ||
|
|
||
| return end($map); | ||
| } | ||
|
|
||
| private function getMySqlVersion(string $version): string | ||
| { | ||
| // The WordPress versions that started a new minimum MySQL version requirement. | ||
| $map = [ | ||
| '6.5' => '5.5.5', | ||
| '4.1' => '5.0', | ||
| ]; | ||
|
|
||
| foreach ($map as $wpMajor => $mysql) { | ||
| if (version_compare($version, $wpMajor, '>=')) { | ||
| return $mysql; | ||
| } | ||
| } | ||
|
|
||
| return end($map); | ||
| } | ||
|
|
||
| private function getNewBundled(): string | ||
| { | ||
| // Appears to be the second latest major version. | ||
| $parts = explode('.', $this->getLatestCoreMajorVersion()); | ||
| if ($parts[1] > 0) { | ||
| --$parts[1]; | ||
| } else { | ||
| $parts[1] = 9; | ||
| --$parts[0]; | ||
| } | ||
|
|
||
| return $parts[0] . '.' . $parts[1]; | ||
| } | ||
|
|
||
| private function getHasNewFiles(string $version): bool | ||
| { | ||
| // WP Core relaxes filesystem checks when the API | ||
| // specifies that it's safe to do. | ||
| // The API specifies this by setting new_files to false. | ||
| // For now, return true so filesystem checks are not relaxed. | ||
| return true; | ||
|
|
||
| // Appears to depend on a comparison between | ||
| // the installed version and the offered version. | ||
| // $hasNewFiles = []; | ||
| // return array_key_exists($version, $hasNewFiles); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This data should be compared with the request body's subkey containing the currently installed translation's data including its updated time string. If the updated value is the same as the API's latest translation for that type/slug/version/locale, then no translation is needed. Otherwise, the API should provide the translation offer. If the value isn't set, or there's no body, or otherwise isn't available, the API should provide the translation offer.