diff --git a/setup/upgrade/lib/UpgradeItems.inc.php b/setup/upgrade/lib/UpgradeItems.inc.php new file mode 100644 index 0000000..36b1aca --- /dev/null +++ b/setup/upgrade/lib/UpgradeItems.inc.php @@ -0,0 +1,492 @@ +. + * + * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco, + * California 94120-7775, or email info@knowledgetree.com. + * + * The interactive user interfaces in modified source and object code versions + * of this program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU General Public License version 3. + * + * In accordance with Section 7(b) of the GNU General Public License version 3, + * these Appropriate Legal Notices must retain the display of the "Powered by + * KnowledgeTree" logo and retain the original copyright notice. If the display of the + * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices + * must display the words "Powered by KnowledgeTree" and retain the original + * copyright notice. + * Contributor( s): ______________________________________ + * + */ + +// {{{ Format of the descriptor +/** + * Format of the descriptor + * + * type*version*phase*simple description for uniqueness + * + * type is: sql, function, subupgrade, upgrade + * version is: 1.2.4, 2.0.0rc5 + * phase is: 0, 1, 0pre. Phase is _only_ evaluated by describeUpgrades. + * description is: anything, unique in terms of version and type. + */ +// }}} + +//require_once(KT_LIB_DIR . '/upgrades/UpgradeFunctions.inc.php'); +require_once('sqlfile.inc.php'); +require_once('datetime.inc'); + +// {{{ Upgrade_Already_Applied +class Upgrade_Already_Applied { //extends PEAR_Error { + function Upgrade_Already_Applied($oUpgradeItem) { + $this->oUpgradeItem = $oUpgradeItem; + } +} +// }}} + +class UpgradeItem extends InstallUtil { + var $type = ""; + var $name; + var $version; + var $description; + var $phase; + var $priority = 0; + var $parent; + var $date; + var $result; + + function UpgradeItem($name, $version, $description = null, $phase = 0, $priority = 0) { + $this->name = $name; + $this->version = $version; + if (is_null($description)) { + $description = $this->type . " upgrade to version " . $version . " phase " . $phase; + } + $this->description = $description; + $this->phase = $phase; + $this->priority = $priority; + parent::__construct(); +// print_r($this); +// die; + } + + function setParent($parent) { + $this->parent = $parent; + } + function setDate($date) { + $this->date = $date; + } + + function getDescriptor() { + return join("*", array($this->type, $this->version, $this->phase, $this->name)); + } + + function getDescription() { + return $this->description; + } + + function getVersion() { + return $this->version; + } + + function getPhase() { + return $this->phase; + } + + function getPriority() { + return $this->priority; + } + + function getType() { + return $this->type; + } + + function runDBQuery($query) { + require_once("../wizard/steps/configuration.php"); // configuration to read the ini path + $wizConfigHandler = new configuration(); + $configPath = $wizConfigHandler->readConfigPathIni(); + if(!is_object($this->iniUtilities)) { + parent::__construct(); + } + $this->iniUtilities->load($configPath); + $dconf = $this->iniUtilities->getSection('db'); + $this->dbUtilities->load($dconf['dbHost'], '', $dconf['dbUser'], $dconf['dbPass'], $dconf['dbName']); + $result = $this->dbUtilities->query($query); + $assArr = $this->dbUtilities->fetchAssoc($result); + return $assArr; + } + + function _upgradeTableInstalled() { + $query = "SELECT COUNT(id) FROM upgrades"; + $res = $this->runDBQuery($query); + if($res) { + return true; + } + return false; + } + + function isAlreadyApplied() { + if (!$this->_upgradeTableInstalled()) { + return false; + } + $query = "SELECT id FROM upgrades WHERE descriptor = '".$this->getDescriptor()."' AND result = 1"; + $res = $this->runDBQuery($query); + if($res) { + return true; + } + return false; + } + + function performUpgrade($force = false) { + $res = $this->isAlreadyApplied(); + if ($res === true) { + if ($force !== true) { + // PHP5: Exception + return new Upgrade_Already_Applied($this); + } + } +// if (PEAR::isError($res)) { +// return $res; +// } + $oCache =& KTCache::getSingleton(); + $save = $oCache->bEnabled; + $oCache->bEnabled = false; + $res = $this->_performUpgrade(); + $oCache->bEnabled = $save; +// if (PEAR::isError($res)) { +// $this->_recordUpgrade(false); +// return $res; +// } + $res = $this->_recordUpgrade(true); +// if (PEAR::isError($res)) { +// return $res; +// } + return true; + } + + function _performUpgrade() { +// return PEAR::raiseError("Unimplemented"); + } + + function _recordUpgrade($result) { + if (is_null($this->date)) { + $this->date = getCurrentDateTime(); + } + if ($this->parent) { + $parentid = $this->parent->getDescriptor(); + } else { + $parentid = null; + } + //TODO: Where is the code? + exit("add code"); + /*return $this->autoInsert(); + + DBUtil::autoInsert("upgrades", array( + "descriptor" => $this->getDescriptor(), + "description" => $this->description, + "date_performed" => $this->date, + "result" => $result, + "parent" => $parentid, + ));*/ + } + + // STATIC + function getAllUpgrades() { + return array(); + } + + +} + +class SQLUpgradeItem extends UpgradeItem { + function SQLUpgradeItem($path, $version = null, $description = null, $phase = null, $priority = null) { + $this->type = "sql"; + $this->priority = 0; + $details = $this->_getDetailsFromFileName($path); + if (is_null($version)) { + $version = $details[1]; + } + if (is_null($description)) { + $description = $details[2]; + } + if (is_null($phase)) { + $phase = $details[3]; + } + if (is_null($priority)) { + $priority = isset($details[4]) ? $details[4] : 0; + } + $this->UpgradeItem($path, $version, $description, $phase, $priority); + } + + /** + * Describe the SQL scripts that will be used to upgrade KnowledgeTree + * + * Return an array of arrays with two components: a string identifier + * that uniquely describes the step to be taken and a string which is an + * HTML-formatted description of the step to be taken. These will be + * returned in any order - describeUpgrade performs the ordering. + * + * @param string Original version (e.g., "1.2.4") + * @param string Current version (e.g., "2.0.2") + * + * @return array Array of SQLUpgradeItem describing steps to be taken + * + * STATIC + */ + function getUpgrades($origVersion, $currVersion) { +// global $default; + +// $sqlupgradedir = KT_DIR . '/sql/' . $default->dbType . '/upgrade/'; + $dbType = 'mysql'; + $sqlupgradedir = KT_DIR . 'sql/' . $dbType . '/upgrade/'; + $ret = array(); + + if (!is_dir($sqlupgradedir)) { +// return PEAR::raiseError("SQL Upgrade directory ($sqlupgradedir) not accessible"); + } + if (!($dh = opendir($sqlupgradedir))) { +// return PEAR::raiseError("SQL Upgrade directory ($sqlupgradedir) not accessible"); + } + + while (($file = readdir($dh)) !== false) { + // Each entry can be a file or a directory + // + // A file is legacy before the upgrade system was created, but + // will be supported anyway. + // + // A directory is the end-result version: so, 2.0.5 contains + // every script that differentiates it from a previous version, + // say, 2.0.5rc1 or 2.0.4. + // + if (in_array($file, array('.', '..', 'CVS'))) { + continue; + } + $fullpath = $sqlupgradedir . $file; + if (is_file($fullpath)) { + // Legacy file support, will be in form of + // 1.2.4-to-2.0.0.sql. + $details = SQLUpgradeItem::_getDetailsFromFileName($file); + if ($details) { + if (!gte_version($details[0], $origVersion)) { + continue; + } + if (!lte_version($details[1], $currVersion)) { + continue; + } + //print "Will run $file\n"; +// print_r($this->util->dbUtilities); +// die; + $ret[] = new SQLUpgradeItem($file); + } + } + if (is_dir($fullpath)) { + $subdir = $file; + if (!($subdh = opendir($fullpath))) { + continue; + } + while (($file = readdir($subdh)) !== false) { + $relpath = $subdir . '/' . $file; + $details = SQLUpgradeItem::_getDetailsFromFileName($relpath); + if ($details) { + if (!gte_version($details[0], $origVersion)) { + continue; + } + if (!lte_version($details[1], $currVersion)) { + continue; + } + //print "Will run $file\n"; +// print_r(SQLUpgradeItem::); +// die; +// new InstallUtil(); + $ret[] = new SQLUpgradeItem($relpath); + } + } + } + } + closedir($dh); + return $ret; + } + + function _getDetailsFromFileName($path) { + // Old format (pre 2.0.6) + $matched = preg_match('#^([\d.]*)-to-([\d.]*).sql$#', $path, $matches); + if ($matched != 0) { + $fromVersion = $matches[1]; + $toVersion = $matches[2]; + $description = "Database upgrade from version $fromVersion to $toVersion"; + $phase = 0; + return array($fromVersion, $toVersion, $description, $phase); + } + $matched = preg_match('#^([\d.]*)/(?:(\d*)-)?(.*)\.sql$#', $path, $matches); + //$matched = preg_match('#^([\d.]*)/(?:(\d*)-)?(.*):(?:(\d*))\.sql$#', $path, $matches); + if ($matched != 0) { + $fromVersion = $matches[1]; + $toVersion = $matches[1]; + $in = array('_'); + $out = array(' '); + $phase = (int)$matches[2]; + + //$priority = (int)$matches[4]; + $priority = 0; + $iPriority = preg_match('#^(.*)-(\d*)$#', $matches[3], $priorities); + if($iPriority != 0){ + $priority = $priorities[2]; + $matches[3] = $priorities[1]; + } + + $description = "Database upgrade to version $toVersion: " . ucfirst(str_replace($in, $out, $matches[3])); + return array($fromVersion, $toVersion, $description, $phase, $priority); + } + // XXX: handle new format + return null; + } + + function _performUpgrade() { +// global $default; + $dbType = 'mysql'; + $sqlupgradedir = KT_DIR . 'sql/' . $dbType . '/upgrade/'; + $queries = SQLFile::sqlFromFile($sqlupgradedir . $this->name); + exit('add code'); +// return DBUtil::runQueries($queries, $default->_admindb); + } + + +} + +class KTRebuildPermissionObserver { + function start() { + $this->lastBeat = time(); + } + function receiveMessage() { + $now = time(); + if ($this->lastBeat + 15 < $now) { + print ""; + ob_flush(); + flush(); + } + } + function end() { + } +} + +class RecordUpgradeItem extends UpgradeItem { + function RecordUpgradeItem ($version, $oldversion = null) { + $this->type = "upgrade"; + if (is_null($oldversion)) { + $this->description = "Upgrade to version $version"; + } else { + $this->description = "Upgrade from version $oldversion to $version"; + } + $this->phase = 99; + $this->version = $version; + $this->name = 'upgrade' . $version; + } + + function _performUpgrade() { +// $this->_deleteSmartyFiles(); +// $this->_deleteProxyFiles(); +// require_once(KT_LIB_DIR . '/cache/cache.inc.php'); +// $oCache =& KTCache::getSingleton(); +// $oCache->deleteAllCaches(); + // TODO : clear cache folder +// require_once(KT_LIB_DIR . '/permissions/permissionutil.inc.php'); + // TODO : What does this do +// $po =& new KTRebuildPermissionObserver($this); +// $po->start(); +// $oChannel =& KTPermissionChannel::getSingleton(); +// $oChannel->addObserver($po); + + set_time_limit(0); + ignore_user_abort(true); + +// KTPermissionUtil::rebuildPermissionLookups(true); +// $po->end(); + + $versionFile=KT_DIR . '/docs/VERSION-NAME.txt'; + $fp = fopen($versionFile,'rt'); + $systemVersion = fread($fp, filesize($versionFile)); + fclose($fp); + + $query = "UPDATE system_settings SET value = '$systemVersion' WHERE name = 'knowledgetreeVersion'"; + $this->runDBQuery($query); + $query = "UPDATE system_settings SET value = '{$this->version}' WHERE name = 'databaseVersion'"; + $assArray = $this->runDBQuery($query); + return !is_null($assArray); + } + + function _deleteSmartyFiles() { + $oConfig =& KTConfig::getSingleton(); + $dir = sprintf('%s/%s', $oConfig->get('urls/varDirectory'), 'tmp'); + + $dh = @opendir($dir); + if (empty($dh)) { + return; + } + $aFiles = array(); + while (false !== ($sFilename = readdir($dh))) { + if (substr($sFilename, -10) == "smarty.inc") { + $aFiles[] = sprintf('%s/%s', $dir, $sFilename); + } + if (substr($sFilename, -10) == "smarty.php") { + $aFiles[] = sprintf('%s/%s', $dir, $sFilename); + } + } + foreach ($aFiles as $sFile) { + @unlink($sFile); + } + } + + + function _deleteProxyFiles() { + $oKTConfig =& KTConfig::getSingleton(); + + + // from ktentityutil::_proxyCreate + $sDirectory = $oKTConfig->get('cache/proxyCacheDirectory'); + + if (!file_exists($sDirectory)) { + return; + } + $sRunningUser = KTUtil::running_user(); + if ($sRunningUser) { + $sDirectory = sprintf("%s/%s", $sDirectory, $sRunningUser); + } + if (!file_exists($sDirectory)) { + return ; + } + + $dh = @opendir($sDirectory); + if (empty($dh)) { + return; + } + $aFiles = array(); + while (false !== ($sFilename = readdir($dh))) { + + if (substr($sFilename, -8) == ".inc.php") { + $aFiles[] = sprintf('%s/%s', $sDirectory, $sFilename); + } + } + + foreach ($aFiles as $sFile) { + @unlink($sFile); + } + } +} + +?> diff --git a/setup/upgrade/lib/datetime.inc b/setup/upgrade/lib/datetime.inc new file mode 100644 index 0000000..6bcffd2 --- /dev/null +++ b/setup/upgrade/lib/datetime.inc @@ -0,0 +1,58 @@ +. + * + * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco, + * California 94120-7775, or email info@knowledgetree.com. + * + * The interactive user interfaces in modified source and object code versions + * of this program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU General Public License version 3. + * + * In accordance with Section 7(b) of the GNU General Public License version 3, + * these Appropriate Legal Notices must retain the display of the "Powered by + * KnowledgeTree" logo and retain the original copyright notice. If the display of the + * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices + * must display the words "Powered by KnowledgeTree" and retain the original + * copyright notice. + * Contributor( s): ______________________________________ + */ + +/** + * Returns the current date time + * + * @return string the current date time (Y-m-d H:i:s) + */ +function getCurrentDateTime() { + return date("Y-m-d H:i:s", time()); +} + +/** + * Returns the specified date time, formatted as Y-m-d H:i:s + * + * @param int the date time to format + * @return string the formatted date time + */ +function formatDateTime($dateTime) { + return date("Y-m-d H:i:s", $dateTime); +} +?> diff --git a/setup/upgrade/lib/sqlfile.inc.php b/setup/upgrade/lib/sqlfile.inc.php new file mode 100644 index 0000000..9fb3ebc --- /dev/null +++ b/setup/upgrade/lib/sqlfile.inc.php @@ -0,0 +1,141 @@ +. + * + * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco, + * California 94120-7775, or email info@knowledgetree.com. + * + * The interactive user interfaces in modified source and object code versions + * of this program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU General Public License version 3. + * + * In accordance with Section 7(b) of the GNU General Public License version 3, + * these Appropriate Legal Notices must retain the display of the "Powered by + * KnowledgeTree" logo and retain the original copyright notice. If the display of the + * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices + * must display the words "Powered by KnowledgeTree" and retain the original + * copyright notice. + * Contributor( s): ______________________________________ + * + */ + +class SQLFile { + function sqlFromFile($path) { + return SQLFile::splitSQL(file_get_contents($path)); + } + + function splitSQL($sql) { + $instring = false; + $i = 0; + $remaining = $sql; + $query = ""; + $aQueries = array(); + + $strlen = strlen($sql); + + for ($i = 0; $i < $strlen; $i++) { + $c = $remaining[$i]; + if ($c === ";") { + $query .= substr($remaining, 0, $i + 1); + $aQueries[] = $query; + $query = ""; + $remaining = trim(substr($remaining, $i + 1)); + $i = 0; + $strlen = strlen($remaining); + continue; + } + if ($c === "`") { + $next = strpos($remaining, "`", $i); + if ($next === false) { + $query .= $remaining; + $aQueries[] = $query; + return $aQueries; + } + $query .= substr($remaining, 0, $next); + $remaining = substr($remaining, $next); + $i = 0; + $strlen = strlen($remaining); + continue; + } + if (($c === "'") || ($c === '"')) { + $stringchar = $c; + $notfound = true; + + while ($notfound) { + $next = strpos($remaining, $stringchar, $i + 1); + if ($next === false) { + $query .= $remaining; + $aQueries[] = $query; + return $aQueries; + } + $i = $next + 1; + $quotes = true; + $b = 1; + while ($remaining[$next - $b] === "\\") { + $quotes = !$quotes; + $b++; + } + if ($quotes) { + $notfound = false; + } + } + $query .= substr($remaining, 0, $next); + $remaining = substr($remaining, $next); + $i = 0; + $strlen = strlen($remaining); + continue; + } + + $nextdelim = SQLFile::_nextDelim($remaining); + if ($nextdelim === false) { + $query .= $remaining; + $aQueries[] = $query; + return $aQueries; + } + // $query .= substr($remaining, 0, $nextdelim); + } + return $aQueries; + } + + function _nextDelim($string) { + $q = strpos($string, "'"); + $d = strpos($string, '"'); + $b = strpos($string, "`"); + $s = strpos($string, ";"); + + $min = false; + foreach (array($q, $d, $b, $s) as $c) { + if ($min === false) { + $min = $c; + continue; + } + if ($c === false) { + continue; + } + if ($c < $min) { + $min = $c; + continue; + } + } + return $min; + } +} + +?> diff --git a/setup/upgrade/lib/upgrade.inc.php b/setup/upgrade/lib/upgrade.inc.php new file mode 100644 index 0000000..f083351 --- /dev/null +++ b/setup/upgrade/lib/upgrade.inc.php @@ -0,0 +1,218 @@ +. + * + * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco, + * California 94120-7775, or email info@knowledgetree.com. + * + * The interactive user interfaces in modified source and object code versions + * of this program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU General Public License version 3. + * + * In accordance with Section 7(b) of the GNU General Public License version 3, + * these Appropriate Legal Notices must retain the display of the "Powered by + * KnowledgeTree" logo and retain the original copyright notice. If the display of the + * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices + * must display the words "Powered by KnowledgeTree" and retain the original + * copyright notice. + * Contributor( s): ______________________________________ + */ + +require_once('UpgradeItems.inc.php'); + +//function setupAdminDatabase() { +// global $default; +// $dsn = array( +// 'phptype' => $default->dbType, +// 'username' => $default->dbAdminUser, +// 'password' => $default->dbAdminPass, +// 'hostspec' => $default->dbHost, +// 'database' => $default->dbName, +// 'port' => $default->dbPort, +// ); +// +// $options = array( +// 'debug' => 2, +// 'portability' => DB_PORTABILITY_ERRORS, +// 'seqname_format' => 'zseq_%s', +// ); +// +// $default->_admindb = &DB::connect($dsn, $options); +// if (PEAR::isError($default->_admindb)) { +// die($default->_admindb->toString()); +// } +// $default->_admindb->setFetchMode(DB_FETCHMODE_ASSOC); +// return; +//} +//setupAdminDatabase(); + +// {{{ Format of the descriptor +/** + * Format of the descriptor + * + * type*version*phase*simple description for uniqueness + * + * type is: sql, function, subupgrade, upgrade + * version is: 1.2.4, 2.0.0rc5 + * phase is: 0, 1, 0pre. Phase is _only_ evaluated by describeUpgrades. + * description is: anything, unique in terms of version and type. + */ +// }}} + +// {{{ describeUpgrade +/** + * Describe the upgrade path between two versions of KnowledgeTree. + * + * @param string Original version (e.g., "1.2.4") + * @param string Current version (e.g., "2.0.2") + * + * @return array Array of UpgradeItem describing steps to be taken + */ +function &describeUpgrade ($origVersion, $currVersion) { + // How to figure out what upgrades to do: + // + // 1. Get all SQL upgrades >= origVersion and <= currVersion + // 2. Get all Function upgrades >= origVersion and <= currVersion + // 3. Categorise each into version they upgrade to + // 4. Sort each version subgroup into correct order + // 5. Add "recordSubUpgrade" for each version there. + // 6. Add back into one big list again + // 7. Add "recordUpgrade" for whole thing + + // $recordUpgrade = array('upgrade*' . $currVersion, 'Upgrade to ' . $currVersion, null); + + $steps = array(); + foreach (array('SQLUpgradeItem') as $itemgen) { + $f = array($itemgen, 'getUpgrades'); + $ssteps =& call_user_func($f, $origVersion, $currVersion); + $scount = count($ssteps); + for ($i = 0; $i < $scount; $i++) { + $steps[] =& $ssteps[$i]; + } + } + $upgradestep =& new RecordUpgradeItem($currVersion, $origVersion); + $steps[] =& $upgradestep; + $stepcount = count($steps); + for ($i = 0; $i < $stepcount; $i++) { + $step =& $steps[$i]; + $step->setParent($upgradestep); + } + usort($steps, 'step_sort_func'); + + return $steps; +} +// }}} + +// {{{ step_sort_func +function step_sort_func ($obj1, $obj2) { + // Ugly hack to ensure that upgrade table is made first... + if ($obj1->name === "2.0.6/create_upgrade_table.sql") { + return -1; + } + if ($obj2->name === "2.0.6/create_upgrade_table.sql") { + return 1; + } + + // Priority upgrades run first + if ($obj1->getPriority() < $obj2->getPriority()) { + return 1; + } + if ($obj1->getPriority() > $obj2->getPriority()) { + return -1; + } + + // early version run first + $res = compare_version($obj1->getVersion(), $obj2->getVersion()); + if ($res !== 0) { + return $res; + } + // Order by phase + if ($obj1->getPhase() > $obj2->getPhase()) { + return 1; + } + if ($obj1->getPhase() < $obj2->getPhase()) { + return -1; + } + // Order by name + if ($obj1->name < $obj2->name) { + return -1; + } + if ($obj1->name > $obj2->name) { + return 1; + } + return 0; +} +// }}} + +// {{{ compare_version +/** + * Compares two version numbers and returns a value based on this comparison + * + * Using standard software version rules, such as 2.0.5rc1 comes before + * 2.0.5, and 2.0.5rc1 comes after 2.0.5alpha1, compare two version + * numbers, and determine which is the higher. + * + * XXX: Actually, just does $version1 < $version2 + * + * @param string First version number + * @param string Second version number + * + * @return int -1, 0, 1 + */ +function compare_version($version1, $version2) { + // XXX: Version comparisons should be better. + if ($version1 < $version2) { + return -1; + } + if ($version1 > $version2) { + return 1; + } + return 0; +} +// }}} + +// {{{ lte_version +/** + * Quick-hand for checking if a version number is lower-than-or-equal-to + */ +function lte_version($version1, $version2) { + if (in_array(compare_version($version1, $version2), array(-1, 0))) { + return true; + } + return false; +} +// }} + +// {{ gte_version +/** + * Quick-hand for checking if a version number is greater-than-or-equal-to + */ +function gte_version($version1, $version2) { + if (in_array(compare_version($version1, $version2), array(0, 1))) { + return true; + } + return false; +} +// }}} + +?> diff --git a/setup/upgrade/steps/upgradeDatabase.php b/setup/upgrade/steps/upgradeDatabase.php index 2b7c385..d84565f 100644 --- a/setup/upgrade/steps/upgradeDatabase.php +++ b/setup/upgrade/steps/upgradeDatabase.php @@ -43,7 +43,8 @@ //require_once('../../config/dmsDefaults.php'); //require_once(KT_LIB_DIR . '/config/config.inc.php'); //require_once(KT_LIB_DIR . '/plugins/pluginutil.inc.php'); -//define('KT_LIB_DIR', SYSTEM_DIR.'lib'.DS); +define('KT_DIR', SYSTEM_DIR); +define('KT_LIB_DIR', SYSTEM_DIR.'lib'); //require_once(SYSTEM_DIR . 'lib/upgrades/upgrade.inc.php'); class upgradeDatabase extends Step @@ -92,10 +93,10 @@ class upgradeDatabase extends Step * @var array */ public $storeInSession = true; - + public $sysVersion = ''; protected $silent = false; protected $temp_variables = array(); - + public $paths = ''; /** * Main control of database setup * @@ -143,17 +144,14 @@ class upgradeDatabase extends Step private function doRun($action = null) { // $this->readConfig(KTConfig::getConfigFilename()); - require_once("../wizard/steps/configuration.php"); // configuration to read the ini path - $wizConfigHandler = new configuration(); - $configPath = $wizConfigHandler->readConfigPathIni(); - $this->readConfig($configPath); - if($this->dbSettings['dbPort'] == '') { - $con = $this->util->dbUtilities->load($this->dbSettings['dbHost'], $this->dbSettings['dbUser'], - $this->dbSettings['dbPass'], $this->dbSettings['dbName']); - } else { - $con = $this->util->dbUtilities->load($this->dbSettings['dbHost'].":".$this->dbSettings['dbPort'], $this->dbSettings['dbUser'], + + $this->readConfig(); +// if($this->dbSettings['dbPort'] == '') { +// $con = $this->util->dbUtilities->load($this->dbSettings['dbHost'], '', $this->dbSettings['dbUser'],$this->dbSettings['dbPass'], $this->dbSettings['dbName']); +// } else { + $con = $this->util->dbUtilities->load($this->dbSettings['dbHost'], $this->dbSettings['dbPort'], $this->dbSettings['dbUser'], $this->dbSettings['dbPass'], $this->dbSettings['dbName']); - } +// } $this->temp_variables['action'] = $action; if (is_null($action) || ($action == 'preview')) { @@ -177,23 +175,19 @@ class upgradeDatabase extends Step } private function generateUpgradeTable() { -// global $default; - $v = $this->readVersion(); -// $this->temp_variables['systemVersion'] = $default->systemVersion; - $this->temp_variables['systemVersion'] = $v; - -// $query = sprintf('SELECT value FROM %s WHERE name = "databaseVersion"', $default->system_settings_table); - $query = sprintf('SELECT value FROM %s WHERE name = "databaseVersion"', 'config_settings'); - + $this->sysVersion = $this->readVersion(); + $this->temp_variables['systemVersion'] = $this->sysVersion; + $dconf = $this->util->iniUtilities->getSection('db'); + $query = sprintf('SELECT value FROM %s WHERE name = "databaseVersion"', 'system_settings'); + $this->util->dbUtilities->load($dconf['dbHost'], '', $dconf['dbUser'], $dconf['dbPass'], $dconf['dbName']); $result = $this->util->dbUtilities->query($query); + $assArr = $this->util->dbUtilities->fetchAssoc($result); if ($result) { - $lastVersionObj = $this->util->dbUtilities->fetchNextObject($result); - $lastVersion = $lastVersionObj->value; + $lastVersion = $assArr[0]['value']; } - $currentVersion = $v; - + $currentVersion = $this->sysVersion; + require_once("lib/upgrade.inc.php"); $upgrades = describeUpgrade($lastVersion, $currentVersion); - $ret = "\n"; $ret .= "\n"; $i=0; @@ -259,8 +253,10 @@ class upgradeDatabase extends Step } } - private function readConfig($path) { - //$ini = $this->util->loadInstallIni($path); + private function readConfig() { + require_once("../wizard/steps/configuration.php"); // configuration to read the ini path + $wizConfigHandler = new configuration(); + $path = $wizConfigHandler->readConfigPathIni(); $this->util->iniUtilities->load($path); $dbSettings = $this->util->iniUtilities->getSection('db'); $this->dbSettings = array('dbHost'=> $dbSettings['dbHost'], @@ -271,7 +267,11 @@ class upgradeDatabase extends Step 'dbAdminUser'=> $dbSettings['dbAdminUser'], 'dbAdminPass'=> $dbSettings['dbAdminPass'], ); + $this->paths = $this->util->iniUtilities->getSection('urls'); + $this->paths = array_merge($this->paths, $this->util->iniUtilities->getSection('cache')); $this->temp_variables['dbSettings'] = $this->dbSettings; + $this->sysVersion = $this->readVersion(); + $this->cachePath = $wizConfigHandler->readCachePath(); } private function upgradeConfirm() @@ -291,9 +291,10 @@ class upgradeDatabase extends Step $errors = false; $this->temp_variables['detail'] = '

