UpdateController.class.php 14 KB

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