forked from fc2blog/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Implement system update feature fc2blog#39
- Loading branch information
Showing
7 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Binary file not shown.
This file contains 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
This file contains 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,102 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Fc2blog\Model; | ||
|
||
use JsonException; | ||
use RuntimeException; | ||
|
||
class SystemUpdateModel | ||
{ | ||
/** @var string */ | ||
static public $releases_url = "https://www.github.com/uzulla/fc2blog/releases"; // TODO: change to fc2blog/blog | ||
/** @var string */ | ||
// static public $releases_api_url = "https://api.github.com/repos/uzulla/tag-release-test/releases"; // TODO: change to fc2blog/blog | ||
static public $releases_api_url = "https://api.github.com/repos/uzulla/fc2blog/releases"; // TODO: change to fc2blog/blog | ||
|
||
/** | ||
* Get release info list from GitHub. | ||
*/ | ||
public static function getReleaseInfo(): ?array | ||
{ | ||
// TODO cache | ||
// app/temp/releases.json の最終更新時刻をみて、1h以内ならそれを利用する | ||
|
||
$options = ['http' => ['header' => "User-Agent: fc2blog_installer"]]; | ||
|
||
$releases_json = @file_get_contents( | ||
static::$releases_api_url, | ||
false, | ||
stream_context_create($options) | ||
); | ||
|
||
// rate limit等 | ||
$pos = strpos($http_response_header[0], '403'); | ||
if ($pos !== false) { | ||
var_dump($http_response_header); | ||
return null; | ||
} | ||
|
||
$pos = strpos($http_response_header[0], '200'); | ||
if ($pos === false) { | ||
throw new RuntimeException("api request failed: status code {$http_response_header[0]}"); | ||
} | ||
|
||
if ($releases_json === false) { | ||
throw new RuntimeException("api request failed"); | ||
} | ||
|
||
try { | ||
$releases_data = json_decode($releases_json, true, 512, JSON_THROW_ON_ERROR); | ||
} catch (JsonException $e) { | ||
throw new RuntimeException("json parse failed:{$e->getMessage()} on {$e->getFile()}:{$e->getLine()}"); | ||
} | ||
|
||
// app/temp/releases.json にjsonを書き出す | ||
|
||
return $releases_data; | ||
} | ||
|
||
/** | ||
* Get download url of `fc2blog_dist.zip` in assets from release array. | ||
* @param array $release | ||
* @return string|null | ||
*/ | ||
public static function getZipDownloadUrl(array $release): ?string | ||
{ | ||
if (!isset($release['assets'])) { | ||
return null; | ||
} | ||
|
||
$found_asset = null; | ||
foreach ($release['assets'] as $release_asset) { | ||
if ($release_asset['name'] === 'fc2blog_dist.zip') { | ||
$found_asset = $release_asset; | ||
} | ||
} | ||
|
||
if (!is_array($found_asset) || !isset($found_asset['browser_download_url'])) { | ||
return null; | ||
} | ||
|
||
return $found_asset['browser_download_url']; | ||
} | ||
|
||
/** | ||
* Get latest release that has vX.X.X tag from release list. | ||
* @param $release_list | ||
* @return array|null | ||
*/ | ||
public static function getValidLatestRelease($release_list): ?array | ||
{ | ||
foreach ($release_list as $release) { | ||
if (preg_match("/\Av[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\\z/", $release['tag_name'])) { | ||
if (isset($release['assets']) && is_array($release['assets']) && count($release['assets']) > 0) { | ||
return $release; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
} |
This file contains 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,24 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Fc2blog\Web\Controller\Admin; | ||
|
||
use Fc2blog\Model\SystemUpdateModel; | ||
use Fc2blog\Web\Request; | ||
|
||
class SystemUpdateController extends AdminController | ||
{ | ||
public function index(Request $request): string | ||
{ | ||
$release_list = SystemUpdateModel::getReleaseInfo(); | ||
$this->set('release_list', $release_list); | ||
|
||
// TODO get this system version | ||
|
||
return 'admin/system_update/index.twig'; | ||
} | ||
|
||
// TODO implement update action | ||
|
||
} | ||
|
This file contains 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
This file contains 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,64 @@ | ||
{% extends 'admin/layouts/default.twig' %} | ||
{% block title %}{{ _('System Update') }}{% endblock %} | ||
|
||
{% block content %} | ||
<header><h2>{{ _('System Update') }}</h2></header> | ||
|
||
<h3>{{ _('Releases') }}</h3> | ||
|
||
<style> | ||
#version-select-table thead td { | ||
background-color: lightgrey; | ||
font-weight: bold; | ||
} | ||
#version-select-table thead td.ver_col { | ||
width: 50px; | ||
text-align: center; | ||
} | ||
#version-select-table thead td.desc_col { | ||
text-align: center; | ||
} | ||
#version-select-table thead td.op_col { | ||
width: 100px; | ||
text-align: center; | ||
} | ||
#version-select-table td.ver_col { | ||
text-align: center; | ||
} | ||
#version-select-table td.op_col { | ||
text-align: center; | ||
} | ||
</style> | ||
|
||
{% if not release_list %} | ||
{{ _('Release information query failed. Please try again later.') }} | ||
{% else %} | ||
<table id="version-select-table"> | ||
<thead> | ||
<tr> | ||
<td class="ver_col">{{ _('Version') }}</td> | ||
<td class="desc_col">{{ _('Description') }}</td> | ||
<td class="op_col">{{ _('Operation') }}</td> | ||
</tr> | ||
</thead> | ||
{% for release in release_list %} | ||
<tr> | ||
<td class="ver_col"><a href="{{ release.html_url }}">{{ release.tag_name }}</a></td> | ||
<td class="desc_col"> | ||
<h4><a href="{{ release.html_url }}">{{ release.name }}</a></h4> | ||
<p>{{ release.body|nl2br }}</p> | ||
</td> | ||
<td class="op_col"> | ||
<button>update</button> | ||
</td> | ||
</tr> | ||
{% endfor %} | ||
</table> | ||
{% endif %} | ||
|
||
{% endblock %} |