public function testConvertToMegabyte()
 {
     $this->assertEquals(100, Kwf_Util_MemoryLimit::convertToMegabyte(100));
     $this->assertEquals(100, Kwf_Util_MemoryLimit::convertToMegabyte('100M'));
     $this->assertEquals(100, Kwf_Util_MemoryLimit::convertToMegabyte('102400K'));
     $this->assertEquals(2048, Kwf_Util_MemoryLimit::convertToMegabyte('2G'));
 }
 public function indexAction()
 {
     Kwf_Util_MemoryLimit::set(1024);
     set_time_limit(0);
     $source = Kwf_Component_Data_Root::getInstance()->getComponentByDbId($this->_getParam('source'), array('ignoreVisible' => true));
     if (!$source) {
         throw new Kwf_Exception_Client("source not found");
     }
     $target = Kwf_Component_Data_Root::getInstance()->getComponentByDbId($this->_getParam('target'), array('ignoreVisible' => true));
     if (!$target) {
         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.
     Kwf_Registry::get('db')->beginTransaction();
     echo "counting pages...";
     $steps = Kwc_Admin::getInstance($source->componentClass)->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);
     Kwc_Admin::getInstance($source->componentClass)->duplicate($source, $target, $progressBar);
     Kwf_Util_Component::afterDuplicate($source, $target);
     $progressBar->finish();
     Kwf_Registry::get('db')->commit();
     exit;
 }
Ejemplo n.º 3
0
 public static function setMaxLimit($limit)
 {
     if (!is_int($limit)) {
         throw new Kwf_Exception('Limit must be an integer');
     }
     self::$_maxLimit = $limit;
 }
 public static function initForTests()
 {
     Kwf_Session::start();
     Kwf_Util_MemoryLimit::set(512);
     Kwf_Component_Data_Root::setComponentClass(false);
     Zend_Registry::set('db', Kwf_Test::getTestDb());
     set_time_limit(0);
     Kwf_Benchmark::disable();
 }
Ejemplo n.º 5
0
 /**
  * @param array possible options: types(=all), output(=false), excludeTypes
  */
 public final function build(array $options)
 {
     $typeNames = $options['types'];
     $output = isset($options['output']) ? $options['output'] : false;
     $excludeTypes = isset($options['excludeTypes']) ? $options['excludeTypes'] : array();
     Kwf_Cache_SimpleStatic::disableFileCache();
     Kwf_Events_ModelObserver::getInstance()->disable();
     Kwf_Util_MemoryLimit::set(1024 * 2);
     Kwf_Registry::set('db', false);
     if ($typeNames == 'all') {
         $types = $this->getTypes();
     } else {
         if (!is_array($typeNames)) {
             $typeNames = explode(',', $typeNames);
         }
         $types = array();
         foreach ($this->getTypes() as $t) {
             if (in_array($t->getTypeName(), $typeNames)) {
                 $types[] = $t;
             }
         }
     }
     if (is_string($excludeTypes)) {
         $excludeTypes = explode(',', $excludeTypes);
     }
     foreach ($types as $k => $i) {
         if (in_array($i->getTypeName(), $excludeTypes)) {
             unset($types[$k]);
         }
     }
     $maxTypeNameLength = 0;
     $countSteps = 0;
     foreach ($types as $type) {
         $type->setVerbosity($output ? Kwf_Util_Build_Types_Abstract::VERBOSE : Kwf_Util_Build_Types_Abstract::SILENT);
         $maxTypeNameLength = max($maxTypeNameLength, strlen($type->getTypeName()));
         $countSteps++;
     }
     $progress = null;
     if (isset($options['progressAdapter'])) {
         $progress = new Zend_ProgressBar($options['progressAdapter'], 0, $countSteps);
     }
     if (!file_exists('build')) {
         mkdir('build');
     }
     $currentStep = 0;
     foreach ($types as $type) {
         $currentStep++;
         if ($progress) {
             $progress->next(1, "building " . $type->getTypeName());
         }
         if ($output) {
             echo "[" . str_repeat(' ', 2 - strlen($currentStep)) . "{$currentStep}/{$countSteps}] ";
             echo "building " . $type->getTypeName() . "..." . str_repeat('.', $maxTypeNameLength - strlen($type->getTypeName())) . " ";
         }
         $t = microtime(true);
         $type->build($options);
         if ($output) {
             if ($type->getSuccess()) {
                 echo "OK";
             } else {
                 echo " [ERROR]";
                 return false;
             }
             echo " (" . round((microtime(true) - $t) * 1000) . "ms)";
             echo "\n";
         }
     }
     Kwf_Events_ModelObserver::getInstance()->enable();
     return $types;
 }
