Skip to content

Commit e876648

Browse files
committed
WIP: Implement system update feature fc2blog#39
1 parent 72168ea commit e876648

File tree

7 files changed

+221
-0
lines changed

7 files changed

+221
-0
lines changed

app/locale/en_US.UTF-8/LC_MESSAGES/messages.po

+15
Original file line numberDiff line numberDiff line change
@@ -2503,3 +2503,18 @@ msgstr ""
25032503

25042504
msgid "Name that cannot be specified"
25052505
msgstr ""
2506+
2507+
msgid "System Update"
2508+
msgstr ""
2509+
2510+
msgid "Releases"
2511+
msgstr ""
2512+
2513+
msgid "Version"
2514+
msgstr ""
2515+
2516+
msgid "Operation"
2517+
msgstr ""
2518+
2519+
msgid "Release information query failed. Please try again later."
2520+
msgstr ""
355 Bytes
Binary file not shown.

app/locale/ja_JP.UTF-8/LC_MESSAGES/messages.po

+15
Original file line numberDiff line numberDiff line change
@@ -2550,3 +2550,18 @@ msgstr "ホスト ポート番号"
25502550

25512551
msgid "Name that cannot be specified"
25522552
msgstr "指定できない名称です"
2553+
2554+
msgid "System Update"
2555+
msgstr "システム更新"
2556+
2557+
msgid "Releases"
2558+
msgstr "リリース一覧"
2559+
2560+
msgid "Version"
2561+
msgstr "バージョン"
2562+
2563+
msgid "Operation"
2564+
msgstr "操作"
2565+
2566+
msgid "Release information query failed. Please try again later."
2567+
msgstr "情報取得に失敗しました、時間をおいてから再度お試しください。"

