UpdateController.class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. //创建item_token表
  144. $sql = "CREATE TABLE IF NOT EXISTS `item_token` (
  145. `id` INTEGER PRIMARY KEY ,
  146. `item_id` int(11) NOT NULL DEFAULT '0' ,
  147. `api_key` CHAR(200) NOT NULL UNIQUE ,
  148. `api_token` CHAR(200) NOT NULL ,
  149. `addtime` int(11) NOT NULL DEFAULT '0' ,
  150. `last_check_time` int(11) NOT NULL DEFAULT '0'
  151. )";
  152. D("UserToken")->execute($sql);
  153. //创建item_top表
  154. $sql = "CREATE TABLE IF NOT EXISTS `item_top` (
  155. `id` INTEGER PRIMARY KEY ,
  156. `item_id` int(11) NOT NULL DEFAULT '0' ,
  157. `uid` int(11) NOT NULL DEFAULT '0' ,
  158. `addtime` int(11) NOT NULL DEFAULT '0'
  159. )";
  160. D("UserToken")->execute($sql);
  161. //item表增加is_archived字段
  162. $columns = M("Item")->getDbFields();
  163. if ($columns) {
  164. $has_it = 0 ;//是否存在该字段
  165. foreach ($columns as $key => $value) {
  166. if ($value == 'is_archived') {
  167. $has_it = 1 ;
  168. }
  169. }
  170. if ($has_it === 0) {
  171. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD is_archived INT( 1 ) NOT NULL DEFAULT '0' ;";
  172. D("Item")->execute($sql);
  173. }
  174. }
  175. //管理员账户和权限
  176. if(D("User")->where("username = 'showdoc' ")->find()){
  177. D("User")->where("username = 'showdoc' ")->save(array("groupid"=> 1)) ;
  178. }else{
  179. D("User")->add(array('username'=>"showdoc" ,"groupid"=>1,'password'=>"a89da13684490eb9ec9e613f91d24d00" , 'reg_time'=>time()));
  180. }
  181. //item表增加is_del字段
  182. $columns = M("Item")->getDbFields();
  183. if ($columns) {
  184. $has_it = 0 ;//是否存在该字段
  185. foreach ($columns as $key => $value) {
  186. if ($value == 'is_del') {
  187. $has_it = 1 ;
  188. }
  189. }
  190. if ($has_it === 0) {
  191. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD is_del INT( 1 ) NOT NULL DEFAULT '0' ;";
  192. D("Item")->execute($sql);
  193. }
  194. }
  195. //page表增加is_del字段
  196. $columns = M("Page")->getDbFields();
  197. if ($columns) {
  198. $has_it = 0 ;//是否存在该字段
  199. foreach ($columns as $key => $value) {
  200. if ($value == 'is_del') {
  201. $has_it = 1 ;
  202. }
  203. }
  204. if ($has_it === 0) {
  205. $sql = "ALTER TABLE ".C('DB_PREFIX')."page ADD is_del INT( 1 ) NOT NULL DEFAULT '0' ;";
  206. D("Page")->execute($sql);
  207. }
  208. }
  209. echo 'OK!';
  210. }
  211. private function _clear_runtime($path = RUNTIME_PATH){
  212. //给定的目录不是一个文件夹
  213. if(!is_dir($path)){
  214. return null;
  215. }
  216. $fh = opendir($path);
  217. while(($row = readdir($fh)) !== false){
  218. //过滤掉虚拟目录
  219. if($row == '.' || $row == '..'|| $row == 'index.html'){
  220. continue;
  221. }
  222. if(!is_dir($path.'/'.$row)){
  223. unlink($path.'/'.$row);
  224. }
  225. $this->_clear_runtime($path.'/'.$row);
  226. }
  227. //关闭目录句柄,否则出Permission denied
  228. closedir($fh);
  229. return true;
  230. }
  231. //转移mysql的数据到sqlite
  232. public function toSqlite(){
  233. $this->_clear_runtime();
  234. if (strtolower(C("DB_TYPE")) == 'mysql' ) {
  235. $this->mysql();
  236. $this->_moveTable("catalog");
  237. $this->_moveTable("item");
  238. $this->_moveTable("item_member");
  239. $this->_moveTable("page");
  240. $this->_moveTable("page_history");
  241. $this->_moveTable("template");
  242. $this->_moveTable("user");
  243. $this->_moveTable("user_token");
  244. $db_config = array(
  245. 'DB_TYPE' => 'Sqlite',
  246. 'DB_NAME' => 'Sqlite/showdoc.db.php',
  247. );
  248. $array = M("item")->db(2,$db_config)->select();
  249. if ($array) {
  250. echo "ok";
  251. }else{
  252. echo 'fail';
  253. }
  254. }
  255. else{
  256. echo "mysql not found";
  257. }
  258. $this->_clear_runtime();
  259. }
  260. //升级mysql数据库
  261. public function mysql(){
  262. //user表的username字段增大了长度,防止长邮箱的用户名注册不了
  263. $sql = "alter table ".C('DB_PREFIX')."user modify column username varchar(50) CHARACTER SET utf8 NOT NULL DEFAULT '' ";
  264. M("Catalog")->execute($sql);
  265. //item表增加last_update_time字段
  266. $columns = M("item")->getDbFields();
  267. if ($columns) {
  268. $has_it = 0 ;//是否存在该字段
  269. foreach ($columns as $key => $value) {
  270. if ($value == 'last_update_time') {
  271. $has_it = 1 ;
  272. }
  273. }
  274. if ($has_it === 0) {
  275. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD last_update_time INT( 11 ) NOT NULL DEFAULT '0' COMMENT '最后更新时间';";
  276. D("Item")->execute($sql);
  277. }
  278. }
  279. //更改catalog表的order字段名为s_number
  280. $columns = M("Catalog")->getDbFields();
  281. if ($columns) {
  282. foreach ($columns as $key => $value) {
  283. if ($value == 'order') {
  284. $sql = "ALTER TABLE `".C('DB_PREFIX')."catalog` CHANGE `order` `s_number` INT( 10 ) NOT NULL DEFAULT '99' COMMENT '顺序号。数字越小越靠前。若此值全部相等时则按id排序';";
  285. M("Catalog")->execute($sql);
  286. }
  287. }
  288. }
  289. //更改page表的order字段名为s_number
  290. $columns = M("Page")->getDbFields();
  291. if ($columns) {
  292. foreach ($columns as $key => $value) {
  293. if ($value == 'order') {
  294. $sql = "ALTER TABLE `".C('DB_PREFIX')."page` CHANGE `order` `s_number` INT( 10 ) NOT NULL DEFAULT '99' COMMENT '顺序号。数字越小越靠前。若此值全部相等时则按id排序';";
  295. M("Page")->execute($sql);
  296. }
  297. }
  298. }
  299. //更改page_history表的order字段名为s_number
  300. $columns = M("PageHistory")->getDbFields();
  301. if ($columns) {
  302. foreach ($columns as $key => $value) {
  303. if ($value == 'order') {
  304. $sql = "ALTER TABLE `".C('DB_PREFIX')."page_history` CHANGE `order` `s_number` INT( 10 ) NOT NULL DEFAULT '99' COMMENT '顺序号。数字越小越靠前。若此值全部相等时则按id排序';";
  305. M("PageHistory")->execute($sql);
  306. }
  307. }
  308. }
  309. //为catalog表增加addtime索引
  310. $indexs = M("Catalog")->query(" show index from ".C('DB_PREFIX')."catalog");
  311. if ($indexs) {
  312. $has_it = 0 ;//是否存在该索引
  313. foreach ($indexs as $key => $value) {
  314. if ($value['column_name'] =='addtime') {
  315. $has_it = 1 ;
  316. }
  317. }
  318. if ($has_it === 0 ) {
  319. M("Catalog")->execute("ALTER TABLE ".C('DB_PREFIX')."catalog ADD INDEX ( `addtime` ) ;");
  320. }
  321. }
  322. //为item表增加addtime索引
  323. $indexs = M("Item")->query(" show index from ".C('DB_PREFIX')."item");
  324. if ($indexs) {
  325. $has_it = 0 ;//是否存在该索引
  326. foreach ($indexs as $key => $value) {
  327. if ($value['column_name'] =='addtime') {
  328. $has_it = 1 ;
  329. }
  330. }
  331. if ($has_it === 0 ) {
  332. M("Item")->execute("ALTER TABLE ".C('DB_PREFIX')."item ADD INDEX ( `addtime` ) ;");
  333. }
  334. }
  335. //为page表增加addtime索引
  336. $indexs = M("Page")->query(" show index from ".C('DB_PREFIX')."page");
  337. if ($indexs) {
  338. $has_it = 0 ;//是否存在该索引
  339. foreach ($indexs as $key => $value) {
  340. if ($value['column_name'] =='addtime') {
  341. $has_it = 1 ;
  342. }
  343. }
  344. if ($has_it === 0 ) {
  345. M("page")->execute("ALTER TABLE ".C('DB_PREFIX')."page ADD INDEX ( `addtime` ) ;");
  346. }
  347. }
  348. //为page_history表增加addtime索引
  349. $indexs = M("PageHistory")->query(" show index from ".C('DB_PREFIX')."page_history");
  350. if ($indexs) {
  351. $has_it = 0 ;//是否存在该索引
  352. foreach ($indexs as $key => $value) {
  353. if ($value['column_name'] =='addtime') {
  354. $has_it = 1 ;
  355. }
  356. }
  357. if ($has_it === 0 ) {
  358. M("PageHistory")->execute("ALTER TABLE ".C('DB_PREFIX')."page_history ADD INDEX ( `addtime` ) ;");
  359. }
  360. }
  361. //为page_history表增加page_id索引
  362. $indexs = M("PageHistory")->query(" show index from ".C('DB_PREFIX')."page_history");
  363. if ($indexs) {
  364. $has_it = 0 ;//是否存在该索引
  365. foreach ($indexs as $key => $value) {
  366. if ($value['column_name'] =='page_id') {
  367. $has_it = 1 ;
  368. }
  369. }
  370. if ($has_it === 0 ) {
  371. M("PageHistory")->execute("ALTER TABLE ".C('DB_PREFIX')."page_history ADD INDEX ( `page_id` ) ;");
  372. }
  373. }
  374. //catalog表增加parent_cat_id字段
  375. $columns = M("catalog")->getDbFields();
  376. if ($columns) {
  377. $has_it = 0 ;//是否存在该字段
  378. foreach ($columns as $key => $value) {
  379. if ($value == 'parent_cat_id') {
  380. $has_it = 1 ;
  381. }
  382. }
  383. if ($has_it === 0) {
  384. $sql = "ALTER TABLE ".C('DB_PREFIX')."catalog ADD parent_cat_id INT( 10 ) NOT NULL DEFAULT '0' COMMENT '上一级目录的id';";
  385. D("catalog")->execute($sql);
  386. }
  387. }
  388. //catalog表增加level字段
  389. $columns = M("catalog")->getDbFields();
  390. if ($columns) {
  391. $has_it = 0 ;//是否存在该字段
  392. foreach ($columns as $key => $value) {
  393. if ($value == 'level') {
  394. $has_it = 1 ;
  395. }
  396. }
  397. if ($has_it === 0) {
  398. $sql = "ALTER TABLE ".C('DB_PREFIX')."catalog ADD level INT( 10 ) NOT NULL DEFAULT '2' COMMENT '2为二级目录,3为三级目录';";
  399. D("catalog")->execute($sql);
  400. }
  401. }
  402. //item表增加item_domain字段
  403. $columns = M("item")->getDbFields();
  404. if ($columns) {
  405. $has_it = 0 ;//是否存在该字段
  406. foreach ($columns as $key => $value) {
  407. if ($value == 'item_domain') {
  408. $has_it = 1 ;
  409. }
  410. }
  411. if ($has_it === 0) {
  412. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD item_domain varchar( 50 ) NOT NULL DEFAULT '' COMMENT 'item的个性域名';";
  413. D("item")->execute($sql);
  414. }
  415. }
  416. $sql = "CREATE TABLE IF NOT EXISTS `".C('DB_PREFIX')."user_token` (
  417. `id` int(10) NOT NULL AUTO_INCREMENT,
  418. `uid` int(10) NOT NULL DEFAULT '0',
  419. `token` varchar(200) NOT NULL DEFAULT '',
  420. `token_expire` int(11) NOT NULL DEFAULT '0' ,
  421. `ip` varchar(200) NOT NULL DEFAULT '',
  422. `addtime` int(11) NOT NULL DEFAULT '0',
  423. PRIMARY KEY (`id`),
  424. KEY `token` (`token`)
  425. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='' AUTO_INCREMENT=1 ";
  426. D("User")->execute($sql);
  427. //创建template表
  428. $sql = "CREATE TABLE IF NOT EXISTS `".C('DB_PREFIX')."template` (
  429. `id` int(10) NOT NULL AUTO_INCREMENT,
  430. `uid` int(10) NOT NULL DEFAULT '0',
  431. `username` varchar(200) NOT NULL DEFAULT '',
  432. `template_title` varchar(200) NOT NULL DEFAULT '' ,
  433. `template_content` text NOT NULL ,
  434. `addtime` int(11) NOT NULL DEFAULT '0',
  435. PRIMARY KEY (`id`),
  436. KEY `uid` (`uid`)
  437. )ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='' AUTO_INCREMENT=1";
  438. D("UserToken")->execute($sql);
  439. //page表增加page_comments字段
  440. $columns = M("Page")->getDbFields();
  441. if ($columns) {
  442. $has_it = 0 ;//是否存在该字段
  443. foreach ($columns as $key => $value) {
  444. if ($value == 'page_comments') {
  445. $has_it = 1 ;
  446. }
  447. }
  448. if ($has_it === 0) {
  449. $sql = "ALTER TABLE ".C('DB_PREFIX')."page ADD page_comments varchar( 255 ) NOT NULL DEFAULT '' COMMENT '页面注释';";
  450. D("Page")->execute($sql);
  451. }
  452. }
  453. //page_history表增加page_comments字段
  454. $columns = M("PageHistory")->getDbFields();
  455. if ($columns) {
  456. $has_it = 0 ;//是否存在该字段
  457. foreach ($columns as $key => $value) {
  458. if ($value == 'page_comments') {
  459. $has_it = 1 ;
  460. }
  461. }
  462. if ($has_it === 0) {
  463. $sql = "ALTER TABLE ".C('DB_PREFIX')."page_history ADD page_comments varchar( 255 ) NOT NULL DEFAULT '' COMMENT '页面注释';";
  464. D("PageHistory")->execute($sql);
  465. }
  466. }
  467. if(D("User")->where("uid = 1 ")->find()){
  468. $db_config = array(
  469. 'DB_TYPE' => 'Sqlite',
  470. 'DB_NAME' => 'Sqlite/showdoc.db.php',
  471. );
  472. M("User")->db(2,$db_config)->where("uid = 1 ")->delete();
  473. }
  474. }
  475. private function _moveTable($table){
  476. $db_config = array(
  477. 'DB_TYPE' => 'Sqlite',
  478. 'DB_NAME' => 'Sqlite/showdoc.db.php',
  479. );
  480. $array = M($table)->select();
  481. if ($array) {
  482. foreach ($array as $key => $value) {
  483. M($table)->db(2,$db_config)->add($value);
  484. }
  485. }
  486. }
  487. }