Ejemplo n.º 6
0
 /**
  * @param array possible options: types(=all), output(=false), refresh(=true), excludeTypes, skipMaintenanceBootstrap, skipOtherServers
  */
 public final function clearCache(array $options)
 {
     $typeNames = $options['types'];
     $output = isset($options['output']) ? $options['output'] : false;
     $refresh = isset($options['refresh']) ? $options['refresh'] : false;
     $excludeTypes = isset($options['excludeTypes']) ? $options['excludeTypes'] : array();
     Kwf_Events_ModelObserver::getInstance()->disable();
     Kwf_Util_MemoryLimit::set(512);
     if (!isset($options['skipMaintenanceBootstrap']) || !$options['skipMaintenanceBootstrap']) {
         Kwf_Util_Maintenance::writeMaintenanceBootstrap($output);
     }
     if ($typeNames == 'all') {
         $types = $this->getTypes();
     } else {
         if (!is_array($typeNames)) {
             $typeNames = explode(',', $typeNames);
         }
         $types = array();
         foreach ($this->getTypes() as $t) {
             if (in_array($t->getTypeName(), $typeNames)) {
                 $types[] = $t;
             }
         }
     }
     if (is_string($excludeTypes)) {
         $excludeTypes = explode(',', $excludeTypes);
     }
     foreach ($types as $k => $i) {
         if (in_array($i->getTypeName(), $excludeTypes)) {
             unset($types[$k]);
         }
     }
     $maxTypeNameLength = 0;
     $countSteps = 0;
     foreach ($types as $type) {
         $type->setVerbosity($output ? Kwf_Util_ClearCache_Types_Abstract::VERBOSE : Kwf_Util_ClearCache_Types_Abstract::SILENT);
         $maxTypeNameLength = max($maxTypeNameLength, strlen($type->getTypeName()));
         if ($type->doesClear()) {
             $countSteps++;
         }
         if ($type->doesRefresh()) {
             $countSteps++;
         }
     }
     $progress = null;
     if (isset($options['progressAdapter'])) {
         $progress = new Zend_ProgressBar($options['progressAdapter'], 0, $countSteps);
     }
     $currentStep = 0;
     foreach ($types as $type) {
         if ($type->doesClear()) {
             $currentStep++;
             if ($progress) {
                 $progress->next(1, "clearing " . $type->getTypeName());
             }
             if ($output) {
                 echo "[" . str_repeat(' ', 2 - strlen($currentStep)) . "{$currentStep}/{$countSteps}] ";
                 echo "clearing " . $type->getTypeName() . "..." . str_repeat('.', $maxTypeNameLength - strlen($type->getTypeName())) . " ";
             }
             $t = microtime(true);
             $type->clearCache($options);
             if ($output) {
                 if ($type->getSuccess()) {
                     echo "OK";
                 } else {
                     echo " [ERROR]";
                 }
                 echo " (" . round((microtime(true) - $t) * 1000) . "ms)";
                 echo "\n";
             }
         }
     }
     if ($refresh) {
         foreach ($types as $type) {
             if ($type->doesRefresh()) {
                 $currentStep++;
                 if ($progress) {
                     $progress->next(1, "refreshing " . $type->getTypeName());
                 }
                 if ($output) {
                     echo "[{$currentStep}/{$countSteps}] refreshing " . $type->getTypeName() . "." . str_repeat('.', $maxTypeNameLength - strlen($type->getTypeName())) . " ";
                 }
                 $t = microtime(true);
                 $mem = memory_get_usage();
                 $type->refreshCache($options);
                 if ($output) {
                     if ($type->getSuccess()) {
                         echo "OK";
                     } else {
                         echo " [ERROR]";
                     }
                     echo " (" . round((microtime(true) - $t) * 1000) . "ms";
                     if (memory_get_usage() - $mem > 1024 * 1024) {
                         echo ", " . round((memory_get_usage() - $mem) / (1024 * 1024), 2) . "MB";
                     }
                     echo ")\n";
                 }
             }
         }
     }
     /*
             TODO re-enable this somehow
               * required at all?
               * own type? or should the different types each also clear the other servers (apc does that already)
     
             $skipOtherServers = isset($options['skipOtherServers']) ? $options['skipOtherServers'] : false;
             if (Kwf_Config::getValue('server.aws') && !$skipOtherServers) {
                 $otherHostsTypes = $this->_getCacheDirs();
                 //add other types
                 $otherHostsTypes[] = 'config';
                 $otherHostsTypes[] = 'setup';
                 $otherHostsTypes[] = 'component';
                 $otherHostsTypes[] = 'events';
                 $otherHostsTypes[] = 'trl';
                 $otherHostsTypes = array_unique($otherHostsTypes);
                 if (in_array('all', $types)) {
                     //use all of $otherHostsTypes
                 } else {
                     $otherHostsTypes = array_intersect($otherHostsTypes, $types);
                 }
                 if ($otherHostsTypes) {
                     $domains = Kwf_Util_Aws_Ec2_InstanceDnsNames::getOther();
                     foreach ($domains as $domain) {
                         if ($output) {
                             echo "executing clear-cache on $domain:\n";
                         }
                         $cmd = Kwf_Config::getValue('server.phpCli')." bootstrap.php clear-cache --type=".implode(',', $otherHostsTypes).' --skip-other-servers';
                         $cmd = "ssh -o 'StrictHostKeyChecking no' $domain ".escapeshellarg('cd '.Kwf_Config::getValue('server.dir').'; '.$cmd);
                         passthru($cmd);
                         if ($output) {
                             echo "\n";
                         }
                     }
                 }
             }
     */
     if (!isset($options['skipMaintenanceBootstrap']) || !$options['skipMaintenanceBootstrap']) {
         Kwf_Util_Maintenance::restoreMaintenanceBootstrap($output);
     }
     Kwf_Events_ModelObserver::getInstance()->enable();
     return $types;
 }
