private function _getUpdate()
 {
     $update = new Kwf_Update_20150309Legacy39000('Kwc_Root_Category_Update_20150309Legacy00002');
     $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);
     $progress = new Zend_ProgressBar($c, 0, $update->countUploads());
     $update->setProgressBar($progress);
     return $update;
 }
 public function initialSync($processInCli = true)
 {
     $stepSize = 100;
     $format = self::_optimalImportExportFormat($this->getProxyModel(), $this->getSourceModel());
     $count = $this->_sourceModel->countRows();
     $progress = null;
     if (PHP_SAPI == 'cli' && $processInCli) {
         $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_ETA, Zend_ProgressBar_Adapter_Console::ELEMENT_TEXT));
         $progress = new Zend_ProgressBar($c, 0, $count);
     }
     $startTime = microtime(true);
     $this->getProxyModel()->deleteRows(array());
     //alles löschen
     for ($offset = 0; $offset < $count; $offset += $stepSize) {
         $s = new Kwf_Model_Select();
         $s->limit($stepSize, $offset);
         /*
         $data = $this->_sourceModel->export($format, $s);
         $this->getProxyModel()->import($format, $data);
         */
         //warning: slow code ahead
         foreach ($this->_sourceModel->getRows($s) as $row) {
             $data = $row->toArray();
             $newRow = $this->createRow($data);
             foreach ($this->getDependentModels() as $rule => $depModel) {
                 if ($depModel instanceof Kwf_Model_RowsSubModel_MirrorCacheSimple) {
                     //dieser code könne vielleicht im Kwf_Model_RowsSubModel_MirrorCacheSimple liegen
                     $m = $depModel->getSourceModel();
                     $ref = $m->getReferenceByModelClass(get_class($this), null);
                     $select = new Kwf_Model_Select();
                     $select->whereEquals($ref['column'], $row->{$this->getPrimaryKey()});
                     $childRows = $m->getRows($select);
                     foreach ($childRows as $childRow) {
                         $newCRow = $newRow->createChildRow($rule, $childRow->toArray());
                     }
                 }
             }
             $newRow->save();
         }
         unset($row);
         unset($newRow);
         unset($newCRow);
         unset($childRows);
         unset($childRow);
         foreach (self::getInstances() as $m) {
             $m->clearRows();
         }
         //echo round(memory_get_usage()/(1024*1024), 3)."MB\n";
         if ($progress) {
             $text = round(($offset + $stepSize) / (microtime(true) - $startTime)) . ' rows/sec';
             $progress->next($stepSize, $text);
         }
     }
 }
 public function calculateDimensionsAction()
 {
     $update = new Kwf_Update_20151012UploadsDimensions('Kwf_Update_20151012UploadsDimensions');
     $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);
     $progress = new Zend_ProgressBar($c, 0, $update->countUploads());
     $update->setProgressBar($progress);
     $update->calculateDimensions();
     exit;
 }
 private function _getProgressBar()
 {
     if (!$this->_expectedTimes) {
         return null;
     }
     if (!isset($this->_progressBar)) {
         $adapter = new Zend_ProgressBar_Adapter_Console();
         $adapter->setElements(array(Zend_ProgressBar_Adapter_Console::ELEMENT_PERCENT, Zend_ProgressBar_Adapter_Console::ELEMENT_BAR, Zend_ProgressBar_Adapter_Console::ELEMENT_TEXT));
         $adapter->setTextWidth(30);
         $this->_progressBar = new Zend_ProgressBar($adapter, 0, array_sum($this->_expectedTimes));
         $this->_currentProgress = 0;
         $this->_currentTest = 0;
     }
     return $this->_progressBar;
 }
 public function testSetInvalidFinishAction()
 {
     try {
         $adapter = new Zend_ProgressBar_Adapter_Console();
         $adapter->setFinishAction('CUSTOM_FINISH_ACTION');
         $this->fail('Expected Zend_ProgressBar_Adapter_Exception');
     } catch (Zend_ProgressBar_Adapter_Exception $e) {
         $this->assertEquals($e->getMessage(), 'Invalid finish action specified');
     }
 }
 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);
     }
 }
 public function testAction()
 {
     $source = Kwf_Component_Data_Root::getInstance()->getComponentByDbId($this->_getParam('source'), array('ignoreVisible' => true));
     if (!$source) {
         throw new Kwf_Exception_Client("source not found");
     }
     $parentTarget = Kwf_Component_Data_Root::getInstance()->getComponentByDbId($this->_getParam('target'), array('ignoreVisible' => true));
     if (!$parentTarget) {
         throw new Kwf_Exception_Client("target not found");
     }
     Kwf_Events_ModelObserver::getInstance()->disable();
     //This would be slow as hell. But luckily we can be sure that for the new (duplicated) components there will be no view cache to clear.
     echo "counting pages...";
     $steps = Kwf_Util_Component::getDuplicateProgressSteps($source);
     echo " " . $steps . "\n";
     $ad = new Zend_ProgressBar_Adapter_Console();
     $ad->setElements(array(Zend_ProgressBar_Adapter_Console::ELEMENT_BAR, Zend_ProgressBar_Adapter_Console::ELEMENT_TEXT, Zend_ProgressBar_Adapter_Console::ELEMENT_ETA));
     $progressBar = new Zend_ProgressBar($ad, 0, $steps);
     $target = Kwf_Util_Component::duplicate($source, $parentTarget, $progressBar);
     Kwf_Util_Component::afterDuplicate($source, $target);
     $progressBar->finish();
     exit;
 }
