BackupDao.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /**
  3. * @name eolinker ams open source,eolinker开源版本
  4. * @link https://www.eolinker.com/
  5. * @package eolinker
  6. * @author www.eolinker.com 广州银云信息科技有限公司 ©2015-2018
  7. * eoLinker是目前全球领先、国内最大的在线API接口管理平台,提供自动生成API文档、API自动化测试、Mock测试、团队协作等功能,旨在解决由于前后端分离导致的开发效率低下问题。
  8. * 如在使用的过程中有任何问题,欢迎加入用户讨论群进行反馈,我们将会以最快的速度,最好的服务态度为您解决问题。
  9. *
  10. * eoLinker AMS开源版的开源协议遵循Apache License 2.0,如需获取最新的eolinker开源版以及相关资讯,请访问:https://www.eolinker.com/#/os/download
  11. *
  12. * 官方网站:https://www.eolinker.com/
  13. * 官方博客以及社区:http://blog.eolinker.com/
  14. * 使用教程以及帮助:http://help.eolinker.com/
  15. * 商务合作邮箱:market@eolinker.com
  16. * 用户讨论QQ群:284421832
  17. */
  18. class BackupDao
  19. {
  20. /**
  21. * 获取备份项目的相关信息
  22. * @param $project_id
  23. * @return array|bool
  24. */
  25. public function getProjectBackupData(&$project_id)
  26. {
  27. $db = getDatabase();
  28. $dumpJson = array();
  29. // 获取项目信息
  30. $dumpJson['projectInfo'] = $db->prepareExecute("SELECT * FROM eo_project WHERE eo_project.projectID = ?;", array(
  31. $project_id
  32. ));
  33. $backup_time = date('Y/m/d H:i', time());
  34. $dumpJson['projectInfo']['projectName'] = "开源备份-{$dumpJson['projectInfo']['projectName']}-{$backup_time}";
  35. $dumpJson['apiGroupList'] = array();
  36. // 获取接口父分组信息
  37. $apiGroupList = $db->prepareExecuteAll("SELECT * FROM eo_api_group WHERE eo_api_group.projectID = ? AND eo_api_group.isChild = 0;", array(
  38. $project_id
  39. ));
  40. $i = 0;
  41. foreach ($apiGroupList as $apiGroup) {
  42. $dumpJson['apiGroupList'][$i] = $apiGroup;
  43. // 获取接口信息
  44. $apiList = $db->prepareExecuteAll("SELECT eo_api_cache.apiJson FROM eo_api_cache WHERE eo_api_cache.projectID = ? AND eo_api_cache.groupID = ?;", array(
  45. $project_id,
  46. $apiGroup['groupID']
  47. ));
  48. $dumpJson['apiGroupList'][$i]['apiList'] = array();
  49. $j = 0;
  50. foreach ($apiList as $api) {
  51. $dumpJson['apiGroupList'][$i]['apiList'][$j] = json_decode($api['apiJson'], TRUE);
  52. // $dumpJson['apiGroupList'][$i]['apiList'][$j]['baseInfo']['starred'] = $api['starred'];
  53. ++$j;
  54. }
  55. $dumpJson['apiGroupList'][$i]['apiGroupChildList'] = array();
  56. $apiGroupChildList = $db->prepareExecuteAll('SELECT * FROM eo_api_group WHERE eo_api_group.projectID = ? AND eo_api_group.parentGroupID = ?', array(
  57. $project_id,
  58. $apiGroup['groupID']
  59. ));
  60. $k = 0;
  61. if ($apiGroupChildList) {
  62. foreach ($apiGroupChildList as $apiChildGroup) {
  63. $dumpJson['apiGroupList'][$i]['apiGroupChildList'][$k] = $apiChildGroup;
  64. // 获取接口信息
  65. $apiList = $db->prepareExecuteAll("SELECT * FROM eo_api_cache WHERE eo_api_cache.projectID = ? AND eo_api_cache.groupID = ?;", array(
  66. $project_id,
  67. $apiChildGroup['groupID']
  68. ));
  69. $dumpJson['apiGroupList'][$i]['apiGroupChildList'][$k]['apiList'] = array();
  70. $l = 0;
  71. foreach ($apiList as $api) {
  72. $dumpJson['apiGroupList'][$i]['apiGroupChildList'][$k]['apiList'][$l] = json_decode($api['apiJson'], TRUE);
  73. // $dumpJson['apiGroupList'][$i]['apiGroupChildList'][$k]['apiList'][$l]['baseInfo']['starred'] = $api['starred'];
  74. ++$l;
  75. }
  76. ++$k;
  77. }
  78. }
  79. ++$i;
  80. }
  81. $dumpJson['statusCodeGroupList'] = array();
  82. // 获取状态码分组信息
  83. $statusCodeGroupList = $db->prepareExecuteAll("SELECT * FROM eo_project_status_code_group WHERE eo_project_status_code_group.projectID = ? AND isChild = 0;", array(
  84. $project_id
  85. ));
  86. $i = 0;
  87. foreach ($statusCodeGroupList as $statusCodeGroup) {
  88. $dumpJson['statusCodeGroupList'][$i] = $statusCodeGroup;
  89. // 获取状态码信息
  90. $statusCodeList = $db->prepareExecuteAll("SELECT * FROM eo_project_status_code WHERE eo_project_status_code.groupID = ?;", array(
  91. $statusCodeGroup['groupID']
  92. ));
  93. $dumpJson['statusCodeGroupList'][$i]['statusCodeList'] = array();
  94. $j = 0;
  95. foreach ($statusCodeList as $statusCode) {
  96. $dumpJson['statusCodeGroupList'][$i]['statusCodeList'][$j] = $statusCode;
  97. ++$j;
  98. }
  99. $statusCodeGroupChildList = $db->prepareExecuteAll("SELECT * FROM eo_project_status_code_group WHERE eo_project_status_code_group.projectID = ? AND parentGroupID = ?;", array(
  100. $project_id,
  101. $statusCodeGroup['groupID']
  102. ));
  103. $k = 0;
  104. $dumpJson['statusCodeGroupList'][$i]['statusCodeGroupChildList'] = array();
  105. if ($statusCodeGroupChildList) {
  106. foreach ($statusCodeGroupChildList as $statusCodeChildGroup) {
  107. $dumpJson['statusCodeGroupList'][$i]['statusCodeGroupChildList'][$k] = $statusCodeChildGroup;
  108. // 获取状态码信息
  109. $statusCodeList = $db->prepareExecuteAll("SELECT * FROM eo_project_status_code WHERE eo_project_status_code.groupID = ?;", array(
  110. $statusCodeChildGroup['groupID']
  111. ));
  112. $dumpJson['statusCodeGroupList'][$i]['statusCodeGroupChildList'][$k]['statusCodeList'] = array();
  113. $l = 0;
  114. foreach ($statusCodeList as $statusCode) {
  115. $dumpJson['statusCodeGroupList'][$i]['statusCodeGroupChildList'][$k]['statusCodeList'][$l] = $statusCode;
  116. ++$l;
  117. }
  118. ++$k;
  119. }
  120. }
  121. ++$i;
  122. }
  123. $dumpJson['env'] = array();
  124. // 获取环境管理相关信息
  125. $envList = $db->prepareExecuteAll('SELECT eo_api_env.envID,eo_api_env.envName FROM eo_api_env WHERE eo_api_env.projectID = ?;', array($project_id));
  126. if ($envList) {
  127. foreach ($envList as &$env) {
  128. $front_uri = $db->prepareExecute('SELECT eo_api_env_front_uri.applyProtocol,eo_api_env_front_uri.uri,eo_api_env_front_uri.uriID FROM eo_api_env_front_uri WHERE eo_api_env_front_uri.envID = ?;', array(
  129. $env['envID']
  130. ));
  131. $headers = $db->prepareExecuteAll('SELECT eo_api_env_header.applyProtocol,eo_api_env_header.headerName,eo_api_env_header.headerValue,eo_api_env_header.headerID FROM eo_api_env_header WHERE eo_api_env_header.envID = ?;', array(
  132. $env['envID']
  133. ));
  134. $params = $db->prepareExecuteAll('SELECT eo_api_env_param.paramKey,eo_api_env_param.paramValue,eo_api_env_param.paramID FROM eo_api_env_param WHERE eo_api_env_param.envID = ?;', array(
  135. $env['envID']
  136. ));
  137. $env['frontURI'] = $front_uri ? $front_uri : array();
  138. $env['headerList'] = $headers ? $headers : array();
  139. $env['paramList'] = $params ? $params : array();
  140. }
  141. }
  142. $dumpJson['env'] = $envList ? $envList : array();
  143. $dumpJson['pageGroupList'] = array();
  144. //获取项目文档分组信息
  145. $documentGroupList = $db->prepareExecuteAll('SELECT eo_project_document_group.* FROM eo_project_document_group WHERE eo_project_document_group.projectID = ? AND eo_project_document_group.isChild = 0;', array(
  146. $project_id
  147. ));
  148. $i = 0;
  149. foreach ($documentGroupList as $documentGroup) {
  150. $dumpJson['pageGroupList'][$i] = $documentGroup;
  151. $dumpJson['pageGroupList'][$i]['pageList'] = array();
  152. //获取文档信息
  153. $documentList = $db->prepareExecuteAll('SELECT eo_project_document.documentID AS pageID,eo_project_document.groupID,eo_project_document.projectID,eo_project_document.contentType,eo_project_document.contentRaw,eo_project_document.content,eo_project_document.title,eo_project_document.updateTime,eo_project_document.userID AS authorID FROM eo_project_document WHERE eo_project_document.groupID = ?;', array($documentGroup['groupID']));
  154. $j = 0;
  155. foreach ($documentList as $document) {
  156. $dumpJson['pageGroupList'][$i]['pageList'][$j] = $document;
  157. $dumpJson['pageGroupList'][$i]['pageList'][$j]['groupName'] = $documentGroup['groupName'];
  158. $j++;
  159. }
  160. $documentGroupChildList = $db->prepareExecuteAll('SELECT eo_project_document_group.* FROM eo_project_document_group WHERE eo_project_document_group.projectID = ? AND eo_project_document_group.parentGroupID = ? AND eo_project_document_group.isChild = 1;', array(
  161. $project_id,
  162. $documentGroup['groupID']
  163. ));
  164. $k = 0;
  165. $dumpJson['pageGroupList'][$i]['pageGroupChildList'] = array();
  166. if ($documentGroupChildList) {
  167. foreach ($documentGroupChildList as $documentChildGroup) {
  168. $dumpJson['pageGroupList'][$i]['pageGroupChildList'][$k] = $documentChildGroup;
  169. $dumpJson['pageGroupList'][$i]['pageGroupChildList'][$k]['pageList'] = array();
  170. //获取文档信息
  171. $documentList = $db->prepareExecuteAll('SELECT eo_project_document.documentID AS pageID,eo_project_document.groupID,eo_project_document.projectID,eo_project_document.contentType,eo_project_document.contentRaw,eo_project_document.content,eo_project_document.title,eo_project_document.updateTime,eo_project_document.userID AS authorID FROM eo_project_document WHERE eo_project_document.groupID = ?;', array($documentChildGroup['groupID']));
  172. $l = 0;
  173. foreach ($documentList as $document) {
  174. $dumpJson['pageGroupList'][$i]['pageGroupChildList'][$k]['pageList'][$l] = $document;
  175. $dumpJson['pageGroupList'][$i]['pageGroupChildList'][$k]['pageList'][$l]['groupName'] = $documentChildGroup['groupName'];
  176. $l++;
  177. }
  178. $k++;
  179. }
  180. }
  181. $i++;
  182. }
  183. if (empty($dumpJson))
  184. return FALSE;
  185. else
  186. return $dumpJson;
  187. }
  188. private $eol = PHP_EOL;
  189. private $sqlEnd = ';';
  190. /**
  191. * 数据库备份基本信息
  192. * @return string
  193. */
  194. private function retrieve()
  195. {
  196. $value = '';
  197. $value .= '/*' . $this->eol;
  198. $value .= 'eoLinker MySQL Data Transfer' . $this->eol;
  199. $value .= $this->eol;
  200. $value .= 'Source Host :' . DB_URL . ':' . DB_PORT . $this->eol;
  201. $value .= 'Source Database :' . DB_NAME . $this->eol;
  202. $value .= 'Date :' . date('Y-m-d H:i:s', time()) . $this->eol;
  203. $value .= $this->eol;
  204. $value .= '*/';
  205. $value .= $this->eol . $this->eol;
  206. return $value;
  207. }
  208. /**
  209. * 插入表结构
  210. * @param $table
  211. * @param $show_table
  212. * @return string
  213. */
  214. private function create_table_structure($table, $show_table)
  215. {
  216. $sql = '';
  217. $sql .= "-- ----------------------------" . $this->eol;
  218. $sql .= "-- Table structure for " . $table . $this->eol;
  219. $sql .= "-- ----------------------------" . $this->eol;
  220. // 如果存在则删除表
  221. $sql .= "DROP TABLE IF EXISTS `" . $table . '`' . $this->sqlEnd . $this->eol;
  222. // 获取详细表信息
  223. $sql .= $show_table['Create Table'];
  224. $sql .= $this->sqlEnd . $this->eol;
  225. // 加上
  226. $sql .= $this->eol;
  227. $sql .= "-- ----------------------------" . $this->eol;
  228. $sql .= "-- Records of " . $table . $this->eol;
  229. $sql .= "-- ----------------------------" . $this->eol;
  230. return $sql;
  231. }
  232. /**
  233. * 插入单条记录
  234. *
  235. * @param $table_name
  236. * @param $fields
  237. * @return string
  238. */
  239. private function insert_record($table_name, $fields)
  240. {
  241. // sql字段逗号分割
  242. $insert = '';
  243. $comma = "";
  244. $insert .= "INSERT INTO `$table_name` VALUES (";
  245. // 循环每个子段下面的内容
  246. foreach ($fields as $key => $value) {
  247. $insert .= $comma . "'" . addslashes($value) . "'";
  248. $comma = ',';
  249. }
  250. $insert .= ");" . $this->eol;
  251. return $insert;
  252. }
  253. /**
  254. * 获取数据库备份sql脚本
  255. * @return string
  256. */
  257. public function getDatabaseBackupSql()
  258. {
  259. $db = getDatabase();
  260. $sql = '';
  261. //插入dump信息
  262. $sql .= $this->retrieve();
  263. //查询所有表
  264. $tables = $db->queryAll('SHOW TABLES');
  265. defined('DB_TABLE_PREFIXION') or define('DB_TABLE_PREFIXION', 'eo');
  266. foreach ($tables as $table) {
  267. $table_name = $table['Tables_in_' . DB_NAME];
  268. if (!(strpos($table_name, DB_TABLE_PREFIXION) === 0))
  269. continue;
  270. //获取表结构
  271. $show_table = $db->query('SHOW CREATE TABLE `' . $table_name . '`');
  272. $sql .= $this->create_table_structure($table_name, $show_table);
  273. $columns = $db->queryAll('SELECT * FROM ' . $table_name);
  274. if (!empty($columns)) {
  275. foreach ($columns as $column) {
  276. $sql .= $this->insert_record($table_name, $column);
  277. }
  278. }
  279. $sql .= $this->eol;
  280. }
  281. return $sql;
  282. }
  283. }