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 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 "\nupdate finished\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);
     }
 }