PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/aoliz/core/model/system/mdl.upgrade.php

http://phpfor.googlecode.com/
PHP | 276 lines | 227 code | 30 blank | 19 comment | 33 complexity | f455356bb0ca663dedc7e85af12ea3e0 MD5 | raw file
  1. <?php
  2. class mdl_upgrade extends modelFactory{
  3. var $line=0;
  4. var $sql=null;
  5. var $succ=false;
  6. function exec($step){
  7. if(method_exists($this,$action = 'action_'.$step)){
  8. $this->$action();
  9. }else{
  10. $this->action_welcome();
  11. }
  12. }
  13. function action_welcome(){
  14. $versionTxt = $this->system->version();
  15. $dbver = $_GET['force_ver']?$_GET['force_ver']:$this->dbVersion();
  16. $this->system->cache->clear();
  17. $smarty = &$this->system->loadModel('system/frontend');
  18. $smarty->compile_dir = HOME_DIR.'/cache/front_tmpl';
  19. $smarty->clear_all_cache();
  20. if($this->checkFileList(BASE_DIR.'/upgrade.php',$failedFiles,1) || $_GET['ignore_lost']){
  21. $scripts = $this->scripts($dbver,$versionTxt['rev']);
  22. $data['scripts'] = &$scripts;
  23. $data['scripts_count'] = count($scripts);
  24. if($data['scripts_count']){
  25. $this->output('ready.html',$data);
  26. }else{
  27. $this->output('done.html',$data);
  28. }
  29. }else{
  30. $data['files'] = $failedFiles;
  31. $this->output('uploading.html',$data);
  32. }
  33. }
  34. function action_runscript(){
  35. header('Content-type: text/html;charset=utf-8');
  36. set_time_limit(0);
  37. $file = $_POST['file'];
  38. if(!file_exists(HOME_DIR.'/logs/upgrade_'.substr( $_POST['file'] ,0 ,-4 ).'_'.$_POST['timeline'].'.log.php')){
  39. error_log('<?php exit()?>'." \n \n",3,HOME_DIR.'/logs/upgrade_'.substr( $_POST['file'] ,0 ,-4 ).'_'.$_POST['timeline'].'.log.php');
  40. }
  41. switch(ext_name($file)){
  42. case '.php':
  43. include(CORE_DIR.'/updatescripts/'.$file);
  44. if(class_exists('UpgradeScript')){
  45. $oUpgrade = new UpgradeScript();
  46. $oUpgrade->step=$_POST['step']?$_POST['step']:'1';
  47. $oUpgrade->runFunc = $_POST['runFunc']?$_POST['runFunc']:'first';
  48. $oUpgrade->status = $_POST['runStatus']?$_POST['runStatus']:'all-finish';
  49. $oUpgrade->version = substr( $file ,0 ,-4 );
  50. // $oUpgrade->runFunc = $runFunc;
  51. $oUpgrade->__Upgrade();
  52. $this->setDbver(substr($file,0,-4));
  53. }
  54. break;
  55. case '.sql':
  56. $this->run_update_sql($file);
  57. break;
  58. case '.db':
  59. $this->diff_db();
  60. break;
  61. }
  62. }
  63. function setDbver($ver){
  64. $this->db->exec("drop table if exists ".DB_PREFIX."dbver");
  65. $this->db->exec("create table ".DB_PREFIX."dbver(`".$ver."` varchar(255))");
  66. $this->db->exec("INSERT INTO ".DB_PREFIX."dbver VALUES('".time()."')");
  67. }
  68. function diff_db(){
  69. $schema=&$this->system->loadModel('utility/schemas');
  70. $dbtables = $schema->get_system_schemas();
  71. foreach($dbtables as $tbname=>$struct){
  72. if($diffsql = $schema->diff($tbname,$struct)){
  73. foreach($diffsql as $sql){
  74. $this->db->exec($sql);
  75. }
  76. }
  77. }
  78. return true;
  79. }
  80. function run_update_sql($file){
  81. /*
  82. foreach($this->db->splitSql(file_get_contents(CORE_DIR.'/updatescripts/'.$file)) as $runningSQL){
  83. if($this->db->exec($runningSQL)){
  84. $output.=update_message($runningSQL);
  85. }else{
  86. $errinfo = $this->db->errorInfo();
  87. $etype = E_ERROR;
  88. if(preg_match('/syntax to use near \'(.*?)\' at line/i',$errinfo,$match)){
  89. $runningSQL = str_replace($match[1],'<b>'.$match[1].'</b>',$runningSQL);
  90. }elseif(preg_match('/Duplicate [a-z]+ name/',$errinfo)){
  91. $etype = E_WARNING;
  92. }
  93. $output.=update_message($runningSQL,$etype);
  94. }
  95. }*/
  96. echo 'success!';//$output;
  97. }
  98. function output($output,$data){
  99. $data['page'] = $output;
  100. $smarty = &$this->system->loadModel('system/frontend');
  101. $smarty->template_dir = CORE_DIR.'/admin/view/';
  102. $data['version'] = $this->system->version();
  103. $smarty->_vars = &$data;
  104. header('Content-Type: text/html;charset=utf-8');
  105. $smarty->display('upgrade/page.html');
  106. }
  107. function checkFileList($file,&$list,$ignore_lines=0){
  108. $list = array();
  109. $handle = fopen($file,'r');
  110. while ($line = fgetcsv($handle, 1000, ',')) {
  111. if($ignore_lines > $i++){
  112. continue;
  113. }
  114. if(md5_file(BASE_DIR.'/'.$line[0]) != $line[1]){
  115. $list[] = $line[0];
  116. }
  117. }
  118. fclose($handle);
  119. return 0==count($list);
  120. }
  121. function scripts($from,$to){
  122. $dir = CORE_DIR.'/updatescripts';
  123. if ($handle = opendir($dir)) {
  124. while (false !== ($file = readdir($handle))){
  125. if (is_file($dir.'/'.$file) && $file{0}!='.' && !strstr($file,'.sql')){
  126. if(preg_match('/^([0-9]+)\.(sql|php)$/i',$file,$match)){
  127. if($match[1]>$from && $match[1]<=$to){
  128. $step[]=$file;
  129. if($match[2]=='php'){
  130. $order[] = $match[1]*2+1;
  131. }else{
  132. $order[] = $match[1]*2;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. closedir($handle);
  139. }
  140. array_multisort($order,$step);
  141. return array_merge(array('upgrade.db'),$step);
  142. }
  143. function dbVersion(){
  144. $rs = $this->db->exec('SHOW TABLES like "'.DB_PREFIX.'dbver"');
  145. if($this->db->getRows($rs, 1)){
  146. $rs = $this->db->exec('select * from '.DB_PREFIX.'dbver');
  147. $col = mysql_fetch_field($rs['rs'], 0);
  148. $time = mysql_fetch_row($rs['rs']);
  149. $rs = null;
  150. if($col->name == 'dbver'){
  151. $col->name = 35789;
  152. }
  153. $dbver = $col->name;
  154. }else{
  155. $dbver = 35789;
  156. }
  157. if($time[0] && $time[0] > time()-180){
  158. return $dbver;
  159. }else{
  160. return $dbver-1;
  161. }
  162. }
  163. }
  164. function update_message($msg,$etype=0,$errinfo=null){
  165. $upgradeVersion = substr( $_POST['file'] ,0 ,-4 );
  166. $logFileName = HOME_DIR.'/logs/upgrade_'.$upgradeVersion.'_'.$_POST['timeline'].'.log.php';
  167. switch($etype){
  168. case E_WARNING:
  169. case E_USER_WARNING:
  170. error_log(__('?? ').$msg.($errinfo?'->'.$errinfo:'')." \n \n",3,$logFileName);
  171. return '<div class="runsqldetail"><div class="sql-body">'.$msg.__('</div><div class="error">[??]').$errinfo.'</div></div>';
  172. case E_ERROR:
  173. case E_USER_ERROR:
  174. error_log(__('?? ').$msg.($errinfo?'->'.$errinfo:'')." \n \n",3,$logFileName);
  175. return '<div class="runsqldetail"><div class="sql-body">'.$msg.__('</div><div class="warning">[??]').$errinfo.'</div></div>';
  176. default:
  177. error_log(__('?? ').$msg.($errinfo?'->'.$errinfo:'')." \n \n",3,$logFileName);
  178. return __('<div class="runsqldetail"><div class="succ">[??]').$msg.'</div></div>';
  179. }
  180. }
  181. class Upgrade {
  182. var $step;
  183. var $msg;
  184. var $status;
  185. var $title = '';
  186. var $funcList = array();
  187. var $runFunc = 'first';
  188. var $version;
  189. var $updateMsg;
  190. function Upgrade(){
  191. $this->system = &$GLOBALS['system'];
  192. $this->db = &$this->system->database();
  193. set_time_limit(0);
  194. foreach( array_diff( get_class_methods($this) , get_class_methods('Upgrade') ) as $func ){
  195. if( substr($func,0,8) != 'upgrade_' )
  196. continue;
  197. $this->funcList[] = substr($func,8);
  198. }
  199. // echo '<input type="hidden" class="allFunc" value="'.implode(',',$this->funcList).'"/>';
  200. }
  201. function upgrade_first(){
  202. }
  203. function upgrade_last(){
  204. }
  205. function __Upgrade(){
  206. eval('$this->status = $this->upgrade_'.$this->runFunc.'();');
  207. echo $this->updateMsg;
  208. if( $this->runFunc == 'first' ){
  209. $this->title = $this->title?$this->title:__('?????');
  210. $this->runFunc = array_key_exists( 0, $this->funcList )?$this->funcList[0]:'last';
  211. $this->status = $this->status?$this->status:'finish';
  212. }else if( $this->runFunc == 'last' ){
  213. $this->status = 'all-finish';
  214. echo '<input type="hidden" class="upgrade-notice" value=\''.json_encode($this->noticeMsg).'\'/>';
  215. }else{
  216. switch($this->status){
  217. case 'continue':
  218. $this->title = $this->title?__('???? ').$this->title:__('???');
  219. //to be continue...
  220. break;
  221. case 'finish':
  222. $this->title = $this->title?$this->title:__('???');
  223. $funcKey = array_search($this->runFunc, $this->funcList);
  224. if(array_key_exists( $funcKey+1, $this->funcList )){
  225. $this->runFunc = $this->funcList[$funcKey+1];
  226. }else{
  227. $this->title = __('????????');
  228. $this->runFunc = 'last';
  229. }
  230. break;
  231. case 'error':
  232. echo '<input type="hidden" class="upgrade-notice" value=\''.json_encode($this->noticeMsg).'\'/>';
  233. $this->title .= __(' ????');
  234. break;
  235. default:
  236. $this->status = 'all-finish';
  237. break;
  238. }
  239. }
  240. echo '<input type="hidden" class="up-title" value="'.$this->title.'"/>';
  241. echo '<input type="hidden" class="runFunc" value="'.$this->runFunc.'" />';
  242. echo '<input type="hidden" class="run-status" value="'.$this->status.'"/>';
  243. // usleep(100000);
  244. }
  245. }
  246. ?>