Ejemplo n.º 7
0
 public static function iterate()
 {
     header('Content-Type: text/plain; charset=utf-8');
     $requiredSpazi = 1024 * 1024 * 128;
     //128MB
     //$requiredSpazi = 1024*1024*300;
     $mem = apc_sma_info(true);
     $memSize = $mem['num_seg'] * $mem['seg_size'];
     $memAvailable = $mem['avail_mem'];
     if ($memAvailable > $requiredSpazi) {
         die;
     }
     $start = microtime(true);
     Kwf_Util_MemoryLimit::set(256);
     ini_set('display_errors', 'on');
     set_time_limit(90);
     echo "size: " . round($memSize / (1024 * 1024)) . " MB\n";
     echo "avail: " . round($memAvailable / (1024 * 1024)) . " MB\n";
     echo "\nduration: " . round(microtime(true) - $start, 3) . " s\n";
     $accessTime = array();
     $it = new APCIterator('user', '#^[^-]+\\-[^-]+\\-(\\-cc|procI|url)-#', APC_ITER_KEY | APC_ITER_MEM_SIZE | APC_ITER_ATIME);
     echo "total count: " . $it->getTotalCount() . "\n";
     echo "total hits: " . $it->getTotalHits() . "\n";
     echo "total size: " . round($it->getTotalSize() / (1024 * 1024)) . " MB\n";
     echo "\nduration: " . round(microtime(true) - $start, 3) . " s\n";
     $webs = array();
     foreach ($it as $i) {
         preg_match('#^([^-]+)\\-([^-]+)\\-+([a-zA-Z0-9]+)#', $i['key'], $m);
         $key = $m[1] . '-' . $m[2];
         if (!isset($webs[$key])) {
             $webs[$key] = 0;
         }
         $webs[$key] += $i['mem_size'];
         $accessTime[] = time() - $i['access_time'];
     }
     echo "min lastAccess: " . min($accessTime) . "\n";
     echo "max lastAccess: " . max($accessTime) . "\n";
     echo "avg lastAccess: " . round(array_sum($accessTime) / count($accessTime)) . "\n";
     echo "\nduration: " . round(microtime(true) - $start, 3) . " s\n";
     unset($accessTime);
     echo "\n";
     arsort($webs);
     foreach ($webs as $web => $items) {
         echo "{$web}: " . round($items / $it->getTotalSize() * 100) . "%\n";
     }
     reset($webs);
     $web = key($webs);
     unset($it);
     unset($webs);
     echo "\nduration: " . round(microtime(true) - $start, 3) . " s\n";
     $it = new APCIterator('user', '#^' . preg_quote($web) . '\\-(\\-cc|procI|url)-#', APC_ITER_KEY | APC_ITER_MEM_SIZE | APC_ITER_ATIME);
     echo "\n{$web}\n";
     echo "total count: " . $it->getTotalCount() . "\n";
     echo "total hits: " . $it->getTotalHits() . "\n";
     echo "total size: " . round($it->getTotalSize() / (1024 * 1024)) . " MB\n";
     echo "\nduration: " . round(microtime(true) - $start, 3) . " s\n";
     $accessTime = array();
     $size = array();
     foreach ($it as $i) {
         $t = time() - $i['access_time'];
         $accessTime[$i['key']] = $t;
         $size[$i['key']] = $i['mem_size'];
     }
     unset($it);
     echo "min lastAccess: " . min($accessTime) . "\n";
     echo "max lastAccess: " . max($accessTime) . "\n";
     echo "avg lastAccess: " . round(array_sum($accessTime) / count($accessTime)) . "\n";
     echo "\nduration: " . round(microtime(true) - $start, 3) . " s\n";
     flush();
     arsort($accessTime);
     $deletedBytes = 0;
     $deletedCount = 0;
     foreach ($accessTime as $key => $t) {
         $deletedBytes += $size[$key];
         $memAvailable += $size[$key];
         //echo "delete $key (".round($deletedBytes/(1024*1024))."MB)\n";
         //flush();
         apc_delete($key);
         $deletedCount++;
         if ($memAvailable > $requiredSpazi + 1024 * 1024 * 2) {
             break;
         }
     }
     echo "deleted {$deletedCount} entries\n";
     echo "deleted " . round($deletedBytes / 1024) . " KB\n";
     echo "\nduration: " . round(microtime(true) - $start, 3) . " s\n";
     exit;
 }
