protected function execute(InputInterface $input, OutputInterface $output) { if (!file_exists('bootstrap.php')) { throw new \Exception("Run this script in the application root directory"); } \Kwf_Setup::setUp(); if (file_exists('setup/initial/dump.sql')) { unlink('setup/initial/dump.sql'); } if (file_exists('setup/initial/uploads')) { foreach (glob('setup/initial/uploads/*') as $f) { unlink($f); } } if ($input->getOption('include-initial-dump')) { $output->writeln("checking for pending updates..."); $pendingUpdatesCount = \Kwf_Util_Update_Helper::countPendingUpdates(); if ($pendingUpdatesCount) { throw new \Exception("{$pendingUpdatesCount} Updates have not been executed. Run update first."); } $output->writeln("creating database dump..."); $dump = DbDump::dump(); if (!file_exists('setup/initial')) { mkdir('setup/initial', 0777, true); $ignore = ""; if (file_exists('setup/.gitignore')) { $ignore = file_get_contents('setup/.gitignore'); } if (!preg_match('#^initial$#m', $ignore)) { $ignore = rtrim($ignore); if ($ignore) { $ignore .= "\n"; } $ignore .= "initial\n"; } file_put_contents('setup/.gitignore', $ignore); } file_put_contents('setup/initial/dump.sql', $dump); $output->writeln("copying uploads..."); if (!file_exists('setup/initial/uploads')) { mkdir('setup/initial/uploads'); } $model = \Kwf_Model_Abstract::getInstance('Kwf_Uploads_Model'); $select = new \Kwf_Model_Select(); $it = new \Kwf_Model_Iterator_Packages(new \Kwf_Model_Iterator_Rows($model, $select)); foreach ($it as $row) { $fileSource = $row->getFileSource(); copy($fileSource, 'setup/initial/uploads/' . basename($fileSource)); } } $excludes = ExcludeFinder::findExcludes('.'); $excludeArgs = ''; foreach ($excludes as $i) { $excludeArgs .= " -x " . escapeshellarg('./' . $i . '*'); } $cmd = "zip deploy.zip . --quiet -r {$excludeArgs}"; $output->writeln("creating deploy.zip archive..."); $this->_systemCheckRet($cmd, $input, $output); $output->writeln("deploy.zip successfully created."); }
public static function executeUpdates($request, $view) { $doneNames = Kwf_Util_Update_Helper::getExecutedUpdatesNames(); $updates = Kwf_Util_Update_Helper::getUpdates(); foreach ($updates as $k => $u) { if (in_array($u->getUniqueName(), $doneNames) || $u->getLegacyName() && in_array($u->getLegacyName(), $doneNames)) { unset($updates[$k]); } } $c = new Kwf_Util_ProgressBar_Adapter_Cache($request->getParam('progressNum')); $runner = new Kwf_Util_Update_Runner($updates); $progress = new Zend_ProgressBar($c, 0, $runner->getProgressSteps()); $runner->setProgressBar($progress); if (!$runner->checkUpdatesSettings()) { throw new Kwf_Exception_Client("checkSettings failed, update stopped"); } $doneNames = array_merge($doneNames, $runner->executeUpdates()); $runner->writeExecutedUpdates($doneNames); $errMsg = ''; $errors = $runner->getErrors(); if ($errors) { $errMsg .= count($errors) . " setup script(s) failed:\n"; foreach ($errors as $error) { $errMsg .= $error['name'] . ": \n"; $errMsg .= $error['message'] . "\n\n"; } } $message = 'Executed ' . count($updates) . " update scripts"; $view->errMsg = $errMsg; $view->message = $message; }
public function testGetUpdatesSql() { Kwf_Registry::get('config')->server->updateTags = array('db'); $updates = Kwf_Util_Update_Helper::getUpdatesForDir('Kwf_Update_UpdateSql_Update'); $this->assertEquals(1, count($updates)); $this->assertEquals(100, $updates[0]->getLegacyRevision()); $this->assertEquals('foo bar;', $updates[0]->sql); }
public function testGetUpdatesSql3() { Kwf_Registry::get('config')->server->updateTags = array('kwf'); $updates = Kwf_Util_Update_Helper::getUpdatesForDir('Kwf_Update_Tags_Update'); $this->assertEquals(1, count($updates)); $this->assertEquals(101, $updates[0]->getLegacyRevision()); $this->assertEquals(array('kwf'), $updates[0]->getTags()); }
public function update() { $db = Kwf_Registry::get('db'); $doneNames = Kwf_Util_Update_Helper::getExecutedUpdatesNames(); $db->query("CREATE TABLE `kwf_updates` (\n `id` int(11) NOT NULL,\n `name` varchar(255) NOT NULL,\n `executed_at` datetime DEFAULT NULL,\n `log` TEXT NOT NULL\n ) ENGINE=InnoDB DEFAULT CHARSET=latin1;"); $db->query("ALTER TABLE `kwf_updates`\n ADD PRIMARY KEY (`id`);"); $db->query("ALTER TABLE `kwf_updates`\n MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;"); foreach ($doneNames as $name) { $db->query("INSERT INTO kwf_updates SET name=?, executed_at=NOW()", $name); } if (in_array('kwf_update', $db->listTables())) { $db->query("RENAME TABLE `kwf_update` TO `kwf_update_backup`"); } }
public function indexAction() { Kwf_Util_MemoryLimit::set(512); Kwf_Events_ModelObserver::getInstance()->disable(); //try to update old-style db config if (file_exists('config.db.ini')) { $db = file_get_contents('config.db.ini'); if (file_exists('config.local.ini')) { $c = file_get_contents('config.local.ini'); } else { $c = "[production]\n"; } $c .= "\n"; $db = str_replace("[database]\n", '', $db); foreach (explode("\n", trim($db)) as $line) { if (trim($line)) { $c .= "database." . $line . "\n"; } } file_put_contents('config.local.ini', $c); unlink('config.db.ini'); } if ($this->_getParam('rev')) { throw new Kwf_Exception("rev parameter is not supported anymore"); } if ($this->_getParam('class')) { throw new Kwf_Exception("class parameter is not supported anymore"); } $skipClearCache = $this->_getParam('skip-clear-cache'); $doneNames = Kwf_Util_Update_Helper::getExecutedUpdatesNames(); if ($this->_getParam('name')) { $updates = Kwf_Util_Update_Helper::getUpdates(); foreach ($updates as $k => $u) { $n = $u->getUniqueName(); $n = substr($n, strrpos($n, '_') + 1); if ($n != $this->_getParam('name')) { unset($updates[$k]); } } } else { if (!$skipClearCache) { Kwf_Util_ClearCache::getInstance()->clearCache(array('types' => 'all', 'output' => true, 'refresh' => false)); } echo "Looking for update-scripts..."; $updates = Kwf_Util_Update_Helper::getUpdates(); foreach ($updates as $k => $u) { if (in_array($u->getUniqueName(), $doneNames) || $u->getLegacyName() && in_array($u->getLegacyName(), $doneNames)) { unset($updates[$k]); } } echo " found " . count($updates) . "\n\n"; } if (!$this->_getParam('debug')) { Kwf_Util_Maintenance::writeMaintenanceBootstrap(); } $c = new Zend_ProgressBar_Adapter_Console(); $c->setElements(array(Zend_ProgressBar_Adapter_Console::ELEMENT_PERCENT, Zend_ProgressBar_Adapter_Console::ELEMENT_BAR, Zend_ProgressBar_Adapter_Console::ELEMENT_TEXT)); $c->setTextWidth(50); $runner = new Kwf_Util_Update_Runner($updates); $progress = new Zend_ProgressBar($c, 0, $runner->getProgressSteps()); $runner->setProgressBar($progress); $runner->setVerbose(true); $runner->setEnableDebug($this->_getParam('debug')); $runner->setSkipClearCache($skipClearCache); if (!$runner->checkUpdatesSettings()) { echo "\ncheckSettings failed, update stopped\n"; } else { $executedUpdates = $runner->executeUpdates(); echo "\n[32mupdate finished[0m\n"; $doneNames = array_unique(array_merge($doneNames, $executedUpdates)); $runner->writeExecutedUpdates($doneNames); } if (!$this->_getParam('debug')) { Kwf_Util_Maintenance::restoreMaintenanceBootstrap(); } $errors = $runner->getErrors(); if ($errors) { echo "\n\n" . str_repeat('=', 16) . "\n"; echo count($errors) . " update script(s) failed:\n"; foreach ($errors as $error) { echo $error['name'] . ": \n"; echo $error['message'] . "\n\n"; } exit(1); } else { echo "\n" . count($updates) . " update script(s) successfully executed.\n"; exit(0); } }
public function getUpdates() { if ($this->_factoryConfig['type'] == 'ClassName') { $id = $this->_factoryConfig['id']; if (strpos($id, '_') === false) { $classPrefix = 'Update_' . $id; } else { $classPrefix = substr($id, 0, strrpos($id, '_')) . '_Update_' . substr($id, strrpos($id, '_') + 1); } return Kwf_Util_Update_Helper::getUpdatesForDir($classPrefix); } else { return array(); } }
public static function getExecutedUpdatesNames() { $db = Kwf_Registry::get('db'); $tables = $db->listTables(); $doneNames = false; if (in_array('kwf_updates', $tables)) { $q = $db->query("SELECT name FROM kwf_updates"); $doneNames = array(); foreach ($q->fetchAll() as $row) { $doneNames[] = $row['name']; } } else { if (in_array('kwf_update', $tables)) { //update pre 4.2 $q = $db->query("SELECT data FROM kwf_update"); $doneNames = $q->fetchColumn(); } } if (!$doneNames) { //fallback for older versions, update used to be a file if (file_exists('update')) { $doneNames = file_get_contents('update'); } else { throw new Kwf_Exception("Can't read kwf_update"); } } if (is_string($doneNames) && is_numeric(trim($doneNames))) { //UPDATE applicaton/update format $r = trim($doneNames); $doneNames = array(); foreach (Kwf_Util_Update_Helper::getUpdates() as $u) { if ($u->getLegacyRevision() && $u->getLegacyRevision() <= $r) { $doneNames[] = $u->getUniqueName(); } } } else { if (is_string($doneNames)) { $doneNames = unserialize($doneNames); if (isset($doneNames['start'])) { //UPDATE applicaton/update format if (!isset($doneNames['done'])) { $doneNames['done'] = array(); foreach (Kwf_Util_Update_Helper::getUpdates() as $u) { if ($u->getLegacyRevision() && $u->getLegacyRevision() <= $doneNames['start']) { $doneNames['done'][] = $u->getLegacyRevision(); } } } $doneNames = $doneNames['done']; } $doneNamesCpy = $doneNames; $doneNames = array(); foreach ($doneNamesCpy as $i) { if (is_numeric($i)) { //UPDATE applicaton/update format static $allUpdates; if (!isset($allUpdates)) { $allUpdates = array(); foreach (Kwf_Util_Update_Helper::getUpdates() as $u) { if ($u->getLegacyRevision()) { if (!isset($allUpdates[$u->getLegacyRevision()])) { $allUpdates[$u->getLegacyRevision()] = array(); } $allUpdates[$u->getLegacyRevision()][] = $u; } } } if (isset($allUpdates[$i])) { foreach ($allUpdates[$i] as $u) { $doneNames[] = $u->getUniqueName(); } } } else { $doneNames[] = $i; } } } } //convert old updates from pre 3.0 times (where kwf was called vps) foreach ($doneNames as &$i) { if (substr($i, 0, 4) == 'Vpc_' || substr($i, 0, 4) == 'Vps_') { $updateWithoutWebname = substr($i, strpos($i, '_', 4) + 1); if (class_exists($updateWithoutWebname)) { $i = $updateWithoutWebname; continue; } $updateSqlFile = str_replace('_', '/', $updateWithoutWebname) . '.sql'; foreach (explode(PATH_SEPARATOR, get_include_path()) as $ip) { if (file_exists($ip . '/' . $updateSqlFile)) { $i = $updateWithoutWebname; continue; } } $i = str_replace('Vps_Update_', 'Vkwf_Update_', $i); $i = str_replace('Vps_', 'Kwf_', $i); $i = str_replace('Vpc_', 'Kwc_', $i); } } if (!$doneNames) { //it's ok to have no updates throw new Kwf_ClientException("Invalid update revision"); } return $doneNames; }
public function indexAction() { if (Kwf_Setup::getBaseUrl() === null || !Kwf_Config::getValue('server.domain')) { throw new Kwf_Exception_Client("Before running setup please set server.domain and server.baseUrl in config.local.ini"); } if (file_exists('update')) { //for pre 3.3 webs, update file got replaced by kwf_update table throw new Kwf_Exception_Client("Application seems to be set up already. (update file exists)"); } try { Kwf_Registry::get('db'); } catch (Exception $e) { throw new Kwf_Exception_Client("Connection to database failed: " . $e->getMessage()); } if (!Kwf_Registry::get('db')) { throw new Kwf_Exception_Client("Database not configured: create a config.local.ini containing database connection."); } try { Kwf_Registry::get('db')->query("SELECT 1"); } catch (Exception $e) { throw new Kwf_Exception_Client("Connection to database failed: " . $e->getMessage()); } try { $tables = Kwf_Registry::get('db')->fetchCol("SHOW TABLES"); } catch (Exception $e) { throw new Kwf_Exception_Client("Fetching Tables failed: " . $e->getMessage()); } if (in_array('kwf_update', $tables)) { echo "Application seems to be set up already. (kwf_update table exists)\n"; echo "Executing update...\n"; $this->forward('index', 'update'); return; } if ($tables) { throw new Kwf_Exception_Client("Database not empty, incomplete kwf installation or other application already exists in this database."); } $updates = array(); if (file_exists('setup/initial/dump.sql')) { $updates[] = new Kwf_Update_Setup_InitialDb('setup/initial/dump.sql'); if (file_exists('setup/initial/uploads')) { $updates[] = new Kwf_Update_Setup_InitialUploads('setup/initial/uploads'); } } else { $updates = array_merge($updates, Kwf_Util_Update_Helper::getUpdates()); foreach (Kwf_Util_Update_Helper::getUpdateTags() as $tag) { $file = KWF_PATH . '/setup/' . $tag . '.sql'; if (file_exists($file)) { $update = new Kwf_Update_Sql(0, null); $update->sql = file_get_contents($file); $updates[] = $update; } } $updates[] = new Kwf_Update_Setup_InitialDb('setup/setup.sql'); $updates[] = new Kwf_Update_Setup_InitialUploads('setup/uploads'); } $c = new Zend_ProgressBar_Adapter_Console(); $c->setElements(array(Zend_ProgressBar_Adapter_Console::ELEMENT_PERCENT, Zend_ProgressBar_Adapter_Console::ELEMENT_BAR, Zend_ProgressBar_Adapter_Console::ELEMENT_TEXT)); $c->setTextWidth(50); $runner = new Kwf_Util_Update_Runner($updates); $progress = new Zend_ProgressBar($c, 0, $runner->getProgressSteps()); $runner->setProgressBar($progress); if (!$runner->checkUpdatesSettings()) { echo "\ncheckSettings failed, setup stopped\n"; exit; } $doneNames = $runner->executeUpdates(); $runner->writeExecutedUpdates($doneNames); $errors = $runner->getErrors(); if ($errors) { echo "\n\n================\n"; echo count($errors) . " setup script(s) failed:\n"; foreach ($errors as $error) { echo $error['name'] . ": \n"; echo $error['message'] . "\n\n"; } } else { echo "\n\nSetup finished.\nThank you for using Koala Framework.\n"; } exit; }
public function jsonInstallAction() { $cfg = "[production]\n"; $cfg .= "database.web.username = "******"\n"; $cfg .= "database.web.password = "******"\n"; $cfg .= "database.web.dbname = " . $this->_getParam('db_dbname') . "\n"; $cfg .= "database.web.host = " . $this->_getParam('db_host') . "\n"; $cfg .= "\n"; $cfg .= "server.domain = " . $this->getRequest()->getHttpHost() . "\n"; $cfg .= "server.baseUrl = \"" . $this->getRequest()->getBaseUrl() . "\"\n"; $cfg .= "\n"; $cfg .= "debug.error.log = " . (!$this->_getParam('display_errors') ? 'true' : 'false') . "\n"; file_put_contents('config.local.ini', $cfg); file_put_contents('config_section', $this->_getParam('config_section')); //re-create config to load changed config.local.ini Kwf_Config::deleteValueCache('database'); Kwf_Config_Web::reload(); Zend_Registry::getInstance()->offsetUnset('db'); Zend_Registry::getInstance()->offsetSet('dao', new Kwf_Dao()); $updates = array(); foreach (Kwf_Util_Update_Helper::getUpdateTags() as $tag) { $file = KWF_PATH . '/setup/' . $tag . '.sql'; if (file_exists($file)) { $update = new Kwf_Update_Sql(0, null); $update->sql = file_get_contents($file); $updates[] = $update; } } $updates = array_merge($updates, Kwf_Util_Update_Helper::getUpdates()); $updates[] = new Kwf_Update_Setup_InitialDb('setup/setup.sql'); $updates[] = new Kwf_Update_Setup_InitialUploads('setup/uploads'); $update = new Kwf_Update_Sql(0, null); $db = Kwf_Registry::get('db'); mt_srand((double) microtime() * 1000000); $passwordSalt = substr(md5(uniqid(mt_rand(), true)), 0, 10); $update->sql = "TRUNCATE TABLE kwf_users;\n"; $update->sql .= "INSERT INTO kwf_users SET\n role='admin',\n email=" . $db->quote($this->_getParam('admin_email')) . ",\n password="******",\n password_salt=" . $db->quote($passwordSalt) . ";\n"; $updates[] = $update; $c = new Kwf_Util_ProgressBar_Adapter_Cache($this->_getParam('progressNum')); $runner = new Kwf_Util_Update_Runner($updates); $progress = new Zend_ProgressBar($c, 0, $runner->getProgressSteps()); $runner->setProgressBar($progress); if (!$runner->checkUpdatesSettings()) { throw new Kwf_Exception_Client("checkSettings failed, setup stopped"); } $doneNames = $runner->executeUpdates(array('refreshCache' => false)); $runner->writeExecutedUpdates($doneNames); $errors = $runner->getErrors(); if ($errors) { $errMsg = count($errors) . " setup script(s) failed:\n"; foreach ($errors as $error) { $errMsg .= $error['name'] . ": \n"; $errMsg .= $error['message'] . "\n\n"; } throw new Kwf_Exception_Client(nl2br($errMsg)); } }
public function testFindKwcUpdates() { $updates = Kwf_Util_Update_Helper::getKwcUpdates(); $this->assertEquals(1, count($updates)); $this->assertTrue($updates[0] instanceof Kwf_Update_ComponentUpdate_TestComponent_Update_20150309Legacy00100); }