UpdateController.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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';
  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表增加level字段
  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. echo 'OK!';
  123. }
  124. private function _clear_runtime($path = RUNTIME_PATH){
  125. //给定的目录不是一个文件夹
  126. if(!is_dir($path)){
  127. return null;
  128. }
  129. $fh = opendir($path);
  130. while(($row = readdir($fh)) !== false){
  131. //过滤掉虚拟目录
  132. if($row == '.' || $row == '..'|| $row == 'index.html'){
  133. continue;
  134. }
  135. if(!is_dir($path.'/'.$row)){
  136. unlink($path.'/'.$row);
  137. }
  138. $this->_clear_runtime($path.'/'.$row);
  139. }
  140. //关闭目录句柄,否则出Permission denied
  141. closedir($fh);
  142. return true;
  143. }
  144. //转移mysql的数据到sqlite
  145. public function toSqlite(){
  146. $this->_clear_runtime();
  147. if (strtolower(C("DB_TYPE")) == 'mysql' ) {
  148. $this->mysql();
  149. $this->_moveTable("catalog");
  150. $this->_moveTable("item");
  151. $this->_moveTable("item_member");
  152. $this->_moveTable("page");
  153. $this->_moveTable("page_history");
  154. $this->_moveTable("template");
  155. $this->_moveTable("user");
  156. $this->_moveTable("user_token");
  157. $db_config = array(
  158. 'DB_TYPE' => 'Sqlite',
  159. 'DB_NAME' => 'Sqlite/showdoc.db.php',
  160. );
  161. $array = M("item")->db(2,$db_config)->select();
  162. if ($array) {
  163. echo "ok";
  164. }else{
  165. echo 'fail';
  166. }
  167. }
  168. else{
  169. echo "mysql not found";
  170. }
  171. $this->_clear_runtime();
  172. }
  173. //升级mysql数据库
  174. public function mysql(){
  175. //user表的username字段增大了长度,防止长邮箱的用户名注册不了
  176. $sql = "alter table ".C('DB_PREFIX')."user modify column username varchar(50) CHARACTER SET utf8 NOT NULL DEFAULT '' ";
  177. M("Catalog")->execute($sql);
  178. //item表增加last_update_time字段
  179. $columns = M("item")->getDbFields();
  180. if ($columns) {
  181. $has_it = 0 ;//是否存在该字段
  182. foreach ($columns as $key => $value) {
  183. if ($value == 'last_update_time') {
  184. $has_it = 1 ;
  185. }
  186. }
  187. if ($has_it === 0) {
  188. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD last_update_time INT( 11 ) NOT NULL DEFAULT '0' COMMENT '最后更新时间';";
  189. D("Item")->execute($sql);
  190. }
  191. }
  192. //更改catalog表的order字段名为s_number
  193. $columns = M("Catalog")->getDbFields();
  194. if ($columns) {
  195. foreach ($columns as $key => $value) {
  196. if ($value == 'order') {
  197. $sql = "ALTER TABLE `".C('DB_PREFIX')."catalog` CHANGE `order` `s_number` INT( 10 ) NOT NULL DEFAULT '99' COMMENT '顺序号。数字越小越靠前。若此值全部相等时则按id排序';";
  198. M("Catalog")->execute($sql);
  199. }
  200. }
  201. }
  202. //更改page表的order字段名为s_number
  203. $columns = M("Page")->getDbFields();
  204. if ($columns) {
  205. foreach ($columns as $key => $value) {
  206. if ($value == 'order') {
  207. $sql = "ALTER TABLE `".C('DB_PREFIX')."page` CHANGE `order` `s_number` INT( 10 ) NOT NULL DEFAULT '99' COMMENT '顺序号。数字越小越靠前。若此值全部相等时则按id排序';";
  208. M("Page")->execute($sql);
  209. }
  210. }
  211. }
  212. //更改page_history表的order字段名为s_number
  213. $columns = M("PageHistory")->getDbFields();
  214. if ($columns) {
  215. foreach ($columns as $key => $value) {
  216. if ($value == 'order') {
  217. $sql = "ALTER TABLE `".C('DB_PREFIX')."page_history` CHANGE `order` `s_number` INT( 10 ) NOT NULL DEFAULT '99' COMMENT '顺序号。数字越小越靠前。若此值全部相等时则按id排序';";
  218. M("PageHistory")->execute($sql);
  219. }
  220. }
  221. }
  222. //为catalog表增加addtime索引
  223. $indexs = M("Catalog")->query(" show index from ".C('DB_PREFIX')."catalog");
  224. if ($indexs) {
  225. $has_it = 0 ;//是否存在该索引
  226. foreach ($indexs as $key => $value) {
  227. if ($value['column_name'] =='addtime') {
  228. $has_it = 1 ;
  229. }
  230. }
  231. if ($has_it === 0 ) {
  232. M("Catalog")->execute("ALTER TABLE ".C('DB_PREFIX')."catalog ADD INDEX ( `addtime` ) ;");
  233. }
  234. }
  235. //为item表增加addtime索引
  236. $indexs = M("Item")->query(" show index from ".C('DB_PREFIX')."item");
  237. if ($indexs) {
  238. $has_it = 0 ;//是否存在该索引
  239. foreach ($indexs as $key => $value) {
  240. if ($value['column_name'] =='addtime') {
  241. $has_it = 1 ;
  242. }
  243. }
  244. if ($has_it === 0 ) {
  245. M("Item")->execute("ALTER TABLE ".C('DB_PREFIX')."item ADD INDEX ( `addtime` ) ;");
  246. }
  247. }
  248. //为page表增加addtime索引
  249. $indexs = M("Page")->query(" show index from ".C('DB_PREFIX')."page");
  250. if ($indexs) {
  251. $has_it = 0 ;//是否存在该索引
  252. foreach ($indexs as $key => $value) {
  253. if ($value['column_name'] =='addtime') {
  254. $has_it = 1 ;
  255. }
  256. }
  257. if ($has_it === 0 ) {
  258. M("page")->execute("ALTER TABLE ".C('DB_PREFIX')."page ADD INDEX ( `addtime` ) ;");
  259. }
  260. }
  261. //为page_history表增加addtime索引
  262. $indexs = M("PageHistory")->query(" show index from ".C('DB_PREFIX')."page_history");
  263. if ($indexs) {
  264. $has_it = 0 ;//是否存在该索引
  265. foreach ($indexs as $key => $value) {
  266. if ($value['column_name'] =='addtime') {
  267. $has_it = 1 ;
  268. }
  269. }
  270. if ($has_it === 0 ) {
  271. M("PageHistory")->execute("ALTER TABLE ".C('DB_PREFIX')."page_history ADD INDEX ( `addtime` ) ;");
  272. }
  273. }
  274. //为page_history表增加page_id索引
  275. $indexs = M("PageHistory")->query(" show index from ".C('DB_PREFIX')."page_history");
  276. if ($indexs) {
  277. $has_it = 0 ;//是否存在该索引
  278. foreach ($indexs as $key => $value) {
  279. if ($value['column_name'] =='page_id') {
  280. $has_it = 1 ;
  281. }
  282. }
  283. if ($has_it === 0 ) {
  284. M("PageHistory")->execute("ALTER TABLE ".C('DB_PREFIX')."page_history ADD INDEX ( `page_id` ) ;");
  285. }
  286. }
  287. //catalog表增加parent_cat_id字段
  288. $columns = M("catalog")->getDbFields();
  289. if ($columns) {
  290. $has_it = 0 ;//是否存在该字段
  291. foreach ($columns as $key => $value) {
  292. if ($value == 'parent_cat_id') {
  293. $has_it = 1 ;
  294. }
  295. }
  296. if ($has_it === 0) {
  297. $sql = "ALTER TABLE ".C('DB_PREFIX')."catalog ADD parent_cat_id INT( 10 ) NOT NULL DEFAULT '0' COMMENT '上一级目录的id';";
  298. D("catalog")->execute($sql);
  299. }
  300. }
  301. //catalog表增加level字段
  302. $columns = M("catalog")->getDbFields();
  303. if ($columns) {
  304. $has_it = 0 ;//是否存在该字段
  305. foreach ($columns as $key => $value) {
  306. if ($value == 'level') {
  307. $has_it = 1 ;
  308. }
  309. }
  310. if ($has_it === 0) {
  311. $sql = "ALTER TABLE ".C('DB_PREFIX')."catalog ADD level INT( 10 ) NOT NULL DEFAULT '2' COMMENT '2为二级目录,3为三级目录';";
  312. D("catalog")->execute($sql);
  313. }
  314. }
  315. //item表增加item_domain字段
  316. $columns = M("item")->getDbFields();
  317. if ($columns) {
  318. $has_it = 0 ;//是否存在该字段
  319. foreach ($columns as $key => $value) {
  320. if ($value == 'item_domain') {
  321. $has_it = 1 ;
  322. }
  323. }
  324. if ($has_it === 0) {
  325. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD item_domain varchar( 50 ) NOT NULL DEFAULT '' COMMENT 'item的个性域名';";
  326. D("item")->execute($sql);
  327. }
  328. }
  329. $sql = "CREATE TABLE IF NOT EXISTS `".C('DB_PREFIX')."user_token` (
  330. `id` int(10) NOT NULL AUTO_INCREMENT,
  331. `uid` int(10) NOT NULL DEFAULT '0',
  332. `token` varchar(200) NOT NULL DEFAULT '',
  333. `token_expire` int(11) NOT NULL DEFAULT '0' ,
  334. `ip` varchar(200) NOT NULL DEFAULT '',
  335. `addtime` int(11) NOT NULL DEFAULT '0',
  336. PRIMARY KEY (`id`),
  337. KEY `token` (`token`)
  338. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='' AUTO_INCREMENT=1 ";
  339. D("User")->execute($sql);
  340. //创建template表
  341. $sql = "CREATE TABLE IF NOT EXISTS `".C('DB_PREFIX')."template` (
  342. `id` int(10) NOT NULL AUTO_INCREMENT,
  343. `uid` int(10) NOT NULL DEFAULT '0',
  344. `username` varchar(200) NOT NULL DEFAULT '',
  345. `template_title` varchar(200) NOT NULL DEFAULT '' ,
  346. `template_content` text NOT NULL ,
  347. `addtime` int(11) NOT NULL DEFAULT '0',
  348. PRIMARY KEY (`id`),
  349. KEY `uid` (`uid`)
  350. )ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='' AUTO_INCREMENT=1";
  351. D("UserToken")->execute($sql);
  352. //page表增加page_comments字段
  353. $columns = M("Page")->getDbFields();
  354. if ($columns) {
  355. $has_it = 0 ;//是否存在该字段
  356. foreach ($columns as $key => $value) {
  357. if ($value == 'page_comments') {
  358. $has_it = 1 ;
  359. }
  360. }
  361. if ($has_it === 0) {
  362. $sql = "ALTER TABLE ".C('DB_PREFIX')."page ADD page_comments varchar( 255 ) NOT NULL DEFAULT '' COMMENT '页面注释';";
  363. D("Page")->execute($sql);
  364. }
  365. }
  366. //page_history表增加page_comments字段
  367. $columns = M("PageHistory")->getDbFields();
  368. if ($columns) {
  369. $has_it = 0 ;//是否存在该字段
  370. foreach ($columns as $key => $value) {
  371. if ($value == 'page_comments') {
  372. $has_it = 1 ;
  373. }
  374. }
  375. if ($has_it === 0) {
  376. $sql = "ALTER TABLE ".C('DB_PREFIX')."page_history ADD page_comments varchar( 255 ) NOT NULL DEFAULT '' COMMENT '页面注释';";
  377. D("PageHistory")->execute($sql);
  378. }
  379. }
  380. if(D("User")->where("uid = 1 ")->find()){
  381. $db_config = array(
  382. 'DB_TYPE' => 'Sqlite',
  383. 'DB_NAME' => 'Sqlite/showdoc.db.php',
  384. );
  385. M("User")->db(2,$db_config)->where("uid = 1 ")->delete();
  386. }
  387. }
  388. private function _moveTable($table){
  389. $db_config = array(
  390. 'DB_TYPE' => 'Sqlite',
  391. 'DB_NAME' => 'Sqlite/showdoc.db.php',
  392. );
  393. $array = M($table)->select();
  394. if ($array) {
  395. foreach ($array as $key => $value) {
  396. M($table)->db(2,$db_config)->add($value);
  397. }
  398. }
  399. }
  400. }