Ejemplo n.º 8
0
 public function checkPagesSubrootAction()
 {
     Kwf_Util_MemoryLimit::set(256);
     $pageClassesThatCanHaveFulltext = array();
     foreach (self::_getAllPossiblePageComponentClasses() as $c) {
         if (self::_canHaveFulltext($c)) {
             $pageClassesThatCanHaveFulltext[] = $c;
         }
     }
     $startTime = microtime(true);
     if (!$this->_getParam('componentId')) {
         throw new Kwf_Exception_Client("componentId parameter required");
     }
     $componentId = $this->_getParam('componentId');
     $queue = array($componentId);
     $stats = array('pages' => 0, 'indexedPages' => 0);
     while ($queue) {
         if (!$queue) {
             break;
         }
         if ($this->_getParam('debug')) {
             echo "queued: " . count($queue) . ' :: ' . round(memory_get_usage() / 1024 / 1024, 2) . "MB\n";
         }
         $componentId = array_shift($queue);
         $stats['pages']++;
         //if ($this->_getParam('debug')) echo "==> ".$componentId;
         $page = Kwf_Component_Data_Root::getInstance()->getComponentById($componentId);
         //if ($this->_getParam('debug')) echo " :: $page->url\n";
         if (!$page) {
             if ($this->_getParam('debug')) {
                 echo "{$componentId} not found!\n";
             }
             continue;
         }
         //echo "$page->url\n";
         if ($this->_getParam('verbose')) {
             echo "getting child pages...";
         }
         $childPages = $page->getChildPseudoPages(array('pageGenerator' => false, 'componentClasses' => $pageClassesThatCanHaveFulltext), array('pseudoPage' => false));
         $childPages = array_merge($childPages, $page->getChildPseudoPages(array('pageGenerator' => true), array('pseudoPage' => false)));
         if ($this->_getParam('verbose')) {
             echo " done\n";
         }
         foreach ($childPages as $c) {
             $i = $c;
             do {
                 if (Kwc_Abstract::getFlag($i->componentClass, 'skipFulltextRecursive')) {
                     continue 2;
                 }
             } while ($i = $i->parent);
             if ($this->_getParam('verbose')) {
                 echo "queued {$c->componentId}\n";
             }
             $queue[] = $c->componentId;
         }
         unset($c);
         $hasFulltext = false;
         if (!Kwc_Abstract::getFlag($page->componentClass, 'skipFulltext') && $page->isPage && ($page->getRecursiveChildComponents(array('flag' => 'hasFulltext', 'inherit' => false)) || Kwc_Abstract::getFlag($page->componentClass, 'hasFulltext'))) {
             $hasFulltext = true;
         }
         if ($hasFulltext) {
             if (!Kwf_Util_Fulltext_Backend_Abstract::getInstance()->documentExists($page)) {
                 if (Kwf_Util_Fulltext_Backend_Abstract::getInstance()->indexPage($page, $this->_getParam('debug'))) {
                     $stats['indexedPages']++;
                     if (!$this->_getParam('silent')) {
                         echo "not found in index: {$page->componentId} has content!!!!\n";
                     }
                 } else {
                     if ($this->_getParam('debug')) {
                         echo "not found in index: {$page->componentId} has NO content (that's ok)\n";
                     }
                 }
             }
         }
         unset($page);
         if ($stats['pages'] % 50 == 0) {
             Kwf_Component_Data_Root::getInstance()->freeMemory();
         }
     }
     if (!$this->_getParam('silent')) {
         echo "processed pages: {$stats['pages']}\n";
         echo "indexed pages: {$stats['indexedPages']}\n";
     }
     exit;
 }
 public function jsonUploadAction()
 {
     Kwf_Util_MemoryLimit::set(1024);
     $uploadModel = Kwf_Model_Abstract::getInstance('Kwf_Uploads_Model');
     $uploadedFile = array('filename' => '', 'extension' => '');
     if (isset($_FILES['Filedata'])) {
         $file = $_FILES['Filedata'];
         if ($file['error']) {
             if ($file['error'] == UPLOAD_ERR_NO_FILE) {
                 throw new Kwf_Exception_Client(trlKwf("No File uploaded, please select a file."));
             } else {
                 if ($file['error'] == UPLOAD_ERR_PARTIAL) {
                     throw new Kwf_Exception_Client(trlKwf("The uploaded file was only partially uploaded."));
                 } else {
                     if ($file['error'] == UPLOAD_ERR_INI_SIZE) {
                         throw new Kwf_Exception_Client(trlKwf("The uploaded file is too large."));
                     } else {
                         throw new Kwf_Exception("Upload error {$file['error']}");
                     }
                 }
             }
         }
         if (!isset($file['tmp_name']) || !is_file($file['tmp_name'])) {
             throw new Kwf_Exception("No File found");
         }
         $maxResolution = (int) $this->_getParam('maxResolution');
         if ($this->_getParam('maxResolution')) {
             $image = getimagesize($file['tmp_name']);
             if (substr($image['mime'], 0, 6) != 'image/') {
                 $maxResolution = 0;
             }
         }
         $filename = substr($file['name'], 0, strrpos($file['name'], '.'));
         $extension = substr(strrchr($file['name'], '.'), 1);
         $uploadedFile['filename'] = $filename;
         $uploadedFile['extension'] = $extension;
         if ($maxResolution > 0) {
             $fileData = Kwf_Media_Image::scale($file['tmp_name'], array('width' => $maxResolution, 'height' => $maxResolution, 'cover' => false));
             Kwf_Uploads_Model::verifyUpload($file);
             $fileRow = $uploadModel->writeFile($fileData, $filename, $extension, $file['type']);
         } else {
             $fileRow = $uploadModel->uploadFile($file);
         }
     } else {
         if (isset($_SERVER['HTTP_X_UPLOAD_NAME'])) {
             $fileData = file_get_contents("php://input");
             if (isset($_SERVER['CONTENT_LENGTH'])) {
                 $contentLength = $_SERVER['CONTENT_LENGTH'];
             } else {
                 $contentLength = $_SERVER['HTTP_CONTENT_LENGTH'];
             }
             if ($contentLength != $_SERVER['HTTP_X_UPLOAD_SIZE']) {
                 throw new Kwf_Exception("Content-Length doesn't match X-Upload-Size");
             }
             if ($contentLength != strlen($fileData)) {
                 throw new Kwf_Exception("Content-Length doesn't match uploaded data");
             }
             $name = rawurldecode($_SERVER['HTTP_X_UPLOAD_NAME']);
             $filename = substr($name, 0, strrpos($name, '.'));
             $extension = substr(strrchr($name, '.'), 1);
             $uploadedFile['filename'] = $filename;
             $uploadedFile['extension'] = $extension;
             $mimeType = null;
             if (isset($_SERVER['HTTP_X_UPLOAD_TYPE'])) {
                 $mimeType = $_SERVER['HTTP_X_UPLOAD_TYPE'];
             }
             if (isset($_SERVER['HTTP_X_UPLOAD_MAXRESOLUTION']) && $_SERVER['HTTP_X_UPLOAD_MAXRESOLUTION'] > 0) {
                 $maxResolution = $_SERVER['HTTP_X_UPLOAD_MAXRESOLUTION'];
                 $tempFile = tempnam('temp', 'upload');
                 file_put_contents($tempFile, $fileData);
                 $fileData = Kwf_Media_Image::scale($tempFile, array('width' => $maxResolution, 'height' => $maxResolution, 'cover' => false));
                 unlink($tempFile);
                 $fileRow = $uploadModel->writeFile($fileData, $filename, $extension, $mimeType);
             } else {
                 $fileRow = $uploadModel->writeFile($fileData, $filename, $extension, $mimeType);
             }
         } else {
             throw new Kwf_Exception_Client(trlKwf("No Filedata received."));
         }
     }
     $this->view->value = $fileRow->getFileInfo();
     $this->view->value['uploaded_filename'] = $uploadedFile['filename'];
     $this->view->value['uploaded_extension'] = $uploadedFile['extension'];
 }
 public function updateChangedJobAction()
 {
     Kwf_Util_MemoryLimit::set(512);
     $start = microtime(true);
     $m = Kwf_Component_PagesMetaModel::getInstance();
     $s = $m->select();
     $s->whereEquals('changed_recursive', true);
     foreach ($m->getRows($s) as $row) {
         if ($this->_getParam('debug')) {
             echo "changed recursive: {$row->page_id}\n";
         }
         $page = Kwf_Component_Data_Root::getInstance()->getComponentById($row->page_id);
         if (!$page) {
             $row->deleteRecursive();
             continue;
         }
         $this->_processRecursive($page);
         $row->changed_recursive = false;
         $row->save();
         if (microtime(true) - $start > 30) {
             if ($this->_getParam('debug')) {
                 echo "stopped after " . round(microtime(true) - $start) . "sec\n";
             }
             break;
         }
     }
     exit;
 }
 public function rebuildAction()
 {
     Kwf_Util_MemoryLimit::set(512);
     if (!$this->_getParam('skip-check-for-invalid')) {
         $cmd = Kwf_Config::getValue('server.phpCli') . " bootstrap.php component-pages-meta check-for-invalid";
         if ($this->_getParam('debug')) {
             $cmd .= " --debug";
         }
         system($cmd);
     }
     $startTime = microtime(true);
     $numProcesses = 0;
     $queueFile = 'temp/pagemetaRebuildQueue';
     $statsFile = 'temp/pagemetaRebuildStats';
     $componentId = 'root';
     if ($this->_getParam('componentId')) {
         $componentId = $this->_getParam('componentId');
     }
     file_put_contents($queueFile, $componentId);
     $stats = array('pages' => 0, 'addedPages' => 0);
     file_put_contents($statsFile, serialize($stats));
     while (true) {
         $numProcesses++;
         $cmd = Kwf_Config::getValue('server.phpCli') . " bootstrap.php component-pages-meta rebuild-worker";
         if ($this->_getParam('debug')) {
             $cmd .= " --debug";
         }
         system($cmd, $status);
         if ($status != 0) {
             throw new Kwf_Exception("child process failed");
         }
         if ($this->_getParam('debug')) {
             echo "memory_usage (parent): " . memory_get_usage() / (1024 * 1024) . "MB\n";
         }
         if (!file_get_contents($queueFile)) {
             if ($this->_getParam('debug')) {
                 echo "fertig.\n";
             }
             break;
         }
     }
     if (!$this->_getParam('silent')) {
         $stats = unserialize(file_get_contents($statsFile));
         echo "fulltext reindex finished.\n";
         echo "duration: " . Kwf_View_Helper_SecondsAsDuration::secondsAsDuration(microtime(true) - $startTime) . "s\n";
         echo "used child processes: {$numProcesses}\n";
         echo "processed pages: {$stats['pages']}\n";
         echo "indexed pages: {$stats['addedPages']}\n";
     }
     exit;
 }
