UpdateController.class.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class UpdateController extends BaseController {
  5. //升级数据库
  6. public function db(){
  7. if (strtolower(C("DB_TYPE")) == 'mysql' ) {
  8. $this->mysql();
  9. }
  10. elseif (strtolower(C("DB_TYPE")) == 'sqlite' ) {
  11. $this->sqlite();
  12. }
  13. clear_runtime();
  14. }
  15. //升级mysql数据库
  16. public function mysql(){
  17. //user表的username字段增大了长度,防止长邮箱的用户名注册不了
  18. $sql = "alter table ".C('DB_PREFIX')."user modify column username varchar(50) CHARACTER SET utf8 NOT NULL DEFAULT '' ";
  19. M("Catalog")->execute($sql);
  20. //item表增加last_update_time字段
  21. $columns = M("item")->getDbFields();
  22. if ($columns) {
  23. $has_it = 0 ;//是否存在该字段
  24. foreach ($columns as $key => $value) {
  25. if ($value == 'last_update_time') {
  26. $has_it = 1 ;
  27. }
  28. }
  29. if ($has_it === 0) {
  30. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD last_update_time INT( 11 ) NOT NULL DEFAULT '0' COMMENT '最后更新时间';";
  31. D("Item")->execute($sql);
  32. }
  33. }
  34. //更改catalog表的order字段名为s_number
  35. $columns = M("Catalog")->getDbFields();
  36. if ($columns) {
  37. foreach ($columns as $key => $value) {
  38. if ($value == 'order') {
  39. $sql = "ALTER TABLE `".C('DB_PREFIX')."catalog` CHANGE `order` `s_number` INT( 10 ) NOT NULL DEFAULT '99' COMMENT '顺序号。数字越小越靠前。若此值全部相等时则按id排序';";
  40. M("Catalog")->execute($sql);
  41. }
  42. }
  43. }
  44. //更改page表的order字段名为s_number
  45. $columns = M("Page")->getDbFields();
  46. if ($columns) {
  47. foreach ($columns as $key => $value) {
  48. if ($value == 'order') {
  49. $sql = "ALTER TABLE `".C('DB_PREFIX')."page` CHANGE `order` `s_number` INT( 10 ) NOT NULL DEFAULT '99' COMMENT '顺序号。数字越小越靠前。若此值全部相等时则按id排序';";
  50. M("Page")->execute($sql);
  51. }
  52. }
  53. }
  54. //更改page_history表的order字段名为s_number
  55. $columns = M("PageHistory")->getDbFields();
  56. if ($columns) {
  57. foreach ($columns as $key => $value) {
  58. if ($value == 'order') {
  59. $sql = "ALTER TABLE `".C('DB_PREFIX')."page_history` CHANGE `order` `s_number` INT( 10 ) NOT NULL DEFAULT '99' COMMENT '顺序号。数字越小越靠前。若此值全部相等时则按id排序';";
  60. M("PageHistory")->execute($sql);
  61. }
  62. }
  63. }
  64. //为catalog表增加addtime索引
  65. $indexs = M("Catalog")->query(" show index from ".C('DB_PREFIX')."catalog");
  66. if ($indexs) {
  67. $has_it = 0 ;//是否存在该索引
  68. foreach ($indexs as $key => $value) {
  69. if ($value['column_name'] =='addtime') {
  70. $has_it = 1 ;
  71. }
  72. }
  73. if ($has_it === 0 ) {
  74. M("Catalog")->execute("ALTER TABLE ".C('DB_PREFIX')."catalog ADD INDEX ( `addtime` ) ;");
  75. }
  76. }
  77. //为item表增加addtime索引
  78. $indexs = M("Item")->query(" show index from ".C('DB_PREFIX')."item");
  79. if ($indexs) {
  80. $has_it = 0 ;//是否存在该索引
  81. foreach ($indexs as $key => $value) {
  82. if ($value['column_name'] =='addtime') {
  83. $has_it = 1 ;
  84. }
  85. }
  86. if ($has_it === 0 ) {
  87. M("Item")->execute("ALTER TABLE ".C('DB_PREFIX')."item ADD INDEX ( `addtime` ) ;");
  88. }
  89. }
  90. //为page表增加addtime索引
  91. $indexs = M("Page")->query(" show index from ".C('DB_PREFIX')."page");
  92. if ($indexs) {
  93. $has_it = 0 ;//是否存在该索引
  94. foreach ($indexs as $key => $value) {
  95. if ($value['column_name'] =='addtime') {
  96. $has_it = 1 ;
  97. }
  98. }
  99. if ($has_it === 0 ) {
  100. M("page")->execute("ALTER TABLE ".C('DB_PREFIX')."page ADD INDEX ( `addtime` ) ;");
  101. }
  102. }
  103. //为page_history表增加addtime索引
  104. $indexs = M("PageHistory")->query(" show index from ".C('DB_PREFIX')."page_history");
  105. if ($indexs) {
  106. $has_it = 0 ;//是否存在该索引
  107. foreach ($indexs as $key => $value) {
  108. if ($value['column_name'] =='addtime') {
  109. $has_it = 1 ;
  110. }
  111. }
  112. if ($has_it === 0 ) {
  113. M("PageHistory")->execute("ALTER TABLE ".C('DB_PREFIX')."page_history ADD INDEX ( `addtime` ) ;");
  114. }
  115. }
  116. //为page_history表增加page_id索引
  117. $indexs = M("PageHistory")->query(" show index from ".C('DB_PREFIX')."page_history");
  118. if ($indexs) {
  119. $has_it = 0 ;//是否存在该索引
  120. foreach ($indexs as $key => $value) {
  121. if ($value['column_name'] =='page_id') {
  122. $has_it = 1 ;
  123. }
  124. }
  125. if ($has_it === 0 ) {
  126. M("PageHistory")->execute("ALTER TABLE ".C('DB_PREFIX')."page_history ADD INDEX ( `page_id` ) ;");
  127. }
  128. }
  129. //catalog表增加parent_cat_id字段
  130. $columns = M("catalog")->getDbFields();
  131. if ($columns) {
  132. $has_it = 0 ;//是否存在该字段
  133. foreach ($columns as $key => $value) {
  134. if ($value == 'parent_cat_id') {
  135. $has_it = 1 ;
  136. }
  137. }
  138. if ($has_it === 0) {
  139. $sql = "ALTER TABLE ".C('DB_PREFIX')."catalog ADD parent_cat_id INT( 10 ) NOT NULL DEFAULT '0' COMMENT '上一级目录的id';";
  140. D("catalog")->execute($sql);
  141. }
  142. }
  143. //catalog表增加level字段
  144. $columns = M("catalog")->getDbFields();
  145. if ($columns) {
  146. $has_it = 0 ;//是否存在该字段
  147. foreach ($columns as $key => $value) {
  148. if ($value == 'level') {
  149. $has_it = 1 ;
  150. }
  151. }
  152. if ($has_it === 0) {
  153. $sql = "ALTER TABLE ".C('DB_PREFIX')."catalog ADD level INT( 10 ) NOT NULL DEFAULT '2' COMMENT '2为二级目录,3为三级目录';";
  154. D("catalog")->execute($sql);
  155. }
  156. }
  157. //item表增加item_domain字段
  158. $columns = M("item")->getDbFields();
  159. if ($columns) {
  160. $has_it = 0 ;//是否存在该字段
  161. foreach ($columns as $key => $value) {
  162. if ($value == 'item_domain') {
  163. $has_it = 1 ;
  164. }
  165. }
  166. if ($has_it === 0) {
  167. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD item_domain varchar( 50 ) NOT NULL DEFAULT '' COMMENT 'item的个性域名';";
  168. D("item")->execute($sql);
  169. }
  170. }
  171. $sql = "CREATE TABLE IF NOT EXISTS `user_token` (
  172. `id` int(10) NOT NULL AUTO_INCREMENT,
  173. `uid` int(10) NOT NULL DEFAULT '0',
  174. `token` varchar(200) NOT NULL DEFAULT '',
  175. `token_expire` int(11) NOT NULL DEFAULT '0' ,
  176. `ip` varchar(200) NOT NULL DEFAULT '',
  177. `addtime` int(11) NOT NULL DEFAULT '0',
  178. PRIMARY KEY (`id`),
  179. KEY `token` (`token`)
  180. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='' AUTO_INCREMENT=1 ";
  181. D("User")->execute($sql);
  182. echo "OK!";
  183. }
  184. public function sqlite(){
  185. //catalog表增加parent_cat_id字段
  186. $columns = M("catalog")->getDbFields();
  187. if ($columns) {
  188. $has_it = 0 ;//是否存在该字段
  189. foreach ($columns as $key => $value) {
  190. if ($value == 'parent_cat_id') {
  191. $has_it = 1 ;
  192. }
  193. }
  194. if ($has_it === 0) {
  195. $sql = "ALTER TABLE ".C('DB_PREFIX')."catalog ADD parent_cat_id INT( 10 ) NOT NULL DEFAULT '0' ;";
  196. D("catalog")->execute($sql);
  197. }
  198. }
  199. //catalog表增加level字段
  200. $columns = M("catalog")->getDbFields();
  201. if ($columns) {
  202. $has_it = 0 ;//是否存在该字段
  203. foreach ($columns as $key => $value) {
  204. if ($value == 'level') {
  205. $has_it = 1 ;
  206. }
  207. }
  208. if ($has_it === 0) {
  209. $sql = "ALTER TABLE ".C('DB_PREFIX')."catalog ADD level INT( 10 ) NOT NULL DEFAULT '2' ;";
  210. D("catalog")->execute($sql);
  211. }
  212. }
  213. //item表增加item_domain字段
  214. $columns = M("item")->getDbFields();
  215. if ($columns) {
  216. $has_it = 0 ;//是否存在该字段
  217. foreach ($columns as $key => $value) {
  218. if ($value == 'item_domain') {
  219. $has_it = 1 ;
  220. }
  221. }
  222. if ($has_it === 0) {
  223. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD item_domain text NOT NULL DEFAULT '';";
  224. D("item")->execute($sql);
  225. }
  226. }
  227. //创建user_token表
  228. $sql = "CREATE TABLE IF NOT EXISTS `user_token` (
  229. `id` INTEGER PRIMARY KEY ,
  230. `uid` int(10) NOT NULL DEFAULT '0',
  231. `token` CHAR(200) NOT NULL DEFAULT '',
  232. `token_expire` int(11) NOT NULL DEFAULT '0' ,
  233. `ip` CHAR(200) NOT NULL DEFAULT '',
  234. `addtime` int(11) NOT NULL DEFAULT '0'
  235. )";
  236. D("UserToken")->execute($sql);
  237. echo 'OK!';
  238. }
  239. }