app/src/Model/SystemUpdateModel.php

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Fc2blog\Model;
5+
6+
use JsonException;
7+
use RuntimeException;
8+
9+
class SystemUpdateModel
10+
{
11+
/** @var string */
12+
static public $releases_url = "https://www.github.com/uzulla/fc2blog/releases"; // TODO: change to fc2blog/blog
13+
/** @var string */
14+
// static public $releases_api_url = "https://api.github.com/repos/uzulla/tag-release-test/releases"; // TODO: change to fc2blog/blog
15+
static public $releases_api_url = "https://api.github.com/repos/uzulla/fc2blog/releases"; // TODO: change to fc2blog/blog
16+
17+
/**
18+
* Get release info list from GitHub.
19+
*/
20+
public static function getReleaseInfo(): ?array
21+
{
22+
// TODO cache
23+
// app/temp/releases.json の最終更新時刻をみて、1h以内ならそれを利用する
24+
25+
$options = ['http' => ['header' => "User-Agent: fc2blog_installer"]];
26+
27+
$releases_json = @file_get_contents(
28+
static::$releases_api_url,
29+
false,
30+
stream_context_create($options)
31+
);
32+
33+
// rate limit等
34+
$pos = strpos($http_response_header[0], '403');
35+
if ($pos !== false) {
36+
var_dump($http_response_header);
37+
return null;
38+
}
39+
40+
$pos = strpos($http_response_header[0], '200');
41+
if ($pos === false) {
42+
throw new RuntimeException("api request failed: status code {$http_response_header[0]}");
43+
}
44+
45+
if ($releases_json === false) {
46+
throw new RuntimeException("api request failed");
47+
}
48+
49+
try {
50+
$releases_data = json_decode($releases_json, true, 512, JSON_THROW_ON_ERROR);
51+
} catch (JsonException $e) {
52+
throw new RuntimeException("json parse failed:{$e->getMessage()} on {$e->getFile()}:{$e->getLine()}");
53+
}
54+
55+
// app/temp/releases.json にjsonを書き出す
56+
57+
return $releases_data;
58+
}
59+
60+
/**
61+
* Get download url of `fc2blog_dist.zip` in assets from release array.
62+
* @param array $release
63+
* @return string|null
64+
*/
65+
public static function getZipDownloadUrl(array $release): ?string
66+
{
67+
if (!isset($release['assets'])) {
68+
return null;
69+
}
70+
71+
$found_asset = null;
72+
foreach ($release['assets'] as $release_asset) {
73+
if ($release_asset['name'] === 'fc2blog_dist.zip') {
74+
$found_asset = $release_asset;
75+
}
76+
}
77+
78+
if (!is_array($found_asset) || !isset($found_asset['browser_download_url'])) {
79+
return null;
80+
}
81+
82+
return $found_asset['browser_download_url'];
83+
}
84+
85+
/**
86+
* Get latest release that has vX.X.X tag from release list.
87+
* @param $release_list
88+
* @return array|null
89+
*/
90+
public static function getValidLatestRelease($release_list): ?array
91+
{
92+
foreach ($release_list as $release) {
93+
if (preg_match("/\Av[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\\z/", $release['tag_name'])) {
94+
if (isset($release['assets']) && is_array($release['assets']) && count($release['assets']) > 0) {
95+
return $release;
96+
}
97+
}
98+
}
99+
return null;
100+
}
101+
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Fc2blog\Web\Controller\Admin;
5+
6+
use Fc2blog\Model\SystemUpdateModel;
7+
use Fc2blog\Web\Request;
8+
9+
class SystemUpdateController extends AdminController
10+
{
11+
public function index(Request $request): string
12+
{
13+
$release_list = SystemUpdateModel::getReleaseInfo();
14+
$this->set('release_list', $release_list);
15+
16+
// TODO get this system version
17+
18+
return 'admin/system_update/index.twig';
19+
}
20+
21+
// TODO implement update action
22+
23+
}
24+

app/twig_templates/admin/layouts/default.twig

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
<li><a href="{{ url(req, 'Categories', 'create') }}">{{ _('Category management') }}</a></li>
9797
<li><a href="{{ url(req, 'Tags', 'index') }}">{{ _('List of tags') }}</a></li>
9898
<li><a href="{{ url(req, 'Blogs', 'edit') }}">{{ _('Blog setting') }}</a></li>
99+
<li><a href="{{ url(req, 'SystemUpdate', 'index') }}">{{ _('System Update') }}</a></li>
99100
</ul>
100101
</div>
101102
{% endif %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{% extends 'admin/layouts/default.twig' %}
2+
{% block title %}{{ _('System Update') }}{% endblock %}
3+
4+
{% block content %}
5+
<header><h2>{{ _('System Update') }}</h2></header>
6+
7+
<h3>{{ _('Releases') }}</h3>
8+
9+
<style>
10+
#version-select-table thead td {
11+
background-color: lightgrey;
12+
font-weight: bold;
13+
}
14+
15+
#version-select-table thead td.ver_col {
16+
width: 50px;
17+
text-align: center;
18+
}
19+
20+
#version-select-table thead td.desc_col {
21+
text-align: center;
22+
}
23+
24+
#version-select-table thead td.op_col {
25+
width: 100px;
26+
text-align: center;
27+
}
28+
29+
#version-select-table td.ver_col {
30+
text-align: center;
31+
}
32+
33+
#version-select-table td.op_col {
34+
text-align: center;
35+
}
36+
</style>
37+
38+
{% if not release_list %}
39+
{{ _('Release information query failed. Please try again later.') }}
40+
{% else %}
41+
<table id="version-select-table">
42+
<thead>
43+
<tr>
44+
<td class="ver_col">{{ _('Version') }}</td>
45+
<td class="desc_col">{{ _('Description') }}</td>
46+
<td class="op_col">{{ _('Operation') }}</td>
47+
</tr>
48+
</thead>
49+
{% for release in release_list %}
50+
<tr>
51+
<td class="ver_col"><a href="{{ release.html_url }}">{{ release.tag_name }}</a></td>
52+
<td class="desc_col">
53+
<h4><a href="{{ release.html_url }}">{{ release.name }}</a></h4>
54+
<p>{{ release.body|nl2br }}</p>
55+
</td>
56+
<td class="op_col">
57+
<button>update</button>
58+
</td>
59+
</tr>
60+
{% endfor %}
61+
</table>
62+
{% endif %}
63+
64+
{% endblock %}

0 commit comments

Comments
 (0)