Ejemplo n.º 12
0
 public function jsonXlsAction()
 {
     Kwf_Util_MemoryLimit::set(768);
     set_time_limit(600);
     // 10 minuten
     if (!isset($this->_permissions['xls']) || !$this->_permissions['xls']) {
         throw new Kwf_Exception("XLS is not allowed.");
     }
     $data = $this->_getExportData(Kwf_Grid_Column::SHOW_IN_XLS, 'xls', 640);
     $xls = new PHPExcel();
     $xls->getProperties()->setCreator("Vivid Planet Software GmbH");
     $xls->getProperties()->setLastModifiedBy("Vivid Planet Software GmbH");
     $xls->getProperties()->setTitle("KWF Excel Export");
     $xls->getProperties()->setSubject("KWF Excel Export");
     $xls->getProperties()->setDescription("KWF Excel Export");
     $xls->getProperties()->setKeywords("KWF Excel Export");
     $xls->getProperties()->setCategory("KWF Excel Export");
     $xls->setActiveSheetIndex(0);
     $sheet = $xls->getActiveSheet();
     // setting width for each column
     $colIndex = 0;
     $renderer = array();
     foreach ($this->_columns as $column) {
         if (!($column->getShowIn() & Kwf_Grid_Column::SHOW_IN_XLS)) {
             continue;
         }
         if (is_null($column->getHeader())) {
             continue;
         }
         if ($column->getXlsWidth()) {
             $width = $column->getXlsWidth();
         } else {
             if ($column->getWidth()) {
                 $width = round($column->getWidth() / 6, 1);
             } else {
                 $width = 15;
             }
         }
         $sheet->getColumnDimension($this->_getColumnLetterByIndex($colIndex))->setWidth($width);
         $renderer[$colIndex] = $column->getRenderer();
         $colIndex++;
     }
     $helperDate = new Kwf_View_Helper_Date();
     $helperDateTime = new Kwf_View_Helper_DateTime();
     foreach ($data as $row => $cols) {
         // row ist index, das andre nicht, passt aber trotzdem so
         // da ja in der ersten Zeile der Header steht
         foreach ($cols as $col => $text) {
             $cell = $this->_getColumnLetterByIndex($col) . ($row + 1);
             if (is_array($text)) {
                 $text = implode(', ', $text);
             }
             // make header bold
             if ($row == 0) {
                 $sheet->getStyle($cell)->getFont()->setBold(true);
             }
             // TODO: Zeilenumbrüche
             $textType = gettype($text);
             $cellType = PHPExcel_Cell_DataType::TYPE_STRING;
             if ($textType == 'boolean') {
                 $cellType = PHPExcel_Cell_DataType::TYPE_BOOL;
             }
             if ($textType == 'integer' || $textType == 'double' || $textType == 'float') {
                 $cellType = PHPExcel_Cell_DataType::TYPE_NUMERIC;
             }
             if ($textType == 'NULL') {
                 $cellType = PHPExcel_Cell_DataType::TYPE_NULL;
             }
             // datum umformatieren
             if (strlen($text) == 10 && preg_match('/^[0-9]{4,4}-[0-9]{2,2}-[0-9]{2,2}$/', $text)) {
                 $text = $helperDate->date($text);
             } else {
                 if (strlen($text) == 19 && preg_match('/^[0-9]{4,4}-[0-9]{2,2}-[0-9]{2,2} [0-9]{2,2}:[0-9]{2,2}:[0-9]{2,2}$/', $text)) {
                     $text = $helperDateTime->dateTime($text);
                 }
             }
             $sheet->setCellValueExplicit($cell, $text, $cellType);
             if ($renderer[$col] == 'clickableLink') {
                 $sheet->getCell($cell)->getHyperlink()->setUrl($text);
             }
         }
         $this->_progressBar->next(1, trlKwf('Writing data. Please be patient.'));
     }
     // write the file
     $downloadkey = uniqid();
     if (class_exists('XMLWriter')) {
         $objWriter = PHPExcel_IOFactory::createWriter($xls, 'Excel2007');
         $objWriter->save('temp/' . $downloadkey . '.xlsx');
     } else {
         $objWriter = PHPExcel_IOFactory::createWriter($xls, 'Excel5');
         $objWriter->save('temp/' . $downloadkey . '.xls');
     }
     $this->_progressBar->finish();
     $this->view->downloadkey = $downloadkey;
 }
 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;
 }