Example #8
0
 protected function _build($options)
 {
     if (!file_exists('build/assets')) {
         mkdir('build/assets');
     }
     Kwf_Assets_BuildCache::getInstance()->building = true;
     Kwf_Assets_BuildCache::getInstance()->clean();
     Kwf_Assets_BuildCache::getInstance()->save(time(), 'assetsVersion');
     $langs = $this->_getAllLanguages();
     $packages = $this->_getAllPackages();
     $exts = array('js', 'defer.js', 'css', 'printcss');
     echo "\ncalculating dependencies...\n";
     $steps = count($packages) * count($exts);
     $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);
     $progress = new Zend_ProgressBar($c, 0, $steps);
     $countDependencies = 0;
     foreach ($packages as $p) {
         $depName = $p->getDependencyName();
         foreach ($exts as $extension) {
             $progress->next(1, "{$depName} {$extension}");
             $countDependencies += count($p->getFilteredUniqueDependencies(self::$_mimeTypeByExtension[$extension]));
             $cacheId = $p->getMaxMTimeCacheId(self::$_mimeTypeByExtension[$extension]);
             if (!$cacheId) {
                 throw new Kwf_Exception("Didn't get cacheId for " . get_class($p));
             }
             $maxMTime = $p->getMaxMTime(self::$_mimeTypeByExtension[$extension]);
             Kwf_Assets_BuildCache::getInstance()->save($maxMTime, $cacheId);
             //save generated caches for clear-cache-watcher
             $fileName = 'build/assets/package-max-mtime-' . $extension;
             if (!file_exists($fileName) || strpos(file_get_contents($fileName), $cacheId . "\n") === false) {
                 file_put_contents($fileName, $cacheId . "\n", FILE_APPEND);
             }
         }
     }
     $progress->finish();
     echo "compiling assets...\n";
     $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);
     $progress = new Zend_ProgressBar($c, 0, $countDependencies);
     foreach ($packages as $p) {
         foreach ($exts as $extension) {
             foreach ($p->getFilteredUniqueDependencies(self::$_mimeTypeByExtension[$extension]) as $dep) {
                 $progress->next(1, "{$dep}");
                 $dep->warmupCaches();
             }
         }
     }
     $progress->finish();
     echo "generating packages...\n";
     $steps = count($packages) * count($langs) * count($exts) * 4;
     $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);
     $progress = new Zend_ProgressBar($c, 0, $steps);
     foreach ($packages as $p) {
         $depName = $p->getDependencyName();
         foreach ($langs as $language) {
             foreach ($exts as $extension) {
                 $packageContents = $p->getPackageContents(self::$_mimeTypeByExtension[$extension], $language);
                 $maxMTime = $p->getMaxMTime(self::$_mimeTypeByExtension[$extension]);
                 $progress->next(1, "{$depName} {$extension} {$language}");
                 $progress->next(1, "{$depName} {$extension} {$language} source");
                 $this->_buildPackageContents($packageContents, $maxMTime, $p, $extension, $language);
                 $progress->next(1, "{$depName} {$extension} {$language} map");
                 $this->_buildPackageSourceMap($packageContents, $maxMTime, $p, $extension, $language);
                 $progress->next(1, "{$depName} {$extension} {$language} url");
                 $urls = $p->getPackageUrls(self::$_mimeTypeByExtension[$extension], $language);
                 if (Kwf_Setup::getBaseUrl()) {
                     foreach ($urls as $k => $i) {
                         $urls[$k] = substr($i, strlen(Kwf_Setup::getBaseUrl()));
                     }
                 }
                 $cacheId = $p->getPackageUrlsCacheId(self::$_mimeTypeByExtension[$extension], $language);
                 Kwf_Assets_BuildCache::getInstance()->save($urls, $cacheId);
             }
         }
     }
     Kwf_Assets_Cache::getInstance()->clean();
     Kwf_Assets_BuildCache::getInstance()->building = false;
     $exts = array('js', 'defer.js', 'css');
     foreach ($packages as $p) {
         $depName = $p->getDependencyName();
         $language = $langs[0];
         foreach ($exts as $extension) {
             $cacheId = Kwf_Assets_Dispatcher::getCacheIdByPackage($p, $extension, $language);
             $cacheContents = Kwf_Assets_BuildCache::getInstance()->load($cacheId);
             echo "{$depName} ";
             $h = new Kwf_View_Helper_FileSize();
             echo "{$extension} size: " . $h->fileSize(strlen(gzencode($cacheContents['contents'], 9, FORCE_GZIP)));
             echo "\n";
         }
     }
     $d = Kwf_Assets_Package_Default::getDefaultProviderList()->findDependency('Frontend');
     foreach ($d->getFilteredUniqueDependencies('text/javascript') as $i) {
         if ($i instanceof Kwf_Assets_Dependency_File && $i->getType() == 'ext2') {
             echo "\n[WARNING] Frontend text/javascript contains ext2\n";
             echo "To improve frontend performance all ext2 dependencies should be moved to defer\n\n";
             break;
         }
     }
 }