The table below describes the upgrades that have occurred to - upgrade your KnowledgeTree installation to ' . $default->systemVersion . ''; + upgrade your KnowledgeTree installation to ' . $this->sysVersion . ''; $pre_res = $this->performPreUpgradeActions(); + if (PEAR::isError($pre_res)) { $errors = true; $this->temp_variables['preUpgrade'] = 'Pre-Upgrade actions failed.'; @@ -336,7 +337,7 @@ class upgradeDatabase extends Step // It should idealy work the same as the upgrades. // global $default; - +// print_r($this->paths);die; // Lock the scheduler $lockFile = $default->cacheDirectory . DIRECTORY_SEPARATOR . 'scheduler.lock'; touch($lockFile); diff --git a/setup/upgrade/steps/upgradeWelcome.php b/setup/upgrade/steps/upgradeWelcome.php index 5d158c8..3e4d056 100644 --- a/setup/upgrade/steps/upgradeWelcome.php +++ b/setup/upgrade/steps/upgradeWelcome.php @@ -40,12 +40,9 @@ * @version Version 0.1 */ -//require_once('../../config/dmsDefaults.php'); -//require_once KT_LIB_DIR . '/authentication/authenticationutil.inc.php'; - class upgradeWelcome extends step { - protected $silent = false; + protected $silent = true; protected $temp_variables = array(); protected $error = array() ; @@ -84,7 +81,7 @@ class upgradeWelcome extends step { private function checkPassword($username, $password) { $dconf = $this->getDataFromPackage('installers', 'database'); // Use info from install if($dconf) { - $this->util->dbUtilities->load($dconf['dhost'], $dconf['duname'], $dconf['dpassword'], $dconf['dname']); + $this->util->dbUtilities->load($dconf['dhost'], $dbconf['dport'], $dconf['duname'], $dconf['dpassword'], $dconf['dname']); } else { require_once("../wizard/steps/configuration.php"); // configuration to read the ini path $wizConfigHandler = new configuration(); @@ -93,7 +90,7 @@ class upgradeWelcome extends step { $dconf = $this->util->iniUtilities->getSection('db'); if($dconf['dbPort'] == 'default') $dconf['dbPort'] = 3306; - $this->util->dbUtilities->load($dconf['dbHost'].":".$dconf['dbPort'], $dconf['dbUser'], $dconf['dbPass'], $dconf['dbName']); + $this->util->dbUtilities->load($dconf['dbHost'],$dconf['dbPort'], $dconf['dbUser'], $dconf['dbPass'], $dconf['dbName']); $sQuery = "SELECT count(*) AS match_count FROM users WHERE username = '$username' AND password = '".md5($password)."'"; $res = $this->util->dbUtilities->query($sQuery); $ass = $this->util->dbUtilities->fetchAssoc($res); @@ -110,7 +107,22 @@ class upgradeWelcome extends step { return $this->error; } + /** + * Returns step variables + * + * @author KnowledgeTree Team + * @param none + * @access public + * @return array + */ + public function getStepVars() + { + return $this->temp_variables; + } + public function storeSilent() { + + } } ?> \ No newline at end of file diff --git a/setup/wizard/dbUtilities.php b/setup/wizard/dbUtilities.php index b015d41..6206fe6 100644 --- a/setup/wizard/dbUtilities.php +++ b/setup/wizard/dbUtilities.php @@ -101,12 +101,14 @@ class dbUtilities { * @access public */ public function __construct() { - + } - public function load($dhost = 'localhost', $duname, $dpassword, $dbname) { + public function load($dhost = 'localhost', $dport = 'default', $duname, $dpassword, $dbname) { if(!$this->isConnected($dhost, $duname, $dpassword, $dbname)) { - $this->dbhost = $dhost; + if($dport == 'default' || $dport == '') + $dport = '3306'; + $this->dbhost = $dhost.":".$dport; $this->dbuname = $duname; $this->dbpassword = $dpassword; $this->dbconnection = @mysql_connect($dhost, $duname, $dpassword); diff --git a/setup/wizard/iniUtilities.php b/setup/wizard/iniUtilities.php index eae907b..e47053a 100644 --- a/setup/wizard/iniUtilities.php +++ b/setup/wizard/iniUtilities.php @@ -55,8 +55,8 @@ class iniUtilities { $this->read($iniFile); } - function __construct() { - } +// function __construct() { +// } /** * Create a backup with the date as an extension in the same location as the original config.ini diff --git a/setup/wizard/steps/complete.php b/setup/wizard/steps/complete.php index e3b66cf..8df2aae 100644 --- a/setup/wizard/steps/complete.php +++ b/setup/wizard/steps/complete.php @@ -142,7 +142,7 @@ class complete extends Step { // retrieve database information from session $dbconf = $this->getDataFromSession("database"); // make db connection - admin - $this->util->dbUtilities->load($dbconf['dhost'], $dbconf['dmsname'], $dbconf['dmspassword'], $dbconf['dname']); + $this->util->dbUtilities->load($dbconf['dhost'], $dbconf['dport'], $dbconf['dmsname'], $dbconf['dmspassword'], $dbconf['dname']); $loaded = $this->util->dbUtilities->getDatabaseLink(); if (!$loaded) { $this->temp_variables['dbConnectAdmin'] .= '

' @@ -157,7 +157,7 @@ class complete extends Step { } // make db connection - user - $this->util->dbUtilities->load($dbconf['dhost'], $dbconf['dmsusername'], $dbconf['dmsuserpassword'], $dbconf['dname']); + $this->util->dbUtilities->load($dbconf['dhost'], $dbconf['dport'], $dbconf['dmsusername'], $dbconf['dmsuserpassword'], $dbconf['dname']); $loaded = $this->util->dbUtilities->getDatabaseLink(); // if we can log in to the database, check access // TODO check write access? diff --git a/setup/wizard/steps/configuration.php b/setup/wizard/steps/configuration.php index 75d5fc6..7024af7 100644 --- a/setup/wizard/steps/configuration.php +++ b/setup/wizard/steps/configuration.php @@ -307,7 +307,7 @@ class configuration extends Step { $conf = $this->getDataFromSession("configuration"); // get data from the server $dbconf = $this->getDataFromSession("database"); - $this->util->dbUtilities->load($dbconf['dhost'], $dbconf['dmsname'], $dbconf['dmspassword'], $dbconf['dname']); + $this->util->dbUtilities->load($dbconf['dhost'], $dbconf['dport'], $dbconf['dmsname'], $dbconf['dmspassword'], $dbconf['dname']); $server = $conf['server']; $paths = $conf['paths']; if ($this->util->isMigration()) { // Check if its an upgrade @@ -355,7 +355,7 @@ class configuration extends Step private function writeDBSection($server) { $dbconf = $this->getDataFromSession("database"); // retrieve database information from session - $this->util->dbUtilities->load($dbconf['dhost'], $dbconf['duname'], $dbconf['dpassword'], $dbconf['dname']); // initialise the db connection + $this->util->dbUtilities->load($dbconf['dhost'], $dbconf['dport'], $dbconf['duname'], $dbconf['dpassword'], $dbconf['dname']); // initialise the db connection $server = $this->registerDBConfig($server, $dbconf); // add db config to server variables $table = 'config_settings'; foreach($server as $item) { // write server settings to config_settings table and config.ini @@ -577,12 +577,41 @@ class configuration extends Step return $this->temp_variables['paths']['configFile']['path']; } $configPath = $this->getContentPath(); - if(!$configPath) return false; + if(!$configPath) { + return false; + } $this->util->iniUtilities->load($configPath); $data = $this->util->iniUtilities->getFileByLine(); $firstline = true; foreach ($data as $k=>$v) { if(preg_match('/config.ini/', $k)) { // Find config.ini + if($k == "config/config.ini") { // Source install and source upgrades + $configIniPath = realpath(SYSTEM_DIR.$k); + if($configIniPath) + return $configIniPath; + } + return $k; + } + } + + return false; + } + + public function readCachePath() { + $cachePath = $this->getCachePath(); + if(!$cachePath) { + return false; + } + $this->util->iniUtilities->load($cachePath); + $data = $this->util->iniUtilities->getFileByLine(); + $firstline = true; + foreach ($data as $k=>$v) { + if(preg_match('/cache/', $k)) { // Find config.ini + if($k == "var/cache") { // Source install and source upgrades + $configIniPath = realpath(SYSTEM_DIR.$k); + if($configIniPath) + return $configIniPath; + } return $k; } } diff --git a/setup/wizard/steps/database.php b/setup/wizard/steps/database.php index 922d5d0..4ee436d 100644 --- a/setup/wizard/steps/database.php +++ b/setup/wizard/steps/database.php @@ -319,11 +319,11 @@ class database extends Step $this->error['dmsuserpassword'] = "Passwords do not match: " . $this->dmsuserpassword." ". $this->getPassword2(); return false; } - if($this->dport == '') { - $this->util->dbUtilities->load($this->dhost, $this->duname, $this->dpassword, $this->dname); - } else { - $this->util->dbUtilities->load($this->dhost.":".$this->dport, $this->duname, $this->dpassword, $this->dname); - } +// if($this->dport == '') { +// $this->util->dbUtilities->load($this->dhost, $this->duname, $this->dpassword, $this->dname); +// } else { + $this->util->dbUtilities->load($this->dhost, $this->dport, $this->duname, $this->dpassword, $this->dname); +// } if (!$this->util->dbUtilities->getDatabaseLink()) { $this->error['con'] = "Could not connect to the database, please check username and password"; return false; @@ -582,7 +582,7 @@ class database extends Step * @return object mysql connection */ private function connectMysql() { - $this->util->dbUtilities->load($this->dhost, $this->duname, $this->dpassword, $this->dname); + $this->util->dbUtilities->load($this->dhost, $this->dport, $this->duname, $this->dpassword, $this->dname); } /** @@ -842,7 +842,7 @@ class database extends Step $this->dpassword = 'root'; $this->dname = 'dms_install'; $this->dbbinary = 'mysql'; - $this->util->dbUtilities->load($this->dhost, $this->duname, $this->dpassword, $this->dname); + $this->util->dbUtilities->load($this->dhost, '', $this->duname, $this->dpassword, $this->dname); $this->createSchema(); echo 'Schema loaded
'; } diff --git a/setup/wizard/steps/install.php b/setup/wizard/steps/install.php index 48ba644..951e0af 100644 --- a/setup/wizard/steps/install.php +++ b/setup/wizard/steps/install.php @@ -107,7 +107,7 @@ class install extends step public function callHome() { $conf = $this->getDataFromSession("install"); // retrieve database information from session $dbconf = $this->getDataFromSession("database"); - $this->util->dbUtilities->load($dbconf['dhost'], $dbconf['duname'], $dbconf['dpassword'], $dbconf['dname']); // initialise the db connection + $this->util->dbUtilities->load($dbconf['dhost'], '', $dbconf['duname'], $dbconf['dpassword'], $dbconf['dname']); // initialise the db connection $complete = 1; if($conf['call_home'] == 'enable'){ $complete = 0; diff --git a/setup/wizard/templates/complete.tpl b/setup/wizard/templates/complete.tpl index e78f57c..35fe8e5 100644 --- a/setup/wizard/templates/complete.tpl +++ b/setup/wizard/templates/complete.tpl @@ -136,7 +136,7 @@ Goto Database Upgrade - Goto Login + Goto Login
CodeDescriptionApplied