Ejemplo n.º 14
0
 /**
  * @param array possible options: types(=all), output(=false), refresh(=true), excludeTypes
  */
 public final function clearCache(array $options)
 {
     $typeNames = $options['types'];
     $output = isset($options['output']) ? $options['output'] : false;
     $refresh = isset($options['refresh']) ? $options['refresh'] : false;
     $excludeTypes = isset($options['excludeTypes']) ? $options['excludeTypes'] : array();
     Kwf_Events_ModelObserver::getInstance()->disable();
     Kwf_Util_MemoryLimit::set(512);
     if ($typeNames == 'all') {
         $types = $this->getTypes();
     } else {
         if (!is_array($typeNames)) {
             $typeNames = explode(',', $typeNames);
         }
         $types = array();
         foreach ($this->getTypes() as $t) {
             if (in_array($t->getTypeName(), $typeNames)) {
                 $types[] = $t;
             }
         }
     }
     if (is_string($excludeTypes)) {
         $excludeTypes = explode(',', $excludeTypes);
     }
     foreach ($types as $k => $i) {
         if (in_array($i->getTypeName(), $excludeTypes)) {
             unset($types[$k]);
         }
     }
     $maxTypeNameLength = 0;
     $countSteps = 0;
     foreach ($types as $type) {
         $type->setVerbosity($output ? Kwf_Util_ClearCache_Types_Abstract::VERBOSE : Kwf_Util_ClearCache_Types_Abstract::SILENT);
         $maxTypeNameLength = max($maxTypeNameLength, strlen($type->getTypeName()));
         if ($type->doesClear()) {
             $countSteps++;
         }
         if ($type->doesRefresh()) {
             $countSteps++;
         }
     }
     $progress = null;
     if (isset($options['progressAdapter'])) {
         $progress = new Zend_ProgressBar($options['progressAdapter'], 0, $countSteps);
     }
     $currentStep = 0;
     foreach ($types as $type) {
         if ($type->doesClear()) {
             $currentStep++;
             if ($progress) {
                 $progress->next(1, "clearing " . $type->getTypeName());
             }
             if ($output) {
                 echo "[" . str_repeat(' ', 2 - strlen($currentStep)) . "{$currentStep}/{$countSteps}] ";
                 echo "clearing " . $type->getTypeName() . "..." . str_repeat('.', $maxTypeNameLength - strlen($type->getTypeName())) . " ";
             }
             $t = microtime(true);
             $type->clearCache($options);
             if ($output) {
                 if ($type->getSuccess()) {
                     echo "OK";
                 } else {
                     echo " [ERROR]";
                 }
                 echo " (" . round((microtime(true) - $t) * 1000) . "ms)";
                 echo "\n";
             }
         }
     }
     if ($refresh) {
         foreach ($types as $type) {
             if ($type->doesRefresh()) {
                 $currentStep++;
                 if ($progress) {
                     $progress->next(1, "refreshing " . $type->getTypeName());
                 }
                 if ($output) {
                     echo "[{$currentStep}/{$countSteps}] refreshing " . $type->getTypeName() . "." . str_repeat('.', $maxTypeNameLength - strlen($type->getTypeName())) . " ";
                 }
                 $t = microtime(true);
                 $mem = memory_get_usage();
                 $type->refreshCache($options);
                 if ($output) {
                     if ($type->getSuccess()) {
                         echo "OK";
                     } else {
                         echo " [ERROR]";
                     }
                     echo " (" . round((microtime(true) - $t) * 1000) . "ms";
                     if (memory_get_usage() - $mem > 1024 * 1024) {
                         echo ", " . round((memory_get_usage() - $mem) / (1024 * 1024), 2) . "MB";
                     }
                     echo ")\n";
                 }
             }
         }
     }
     Kwf_Events_ModelObserver::getInstance()->enable();
     return $types;
 }
