UpdateController.class.php 14 KB

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