You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

305 lines
12 KiB

4 years ago
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class UpdateController extends BaseController {
  5. //检测数据库并更新
  6. public function checkDb(){
  7. $version_num = 2 ;
  8. $db_version_num = D("Options")->get("db_version_num");
  9. if(!$db_version_num || $db_version_num < $version_num ){
  10. $r = $this->updateSqlite();
  11. if($r){
  12. D("Options")->set("db_version_num" , $version_num );
  13. }
  14. //echo '执行数据库升级';
  15. }
  16. $this->sendResult(array());
  17. }
  18. public function updateSqlite(){
  19. //catalog表增加parent_cat_id字段
  20. if (!$this->_is_column_exist("catalog","parent_cat_id")) {
  21. $sql = "ALTER TABLE ".C('DB_PREFIX')."catalog ADD parent_cat_id INT( 10 ) NOT NULL DEFAULT '0' ;";
  22. D("catalog")->execute($sql);
  23. }
  24. //catalog表增加level字段
  25. if (!$this->_is_column_exist("catalog","level")) {
  26. $sql = "ALTER TABLE ".C('DB_PREFIX')."catalog ADD level INT( 10 ) NOT NULL DEFAULT '2' ;";
  27. D("catalog")->execute($sql);
  28. }
  29. //item表增加item_domain字段
  30. if (!$this->_is_column_exist("item","item_domain")) {
  31. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD item_domain text NOT NULL DEFAULT '';";
  32. D("catalog")->execute($sql);
  33. }
  34. //创建user_token表
  35. $sql = "CREATE TABLE IF NOT EXISTS `user_token` (
  36. `id` INTEGER PRIMARY KEY ,
  37. `uid` int(10) NOT NULL DEFAULT '0',
  38. `token` CHAR(200) NOT NULL DEFAULT '',
  39. `token_expire` int(11) NOT NULL DEFAULT '0' ,
  40. `ip` CHAR(200) NOT NULL DEFAULT '',
  41. `addtime` int(11) NOT NULL DEFAULT '0'
  42. )";
  43. D("UserToken")->execute($sql);
  44. //创建template表
  45. $sql = "CREATE TABLE IF NOT EXISTS `template` (
  46. `id` INTEGER PRIMARY KEY ,
  47. `uid` int(10) NOT NULL DEFAULT '0',
  48. `username` CHAR(200) NOT NULL DEFAULT '',
  49. `template_title` CHAR(200) NOT NULL DEFAULT '' ,
  50. `template_content` text NOT NULL DEFAULT '',
  51. `addtime` int(11) NOT NULL DEFAULT '0'
  52. )";
  53. D("UserToken")->execute($sql);
  54. //page表增加page_comments字段
  55. if (!$this->_is_column_exist("page","page_comments")) {
  56. $sql = "ALTER TABLE ".C('DB_PREFIX')."page ADD page_comments text NOT NULL DEFAULT '' ;";
  57. D("catalog")->execute($sql);
  58. }
  59. //page_history 表增加page_comments字段
  60. if (!$this->_is_column_exist("PageHistory","page_comments")) {
  61. $sql = "ALTER TABLE ".C('DB_PREFIX')."page_history ADD page_comments text NOT NULL DEFAULT '';";
  62. D("catalog")->execute($sql);
  63. }
  64. //item_member表增加member_group_id字段
  65. if (!$this->_is_column_exist("ItemMember","member_group_id")) {
  66. $sql = "ALTER TABLE ".C('DB_PREFIX')."item_member ADD member_group_id INT( 1 ) NOT NULL DEFAULT '1' ;";
  67. D("ItemMember")->execute($sql);
  68. }
  69. //item表增加item_type字段
  70. if (!$this->_is_column_exist("Item","item_type")) {
  71. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD item_type INT( 1 ) NOT NULL DEFAULT '1' ;";
  72. D("ItemMember")->execute($sql);
  73. }
  74. //创建options表
  75. $sql = "CREATE TABLE IF NOT EXISTS `options` (
  76. `option_id` INTEGER PRIMARY KEY ,
  77. `option_name` CHAR(200) NOT NULL UNIQUE ,
  78. `option_value` CHAR(200) NOT NULL
  79. )";
  80. D("UserToken")->execute($sql);
  81. //创建item_token表
  82. $sql = "CREATE TABLE IF NOT EXISTS `item_token` (
  83. `id` INTEGER PRIMARY KEY ,
  84. `item_id` int(11) NOT NULL DEFAULT '0' ,
  85. `api_key` CHAR(200) NOT NULL UNIQUE ,
  86. `api_token` CHAR(200) NOT NULL ,
  87. `addtime` int(11) NOT NULL DEFAULT '0' ,
  88. `last_check_time` int(11) NOT NULL DEFAULT '0'
  89. )";
  90. D("UserToken")->execute($sql);
  91. //创建item_top表
  92. $sql = "CREATE TABLE IF NOT EXISTS `item_top` (
  93. `id` INTEGER PRIMARY KEY ,
  94. `item_id` int(11) NOT NULL DEFAULT '0' ,
  95. `uid` int(11) NOT NULL DEFAULT '0' ,
  96. `addtime` int(11) NOT NULL DEFAULT '0'
  97. )";
  98. D("UserToken")->execute($sql);
  99. //item表增加is_archived字段
  100. if (!$this->_is_column_exist("Item","is_archived")) {
  101. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD is_archived INT( 1 ) NOT NULL DEFAULT '0' ;";
  102. D("ItemMember")->execute($sql);
  103. }
  104. //管理员账户和权限
  105. if(D("User")->where("username = 'showdoc' ")->find()){
  106. D("User")->where("username = 'showdoc' ")->save(array("groupid"=> 1)) ;
  107. }else{
  108. D("User")->add(array('username'=>"showdoc" ,"groupid"=>1,'password'=>"a89da13684490eb9ec9e613f91d24d00" , 'reg_time'=>time()));
  109. }
  110. //item表增加is_del字段
  111. if (!$this->_is_column_exist("Item","is_del")) {
  112. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD is_del INT( 1 ) NOT NULL DEFAULT '0' ;";
  113. D("ItemMember")->execute($sql);
  114. }
  115. //page表增加is_del字段
  116. if (!$this->_is_column_exist("Page","is_del")) {
  117. $sql = "ALTER TABLE ".C('DB_PREFIX')."page ADD is_del INT( 1 ) NOT NULL DEFAULT '0' ;";
  118. D("ItemMember")->execute($sql);
  119. }
  120. //创建team表
  121. $sql = "CREATE TABLE IF NOT EXISTS `team` (
  122. `id` INTEGER PRIMARY KEY ,
  123. `team_name` CHAR(200) NOT NULL DEFAULT '',
  124. `uid` int(11) NOT NULL DEFAULT '0' ,
  125. `username` CHAR(200) NOT NULL DEFAULT '',
  126. `addtime` int(11) NOT NULL DEFAULT '0' ,
  127. `last_update_time` int(11) NOT NULL DEFAULT '0'
  128. )";
  129. D("User")->execute($sql);
  130. //创建team_item表
  131. $sql = "CREATE TABLE IF NOT EXISTS `team_item` (
  132. `id` INTEGER PRIMARY KEY ,
  133. `team_id` int(11) NOT NULL DEFAULT '0' ,
  134. `item_id` int(11) NOT NULL DEFAULT '0' ,
  135. `addtime` int(11) NOT NULL DEFAULT '0' ,
  136. `last_update_time` int(11) NOT NULL DEFAULT '0'
  137. )";
  138. D("User")->execute($sql);
  139. //创建team_item_member表
  140. $sql = "CREATE TABLE IF NOT EXISTS `team_item_member` (
  141. `id` INTEGER PRIMARY KEY ,
  142. `team_id` int(11) NOT NULL DEFAULT '0' ,
  143. `item_id` int(11) NOT NULL DEFAULT '0' ,
  144. `member_group_id` int(11) NOT NULL DEFAULT '0' ,
  145. `member_uid` int(11) NOT NULL DEFAULT '0' ,
  146. `member_username` CHAR(200) NOT NULL DEFAULT '',
  147. `addtime` int(11) NOT NULL DEFAULT '0' ,
  148. `last_update_time` int(11) NOT NULL DEFAULT '0'
  149. )";
  150. D("User")->execute($sql);
  151. //创建team_member表
  152. $sql = "CREATE TABLE IF NOT EXISTS `team_member` (
  153. `id` INTEGER PRIMARY KEY ,
  154. `team_id` int(11) NOT NULL DEFAULT '0' ,
  155. `member_uid` int(11) NOT NULL DEFAULT '0' ,
  156. `member_username` CHAR(200) NOT NULL DEFAULT '',
  157. `addtime` int(11) NOT NULL DEFAULT '0' ,
  158. `last_update_time` int(11) NOT NULL DEFAULT '0'
  159. )";
  160. D("User")->execute($sql);
  161. //创建upload_file表
  162. $sql = "CREATE TABLE IF NOT EXISTS `upload_file` (
  163. `file_id` INTEGER PRIMARY KEY ,
  164. `sign` CHAR(200) NOT NULL DEFAULT '',
  165. `display_name` CHAR(200) NOT NULL DEFAULT '',
  166. `file_type` CHAR(200) NOT NULL DEFAULT '',
  167. `file_size` CHAR(200) NOT NULL DEFAULT '',
  168. `uid` int(11) NOT NULL DEFAULT '0' ,
  169. `page_id` int(11) NOT NULL DEFAULT '0' ,
  170. `item_id` int(11) NOT NULL DEFAULT '0' ,
  171. `visit_times` int(11) NOT NULL DEFAULT '0' ,
  172. `addtime` int(11) NOT NULL DEFAULT '0' ,
  173. `real_url` CHAR(200) NOT NULL DEFAULT '',
  174. `last_update_time` int(11) NOT NULL DEFAULT '0'
  175. )";
  176. D("User")->execute($sql);
  177. //创建item_sort表
  178. $sql = "CREATE TABLE IF NOT EXISTS `item_sort` (
  179. `id` INTEGER PRIMARY KEY ,
  180. `uid` int(10) NOT NULL DEFAULT '0',
  181. `item_sort_data` text NOT NULL DEFAULT '',
  182. `addtime` int(11) NOT NULL DEFAULT '0'
  183. )";
  184. D("UserToken")->execute($sql);
  185. //创建single_page表
  186. $sql = "CREATE TABLE IF NOT EXISTS `single_page` (
  187. `id` INTEGER PRIMARY KEY ,
  188. `unique_key` CHAR(200) NOT NULL DEFAULT '',
  189. `page_id` int(11) NOT NULL DEFAULT '0'
  190. )";
  191. D("User")->execute($sql);
  192. //创建captcha表
  193. $sql = "CREATE TABLE IF NOT EXISTS `captcha` (
  194. `captcha_id` INTEGER PRIMARY KEY ,
  195. `mobile` CHAR(200) NOT NULL DEFAULT '',
  196. `captcha` CHAR(200) NOT NULL DEFAULT '',
  197. `expire_time` int(11) NOT NULL DEFAULT '0'
  198. )";
  199. D("User")->execute($sql);
  200. //创建recycle表
  201. $sql = "CREATE TABLE IF NOT EXISTS `recycle` (
  202. `id` INTEGER PRIMARY KEY ,
  203. `item_id` int(11) NOT NULL DEFAULT '0',
  204. `page_id` int(11) NOT NULL DEFAULT '0',
  205. `page_title` CHAR(200) NOT NULL DEFAULT '',
  206. `del_by_uid` int(11) NOT NULL DEFAULT '0',
  207. `del_by_username` CHAR(200) NOT NULL DEFAULT '',
  208. `del_time` int(11) NOT NULL DEFAULT '0'
  209. )";
  210. D("User")->execute($sql);
  211. //创建page_lock表
  212. $sql = "CREATE TABLE IF NOT EXISTS `page_lock` (
  213. `id` INTEGER PRIMARY KEY ,
  214. `page_id` int(11) NOT NULL DEFAULT '0',
  215. `lock_uid` int(11) NOT NULL DEFAULT '0',
  216. `lock_username` CHAR(200) NOT NULL DEFAULT '',
  217. `lock_to` int(11) NOT NULL DEFAULT '0',
  218. `addtime` int(11) NOT NULL DEFAULT '0'
  219. )";
  220. D("User")->execute($sql);
  221. //item_member表增加cat_id字段
  222. if (!$this->_is_column_exist("item_member","cat_id")) {
  223. $sql = "ALTER TABLE ".C('DB_PREFIX')."item_member ADD cat_id INT( 10 ) NOT NULL DEFAULT '0' ;";
  224. D("User")->execute($sql);
  225. }
  226. //team_item_member表增加cat_id字段
  227. if (!$this->_is_column_exist("team_item_member","cat_id")) {
  228. $sql = "ALTER TABLE ".C('DB_PREFIX')."team_item_member ADD cat_id INT( 10 ) NOT NULL DEFAULT '0' ;";
  229. D("User")->execute($sql);
  230. }
  231. //创建item_variable表
  232. $sql = "CREATE TABLE IF NOT EXISTS `item_variable` (
  233. `id` INTEGER PRIMARY KEY ,
  234. `var_name` CHAR(2000) NOT NULL DEFAULT '',
  235. `var_value` CHAR(2000) NOT NULL DEFAULT '',
  236. `uid` int(11) NOT NULL DEFAULT '0',
  237. `item_id` int(11) NOT NULL DEFAULT '0',
  238. `addtime` int(11) NOT NULL DEFAULT '0'
  239. )";
  240. D("User")->execute($sql);
  241. //创建file_flow表
  242. $sql = "CREATE TABLE IF NOT EXISTS `file_flow` (
  243. `id` INTEGER PRIMARY KEY ,
  244. `uid` int(11) NOT NULL DEFAULT '0',
  245. `used` int(11) NOT NULL DEFAULT '0',
  246. `date_month` CHAR(2000) NOT NULL DEFAULT ''
  247. )";
  248. D("User")->execute($sql);
  249. //留个注释提醒自己,如果更新数据库结构,务必更改上面的$version_num
  250. //留个注释提醒自己,如果更新数据库结构,务必更改上面的$version_num
  251. //留个注释提醒自己,如果更新数据库结构,务必更改上面的$version_num
  252. return true ;
  253. }
  254. private function _is_column_exist($table , $column){
  255. $has_it = false ;//是否存在该字段
  256. $columns = M($table)->getDbFields();
  257. if ($columns) {
  258. foreach ($columns as $key => $value) {
  259. if ($value == $column) {
  260. $has_it = true ;
  261. }
  262. }
  263. }
  264. return $has_it ;
  265. }
  266. }