Skip to content

Commit b3fa064

Browse files
author
chenyi
committed
Merge commit 'a7f495e15503c4024d7d6ed094d4c4d34d8f9fd2' as 'UpdateAssistant'
2 parents 25c4ad9 + a7f495e commit b3fa064

File tree

9 files changed

+610
-0
lines changed

9 files changed

+610
-0
lines changed

UpdateAssistant/Action.php

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
/**
3+
* Typecho update assistant.
4+
*
5+
* @package UpdateAssistant
6+
* @author mrgeneral
7+
* @version 1.0.1
8+
* @link https://www.chengxiaobai.cn
9+
*/
10+
11+
class UpdateAssistant_Action extends Typecho_Widget implements Widget_Interface_Do
12+
{
13+
private $pluginRootPath;
14+
15+
private $isDevelop;
16+
17+
/**
18+
* @var array realPath => realRootPath
19+
*/
20+
private $updateList = [
21+
// admin
22+
__TYPECHO_ROOT_DIR__ . __TYPECHO_ADMIN_DIR__ => __TYPECHO_ROOT_DIR__,
23+
// var
24+
__TYPECHO_ROOT_DIR__ . DIRECTORY_SEPARATOR . 'var/' => __TYPECHO_ROOT_DIR__,
25+
//index.php
26+
__TYPECHO_ROOT_DIR__ . DIRECTORY_SEPARATOR . 'index.php' => __TYPECHO_ROOT_DIR__,
27+
];
28+
29+
/**
30+
* @var array realPath => rawName
31+
*/
32+
private $updateNameMap = [
33+
__TYPECHO_ROOT_DIR__ . __TYPECHO_ADMIN_DIR__ => 'admin',
34+
__TYPECHO_ROOT_DIR__ . DIRECTORY_SEPARATOR . 'var/' => 'var',
35+
__TYPECHO_ROOT_DIR__ . DIRECTORY_SEPARATOR . 'index.php' => 'index.php',
36+
];
37+
38+
public function __construct($request, $response, $params = null)
39+
{
40+
parent::__construct($request, $response, $params);
41+
42+
if (!Typecho_Widget::widget('Widget_User')->pass('administrator', true)) {
43+
throw new Typecho_Exception(_t('Forbidden'), 403);
44+
}
45+
46+
$this->pluginRootPath = dirname(realpath(__FILE__));
47+
$this->isDevelop = (bool)Helper::options()->plugin('UpdateAssistant')->isDevelop;
48+
49+
$this->autoLoad();
50+
}
51+
52+
public function action()
53+
{
54+
if (Version::compare(Typecho_Common::VERSION, Version::getVersion($this->isDevelop), '=')) {
55+
$this->response('Already up-to-date!');
56+
}
57+
58+
// download
59+
$archiveName = ($this->isDevelop ? 'develop_' : 'release_') . Version::toString(Version::getVersion($this->isDevelop));
60+
Downloader::down($this->isDevelop, $archiveName, $this->pluginRootPath);
61+
62+
// backup
63+
Archive::compress(sprintf('local_%s', Version::toString(Typecho_Common::VERSION)), $this->updateList, $this->pluginRootPath);
64+
65+
// decompression
66+
$resultPath = Archive::decompression($archiveName, $this->pluginRootPath);
67+
68+
// update
69+
foreach ($this->updateList as $realPath => $realRootPath) {
70+
Archive::clearPath($realPath);
71+
rename($resultPath . $this->updateNameMap[$realPath], $realPath);
72+
}
73+
74+
// finish
75+
$this->response('success');
76+
}
77+
78+
public function getVersion()
79+
{
80+
$this->response(Version::getVersion($this->isDevelop));
81+
}
82+
83+
protected function response($data, $code = 0)
84+
{
85+
headers_sent() || header('Content-Type: application/json; charset=utf-8;');
86+
87+
echo json_encode(
88+
[
89+
'code' => $code,
90+
'data' => $data,
91+
], JSON_UNESCAPED_UNICODE
92+
);
93+
94+
exit(0);
95+
}
96+
97+
protected function autoLoad()
98+
{
99+
$dependencePath = $this->pluginRootPath . DIRECTORY_SEPARATOR . 'library';
100+
101+
// First load base class
102+
include_once $dependencePath . DIRECTORY_SEPARATOR . 'Base.php';
103+
104+
foreach (scandir($dependencePath) as $item) {
105+
if (strpos($item, '.php') !== false && strpos($item, 'Base.php') === false) {
106+
include_once $dependencePath . DIRECTORY_SEPARATOR . $item;
107+
}
108+
}
109+
}
110+
}