Example #9
0
 protected function _build($options)
 {
     if (!file_exists('build/assets')) {
         mkdir('build/assets');
     }
     Kwf_Assets_BuildCache::getInstance()->building = true;
     Kwf_Assets_BuildCache::getInstance()->clean();
     Kwf_Assets_BuildCache::getInstance()->save(time(), 'assetsVersion');
     $langs = $this->getAllLanguages();
     $packages = $this->getAllPackages();
     $exts = array('js', 'defer.js', 'css');
     echo "\ncalculating dependencies...\n";
     $steps = count($packages) * count($exts);
     $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);
     $progress = new Zend_ProgressBar($c, 0, $steps);
     $countDependencies = 0;
     foreach ($packages as $p) {
         $depName = $p->getDependencyName();
         foreach ($exts as $extension) {
             $progress->next(1, "{$depName} {$extension}");
             $p->getFilteredUniqueDependencies(self::$_mimeTypeByExtension[$extension]);
             $cacheId = $p->getMaxMTimeCacheId(self::$_mimeTypeByExtension[$extension]);
             if (!$cacheId) {
                 throw new Kwf_Exception("Didn't get cacheId for " . get_class($p));
             }
             $maxMTime = $p->getMaxMTime(self::$_mimeTypeByExtension[$extension]);
             Kwf_Assets_BuildCache::getInstance()->save($maxMTime, $cacheId);
             //save generated caches for clear-cache-watcher
             $fileName = 'build/assets/package-max-mtime-' . $extension;
             if (!file_exists($fileName) || strpos(file_get_contents($fileName), $cacheId . "\n") === false) {
                 file_put_contents($fileName, $cacheId . "\n", FILE_APPEND);
             }
         }
         $it = new RecursiveIteratorIterator(new Kwf_Assets_Dependency_Iterator_UniqueFilter(new Kwf_Assets_Dependency_Iterator_Recursive($p->getDependency(), Kwf_Assets_Dependency_Abstract::DEPENDENCY_TYPE_ALL)), RecursiveIteratorIterator::CHILD_FIRST);
         foreach ($it as $i) {
             $countDependencies++;
         }
     }
     $progress->finish();
     echo "compiling assets...\n";
     $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);
     $progress = new Zend_ProgressBar($c, 0, $countDependencies);
     foreach ($packages as $p) {
         $it = new RecursiveIteratorIterator(new Kwf_Assets_Dependency_Iterator_UniqueFilter(new Kwf_Assets_Dependency_Iterator_Recursive($p->getDependency(), Kwf_Assets_Dependency_Abstract::DEPENDENCY_TYPE_ALL)), RecursiveIteratorIterator::CHILD_FIRST);
         foreach ($it as $dep) {
             $progress->next(1, "{$dep}");
             $dep->warmupCaches();
         }
     }
     $progress->finish();
     echo "generating packages...\n";
     $steps = count($packages) * count($langs) * count($exts) * 4;
     $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);
     $progress = new Zend_ProgressBar($c, 0, $steps);
     foreach ($packages as $p) {
         $depName = $p->getDependencyName();
         foreach ($langs as $language) {
             foreach ($exts as $extension) {
                 $progress->next(1, "{$depName} {$extension} {$language}");
                 $packageContents = $p->getPackageContents(self::$_mimeTypeByExtension[$extension], $language);
                 $maxMTime = $p->getMaxMTime(self::$_mimeTypeByExtension[$extension]);
                 $progress->next(1, "{$depName} {$extension} {$language} source");
                 $this->_buildPackageContents($packageContents, $maxMTime, $p, $extension, $language);
                 $progress->next(1, "{$depName} {$extension} {$language} map");
                 $this->_buildPackageSourceMap($packageContents, $maxMTime, $p, $extension, $language);
                 $progress->next(1, "{$depName} {$extension} {$language} url");
                 $urls = $p->getPackageUrls(self::$_mimeTypeByExtension[$extension], $language);
                 if (Kwf_Setup::getBaseUrl()) {
                     foreach ($urls as $k => $i) {
                         $urls[$k] = substr($i, strlen(Kwf_Setup::getBaseUrl()));
                     }
                 }
                 $cacheId = $p->getPackageUrlsCacheId(self::$_mimeTypeByExtension[$extension], $language);
                 Kwf_Assets_BuildCache::getInstance()->save($urls, $cacheId);
             }
         }
     }
     Kwf_Assets_Cache::getInstance()->clean();
     Kwf_Assets_BuildCache::getInstance()->building = false;
 }
 public function indexAction()
 {
     Kwf_Util_MemoryLimit::set(512);
     $select = new Kwf_Model_Select();
     if ($this->_getParam('all')) {
     }
     if ($this->_getParam('dbId')) {
         $select->where(new Kwf_Model_Select_Expr_Like('db_id', $this->_getParam('dbId')));
     }
     if ($this->_getParam('id')) {
         $select->where(new Kwf_Model_Select_Expr_Like('component_id', $this->_getParam('id')));
     }
     if ($this->_getParam('expandedId')) {
         $select->where(new Kwf_Model_Select_Expr_Like('expanded_component_id', $this->_getParam('expandedId')));
     }
     if ($this->_getParam('type')) {
         $select->where(new Kwf_Model_Select_Expr_Like('type', $this->_getParam('type')));
     }
     if ($this->_getParam('class')) {
         $c = $this->_getParam('class');
         if (strpos($c, '%') === false) {
             $whereClass = array($c);
             foreach (Kwc_Abstract::getComponentClasses() as $cls) {
                 if (in_array($c, Kwc_Abstract::getSetting($cls, 'parentClasses'))) {
                     $whereClass[] = $cls;
                 }
             }
             $select->whereEquals('component_class', $whereClass);
         } else {
             $select->where(new Kwf_Model_Select_Expr_Like('component_class', $this->_getParam('class')));
         }
     }
     if (!$this->_getParam('all') && !$this->_getParam('dbId') && !$this->_getParam('id') && !$this->_getParam('expandedId') && !$this->_getParam('type') && !$this->_getParam('class')) {
         throw new Kwf_Exception_Client("required parameter: --all, --id, --dbId, --expandedId, --type or --class");
     }
     $select->whereEquals('deleted', false);
     $model = Kwf_Component_Cache::getInstance()->getModel();
     $entries = $model->countRows($select);
     if (!$entries) {
         echo "No active view cache entries found; nothing to do.\n";
         exit;
     }
     if (!$this->_getParam('force')) {
         echo "Will delete {$entries} view cache entries. Continue? [Y/n]\n";
         $stdin = fopen('php://stdin', 'r');
         $input = trim(strtolower(fgets($stdin, 2)));
         fclose($stdin);
         if (!($input == '' || $input == 'j' || $input == 'y')) {
             exit(1);
         }
     }
     echo "Deleting view cache...\n";
     $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);
     Kwf_Component_Cache::getInstance()->deleteViewCache($select, $c);
     echo "done\n";
     if ($this->_getParam('clear')) {
         echo "Clearing table...";
         $model->deleteRows($select);
         echo "done\n";
     }
     exit;
 }
 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;
 }
 protected function _build($options)
 {
     if (!file_exists('build/assets')) {
         mkdir('build/assets');
     }
     Kwf_Assets_BuildCache::getInstance()->building = true;
     Kwf_Assets_BuildCache::getInstance()->clean();
     Kwf_Assets_BuildCache::getInstance()->save(time(), 'assetsVersion');
     $langs = $this->getAllLanguages();
     $packages = $this->getAllPackages();
     $exts = array('js', 'defer.js', 'css', 'ie8.css', 'ie8.js');
     $providers = array();
     foreach ($packages as $p) {
         foreach ($p->getProviderList()->getProviders() as $provider) {
             if (!in_array($provider, $providers)) {
                 $providers[] = $provider;
             }
         }
     }
     echo "\ninitializing providers...\n";
     $steps = count($providers);
     $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(80);
     $progress = new Zend_ProgressBar($c, 0, $steps);
     foreach ($providers as $provider) {
         $progress->next(1, get_class($provider));
         $provider->initialize();
     }
     $progress->finish();
     echo "calculating dependencies...\n";
     $steps = count($packages) * count($exts);
     $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(80);
     $progress = new Zend_ProgressBar($c, 0, $steps);
     $countDependencies = 0;
     foreach ($packages as $p) {
         $depName = $p->getDependencyName();
         foreach ($exts as $extension) {
             $progress->next(1, "{$depName} {$extension}");
             $p->getFilteredUniqueDependencies(self::$_mimeTypeByExtension[$extension]);
         }
         $it = new RecursiveIteratorIterator(new Kwf_Assets_Dependency_Iterator_UniqueFilter(new Kwf_Assets_Dependency_Iterator_Recursive($p->getDependency(), Kwf_Assets_Dependency_Abstract::DEPENDENCY_TYPE_ALL)), RecursiveIteratorIterator::CHILD_FIRST);
         foreach ($it as $i) {
             $countDependencies++;
         }
     }
     $progress->finish();
     echo "compiling assets...\n";
     $c = new Zend_ProgressBar_Adapter_Console();
     $c->setElements(array(Zend_ProgressBar_Adapter_Console::ELEMENT_ETA, Zend_ProgressBar_Adapter_Console::ELEMENT_BAR, Zend_ProgressBar_Adapter_Console::ELEMENT_TEXT));
     $c->setTextWidth(80);
     $progress = new Zend_ProgressBar($c, 0, $countDependencies);
     foreach ($packages as $p) {
         $it = new RecursiveIteratorIterator(new Kwf_Assets_Dependency_Iterator_UniqueFilter(new Kwf_Assets_Dependency_Iterator_Recursive($p->getDependency(), Kwf_Assets_Dependency_Abstract::DEPENDENCY_TYPE_ALL)), RecursiveIteratorIterator::CHILD_FIRST);
         foreach ($it as $dep) {
             $progress->next(1, "{$dep}");
             if ($dep->getMimeType()) {
                 $mimeType = $dep->getMimeType();
                 $p->warmupDependencyCaches($dep, $mimeType, $progress);
                 if ($mimeType == 'text/css') {
                     $p->warmupDependencyCaches($dep, 'text/css; ie8', $progress);
                 }
             }
         }
     }
     $progress->finish();
     echo "generating package urls...\n";
     $steps = count($packages) * count($langs) * count($exts);
     $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(80);
     $progress = new Zend_ProgressBar($c, 0, $steps);
     $allUrls = array();
     foreach ($packages as $p) {
         $depName = $p->getDependencyName();
         foreach ($langs as $language) {
             foreach ($exts as $extension) {
                 $progress->next(1, "{$depName} {$extension} {$language}");
                 $urls = $p->getPackageUrls(self::$_mimeTypeByExtension[$extension], $language);
                 if (Kwf_Setup::getBaseUrl()) {
                     foreach ($urls as $k => $i) {
                         $urls[$k] = substr($i, strlen(Kwf_Setup::getBaseUrl()));
                     }
                 }
                 $cacheId = $p->getPackageUrlsCacheId(self::$_mimeTypeByExtension[$extension], $language);
                 Kwf_Assets_BuildCache::getInstance()->save($urls, $cacheId);
                 foreach ($urls as $url) {
                     if (!in_array($url, $allUrls)) {
                         $allUrls[] = $url;
                     }
                 }
             }
         }
     }
     echo "generating package contents...\n";
     $steps = count($allUrls) * 2;
     $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(80);
     $progress = new Zend_ProgressBar($c, 0, $steps);
     foreach ($allUrls as $url) {
         if (substr($url, 0, 8) != '/assets/') {
             continue;
         }
         $param = explode('/', $url);
         if (count($param) != 7) {
             continue;
         }
         $dependencyClass = $param[3];
         $dependencyParams = $param[4];
         $urlLanguage = $param[5];
         $urlExtension = $param[6];
         $urlExtension = substr($urlExtension, 0, strpos($urlExtension, '?'));
         if (!class_exists($dependencyClass)) {
             throw new Kwf_Exception();
         }
         if (!is_instance_of($dependencyClass, 'Kwf_Assets_Interface_UrlResolvable')) {
             throw new Kwf_Exception();
         }
         $p = call_user_func(array($dependencyClass, 'fromUrlParameter'), $dependencyClass, $dependencyParams);
         if (!$p instanceof Kwf_Assets_Package) {
             throw new Kwf_Exception();
         }
         $contents = $p->getUrlContents($urlExtension, $urlLanguage);
         $progress->next(1, "{$p->getDependencyName()} {$urlExtension} {$urlLanguage} source");
         $cacheId = Kwf_Assets_Dispatcher::getInstance()->getCacheIdByPackage($p, $urlExtension, $urlLanguage);
         Kwf_Assets_BuildCache::getInstance()->save($contents, $cacheId);
         //save generated caches for clear-cache-watcher
         $fileName = 'build/assets/output-cache-ids-' . $urlExtension;
         if (!file_exists($fileName) || strpos(file_get_contents($fileName), $cacheId . "\n") === false) {
             file_put_contents($fileName, $cacheId . "\n", FILE_APPEND);
         }
         $contents = $p->getUrlContents($urlExtension . '.map', $urlLanguage);
         $progress->next(1, "{$p->getDependencyName()} {$urlExtension} {$urlLanguage} map");
         $cacheId = Kwf_Assets_Dispatcher::getInstance()->getCacheIdByPackage($p, $urlExtension . '.map', $urlLanguage);
         Kwf_Assets_BuildCache::getInstance()->save($contents, $cacheId);
     }
     Kwf_Assets_Cache::getInstance()->clean();
     Kwf_Assets_BuildCache::getInstance()->building = false;
 }