upgrade.inc.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * [³¬¼¶»î¶¯(xj_event.upgrade)] (C)2012-2099 Powered by åÐÒ£Éè¼Æ.
  4. * Version: 1.1
  5. * Date: 2012-11-5 08:25
  6. */
  7. if(!defined('IN_DISCUZ')) {
  8. exit('Access Denied');
  9. }
  10. global $_G;
  11. $plugin_xj_wxopen_login = DB::table('xj_wxopen_login');
  12. $plugin_xj_wxopen_session = DB::table('xj_wxopen_session');
  13. $sql = <<<EOF
  14. CREATE TABLE IF NOT EXISTS `$plugin_xj_wxopen_login` (
  15. `id` int(10) NOT NULL,
  16. `sessionkey` varchar(32) NOT NULL,
  17. `openid` varchar(32) NOT NULL,
  18. `dateline` int(10) NOT NULL,
  19. `unionid` varchar(32) NOT NULL,
  20. `uid` int(10) NOT NULL DEFAULT '0'
  21. ) ENGINE=MyISAM;
  22. CREATE TABLE `$plugin_xj_wxopen_session` (
  23. `threekey` varchar(32) NOT NULL,
  24. `sessionkey` varchar(32) NOT NULL,
  25. `openid` varchar(32) NOT NULL,
  26. `uid` int(10) NOT NULL DEFAULT '0',
  27. `unionid` varchar(32) NOT NULL,
  28. `dateline` int(10) NOT NULL DEFAULT '0'
  29. ) ENGINE=MEMORY;
  30. ALTER TABLE `$plugin_xj_wxopen_login` ADD PRIMARY KEY (`id`);
  31. ALTER TABLE `$plugin_xj_wxopen_session` ADD KEY `threekey` (`threekey`);
  32. ALTER TABLE `$plugin_xj_wxopen_login` MODIFY `id` int(10) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
  33. EOF;
  34. myrunquery($sql);
  35. showmessage('Upgrade is complete');
  36. function createtable($sql, $dbcharset) {
  37. $type = strtoupper(preg_replace("/^\s*CREATE TABLE\s+.+\s+\(.+?\).*(ENGINE|TYPE)\s*=\s*([a-z]+?).*$/isU", "\\2", $sql));
  38. $type = in_array($type, array('MYISAM', 'HEAP')) ? $type : 'MYISAM';
  39. return preg_replace("/^\s*(CREATE TABLE\s+.+\s+\(.+?\)).*$/isU", "\\1", $sql).
  40. (mysql_get_server_info() > '4.1' ? " ENGINE=$type DEFAULT CHARSET=$dbcharset" : " TYPE=$type");
  41. }
  42. function myrunquery($sql) {
  43. global $_G;
  44. $tablepre = $_G['config']['db'][1]['tablepre'];
  45. $dbcharset = $_G['config']['db'][1]['dbcharset'];
  46. $sql = str_replace(array(' cdb_', ' `cdb_', ' pre_', ' `pre_'), array(' {tablepre}', ' `{tablepre}', ' {tablepre}', ' `{tablepre}'), $sql);
  47. $sql = str_replace("\r", "\n", str_replace(array(' {tablepre}', ' `{tablepre}'), array(' '.$tablepre, ' `'.$tablepre), $sql));
  48. $ret = array();
  49. $num = 0;
  50. foreach(explode(";\n", trim($sql)) as $query) {
  51. $queries = explode("\n", trim($query));
  52. foreach($queries as $query) {
  53. $ret[$num] .= $query[0] == '#' || $query[0].$query[1] == '--' ? '' : $query;
  54. }
  55. $num++;
  56. }
  57. unset($sql);
  58. foreach($ret as $query) {
  59. $query = trim($query);
  60. if($query) {
  61. if(substr($query, 0, 12) == 'CREATE TABLE') {
  62. $name = preg_replace("/CREATE TABLE ([a-z0-9_]+) .*/is", "\\1", $query);
  63. DB::query(createtable($query, $dbcharset));
  64. } else {
  65. DB::query($query);
  66. }
  67. }
  68. }
  69. }
  70. ?>