UpdateAssistant/Plugin.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Update Assistant
4+
*
5+
* @package UpdateAssistant
6+
* @author mrgeneral
7+
* @version 1.0.1
8+
* @link https://www.chengxiaobai.cn
9+
*/
10+
11+
class UpdateAssistant_Plugin implements Typecho_Plugin_Interface
12+
{
13+
public static function activate()
14+
{
15+
Helper::addPanel(1, 'UpdateAssistant/Start.php', _t('Update Blogging Platform'), _t('Update Blogging Platform'), 'administrator');
16+
17+
Helper::addRoute('version_latest', '/update-assistant/version/latest', 'UpdateAssistant_Action', 'getVersion');
18+
Helper::addRoute('version_process', '/update-assistant/version/process', 'UpdateAssistant_Action', 'action');
19+
}
20+
21+
/**
22+
* For safety's sake, history is not deleted
23+
*/
24+
public static function deactivate()
25+
{
26+
Helper::removePanel(1, 'UpdateAssistant/Start.php');
27+
28+
Helper::removeRoute('version_latest');
29+
Helper::removeRoute('version_process');
30+
}
31+
32+
public static function config(Typecho_Widget_Helper_Form $form)
33+
{
34+
$element = new Typecho_Widget_Helper_Form_Element_Radio('isDevelop', [0 => _t('No'), 1 => _t('Yes')], 1, _t('Upgrade to developer edition'));
35+
$form->addInput($element);
36+
}
37+
38+
public static function personalConfig(Typecho_Widget_Helper_Form $form)
39+
{
40+
// TODO: Implement personalConfig() method.
41+
}
42+
}

