Skip to content

Commit

Permalink
WIP: Implement system update feature fc2blog#39
Browse files Browse the repository at this point in the history
  • Loading branch information
uzulla committed Mar 15, 2021
1 parent e55cb85 commit cc0bc7f
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 0 deletions.
15 changes: 15 additions & 0 deletions app/locale/en_US.UTF-8/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -2503,3 +2503,18 @@ msgstr ""

msgid "Name that cannot be specified"
msgstr ""

msgid "System Update"
msgstr ""

msgid "Releases"
msgstr ""

msgid "Version"
msgstr ""

msgid "Operation"
msgstr ""

msgid "Release information query failed. Please try again later."
msgstr ""
Binary file modified app/locale/ja_JP.UTF-8/LC_MESSAGES/messages.mo
Binary file not shown.
15 changes: 15 additions & 0 deletions app/locale/ja_JP.UTF-8/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -2550,3 +2550,18 @@ msgstr "ホスト ポート番号"

msgid "Name that cannot be specified"
msgstr "指定できない名称です"

msgid "System Update"
msgstr "システム更新"

msgid "Releases"
msgstr "リリース一覧"

msgid "Version"
msgstr "バージョン"

msgid "Operation"
msgstr "操作"

msgid "Release information query failed. Please try again later."
msgstr "情報取得に失敗しました、時間をおいてから再度お試しください。"
102 changes: 102 additions & 0 deletions app/src/Model/SystemUpdateModel.php
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;
}

}
24 changes: 24 additions & 0 deletions app/src/Web/Controller/Admin/SystemUpdateController.php
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

}

1 change: 1 addition & 0 deletions app/twig_templates/admin/layouts/default.twig
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<li><a href="{{ url(req, 'Categories', 'create') }}">{{ _('Category management') }}</a></li>
<li><a href="{{ url(req, 'Tags', 'index') }}">{{ _('List of tags') }}</a></li>
<li><a href="{{ url(req, 'Blogs', 'edit') }}">{{ _('Blog setting') }}</a></li>
<li><a href="{{ url(req, 'SystemUpdate', 'index') }}">{{ _('System Update') }}</a></li>
</ul>
</div>
{% endif %}
Expand Down
64 changes: 64 additions & 0 deletions app/twig_templates/admin/system_update/index.twig
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 %}

0 comments on commit cc0bc7f

Please sign in to comment.