UpdateController.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class UpdateController extends BaseController {
  5. //升级数据库
  6. public function db(){
  7. $this->_clear_runtime();
  8. if (strtolower(C("DB_TYPE")) == 'mysql' ) {
  9. //$this->mysql();
  10. echo 'ShowDoc does not support mysql any more . https://www.showdoc.cc/help?page_id=31990 ';
  11. }
  12. elseif (strtolower(C("DB_TYPE")) == 'sqlite' ) {
  13. $this->sqlite();
  14. }
  15. $this->_clear_runtime();
  16. }
  17. public function sqlite(){
  18. //catalog表增加parent_cat_id字段
  19. $columns = M("catalog")->getDbFields();
  20. if ($columns) {
  21. $has_it = 0 ;//是否存在该字段
  22. foreach ($columns as $key => $value) {
  23. if ($value == 'parent_cat_id') {
  24. $has_it = 1 ;
  25. }
  26. }
  27. if ($has_it === 0) {
  28. $sql = "ALTER TABLE ".C('DB_PREFIX')."catalog ADD parent_cat_id INT( 10 ) NOT NULL DEFAULT '0' ;";
  29. D("catalog")->execute($sql);
  30. }
  31. }
  32. //catalog表增加level字段
  33. $columns = M("catalog")->getDbFields();
  34. if ($columns) {
  35. $has_it = 0 ;//是否存在该字段
  36. foreach ($columns as $key => $value) {
  37. if ($value == 'level') {
  38. $has_it = 1 ;
  39. }
  40. }
  41. if ($has_it === 0) {
  42. $sql = "ALTER TABLE ".C('DB_PREFIX')."catalog ADD level INT( 10 ) NOT NULL DEFAULT '2' ;";
  43. D("catalog")->execute($sql);
  44. }
  45. }
  46. //item表增加item_domain字段
  47. $columns = M("item")->getDbFields();
  48. if ($columns) {
  49. $has_it = 0 ;//是否存在该字段
  50. foreach ($columns as $key => $value) {
  51. if ($value == 'item_domain') {
  52. $has_it = 1 ;
  53. }
  54. }
  55. if ($has_it === 0) {
  56. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD item_domain text NOT NULL DEFAULT '';";
  57. D("item")->execute($sql);
  58. }
  59. }
  60. //创建user_token表
  61. $sql = "CREATE TABLE IF NOT EXISTS `user_token` (
  62. `id` INTEGER PRIMARY KEY ,
  63. `uid` int(10) NOT NULL DEFAULT '0',
  64. `token` CHAR(200) NOT NULL DEFAULT '',
  65. `token_expire` int(11) NOT NULL DEFAULT '0' ,
  66. `ip` CHAR(200) NOT NULL DEFAULT '',
  67. `addtime` int(11) NOT NULL DEFAULT '0'
  68. )";
  69. D("UserToken")->execute($sql);
  70. //创建template表
  71. $sql = "CREATE TABLE IF NOT EXISTS `template` (
  72. `id` INTEGER PRIMARY KEY ,
  73. `uid` int(10) NOT NULL DEFAULT '0',
  74. `username` CHAR(200) NOT NULL DEFAULT '',
  75. `template_title` CHAR(200) NOT NULL DEFAULT '' ,
  76. `template_content` text NOT NULL DEFAULT '',
  77. `addtime` int(11) NOT NULL DEFAULT '0'
  78. )";
  79. D("UserToken")->execute($sql);
  80. //page表增加page_comments字段
  81. $columns = D("Page")->getDbFields();
  82. if ($columns) {
  83. $has_it = 0 ;//是否存在该字段
  84. foreach ($columns as $key => $value) {
  85. if ($value == 'page_comments') {
  86. $has_it = 1 ;
  87. }
  88. }
  89. if ($has_it === 0) {
  90. $sql = "ALTER TABLE ".C('DB_PREFIX')."page ADD page_comments text NOT NULL DEFAULT '' ;";
  91. D("Page")->execute($sql);
  92. }
  93. }
  94. //page_history 表增加page_comments字段
  95. $columns = D("PageHistory")->getDbFields();
  96. if ($columns) {
  97. $has_it = 0 ;//是否存在该字段
  98. foreach ($columns as $key => $value) {
  99. if ($value == 'page_comments') {
  100. $has_it = 1 ;
  101. }
  102. }
  103. if ($has_it === 0) {
  104. $sql = "ALTER TABLE ".C('DB_PREFIX')."page_history ADD page_comments text NOT NULL DEFAULT '';";
  105. D("PageHistory")->execute($sql);
  106. }
  107. }
  108. //item_member表增加member_group_id字段
  109. $columns = M("ItemMember")->getDbFields();
  110. if ($columns) {
  111. $has_it = 0 ;//是否存在该字段
  112. foreach ($columns as $key => $value) {
  113. if ($value == 'member_group_id') {
  114. $has_it = 1 ;
  115. }
  116. }
  117. if ($has_it === 0) {
  118. $sql = "ALTER TABLE ".C('DB_PREFIX')."item_member ADD member_group_id INT( 1 ) NOT NULL DEFAULT '1' ;";
  119. D("ItemMember")->execute($sql);
  120. }
  121. }
  122. //item表增加item_type字段
  123. $columns = M("Item")->getDbFields();
  124. if ($columns) {
  125. $has_it = 0 ;//是否存在该字段
  126. foreach ($columns as $key => $value) {
  127. if ($value == 'item_type') {
  128. $has_it = 1 ;
  129. }
  130. }
  131. if ($has_it === 0) {
  132. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD item_type INT( 1 ) NOT NULL DEFAULT '1' ;";
  133. D("Item")->execute($sql);
  134. }
  135. }
  136. //创建options表
  137. $sql = "CREATE TABLE IF NOT EXISTS `options` (
  138. `option_id` INTEGER PRIMARY KEY ,
  139. `option_name` CHAR(200) NOT NULL UNIQUE ,
  140. `option_value` CHAR(200) NOT NULL
  141. )";
  142. D("UserToken")->execute($sql);
  143. echo 'OK!';
  144. }
  145. private function _clear_runtime($path = RUNTIME_PATH){
  146. //给定的目录不是一个文件夹
  147. if(!is_dir($path)){
  148. return null;
  149. }
  150. $fh = opendir($path);
  151. while(($row = readdir($fh)) !== false){
  152. //过滤掉虚拟目录
  153. if($row == '.' || $row == '..'|| $row == 'index.html'){
  154. continue;
  155. }
  156. if(!is_dir($path.'/'.$row)){
  157. unlink($path.'/'.$row);
  158. }
  159. $this->_clear_runtime($path.'/'.$row);
  160. }
  161. //关闭目录句柄,否则出Permission denied
  162. closedir($fh);
  163. return true;
  164. }
  165. //转移mysql的数据到sqlite
  166. public function toSqlite(){
  167. $this->_clear_runtime();
  168. if (strtolower(C("DB_TYPE")) == 'mysql' ) {
  169. $this->mysql();
  170. $this->_moveTable("catalog");
  171. $this->_moveTable("item");
  172. $this->_moveTable("item_member");
  173. $this->_moveTable("page");
  174. $this->_moveTable("page_history");
  175. $this->_moveTable("template");
  176. $this->_moveTable("user");
  177. $this->_moveTable("user_token");
  178. $db_config = array(
  179. 'DB_TYPE' => 'Sqlite',
  180. 'DB_NAME' => 'Sqlite/showdoc.db.php',
  181. );
  182. $array = M("item")->db(2,$db_config)->select();
  183. if ($array) {
  184. echo "ok";
  185. }else{
  186. echo 'fail';
  187. }
  188. }
  189. else{
  190. echo "mysql not found";
  191. }
  192. $this->_clear_runtime();
  193. }
  194. //升级mysql数据库
  195. public function mysql(){
  196. //user表的username字段增大了长度,防止长邮箱的用户名注册不了
  197. $sql = "alter table ".C('DB_PREFIX')."user modify column username varchar(50) CHARACTER SET utf8 NOT NULL DEFAULT '' ";
  198. M("Catalog")->execute($sql);
  199. //item表增加last_update_time字段
  200. $columns = M("item")->getDbFields();
  201. if ($columns) {
  202. $has_it = 0 ;//是否存在该字段
  203. foreach ($columns as $key => $value) {
  204. if ($value == 'last_update_time') {
  205. $has_it = 1 ;
  206. }
  207. }
  208. if ($has_it === 0) {
  209. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD last_update_time INT( 11 ) NOT NULL DEFAULT '0' COMMENT '最后更新时间';";
  210. D("Item")->execute($sql);
  211. }
  212. }
  213. //更改catalog表的order字段名为s_number
  214. $columns = M("Catalog")->getDbFields();
  215. if ($columns) {
  216. foreach ($columns as $key => $value) {
  217. if ($value == 'order') {
  218. $sql = "ALTER TABLE `".C('DB_PREFIX')."catalog` CHANGE `order` `s_number` INT( 10 ) NOT NULL DEFAULT '99' COMMENT '顺序号。数字越小越靠前。若此值全部相等时则按id排序';";
  219. M("Catalog")->execute($sql);
  220. }
  221. }
  222. }
  223. //更改page表的order字段名为s_number
  224. $columns = M("Page")->getDbFields();
  225. if ($columns) {
  226. foreach ($columns as $key => $value) {
  227. if ($value == 'order') {
  228. $sql = "ALTER TABLE `".C('DB_PREFIX')."page` CHANGE `order` `s_number` INT( 10 ) NOT NULL DEFAULT '99' COMMENT '顺序号。数字越小越靠前。若此值全部相等时则按id排序';";
  229. M("Page")->execute($sql);
  230. }
  231. }
  232. }
  233. //更改page_history表的order字段名为s_number
  234. $columns = M("PageHistory")->getDbFields();
  235. if ($columns) {
  236. foreach ($columns as $key => $value) {
  237. if ($value == 'order') {
  238. $sql = "ALTER TABLE `".C('DB_PREFIX')."page_history` CHANGE `order` `s_number` INT( 10 ) NOT NULL DEFAULT '99' COMMENT '顺序号。数字越小越靠前。若此值全部相等时则按id排序';";
  239. M("PageHistory")->execute($sql);
  240. }
  241. }
  242. }
  243. //为catalog表增加addtime索引
  244. $indexs = M("Catalog")->query(" show index from ".C('DB_PREFIX')."catalog");
  245. if ($indexs) {
  246. $has_it = 0 ;//是否存在该索引
  247. foreach ($indexs as $key => $value) {
  248. if ($value['column_name'] =='addtime') {
  249. $has_it = 1 ;
  250. }
  251. }
  252. if ($has_it === 0 ) {
  253. M("Catalog")->execute("ALTER TABLE ".C('DB_PREFIX')."catalog ADD INDEX ( `addtime` ) ;");
  254. }
  255. }
  256. //为item表增加addtime索引
  257. $indexs = M("Item")->query(" show index from ".C('DB_PREFIX')."item");
  258. if ($indexs) {
  259. $has_it = 0 ;//是否存在该索引
  260. foreach ($indexs as $key => $value) {
  261. if ($value['column_name'] =='addtime') {
  262. $has_it = 1 ;
  263. }
  264. }
  265. if ($has_it === 0 ) {
  266. M("Item")->execute("ALTER TABLE ".C('DB_PREFIX')."item ADD INDEX ( `addtime` ) ;");
  267. }
  268. }
  269. //为page表增加addtime索引
  270. $indexs = M("Page")->query(" show index from ".C('DB_PREFIX')."page");
  271. if ($indexs) {
  272. $has_it = 0 ;//是否存在该索引
  273. foreach ($indexs as $key => $value) {
  274. if ($value['column_name'] =='addtime') {
  275. $has_it = 1 ;
  276. }
  277. }
  278. if ($has_it === 0 ) {
  279. M("page")->execute("ALTER TABLE ".C('DB_PREFIX')."page ADD INDEX ( `addtime` ) ;");
  280. }
  281. }
  282. //为page_history表增加addtime索引
  283. $indexs = M("PageHistory")->query(" show index from ".C('DB_PREFIX')."page_history");
  284. if ($indexs) {
  285. $has_it = 0 ;//是否存在该索引
  286. foreach ($indexs as $key => $value) {
  287. if ($value['column_name'] =='addtime') {
  288. $has_it = 1 ;
  289. }
  290. }
  291. if ($has_it === 0 ) {
  292. M("PageHistory")->execute("ALTER TABLE ".C('DB_PREFIX')."page_history ADD INDEX ( `addtime` ) ;");
  293. }
  294. }
  295. //为page_history表增加page_id索引
  296. $indexs = M("PageHistory")->query(" show index from ".C('DB_PREFIX')."page_history");
  297. if ($indexs) {
  298. $has_it = 0 ;//是否存在该索引
  299. foreach ($indexs as $key => $value) {
  300. if ($value['column_name'] =='page_id') {
  301. $has_it = 1 ;
  302. }
  303. }
  304. if ($has_it === 0 ) {
  305. M("PageHistory")->execute("ALTER TABLE ".C('DB_PREFIX')."page_history ADD INDEX ( `page_id` ) ;");
  306. }
  307. }
  308. //catalog表增加parent_cat_id字段
  309. $columns = M("catalog")->getDbFields();
  310. if ($columns) {
  311. $has_it = 0 ;//是否存在该字段
  312. foreach ($columns as $key => $value) {
  313. if ($value == 'parent_cat_id') {
  314. $has_it = 1 ;
  315. }
  316. }
  317. if ($has_it === 0) {
  318. $sql = "ALTER TABLE ".C('DB_PREFIX')."catalog ADD parent_cat_id INT( 10 ) NOT NULL DEFAULT '0' COMMENT '上一级目录的id';";
  319. D("catalog")->execute($sql);
  320. }
  321. }
  322. //catalog表增加level字段
  323. $columns = M("catalog")->getDbFields();
  324. if ($columns) {
  325. $has_it = 0 ;//是否存在该字段
  326. foreach ($columns as $key => $value) {
  327. if ($value == 'level') {
  328. $has_it = 1 ;
  329. }
  330. }
  331. if ($has_it === 0) {
  332. $sql = "ALTER TABLE ".C('DB_PREFIX')."catalog ADD level INT( 10 ) NOT NULL DEFAULT '2' COMMENT '2为二级目录,3为三级目录';";
  333. D("catalog")->execute($sql);
  334. }
  335. }
  336. //item表增加item_domain字段
  337. $columns = M("item")->getDbFields();
  338. if ($columns) {
  339. $has_it = 0 ;//是否存在该字段
  340. foreach ($columns as $key => $value) {
  341. if ($value == 'item_domain') {
  342. $has_it = 1 ;
  343. }
  344. }
  345. if ($has_it === 0) {
  346. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD item_domain varchar( 50 ) NOT NULL DEFAULT '' COMMENT 'item的个性域名';";
  347. D("item")->execute($sql);
  348. }
  349. }
  350. $sql = "CREATE TABLE IF NOT EXISTS `".C('DB_PREFIX')."user_token` (
  351. `id` int(10) NOT NULL AUTO_INCREMENT,
  352. `uid` int(10) NOT NULL DEFAULT '0',
  353. `token` varchar(200) NOT NULL DEFAULT '',
  354. `token_expire` int(11) NOT NULL DEFAULT '0' ,
  355. `ip` varchar(200) NOT NULL DEFAULT '',
  356. `addtime` int(11) NOT NULL DEFAULT '0',
  357. PRIMARY KEY (`id`),
  358. KEY `token` (`token`)
  359. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='' AUTO_INCREMENT=1 ";
  360. D("User")->execute($sql);
  361. //创建template表
  362. $sql = "CREATE TABLE IF NOT EXISTS `".C('DB_PREFIX')."template` (
  363. `id` int(10) NOT NULL AUTO_INCREMENT,
  364. `uid` int(10) NOT NULL DEFAULT '0',
  365. `username` varchar(200) NOT NULL DEFAULT '',
  366. `template_title` varchar(200) NOT NULL DEFAULT '' ,
  367. `template_content` text NOT NULL ,
  368. `addtime` int(11) NOT NULL DEFAULT '0',
  369. PRIMARY KEY (`id`),
  370. KEY `uid` (`uid`)
  371. )ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='' AUTO_INCREMENT=1";
  372. D("UserToken")->execute($sql);
  373. //page表增加page_comments字段
  374. $columns = M("Page")->getDbFields();
  375. if ($columns) {
  376. $has_it = 0 ;//是否存在该字段
  377. foreach ($columns as $key => $value) {
  378. if ($value == 'page_comments') {
  379. $has_it = 1 ;
  380. }
  381. }
  382. if ($has_it === 0) {
  383. $sql = "ALTER TABLE ".C('DB_PREFIX')."page ADD page_comments varchar( 255 ) NOT NULL DEFAULT '' COMMENT '页面注释';";
  384. D("Page")->execute($sql);
  385. }
  386. }
  387. //page_history表增加page_comments字段
  388. $columns = M("PageHistory")->getDbFields();
  389. if ($columns) {
  390. $has_it = 0 ;//是否存在该字段
  391. foreach ($columns as $key => $value) {
  392. if ($value == 'page_comments') {
  393. $has_it = 1 ;
  394. }
  395. }
  396. if ($has_it === 0) {
  397. $sql = "ALTER TABLE ".C('DB_PREFIX')."page_history ADD page_comments varchar( 255 ) NOT NULL DEFAULT '' COMMENT '页面注释';";
  398. D("PageHistory")->execute($sql);
  399. }
  400. }
  401. if(D("User")->where("uid = 1 ")->find()){
  402. $db_config = array(
  403. 'DB_TYPE' => 'Sqlite',
  404. 'DB_NAME' => 'Sqlite/showdoc.db.php',
  405. );
  406. M("User")->db(2,$db_config)->where("uid = 1 ")->delete();
  407. }
  408. }
  409. private function _moveTable($table){
  410. $db_config = array(
  411. 'DB_TYPE' => 'Sqlite',
  412. 'DB_NAME' => 'Sqlite/showdoc.db.php',
  413. );
  414. $array = M($table)->select();
  415. if ($array) {
  416. foreach ($array as $key => $value) {
  417. M($table)->db(2,$db_config)->add($value);
  418. }
  419. }
  420. }
  421. }