* finish task #1791.
This commit is contained in:
89
lib/zdb/zdb.class.php
Normal file
89
lib/zdb/zdb.class.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
/**
|
||||
* The zdb library of zentaopms. This lib operate db.
|
||||
*
|
||||
* @copyright Copyright 2009-2013 QingDao Nature Easy Soft Network Technology Co,LTD (www.cnezsoft.com)
|
||||
* @license LGPL (http://www.gnu.org/licenses/lgpl.html)
|
||||
* @author Yidong Wang <yidong@cnezsoft.com>
|
||||
* @package Zdb
|
||||
* @version $Id$
|
||||
* @link http://www.zentao.net
|
||||
*/
|
||||
class zdb
|
||||
{
|
||||
/**
|
||||
* dbh
|
||||
*
|
||||
* @var int
|
||||
* @access public
|
||||
*/
|
||||
public $dbh;
|
||||
|
||||
/**
|
||||
* Construct
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
global $dbh;
|
||||
$this->dbh = $dbh;
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup db.
|
||||
*
|
||||
* @param string $fileName
|
||||
* @param array $tables
|
||||
* @access public
|
||||
* @return string|int
|
||||
*/
|
||||
public function backupDB($fileName, $tables = array())
|
||||
{
|
||||
if(empty($tables))
|
||||
{
|
||||
$stmt = $this->dbh->query('show tables');
|
||||
while($table = $stmt->fetch(PDO::FETCH_ASSOC)) $tables[] = current($table);
|
||||
}
|
||||
|
||||
if(empty($fileName)) return 'Has not file';
|
||||
if(!is_writable(dirname($fileName))) return 'The directory is not writable';
|
||||
$fp = fopen($fileName, 'w');
|
||||
|
||||
fwrite($fp, "SET NAMES utf8;\n");
|
||||
foreach($tables as $table)
|
||||
{
|
||||
$backupSql = "DROP TABLE IF EXISTS `$table`;\n";
|
||||
|
||||
$createSql = $this->dbh->query("show create table `$table`")->fetch(PDO::FETCH_ASSOC);
|
||||
$backupSql .= $createSql['Create Table'] . ";\n";
|
||||
|
||||
$datas = $this->dbh->query("select * from `$table`")->fetchAll(PDO::FETCH_ASSOC);
|
||||
if(!empty($datas))
|
||||
{
|
||||
$keys = array_keys(current($datas));
|
||||
$keys = array_map('addslashes', $keys);
|
||||
$keys = join('`,`', $keys);
|
||||
$keys = "`" . $keys . "`";
|
||||
|
||||
$values = array();
|
||||
foreach($datas as $data)
|
||||
{
|
||||
$value = array_values($data);
|
||||
$value = array_map('addslashes', $value);
|
||||
$value = join("','", $value);
|
||||
$value = "'" . $value . "'";
|
||||
|
||||
$values[] = "($value)";
|
||||
}
|
||||
$backupSql .= "INSERT INTO `$table`($keys) VALUES" . join(',', $values) . ";\n";
|
||||
}
|
||||
|
||||
fwrite($fp, $backupSql);
|
||||
}
|
||||
fclose ($fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
11
module/extension/control.php
Normal file → Executable file
11
module/extension/control.php
Normal file → Executable file
@@ -339,8 +339,15 @@ class extension extends control
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function uninstall($extension)
|
||||
public function uninstall($extension, $confirm = 'no')
|
||||
{
|
||||
$dbFile = $this->extension->getDBFile($extension, 'uninstall');
|
||||
if($confirm == 'no' and file_exists($dbFile))
|
||||
{
|
||||
$this->view->confirm = 'no';
|
||||
$this->view->code = $extension;
|
||||
die($this->display());
|
||||
}
|
||||
|
||||
$dependsExts = $this->extension->checkDepends($extension);
|
||||
if($dependsExts)
|
||||
@@ -351,6 +358,8 @@ class extension extends control
|
||||
|
||||
if($preUninstallHook = $this->extension->getHookFile($extension, 'preuninstall')) include $preUninstallHook;
|
||||
|
||||
if(file_exists($dbFile)) $this->view->backDBName = $this->extension->backupDB($extension);
|
||||
|
||||
$this->extension->executeDB($extension, 'uninstall');
|
||||
$this->extension->updateExtension($extension, array('status' => 'available'));
|
||||
$this->view->removeCommands = $this->extension->removePackage($extension);
|
||||
|
||||
3
module/extension/css/common.css
Normal file → Executable file
3
module/extension/css/common.css
Normal file → Executable file
@@ -1,6 +1,7 @@
|
||||
.active a {color:blue; font-weight:bold;}
|
||||
.button-c {padding:1px 3px; border:1px solid gray; background:#e4e4ef; color:black; text-decoration:none}
|
||||
.error {color:red; font-size:14px}
|
||||
.waring {color:red; font-size:14px}
|
||||
.success {color:green; font-size:14px}
|
||||
|
||||
.exttable td {padding:10px 5px 5px 10px}
|
||||
@@ -12,4 +13,4 @@
|
||||
|
||||
small{font-weight:normal}
|
||||
.exttable .button-c,.exttable .button-s{width: auto;min-width: inherit;}
|
||||
.side .box-content{margin-bottom: 10px;}
|
||||
.side .box-content{margin-bottom: 10px;}
|
||||
|
||||
@@ -64,6 +64,7 @@ $lang->extension->byCategory = 'By Category';
|
||||
|
||||
$lang->extension->installFailed = '%s failed, the reason is:';
|
||||
$lang->extension->uninstallFailed = 'Uninstall failed, the reason is:';
|
||||
$lang->extension->confirmUninstall = 'Uninstall will delete or modify database, whether to uninstall?';
|
||||
$lang->extension->installFinished = 'Good, the extension has been %s successfully.';
|
||||
$lang->extension->refreshPage = 'Refresh';
|
||||
$lang->extension->uninstallFinished = 'Extension has been successfully uninstalled.';
|
||||
@@ -78,6 +79,7 @@ $lang->extension->successInstallDB = 'Successfully installed database.';
|
||||
$lang->extension->viewInstalled = 'View installed extensions.';
|
||||
$lang->extension->viewAvailable = 'View available extensions';
|
||||
$lang->extension->viewDeactivated = 'View deactivated extensions';
|
||||
$lang->extension->backDBFile = 'Plug-related data has been backed up to %s file!';
|
||||
|
||||
$lang->extension->upgradeExt = 'Upgrade';
|
||||
$lang->extension->installExt = 'Install';
|
||||
|
||||
2
module/extension/lang/zh-cn.php
Normal file → Executable file
2
module/extension/lang/zh-cn.php
Normal file → Executable file
@@ -64,6 +64,7 @@ $lang->extension->byCategory = '分类浏览';
|
||||
|
||||
$lang->extension->installFailed = '%s失败,错误原因如下:';
|
||||
$lang->extension->uninstallFailed = '卸载失败,错误原因如下:';
|
||||
$lang->extension->confirmUninstall = '卸载插件会删除或修改相关的数据库,是否继续卸载?';
|
||||
$lang->extension->installFinished = '恭喜您,插件顺利的%s成功!';
|
||||
$lang->extension->refreshPage = '刷新页面';
|
||||
$lang->extension->uninstallFinished = '插件已经成功卸载';
|
||||
@@ -78,6 +79,7 @@ $lang->extension->successInstallDB = '成功安装数据库';
|
||||
$lang->extension->viewInstalled = '查看已安装插件';
|
||||
$lang->extension->viewAvailable = '查看可安装插件';
|
||||
$lang->extension->viewDeactivated = '查看已禁用插件';
|
||||
$lang->extension->backDBFile = '插件相关数据已经备份到 %s 文件中!';
|
||||
|
||||
$lang->extension->upgradeExt = '升级';
|
||||
$lang->extension->installExt = '安装';
|
||||
|
||||
35
module/extension/model.php
Normal file → Executable file
35
module/extension/model.php
Normal file → Executable file
@@ -410,6 +410,7 @@ class extensionModel extends model
|
||||
public function checkDownloadPath()
|
||||
{
|
||||
/* Init the return. */
|
||||
$return = new stdclass();
|
||||
$return->result = 'ok';
|
||||
$return->error = '';
|
||||
|
||||
@@ -732,6 +733,40 @@ class extensionModel extends model
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup db when uninstall extension.
|
||||
*
|
||||
* @param string $extension
|
||||
* @access public
|
||||
* @return bool|string
|
||||
*/
|
||||
public function backupDB($extension)
|
||||
{
|
||||
$zdb = $this->app->loadClass('zdb');
|
||||
|
||||
$sqls = file_get_contents($this->getDBFile($extension, 'uninstall'));
|
||||
$sqls = explode(';', $sqls);
|
||||
|
||||
$backupTables = array();
|
||||
foreach($sqls as $sql)
|
||||
{
|
||||
$sql = trim($sql);
|
||||
if(preg_match('/^DROP TABLE `?([^` ]*)`?|^ALTER TABLE `?([^` ]*)`? .*DROP .+/i', $sql, $out))
|
||||
{
|
||||
if(!empty($out[1])) $backupTables[] = $out[1];
|
||||
if(!empty($out[2])) $backupTables[] = $out[2];
|
||||
}
|
||||
}
|
||||
|
||||
if($backupTables)
|
||||
{
|
||||
$backDBName = $this->app->getTmpRoot() . $extension . date('Ymd') . '.sql';
|
||||
if($zdb->backupDB($backDBName, $backupTables) == 0) return $backDBName;
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the extension to database.
|
||||
*
|
||||
|
||||
17
module/extension/view/uninstall.html.php
Normal file → Executable file
17
module/extension/view/uninstall.html.php
Normal file → Executable file
@@ -15,14 +15,21 @@
|
||||
<caption><?php echo $title;?></caption>
|
||||
<tr>
|
||||
<td valign='middle'>
|
||||
<?php if(!empty($error)):?>
|
||||
<?php
|
||||
echo "<h3 class='error'>" . $lang->extension->uninstallFailed . "</h3>";
|
||||
echo "<p>$error</p>";
|
||||
?>
|
||||
<?php if(isset($confirm) and $confirm == 'no'):?>
|
||||
<?php
|
||||
echo "<p class='waring'>{$lang->extension->confirmUninstall}";
|
||||
echo html::a(inlink('uninstall', "extension=$code&confirm=yes"), $lang->extension->uninstall);
|
||||
echo "</p>";
|
||||
?>
|
||||
<?php elseif(!empty($error)):?>
|
||||
<?php
|
||||
echo "<h3 class='error'>" . $lang->extension->uninstallFailed . "</h3>";
|
||||
echo "<p>$error</p>";
|
||||
?>
|
||||
<?php else:?>
|
||||
<?php
|
||||
echo "<h3 class='a-center success'>{$title}</h3>";
|
||||
if(!empty($backDBName)) echo '<p>' . sprintf($lang->extension->backDBFile, $backDBName) . '</p>';
|
||||
if($removeCommands)
|
||||
{
|
||||
echo "<p class='strong'>{$lang->extension->unremovedFiles}</p>";
|
||||
|
||||
Reference in New Issue
Block a user