Index: trunk/core/install.php =================================================================== diff -u -N -r7702 -r7855 --- trunk/core/install.php (.../install.php) (revision 7702) +++ trunk/core/install.php (.../install.php) (revision 7855) @@ -11,14 +11,14 @@ * Upgrade sqls are located using this mask * */ - define('UPGRADES_FILE', FULL_PATH.'/%sinstall/upgrades.sql'); - + define('UPGRADES_FILE', FULL_PATH.'/%sinstall/upgrades.%s'); + /** * Format of version identificator in upgrade files * */ define('VERSION_MARK', '# ===== v ([\d]+\.[\d]+\.[\d]+) ====='); - + // print_pre($_POST); $install_engine = new kInstallator(); @@ -97,7 +97,7 @@ * @var Array */ var $skipLoginSteps = Array ('root_password', 'choose_modules', 'finish', -1); - + /** * Steps, on which kApplication should not be initialized, because of missing correct db table structure * @@ -151,7 +151,7 @@ // can't check login on steps where no application present anyways :) $this->skipLoginSteps = array_unique(array_merge($this->skipLoginSteps, $this->skipApplicationSteps)); - + $this->SelectPreset(); if (!$this->currentStep) { @@ -166,7 +166,7 @@ reset($this->steps[$this->stepsPreset]); $this->currentStep = current($this->steps[$this->stepsPreset]); } - + /** * Selects preset to proceed based on various criteria * @@ -192,7 +192,7 @@ $this->stepsPreset = $preset; } - + function GetVar($name) { return isset($_REQUEST[$name]) ? $_REQUEST[$name] : false; @@ -214,7 +214,7 @@ $this->SetFirstStep(); } } - + switch ($this->currentStep) { case 'check_paths': foreach ($this->writeableFolders as $folder_path) { @@ -261,18 +261,18 @@ if (!$modules) { $this->currentStep = $this->GetNextStep(); } - break; - + break; + case 'install_setup': $next_preset = $this->Application->GetVar('next_preset'); if ($next_preset !== false && $this->Application->GetVar('login') == 'root') { // option was choosen, then verify password & login user $login_event = new kEvent('u.current:OnLogin'); $this->Application->HandleEvent($login_event); - + if ($login_event->status == erSUCCESS) { // login succeeded - + if (!isset($this->steps[$next_preset])) { $this->errorMessage = 'Preset "'.$next_preset.'" not yet implemented'; } @@ -284,7 +284,7 @@ // login failed $user =& $this->Application->recallObject('u.current'); /* @var $user UsersItem */ - + $this->errorMessage = $user->GetErrorMsg('ValidateLogin').'. If you don\'t know your username or password, contact Intechnic Support'; } } @@ -368,7 +368,7 @@ // import base data into database $this->RunSQL('/core/install/install_schema.sql'); $this->RunSQL('/core/install/install_data.sql'); - + // set module "Core" version after install (based on upgrade scripts) $this->SetModuleVersion('Core'); break; @@ -400,18 +400,15 @@ $install_file = MODULES_PATH.'/'.$module.'/install.php'; if (file_exists($install_file)) { include_once($install_file); - + // set module version after install (based on upgrade scripts) $this->SetModuleVersion($module); } } } // scan themes - $themes_helper =& $this->Application->recallObject('ThemesHelper'); - /* @var $themes_helper kThemesHelper */ + $this->Application->HandleEvent($themes_event, 'adm:OnRebuildThemes'); - $themes_helper->refreshThemes(); - $this->Conn->Query('UPDATE '.TABLE_PREFIX.'Theme SET Enabled=1, PrimaryTheme =1 LIMIT 1'); // update categories cache @@ -420,23 +417,29 @@ $updater->OneStepRun(); break; - + case 'upgrade_modules': // get installed modules from db and compare their versions to upgrade script $modules = $this->Application->GetVar('modules'); if ($modules) { $upgrade_data = $this->GetUpgradableModules(); - + foreach ($modules as $module_name) { $module_info = $upgrade_data[$module_name]; - $upgrades_file = sprintf(UPGRADES_FILE, $module_info['Path']); - + $upgrades_file = sprintf(UPGRADES_FILE, $module_info['Path'], 'sql'); + $sqls = file_get_contents($upgrades_file); $version_mark = preg_replace('/(\(.*?\))/', $module_info['FromVersion'], VERSION_MARK); - + + // get only sqls from next (relative to current) version to end of file $start_pos = strpos($sqls, $version_mark); $sqls = substr($sqls, $start_pos); + + preg_match_all('/'.VERSION_MARK.'/s', $sqls, $regs); + + $this->RunUpgrades($module_info['Path'], $regs[1], 'before'); $this->RunSQLText($sqls); + $this->RunUpgrades($module_info['Path'], $regs[1], 'after'); // after upgrade sqls are executed update version $this->SetModuleVersion($module_name, $module_info['ToVersion']); @@ -445,15 +448,15 @@ else { $this->errorMessage = 'Please select module(-s) to upgrade'; } - break; - + break; + case 'finish': // delete cache $sql = 'DELETE FROM '.TABLE_PREFIX.'Cache WHERE VarName IN ("config_files","configs_parsed","sections_parsed")'; $this->Conn->Query($sql); - - // set installation finished mark + + // set installation finished mark if ($this->Application->ConfigValue('InstallFinished') === false) { $fields_hash = Array ( 'VariableName' => 'InstallFinished', @@ -481,6 +484,39 @@ } /** + * Run upgrade PHP scripts for module with specified path + * + * @param string $module_path + * @param Array $versions + * @param string $mode upgrade mode = {before,after} + */ + function RunUpgrades($module_path, $versions, $mode) + { + static $upgrade_classes = Array (); + + $upgrades_file = sprintf(UPGRADES_FILE, $module_path, 'php'); + if (!file_exists($upgrades_file) || !$versions) { + return ; + } + + if (!isset($upgrade_classes[$module_path])) { + // save class name, because 2nd time + // (in after call $upgrade_class variable will not be present) + include_once $upgrades_file; + $upgrade_classes[$module_path] = $upgrade_class; + } + + $upgrade_object = new $upgrade_classes[$module_path](); + + foreach ($versions as $version) { + $upgrade_method = 'Upgrade_'.str_replace('.', '_', $version); + if (method_exists($upgrade_object, $upgrade_method)) { + $upgrade_object->$upgrade_method($mode); + } + } + } + + /** * Sets module version to passed * * @param string $module_name @@ -491,16 +527,16 @@ if ($version === false) { $version = $this->GetMaxModuleVersion($module_name); } - + $table_prefix = $this->systemConfig['Database']['TablePrefix']; - + $sql = 'UPDATE '.$table_prefix.'Modules SET Version = "'.$version.'" WHERE Name = "'.$module_name.'"'; $this->Conn->Query($sql); } - - + + /** * Sets new configuration variable value * @@ -556,19 +592,19 @@ function GetMaxModuleVersion($module_name) { - $upgrades_file = sprintf(UPGRADES_FILE, strtolower($module_name).'/'); + $upgrades_file = sprintf(UPGRADES_FILE, strtolower($module_name).'/', 'sql'); if (!file_exists($upgrades_file)) { // no upgrade file return '4.0.1'; } - + $sqls = file_get_contents($upgrades_file); $versions_found = preg_match_all('/'.VERSION_MARK.'/s', $sqls, $regs); if (!$versions_found) { // upgrades file doesn't contain version definitions return '4.0.1'; } - + return end($regs[1]); } @@ -722,7 +758,7 @@ // replace something additionally, e.g. module root category $sqls = str_replace($replace_from, $replace_to, $sqls); } - + $sqls = str_replace("\r\n", "\n", $sqls); // convert to linux line endings $sqls = preg_replace("/#(.*?)\n/", '', $sqls); // remove all comments $sqls = explode(";\n", $sqls); @@ -740,7 +776,7 @@ } } } - + function ImportLanguage($lang_file) { $lang_file = FULL_PATH.$lang_file.'.lang'; @@ -791,12 +827,12 @@ function ConvertModuleVersion($version) { $parts = explode('.', $version); - + $bin = ''; foreach ($parts as $part) { $bin .= str_pad(decbin($part), 8, '0', STR_PAD_LEFT); } - + return bindec($bin); } @@ -807,21 +843,21 @@ function GetUpgradableModules() { $ret = Array (); - + foreach ($this->Application->ModuleInfo as $module_name => $module_info) { - $upgrades_file = sprintf(UPGRADES_FILE, $module_info['Path']); + $upgrades_file = sprintf(UPGRADES_FILE, $module_info['Path'], 'sql'); if (!file_exists($upgrades_file)) { // no upgrade file continue; } - + $sqls = file_get_contents($upgrades_file); $versions_found = preg_match_all('/'.VERSION_MARK.'/s', $sqls, $regs); if (!$versions_found) { // upgrades file doesn't contain version definitions continue; } - + $to_version = end($regs[1]); $this_version = $this->ConvertModuleVersion($module_info['Version']); if ($this->ConvertModuleVersion($to_version) > $this_version) { @@ -832,19 +868,19 @@ break; } } - + $version_info = Array ( 'FromVersion' => $from_version, 'ToVersion' => $to_version, ); - + $ret[$module_name] = array_merge_recursive2($module_info, $version_info); } } - + return $ret; } - + /** * Returns content to show for current step *