add backup panel

This commit is contained in:
joyqi 2017-08-16 18:09:39 +08:00
parent 43995a346d
commit 3d8fbc8a61
4 changed files with 149 additions and 1 deletions

42
admin/backup.php Normal file
View File

@ -0,0 +1,42 @@
<?php
include 'common.php';
include 'header.php';
include 'menu.php';
?>
<div class="main">
<div class="body container">
<?php include 'page-title.php'; ?>
<div class="row typecho-page-main" role="main">
<div class="col-mb-12 col-tb-8">
<div id="typecho-welcome">
<form action="<?php echo $security->getTokenUrl(
Typecho_Router::url('do', array('action' => 'backup', 'widget' => 'Backup'),
Typecho_Common::url('index.php', $options->rootUrl))); ?>" method="post">
<h3><?php _e('备份您的数据'); ?></h3>
<ul>
<li><?php _e('此备份操作仅包含<strong>内容数据</strong>, 并不会涉及任何<strong>设置信息</strong>'); ?></li>
<li><?php _e('如果您的数据量过大, 为了避免操作超时, 建议您直接使用数据库提供的备份工具备份数据'); ?></li>
<li><strong class="warning"><?php _e('为了缩小备份文件体积, 建议您在备份前删除不必要的数据'); ?></strong></li>
</ul>
<p><button class="btn primary" type="submit"><?php _e('开始备份 &raquo;'); ?></button></p>
<input type="hidden" name="do" value="export">
</form>
</div>
</div>
<div class="col-mb-12 col-tb-4" role="form">
<h4><?php _e('上传一个备份文件恢复数据'); ?></h4>
<div id="upload-panel" class="p">
<div class="upload-area" draggable="true"><?php _e('拖放文件到这里<br>或者 %s选择文件上传%s', '<a href="###" class="upload-file">', '</a>'); ?></div>
</div>
</div>
</div>
</div>
</div>
<?php
include 'copyright.php';
include 'common-js.php';
include 'footer.php';
?>

104
var/Widget/Backup.php Normal file
View File

@ -0,0 +1,104 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* Typecho Blog Platform
*
* @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
/**
* 备份工具
*
* @package Widget
*/
class Widget_Backup extends Widget_Abstract_Options implements Widget_Interface_Do
{
/**
* @var array
*/
private $_types = [
'contents' => 1,
'comments' => 2,
'metas' => 3,
'relationships' => 4,
'users' => 5,
'fields' => 6
];
/**
* @param $type
* @param $data
* @return string
*/
private function buildBuffer($type, $data)
{
$buffer = '';
$body = '';
$schema = [];
foreach ($data as $key => $val) {
$schema[$key] = strlen($val);
$body .= $val;
}
$schemaHeader = json_encode($schema);
$buffer .= pack('C', $type); // 写入类型
$buffer .= pack('v', strlen($schemaHeader)); // 写入头
$buffer .= $schemaHeader . $body;
return $buffer;
}
/**
* 导出数据
*/
private function export()
{
$host = parse_url($this->options->siteUrl, PHP_URL_HOST);
$this->response->setContentType('application/octet-stream');
$this->response->setHeader('Content-Disposition', 'attachment; filename="'
. date('Ymd') . '_' . $host . '_' . uniqid() . '.dat"');
$buffer = '';
$db = Typecho_Db::get();
foreach ($this->_types as $type => $val) {
$page = 1;
do {
$rows = $db->fetchAll($db->select()->from('table.' . $type)->page($page, 20));
$page ++;
foreach ($rows as $row) {
$buffer .= $this->buildBuffer($val, $row);
if (sizeof($buffer) >= 1024 * 1024) {
echo $buffer;
ob_flush();
$buffer = '';
}
}
} while (count($rows) == 20);
}
if (!empty($buffer)) {
echo $buffer;
ob_flush();
}
Typecho_Plugin::factory(__CLASS__)->export();
}
/**
* 绑定动作
*/
public function action()
{
$this->user->pass('administrator');
$this->security->protect();
$this->on($this->request->is('do=export'))->export();
$this->on($this->request->is('do=import'))->import();
}
}

View File

@ -43,7 +43,8 @@ class Widget_Do extends Typecho_Widget
'plugins-edit' => 'Widget_Plugins_Edit',
'themes-edit' => 'Widget_Themes_Edit',
'users-edit' => 'Widget_Users_Edit',
'users-profile' => 'Widget_Users_Profile'
'users-profile' => 'Widget_Users_Profile',
'backup' => 'Widget_Backup'
);
/**

View File

@ -116,6 +116,7 @@ class Widget_Menu extends Typecho_Widget
array(_t('外观'), _t('网站外观'), 'themes.php', 'administrator'),
array(array('Widget_Themes_Files', 'getMenuTitle'), array('Widget_Themes_Files', 'getMenuTitle'), 'theme-editor.php', 'administrator', true),
array(_t('设置外观'), _t('设置外观'), 'options-theme.php', 'administrator', true),
array(_t('备份'), _t('备份'), 'backup.php', 'administrator'),
array(_t('升级'), _t('升级程序'), 'upgrade.php', 'administrator', true),
array(_t('欢迎'), _t('欢迎使用'), 'welcome.php', 'subscriber', true)
),