UpdateController.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. }
  11. elseif (strtolower(C("DB_TYPE")) == 'sqlite' ) {
  12. $this->sqlite();
  13. }
  14. clear_runtime();
  15. }
  16. //升级mysql数据库
  17. public function mysql(){
  18. //user表的username字段增大了长度,防止长邮箱的用户名注册不了
  19. $sql = "alter table ".C('DB_PREFIX')."user modify column username varchar(50) CHARACTER SET utf8 NOT NULL DEFAULT '' ";
  20. M("Catalog")->execute($sql);
  21. //item表增加last_update_time字段
  22. $columns = M("item")->getDbFields();
  23. if ($columns) {
  24. $has_it = 0 ;//是否存在该字段
  25. foreach ($columns as $key => $value) {
  26. if ($value == 'last_update_time') {
  27. $has_it = 1 ;
  28. }
  29. }
  30. if ($has_it === 0) {
  31. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD last_update_time INT( 11 ) NOT NULL DEFAULT '0' COMMENT '最后更新时间';";
  32. D("Item")->execute($sql);
  33. }
  34. }
  35. //更改catalog表的order字段名为s_number
  36. $columns = M("Catalog")->getDbFields();
  37. if ($columns) {
  38. foreach ($columns as $key => $value) {
  39. if ($value == 'order') {
  40. $sql = "ALTER TABLE `".C('DB_PREFIX')."catalog` CHANGE `order` `s_number` INT( 10 ) NOT NULL DEFAULT '99' COMMENT '顺序号。数字越小越靠前。若此值全部相等时则按id排序';";
  41. M("Catalog")->execute($sql);
  42. }
  43. }
  44. }
  45. //更改page表的order字段名为s_number
  46. $columns = M("Page")->getDbFields();
  47. if ($columns) {
  48. foreach ($columns as $key => $value) {
  49. if ($value == 'order') {
  50. $sql = "ALTER TABLE `".C('DB_PREFIX')."page` CHANGE `order` `s_number` INT( 10 ) NOT NULL DEFAULT '99' COMMENT '顺序号。数字越小越靠前。若此值全部相等时则按id排序';";
  51. M("Page")->execute($sql);
  52. }
  53. }
  54. }
  55. //更改page_history表的order字段名为s_number
  56. $columns = M("PageHistory")->getDbFields();
  57. if ($columns) {
  58. foreach ($columns as $key => $value) {
  59. if ($value == 'order') {
  60. $sql = "ALTER TABLE `".C('DB_PREFIX')."page_history` CHANGE `order` `s_number` INT( 10 ) NOT NULL DEFAULT '99' COMMENT '顺序号。数字越小越靠前。若此值全部相等时则按id排序';";
  61. M("PageHistory")->execute($sql);
  62. }
  63. }
  64. }
  65. //为catalog表增加addtime索引
  66. $indexs = M("Catalog")->query(" show index from ".C('DB_PREFIX')."catalog");
  67. if ($indexs) {
  68. $has_it = 0 ;//是否存在该索引
  69. foreach ($indexs as $key => $value) {
  70. if ($value['column_name'] =='addtime') {
  71. $has_it = 1 ;
  72. }
  73. }
  74. if ($has_it === 0 ) {
  75. M("Catalog")->execute("ALTER TABLE ".C('DB_PREFIX')."catalog ADD INDEX ( `addtime` ) ;");
  76. }
  77. }
  78. //为item表增加addtime索引
  79. $indexs = M("Item")->query(" show index from ".C('DB_PREFIX')."item");
  80. if ($indexs) {
  81. $has_it = 0 ;//是否存在该索引
  82. foreach ($indexs as $key => $value) {
  83. if ($value['column_name'] =='addtime') {
  84. $has_it = 1 ;
  85. }
  86. }
  87. if ($has_it === 0 ) {
  88. M("Item")->execute("ALTER TABLE ".C('DB_PREFIX')."item ADD INDEX ( `addtime` ) ;");
  89. }
  90. }
  91. //为page表增加addtime索引
  92. $indexs = M("Page")->query(" show index from ".C('DB_PREFIX')."page");
  93. if ($indexs) {
  94. $has_it = 0 ;//是否存在该索引
  95. foreach ($indexs as $key => $value) {
  96. if ($value['column_name'] =='addtime') {
  97. $has_it = 1 ;
  98. }
  99. }
  100. if ($has_it === 0 ) {
  101. M("page")->execute("ALTER TABLE ".C('DB_PREFIX')."page ADD INDEX ( `addtime` ) ;");
  102. }
  103. }
  104. //为page_history表增加addtime索引
  105. $indexs = M("PageHistory")->query(" show index from ".C('DB_PREFIX')."page_history");
  106. if ($indexs) {
  107. $has_it = 0 ;//是否存在该索引
  108. foreach ($indexs as $key => $value) {
  109. if ($value['column_name'] =='addtime') {
  110. $has_it = 1 ;
  111. }
  112. }
  113. if ($has_it === 0 ) {
  114. M("PageHistory")->execute("ALTER TABLE ".C('DB_PREFIX')."page_history ADD INDEX ( `addtime` ) ;");
  115. }
  116. }
  117. //为page_history表增加page_id索引
  118. $indexs = M("PageHistory")->query(" show index from ".C('DB_PREFIX')."page_history");
  119. if ($indexs) {
  120. $has_it = 0 ;//是否存在该索引
  121. foreach ($indexs as $key => $value) {
  122. if ($value['column_name'] =='page_id') {
  123. $has_it = 1 ;
  124. }
  125. }
  126. if ($has_it === 0 ) {
  127. M("PageHistory")->execute("ALTER TABLE ".C('DB_PREFIX')."page_history ADD INDEX ( `page_id` ) ;");
  128. }
  129. }
  130. //catalog表增加parent_cat_id字段
  131. $columns = M("catalog")->getDbFields();
  132. if ($columns) {
  133. $has_it = 0 ;//是否存在该字段
  134. foreach ($columns as $key => $value) {
  135. if ($value == 'parent_cat_id') {
  136. $has_it = 1 ;
  137. }
  138. }
  139. if ($has_it === 0) {
  140. $sql = "ALTER TABLE ".C('DB_PREFIX')."catalog ADD parent_cat_id INT( 10 ) NOT NULL DEFAULT '0' COMMENT '上一级目录的id';";
  141. D("catalog")->execute($sql);
  142. }
  143. }
  144. //catalog表增加level字段
  145. $columns = M("catalog")->getDbFields();
  146. if ($columns) {
  147. $has_it = 0 ;//是否存在该字段
  148. foreach ($columns as $key => $value) {
  149. if ($value == 'level') {
  150. $has_it = 1 ;
  151. }
  152. }
  153. if ($has_it === 0) {
  154. $sql = "ALTER TABLE ".C('DB_PREFIX')."catalog ADD level INT( 10 ) NOT NULL DEFAULT '2' COMMENT '2为二级目录,3为三级目录';";
  155. D("catalog")->execute($sql);
  156. }
  157. }
  158. //item表增加item_domain字段
  159. $columns = M("item")->getDbFields();
  160. if ($columns) {
  161. $has_it = 0 ;//是否存在该字段
  162. foreach ($columns as $key => $value) {
  163. if ($value == 'item_domain') {
  164. $has_it = 1 ;
  165. }
  166. }
  167. if ($has_it === 0) {
  168. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD item_domain varchar( 50 ) NOT NULL DEFAULT '' COMMENT 'item的个性域名';";
  169. D("item")->execute($sql);
  170. }
  171. }
  172. $sql = "CREATE TABLE IF NOT EXISTS `".C('DB_PREFIX')."user_token` (
  173. `id` int(10) NOT NULL AUTO_INCREMENT,
  174. `uid` int(10) NOT NULL DEFAULT '0',
  175. `token` varchar(200) NOT NULL DEFAULT '',
  176. `token_expire` int(11) NOT NULL DEFAULT '0' ,
  177. `ip` varchar(200) NOT NULL DEFAULT '',
  178. `addtime` int(11) NOT NULL DEFAULT '0',
  179. PRIMARY KEY (`id`),
  180. KEY `token` (`token`)
  181. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='' AUTO_INCREMENT=1 ";
  182. D("User")->execute($sql);
  183. //创建template表
  184. $sql = "CREATE TABLE IF NOT EXISTS `".C('DB_PREFIX')."template` (
  185. `id` int(10) NOT NULL AUTO_INCREMENT,
  186. `uid` int(10) NOT NULL DEFAULT '0',
  187. `username` varchar(200) NOT NULL DEFAULT '',
  188. `template_title` varchar(200) NOT NULL DEFAULT '' ,
  189. `template_content` text NOT NULL DEFAULT '',
  190. `addtime` int(11) NOT NULL DEFAULT '0',
  191. PRIMARY KEY (`id`),
  192. KEY `uid` (`uid`)
  193. )ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='' AUTO_INCREMENT=1";
  194. D("UserToken")->execute($sql);
  195. //page表增加page_comments字段
  196. $columns = M("Page")->getDbFields();
  197. if ($columns) {
  198. $has_it = 0 ;//是否存在该字段
  199. foreach ($columns as $key => $value) {
  200. if ($value == 'page_comments') {
  201. $has_it = 1 ;
  202. }
  203. }
  204. if ($has_it === 0) {
  205. $sql = "ALTER TABLE ".C('DB_PREFIX')."page ADD page_comments varchar( 255 ) NOT NULL DEFAULT '' COMMENT '页面注释';";
  206. D("Page")->execute($sql);
  207. }
  208. }
  209. //page_history表增加page_comments字段
  210. $columns = M("PageHistory")->getDbFields();
  211. if ($columns) {
  212. $has_it = 0 ;//是否存在该字段
  213. foreach ($columns as $key => $value) {
  214. if ($value == 'page_comments') {
  215. $has_it = 1 ;
  216. }
  217. }
  218. if ($has_it === 0) {
  219. $sql = "ALTER TABLE ".C('DB_PREFIX')."page_history ADD page_comments varchar( 255 ) NOT NULL DEFAULT '' COMMENT '页面注释';";
  220. D("PageHistory")->execute($sql);
  221. }
  222. }
  223. echo "OK!";
  224. }
  225. public function sqlite(){
  226. //catalog表增加parent_cat_id字段
  227. $columns = M("catalog")->getDbFields();
  228. if ($columns) {
  229. $has_it = 0 ;//是否存在该字段
  230. foreach ($columns as $key => $value) {
  231. if ($value == 'parent_cat_id') {
  232. $has_it = 1 ;
  233. }
  234. }
  235. if ($has_it === 0) {
  236. $sql = "ALTER TABLE ".C('DB_PREFIX')."catalog ADD parent_cat_id INT( 10 ) NOT NULL DEFAULT '0' ;";
  237. D("catalog")->execute($sql);
  238. }
  239. }
  240. //catalog表增加level字段
  241. $columns = M("catalog")->getDbFields();
  242. if ($columns) {
  243. $has_it = 0 ;//是否存在该字段
  244. foreach ($columns as $key => $value) {
  245. if ($value == 'level') {
  246. $has_it = 1 ;
  247. }
  248. }
  249. if ($has_it === 0) {
  250. $sql = "ALTER TABLE ".C('DB_PREFIX')."catalog ADD level INT( 10 ) NOT NULL DEFAULT '2' ;";
  251. D("catalog")->execute($sql);
  252. }
  253. }
  254. //item表增加item_domain字段
  255. $columns = M("item")->getDbFields();
  256. if ($columns) {
  257. $has_it = 0 ;//是否存在该字段
  258. foreach ($columns as $key => $value) {
  259. if ($value == 'item_domain') {
  260. $has_it = 1 ;
  261. }
  262. }
  263. if ($has_it === 0) {
  264. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD item_domain text NOT NULL DEFAULT '';";
  265. D("item")->execute($sql);
  266. }
  267. }
  268. //创建user_token表
  269. $sql = "CREATE TABLE IF NOT EXISTS `user_token` (
  270. `id` INTEGER PRIMARY KEY ,
  271. `uid` int(10) NOT NULL DEFAULT '0',
  272. `token` CHAR(200) NOT NULL DEFAULT '',
  273. `token_expire` int(11) NOT NULL DEFAULT '0' ,
  274. `ip` CHAR(200) NOT NULL DEFAULT '',
  275. `addtime` int(11) NOT NULL DEFAULT '0'
  276. )";
  277. D("UserToken")->execute($sql);
  278. //创建template表
  279. $sql = "CREATE TABLE IF NOT EXISTS `template` (
  280. `id` INTEGER PRIMARY KEY ,
  281. `uid` int(10) NOT NULL DEFAULT '0',
  282. `username` CHAR(200) NOT NULL DEFAULT '',
  283. `template_title` CHAR(200) NOT NULL DEFAULT '' ,
  284. `template_content` text NOT NULL DEFAULT '',
  285. `addtime` int(11) NOT NULL DEFAULT '0'
  286. )";
  287. D("UserToken")->execute($sql);
  288. //page表增加page_comments字段
  289. $columns = D("Page")->getDbFields();
  290. if ($columns) {
  291. $has_it = 0 ;//是否存在该字段
  292. foreach ($columns as $key => $value) {
  293. if ($value == 'page_comments') {
  294. $has_it = 1 ;
  295. }
  296. }
  297. if ($has_it === 0) {
  298. $sql = "ALTER TABLE ".C('DB_PREFIX')."page ADD page_comments text NOT NULL DEFAULT '' ;";
  299. D("Page")->execute($sql);
  300. }
  301. }
  302. //page_history 表增加page_comments字段
  303. $columns = D("PageHistory")->getDbFields();
  304. if ($columns) {
  305. $has_it = 0 ;//是否存在该字段
  306. foreach ($columns as $key => $value) {
  307. if ($value == 'page_comments') {
  308. $has_it = 1 ;
  309. }
  310. }
  311. if ($has_it === 0) {
  312. $sql = "ALTER TABLE ".C('DB_PREFIX')."page_history ADD page_comments text NOT NULL DEFAULT '';";
  313. D("PageHistory")->execute($sql);
  314. }
  315. }
  316. echo 'OK!';
  317. }
  318. }