UpdateAssistant/README.md

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Update Assistant for Typecho
2+
3+
This is a Typecho update plugin. It can help you update the Typecho to the latest version.
4+
5+
You can choose to update to the latest developer version or the latest release version, of course, you can switch between the two.
6+
7+
Don't worry. The update operation follows [the official update guide](http://docs.typecho.org/upgrade) and will be backed up before each operation, safe and secure.
8+
9+
## Requires
10+
11+
1. Plugin directory has writable permissions
12+
2. zip extension
13+
3. curl extension / Set `allow_url_fopen = 1` in your php.ini
14+
15+
16+
## Installation
17+
18+
1. [Download the plugin](https://github.com/mrgeneralgoo/typecho-update-assistant/archive/master.zip)
19+
2. Rename the folder name to "UpdateAssistant"
20+
3. Enable this plugin
21+
22+
## Usage
23+
24+
1. Settings allowed to update to developer version (default is true)
25+
2. Execute the update operation on the control panel
26+
27+
### Reporting issues
28+
29+
You can [create an issue](https://github.com/mrgeneralgoo/typecho-update-assistant/issues/new)
30+
31+
## Blog
32+
33+
https://www.chengxiaobai.cn/
34+
35+
------
36+
37+
这是一个 Typecho 升级插件,它能帮你把 Typecho 升级到最新版本。
38+
39+
你可以选择升级到最新的 developer 版本或者 release 版本,当然你也可以在两者之间切换。
40+
41+
放心,升级操作遵循[官方升级指南](http://docs.typecho.org/upgrade),每次操作前均会备份,安全无虞。
42+
43+
## 依赖
44+
45+
1. 插件目录有可写权限
46+
2. zip 扩展
47+
3. curl 扩展 / 设置 `allow_url_fopen = 1` 在你的 php.ini 中
48+
49+
## 安装
50+
51+
1. [下载这个插件](https://github.com/mrgeneralgoo/typecho-update-assistant/archive/master.zip)
52+
2. 将文件夹名称修改为 "UpdateAssistant"
53+
3. 添加到你的项目中并启用它
54+
55+
## 使用
56+
57+
1. 设置是否升级到 developer 版本(默认为 true)
58+
2. 在控制面板执行升级操作
59+
60+
### 报告问题
61+
62+
[你可以直接点击这里提出你的问题](https://github.com/mrgeneralgoo/typecho-update-assistant/issues/new)
63+
64+
## 我的博客
65+
https://www.chengxiaobai.cn/

UpdateAssistant/Start.php

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Typecho update assistant.
4+
*
5+
* @package UpdateAssistant
6+
* @author mrgeneral
7+
* @version 1.0.1
8+
* @link https://www.chengxiaobai.cn
9+
*/
10+
11+
include 'common.php';
12+
include 'header.php';
13+
include 'menu.php';
14+
include 'library/Base.php';
15+
include 'library/Version.php';
16+
?>
17+
18+
<?php
19+
$isDevelop = (bool)Helper::options()->plugin('UpdateAssistant')->isDevelop;
20+
$remoteVersion = Version::getVersion($isDevelop);
21+
?>
22+
<div class="main">
23+
<div class="body container">
24+
<?php include 'page-title.php'; ?>
25+
<div class="row typecho-page-main" role="main">
26+
<div class="col-mb-12">
27+
<div id="typecho-welcome">
28+
<?php if (!Version::compare(Typecho_Common::VERSION, $remoteVersion, '=')): ?>
29+
<h3><?php _e('<strong class="warning">%s</strong>', 'Found new edition'); ?></h3>
30+
<ul>
31+
<li><?php _e('Blog will be upgraded from <strong>%s</strong> to <strong>%s (%s)</strong>.', Typecho_Common::VERSION, $remoteVersion, $isDevelop ? 'dev' : 'release'); ?></li>
32+
<br>
33+
<li><?php _e('Please be patient. <strong>Depend on the network speed</strong>.'); ?></li>
34+
<br>
35+
<li><?php _e('Don\'t worry! The system will automatically backup your data. You can find it at <strong>\'%s\'</strong>', 'usr/plugins/UpdateAssistant/archive/back/'); ?></li>
36+
</ul>
37+
<p>
38+
<button class="btn primary" id="start_action"><?php _e('Action'); ?></button>
39+
</p>
40+
<?php else: ?>
41+
<h3><?php _e('Already up-to-date'); ?></h3>
42+
<li><?php _e('Current Version : <strong>%s</strong> Remote Version : <strong>%s (%s)</strong>', Typecho_Common::VERSION, $remoteVersion, $isDevelop ? 'dev' : 'release'); ?></li>
43+
<?php endif; ?>
44+
</div>
45+
</div>
46+
</div>
47+
</div>
48+
</div>
49+
50+
<?php
51+
include 'copyright.php';
52+
include 'common-js.php';
53+
?>
54+
<script>
55+
$("#start_action").click(function () {
56+
$(this).text("...").attr("disabled", "true");
57+
$.ajax({
58+
type: "GET",
59+
url: "<?php echo Helper::options()->siteUrl . 'update-assistant/version/process';?>",
60+
cache: false,
61+
async: true
62+
}).success(function (data) {
63+
var button = $("#start_action");
64+
button.text("Success");
65+
if (data.code === 0) {
66+
alert("Congratulations! Please login again");
67+
location.href = "<?php echo Helper::options()->siteUrl . "action/logout"; ?>";
68+
} else {
69+
button.text("Retry").removeAttr("disabled");
70+
alert(data.data);
71+
}
72+
}).error(function (xhr, status, error) {
73+
alert("Request fail!");
74+
});
75+
});
76+
</script>
77+
<?php include 'footer.php'; ?>
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#This is your backup folder.
2+
3+
**fileName style : local_{$currentBlogVersion}.zip**

0 commit comments

Comments
 (0)