Ejemplo n.º 15
0
 private function _buildOutputForUrl($url)
 {
     Kwf_Util_MemoryLimit::set(768);
     class_exists('Kwf_Trl');
     //required because setup doesn't load Trl.php before dispatching assets
     $param = explode('/', $url);
     if (count($param) != 4) {
         throw new Kwf_Exception_NotFound();
     }
     $dependencyClass = $param[0];
     $dependencyParams = $param[1];
     $language = $param[2];
     $extension = $param[3];
     if (!class_exists($dependencyClass)) {
         throw new Kwf_Exception_NotFound();
     }
     if (!is_instance_of($dependencyClass, 'Kwf_Assets_Interface_UrlResolvable')) {
         throw new Kwf_Exception_NotFound();
     }
     //build whole assets dependency tree
     //required for eg. Modernizr to pick up all required tests
     $buildType = new Kwf_Util_Build_Types_Assets();
     $packages = $buildType->getAllPackages();
     $providers = array();
     foreach ($packages as $p) {
         foreach ($p->getProviderList()->getProviders() as $provider) {
             if (!in_array($provider, $providers)) {
                 $providers[] = $provider;
                 $provider->initialize();
             }
         }
     }
     foreach ($packages as $p) {
         $p->getDependency()->getRecursiveDependencies();
     }
     $package = call_user_func(array($dependencyClass, 'fromUrlParameter'), $dependencyClass, $dependencyParams);
     if (!$package instanceof Kwf_Assets_Package) {
         throw new Kwf_Exception_NotFound();
     }
     return $package->getUrlContents($extension, $language);
 }
 public function jsonPasteAction()
 {
     $session = new Kwf_Session_Namespace('PagesController:copy');
     $id = $session->id;
     if (!$id || !Kwf_Component_Data_Root::getInstance()->getComponentByDbId($id, array('ignoreVisible' => true))) {
         throw new Kwf_Exception_Client(trlKwf('Clipboard is empty'));
     }
     $source = Kwf_Component_Data_Root::getInstance()->getComponentByDbId($id, array('ignoreVisible' => true));
     $target = Kwf_Component_Data_Root::getInstance()->getComponentByDbId($this->_getParam('id'), array('ignoreVisible' => true));
     $user = Zend_Registry::get('userModel')->getAuthedUser();
     $acl = Kwf_Registry::get('acl')->getComponentAcl();
     if (!$acl->isAllowed($user, $source) || !$acl->isAllowed($user, $target)) {
         throw new Kwf_Exception_AccessDenied();
     }
     $progressBar = new Zend_ProgressBar(new Kwf_Util_ProgressBar_Adapter_Cache($this->_getParam('progressNum')), 0, Kwf_Util_Component::getDuplicateProgressSteps($source));
     Kwf_Util_MemoryLimit::set(256);
     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.
     $newPage = Kwf_Util_Component::duplicate($source, $target, $progressBar);
     Kwf_Util_Component::afterDuplicate($source, $newPage);
     Kwf_Events_ModelObserver::getInstance()->enable();
     $progressBar->finish();
     $s = new Kwf_Model_Select();
     $s->whereEquals('parent_id', $newPage->row->parent_id);
     $s->order('pos', 'DESC');
     $s->limit(1);
     $lastRow = $newPage->generator->getModel()->getRow($s);
     $row = $newPage->generator->getModel()->getRow($newPage->row->id);
     $row->pos = $lastRow ? $lastRow->pos + 1 : 1;
     $row->visible = false;
     $row->save();
 }
 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 checkContentsSubrootAction()
 {
     Kwf_Util_MemoryLimit::set(256);
     $subroot = Kwf_Component_Data_Root::getInstance()->getComponentById($this->_getParam('subroot'));
     if (!$subroot) {
         $subroot = Kwf_Component_Data_Root::getInstance();
     }
     $i = 0;
     $stats = array('indexedPages' => 0, 'diffPages' => 0);
     $pagesMetaModel = Kwf_Component_PagesMetaModel::getInstance();
     $select = new Kwf_Model_Select();
     $select->whereEquals('deleted', false);
     $select->whereEquals('fulltext_skip', false);
     $select->whereEquals('subroot_component_id', $subroot->componentId);
     $it = new Kwf_Model_Iterator_Packages(new Kwf_Model_Iterator_Rows($pagesMetaModel, $select));
     if ($this->_getParam('debug')) {
         $it = new Kwf_Iterator_ConsoleProgressBar($it);
     }
     foreach ($it as $row) {
         $componentId = $row->page_id;
         $page = Kwf_Component_Data_Root::getInstance()->getComponentById($componentId);
         if (!$this->_getParam('skip-diff')) {
             $docContent = Kwf_Util_Fulltext_Backend_Abstract::getInstance()->getDocumentContent($page);
         } else {
             $docContent = '';
         }
         $newDoc = Kwf_Util_Fulltext_Backend_Abstract::getInstance()->getFulltextContentForPage($page);
         if (!$newDoc) {
             //this can happen (if there is no content)
             Kwf_Util_Fulltext_Backend_Abstract::getInstance()->deleteDocument($subroot, $componentId);
             $row = $pagesMetaModel->getRow($componentId);
             if ($row) {
                 $row->delete();
             }
             continue;
         }
         if (trim($newDoc['content']) != trim($docContent)) {
             $stats['diffPages']++;
             if (Kwf_Util_Fulltext_Backend_Abstract::getInstance()->indexPage($page)) {
                 $stats['indexedPages']++;
             }
             if (!$this->_getParam('silent') && !$this->_getParam('skip-diff')) {
                 echo "DIFF: {$componentId}\n";
             }
         }
         unset($page);
         if ($i++ % 10) {
             Kwf_Component_Data_Root::getInstance()->freeMemory();
         }
         //if ($this->_getParam('debug')) echo "memory_usage ".(memory_get_usage()/(1024*1024))."MB\n";
     }
     if ($this->_getParam('debug')) {
         if (!$this->_getParam('skip-diff')) {
             echo "pages with diff: {$stats['diffPages']}\n";
         }
         echo "indexed pages: {$stats['indexedPages']}\n";
     }
     exit;
 }