Exemplo n.º 1
0
 public function getChildData($parentData, $select = array())
 {
     Kwf_Benchmark::count('GenStatic::getChildData');
     if (is_array($select)) {
         $select = new Kwf_Component_Select($select);
     }
     $pData = $parentData;
     $ret = array();
     if ($p = $select->getPart(Kwf_Component_Select::WHERE_ID)) {
         if (!$this->_idMatches($p)) {
             return $ret;
         }
     }
     if (!$parentData) {
         if ($p = $select->getPart(Kwf_Component_Select::WHERE_CHILD_OF)) {
             throw new Kwf_Exception("this must not happen");
             $p = $p->getPageOrRoot();
             $parentData = $p->getRecursiveChildComponents(array('componentClass' => $this->_class));
         } else {
             $parentSelect = new Kwf_Component_Select();
             $parentSelect->copyParts(array(Kwf_Component_Select::WHERE_SUBROOT, Kwf_Component_Select::IGNORE_VISIBLE, Kwf_Component_Select::WHERE_CHILD_OF), $select);
             $parentData = Kwf_Component_Data_Root::getInstance()->getComponentsBySameClass($this->_class, $parentSelect);
         }
     }
     $parentDatas = is_array($parentData) ? $parentData : array($parentData);
     foreach ($this->_fetchKeys($pData, $select) as $key) {
         foreach ($parentDatas as $parentData) {
             $data = $this->_createData($parentData, $key, $select);
             if ($data) {
                 $ret[] = $data;
             }
         }
     }
     return $ret;
 }
Exemplo n.º 2
0
 protected static function _callProcessInput($process)
 {
     static $benchmarkEnabled;
     if (!isset($benchmarkEnabled)) {
         $benchmarkEnabled = Kwf_Benchmark::isEnabled();
     }
     $postData = self::_getRequestWithFiles();
     foreach ($process as $i) {
         Kwf_Benchmark::count('processInput', $i->componentId);
         if (method_exists($i->getComponent(), 'preProcessInput')) {
             if ($benchmarkEnabled) {
                 $startTime = microtime(true);
             }
             $i->getComponent()->preProcessInput($postData);
             if ($benchmarkEnabled) {
                 Kwf_Benchmark::subCheckpoint($i->componentId . ' preProcessInput', microtime(true) - $startTime);
             }
         }
     }
     foreach ($process as $i) {
         if (method_exists($i->getComponent(), 'processInput')) {
             if ($benchmarkEnabled) {
                 $startTime = microtime(true);
             }
             $i->getComponent()->processInput($postData);
             if ($benchmarkEnabled) {
                 Kwf_Benchmark::subCheckpoint($i->componentId, microtime(true) - $startTime);
             }
         }
     }
     if (class_exists('Kwf_Events_ModelObserver', false)) {
         //Nur wenn klasse jemals geladen wurde kann auch was zu processen drin sein
         Kwf_Events_ModelObserver::getInstance()->process(false);
     }
 }
Exemplo n.º 3
0
 public function loadWithMetadata($componentId, $renderer = 'component', $type = 'component', $value = '')
 {
     if ($componentId instanceof Kwf_Component_Data) {
         $componentId = $componentId->componentId;
     }
     $cacheId = $this->_getCacheId($componentId, $renderer, $type, $value);
     $data = Kwf_Component_Cache_Memory::getInstance()->loadWithMetaData($cacheId);
     if ($data === false || !is_array($data)) {
         Kwf_Benchmark::count('comp cache mysql');
         $select = $this->getModel('cache')->select()->whereEquals('component_id', (string) $componentId)->whereEquals('renderer', $renderer)->whereEquals('type', $type)->whereEquals('deleted', false)->whereEquals('value', $value)->where(new Kwf_Model_Select_Expr_Or(array(new Kwf_Model_Select_Expr_Higher('expire', time()), new Kwf_Model_Select_Expr_IsNull('expire'))));
         if ($data !== false) {
             $select->where(new Kwf_Model_Select_Expr_Higher('microtime', $data));
         }
         $options = array('columns' => array('content', 'expire'));
         $row = $this->getModel('cache')->export(Kwf_Model_Db::FORMAT_ARRAY, $select, $options);
         if (isset($row[0])) {
             Kwf_Benchmark::countLog('viewcache-db');
             $ttl = null;
             if ($row[0]['expire']) {
                 $ttl = $row[0]['expire'] - time();
             }
             Kwf_Component_Cache_Memory::getInstance()->save($row[0]['content'], $cacheId, $ttl);
             $data = array('contents' => $row[0]['content'], 'expire' => $row[0]['expire']);
         } else {
             Kwf_Benchmark::countLog('viewcache-miss');
             $data = null;
         }
     } else {
         Kwf_Benchmark::countLog('viewcache-mem');
     }
     return $data;
 }
Exemplo n.º 4
0
 protected function __construct($class, $settings)
 {
     //self::$objectsCount++;
     $this->_class = $class;
     $this->_settings = $settings;
     $this->_init();
     Kwf_Benchmark::count('generators');
 }
Exemplo n.º 5
0
 public function renderMaster($component)
 {
     static $benchmarkEnabled;
     if (!isset($benchmarkEnabled)) {
         $benchmarkEnabled = Kwf_Benchmark::isEnabled();
     }
     $content = false;
     if ($this->_enableCache) {
         $content = Kwf_Component_Cache::getInstance()->load($component->componentId, $this->_getRendererName(), 'fullPage');
         $this->_minLifetime = null;
     }
     Kwf_Benchmark::checkpoint('load fullPage cache');
     $statType = null;
     if (!$content) {
         if ($benchmarkEnabled) {
             $startTime = microtime(true);
         }
         if (!$this->_enableCache || ($content = Kwf_Component_Cache::getInstance()->load($component, $this->_getRendererName(), 'page')) === null) {
             $masterHelper = new Kwf_Component_View_Helper_Master();
             $masterHelper->setRenderer($this);
             $content = $masterHelper->master($component);
             if ($this->_enableCache) {
                 Kwf_Component_Cache::getInstance()->save($component, $content, $this->_getRendererName(), 'page', '', '', null);
                 $statType = 'miss';
             } else {
                 $statType = 'noviewcache';
             }
         } else {
             $statType = 'hit';
         }
         if ($statType) {
             Kwf_Benchmark::count("rendered {$statType}", $component->componentId . ': page');
         }
         Kwf_Benchmark::countLog('render-' . $statType);
         if ($benchmarkEnabled) {
             Kwf_Benchmark::subCheckpoint($component->componentId . ' page', microtime(true) - $startTime);
         }
         Kwf_Benchmark::checkpoint('render page');
         $pass1Cacheable = true;
         $content = $this->_renderPass1($content, $pass1Cacheable);
         Kwf_Benchmark::checkpoint('render pass 1');
         if ($this->_enableCache && $pass1Cacheable) {
             Kwf_Component_Cache::getInstance()->save($component, $content, $this->_getRendererName(), 'fullPage', '', '', $this->_minLifetime);
         }
         Kwf_Benchmark::count("rendered miss", $component->componentId . ': fullPage');
         Kwf_Benchmark::countLog('fullpage-miss');
     } else {
         Kwf_Benchmark::count("rendered hit", $component->componentId . ': fullPage');
         Kwf_Benchmark::countLog('fullpage-hit');
     }
     $content = $this->_renderPass2($content);
     Kwf_Benchmark::checkpoint('render pass 2');
     Kwf_Component_Cache::getInstance()->writeBuffer();
     return $content;
 }
Exemplo n.º 6
0
 public function getChildData($parentData, $select = array())
 {
     Kwf_Benchmark::count('GenTable::getChildData');
     if (is_array($select)) {
         $select = new Kwf_Component_Select($select);
     }
     $ret = array();
     if (!$parentData && ($p = $select->getPart(Kwf_Component_Select::WHERE_CHILD_OF)) && !$this->_getUseComponentId()) {
         $parentDatas = $p->getRecursiveChildComponents(array('componentClass' => $this->_class));
     } else {
         $parentDatas = array($parentData);
     }
     foreach ($parentDatas as $parentData) {
         $s = $this->_formatSelect($parentData, clone $select);
         $rows = array();
         if ($s) {
             $rows = $this->_fetchRows($parentData, $s);
         }
         foreach ($rows as $row) {
             $currentPd = $parentData;
             if (!$currentPd) {
                 $currentPd = $this->_getParentDataByRow($row, $s);
             }
             if (!is_array($currentPd)) {
                 if ($currentPd) {
                     $currentPds = array($currentPd);
                 } else {
                     $currentPds = array();
                 }
             } else {
                 $currentPds = $currentPd;
             }
             foreach ($currentPds as $currentPd) {
                 if (!$currentPd) {
                     throw new Kwf_Exception("No parentData returned in '" . get_class($this) . "'");
                 }
                 $data = $this->_createData($currentPd, $row, $s);
                 if ($data) {
                     $ret[] = $data;
                 }
             }
         }
     }
     return $ret;
 }
 public function getChildData($parentData, $select = array())
 {
     if (is_array($select)) {
         $select = new Kwf_Component_Select($select);
     }
     if (!is_instance_of($this->_class, 'Kwc_Root_Component') && $select->hasPart(Kwf_Component_Select::WHERE_SUBROOT)) {
         //abkürzung wenn mehrere domains mit unterschiedlichen component-Klassen
         //im prinzip gleicher code wie in _GetParentDataByRow wenn return null gemacht wird, aber das hier wird früher gemacht
         $subroot = $select->getPart(Kwf_Component_Select::WHERE_SUBROOT);
         $component = $subroot[0];
         while (!Kwc_Abstract::getFlag($component->componentClass, 'subroot')) {
             $component = $component->parent;
         }
         if ($component->componentClass != $this->getClass()) {
             Kwf_Benchmark::count('GenTable::getChildData skipped');
             return array();
         }
     }
     return parent::getChildData($parentData, $select);
 }
 public function indexAction()
 {
     $id = $this->_getParam('id');
     if (!$id) {
         throw new Kwf_ClientException("Missing Parameter: id");
     }
     $c = Kwf_Component_Data_Root::getInstance()->getComponentById($id, array('ignoreVisible' => true));
     if (!$c) {
         $c = Kwf_Component_Data_Root::getInstance()->getComponentByDbId($id, array('ignoreVisible' => true));
     }
     if (!$c) {
         throw new Kwf_ClientException("Component with id '{$id}' not found");
     }
     $process = $c->getRecursiveChildComponents(array('page' => false, 'flags' => array('processInput' => true)));
     if (Kwf_Component_Abstract::getFlag($c->componentClass, 'processInput')) {
         $process[] = $c;
     }
     $postData = array();
     foreach ($process as $i) {
         Kwf_Benchmark::count('processInput', $i->componentId);
         if (method_exists($i->getComponent(), 'preProcessInput')) {
             $i->getComponent()->preProcessInput($postData);
         }
     }
     foreach ($process as $i) {
         if (method_exists($i->getComponent(), 'processInput')) {
             $i->getComponent()->processInput($postData);
         }
     }
     /*
     //deaktivert: funktioniert nicht
     $c->getComponent()->sendContent('views/component-master.tpl', true);
     */
     //zwischenlösung:
     //(unschön: keine assets, kein html-header usw)
     echo $c->render();
     Kwf_Benchmark::output();
     exit;
 }
Exemplo n.º 9
0
 /**
  * Render components (ie. expand <kwc ...>)
  *
  * Pass 1 for content that can be stored in fullPage cache,
  * 2 for everything else. 2 includes 1, so calling just with 2 also works
  * @param string render content
  */
 protected function _renderPass2($ret, &$hasDynamicParts = false)
 {
     //execute all plugins that where added in pass 1
     $ret = $this->_findAndExecutePlugins($ret, self::PLUGIN_TYPE_USECACHE, $hasDynamicParts);
     $ret = $this->_findAndExecutePlugins($ret, self::PLUGIN_TYPE_BEFORE, $hasDynamicParts);
     $ret = $this->_findAndExecutePlugins($ret, self::PLUGIN_TYPE_REPLACE, $hasDynamicParts);
     $ret = $this->_findAndExecuteUseCacheDynamic($ret, $hasDynamicParts);
     static $benchmarkEnabled;
     if (!isset($benchmarkEnabled)) {
         $benchmarkEnabled = Kwf_Benchmark::isEnabled();
     }
     $offset = 0;
     while ($target = $this->_getNextRenderTarget($ret, 2, $offset)) {
         if ($benchmarkEnabled) {
             $startTime = microtime(true);
         }
         if ($target['type'] == 'dynamic' && $target['config']['class'] == 'Kwf_Component_Dynamic_SessionToken' && !Kwf_Setup::hasAuthedUser()) {
             $hasDynamicParts = true;
             //yes, this is cheating, but a very common case that's worth optimizing using this hack
             $ret = substr($ret, 0, $target['start']) . '' . substr($ret, $target['end'] + 1);
             continue;
         }
         $helper = $this->_getHelper($target['type']);
         $statType = null;
         $content = null;
         if ($this->_enableCache && $target['isCacheable']) {
             $content = $this->_cacheLoad($target['componentId'], $target['type'], $target['value']);
         }
         if (!is_null($content)) {
             //cache hit
             $statType = 'hit';
             //look for UseViewCache plugin in $content
             if ($p = $this->_findSinglePlugin(self::PLUGIN_TYPE_USECACHE, $content)) {
                 $hasDynamicParts = true;
                 if (!$p['plugin']->useViewCache($this)) {
                     //re-render, without <pluginC
                     $content = $this->_renderUncached($target['componentId'], $target['type'], $target['config']);
                 } else {
                     //continue with content
                     $content = $p['content'];
                 }
             } else {
                 //execute replace and before plugin
                 if ($p = $this->_findSinglePlugin(self::PLUGIN_TYPE_REPLACE, $content)) {
                     $hasDynamicParts = true;
                     $r = $p['plugin']->replaceOutput($this);
                     if ($r !== false) {
                         $content = $r;
                     } else {
                         $content = $p['content'];
                     }
                 }
                 $content = $this->_findAndExecutePlugins($content, self::PLUGIN_TYPE_BEFORE, $hasDynamicParts);
             }
             $content = $this->_findAndExecuteUseCacheDynamic($content, $hasDynamicParts);
         } else {
             if ($this->_enableCache && $target['isCacheable']) {
                 //cache miss
                 $statType = 'miss';
                 $content = $this->_renderAndCache($target['componentId'], $target['type'], $target['value'], $target['config'], false);
             } else {
                 $hasDynamicParts = true;
                 //view cache disabled
                 $statType = 'noviewcache';
                 $content = $this->_renderUncached($target['componentId'], $target['type'], $target['config']);
             }
         }
         $content = $helper->renderCached($content, $target['componentId'], $target['config']);
         $ret = substr($ret, 0, $target['start']) . $content . substr($ret, $target['end'] + 1);
         if ($statType) {
             if ($benchmarkEnabled) {
                 Kwf_Benchmark::count("rendered {$statType}", $target['statId']);
             }
             Kwf_Benchmark::countLog('render-' . $statType);
         }
         if ($benchmarkEnabled) {
             Kwf_Benchmark::subCheckpoint($target['componentId'] . ' ' . $target['type'], microtime(true) - $startTime);
         }
     }
     //execute Render Cached Dynamic, used eg for callback link modifier in componentLink
     while (($start = strpos($ret, '<rcd ')) !== false) {
         $hasDynamicParts = true;
         $startEnd = strpos($ret, '>', $start);
         $args = explode(' ', substr($ret, $start + 5, $startEnd - $start - 5));
         $end = strpos($ret, '</rcd ' . $args[0] . '>');
         $content = substr($ret, $startEnd + 1, $end - $startEnd - 1);
         if ($benchmarkEnabled) {
             $startTime = microtime(true);
         }
         $componentId = $args[0];
         $type = $args[1];
         $settings = json_decode($args[2], true);
         $content = $this->_getHelper($type)->renderCachedDynamic($content, $componentId, $settings);
         if ($benchmarkEnabled) {
             Kwf_Benchmark::subCheckpoint("renderCachedDynamic {$type} {$componentId}", microtime(true) - $startTime);
         }
         $ret = substr($ret, 0, $start) . $content . substr($ret, $end + 7 + strlen($args[0]));
     }
     $ret = $this->_findAndExecutePlugins($ret, self::PLUGIN_TYPE_AFTER, $hasDynamicParts);
     return $ret;
 }
Exemplo n.º 10
0
 public function getChildData($parentData, $select = array())
 {
     Kwf_Benchmark::count('GenStaticSelect::getChildData');
     if (is_array($select)) {
         $select = new Kwf_Component_Select($select);
     }
     if ($select->hasPart(Kwf_Component_Select::WHERE_COMPONENT_CLASSES)) {
         $continue = false;
         foreach ($select->getPart(Kwf_Component_Select::WHERE_COMPONENT_CLASSES) as $componentClass) {
             if (in_array($componentClass, $this->getChildComponentClasses())) {
                 $continue = true;
             }
         }
         if (!$continue) {
             return array();
         }
     }
     if (!$parentData) {
         if (!$select->hasPart(Kwf_Component_Select::WHERE_COMPONENT_CLASSES)) {
             throw new Kwf_Exception_NotYetImplemented();
         }
         $selectClasses = $select->getPart(Kwf_Component_Select::WHERE_COMPONENT_CLASSES);
         $possibleClasses = $this->getChildComponentClasses();
         if (in_array(array_shift($possibleClasses), $selectClasses)) {
             throw new Kwf_Exception("You can't search for component which is first (=default) in StaticSelect");
         }
         $searchFor = array();
         foreach ($selectClasses as $c) {
             $searchFor[] = array_search($c, $possibleClasses);
         }
         $s = new Kwf_Model_Select();
         $s->whereEquals('component', $searchFor);
         $s->where(new Kwf_Model_Select_Expr_Like('component_id', '%-' . $this->getGeneratorKey()));
         $rows = $this->_getModel()->export(Kwf_Model_Abstract::FORMAT_ARRAY, $s, array('columns' => array('component_id')));
         $parentDatas = array();
         foreach ($rows as $row) {
             $id = substr($row['component_id'], 0, -(strlen($this->getGeneratorKey()) + 1));
             $s = new Kwf_Component_Select();
             $s->copyParts(array(Kwf_Component_Select::IGNORE_VISIBLE, Kwf_Component_Select::WHERE_SUBROOT), $select);
             $d = Kwf_Component_Data_Root::getInstance()->getComponentByDbId($id, $s);
             if ($d) {
                 $parentDatas[] = $d;
             }
         }
     } else {
         $parentDatas = array($parentData);
     }
     $ret = array();
     foreach ($parentDatas as $parentData) {
         $data = $this->_createData($parentData, $this->getGeneratorKey(), $select);
         if (!$data) {
             continue;
         }
         if ($select->hasPart('whereId')) {
             if ('-' . $data->id != $select->getPart('whereId')) {
                 continue;
             }
         }
         $ret[] = $data;
     }
     return $ret;
 }
Exemplo n.º 11
0
 public static function loadClass($class)
 {
     parent::loadClass($class);
     Kwf_Benchmark::count('classes included', $class);
 }
Exemplo n.º 12
0
 /**
  * Look up item(s) by ASIN
  *
  * @param  string $asin    Amazon ASIN ID
  * @param  array  $options Query Options
  * @see http://docs.amazonwebservices.com/AWSEcommerceService/2011-08-01/ApiReference/BrowseNodeLookupOperation.html
  * @throws Zend_Service_Exception
  * @return Kwf_Service_Amazon_BrowseNode
  */
 public function browseNodeLookup($nodeId, array $options = array())
 {
     Kwf_Benchmark::count('Service Amazon request', 'browseNodeLookup');
     $client = $this->getRestClient();
     $client->setUri($this->_baseUri);
     $client->getHttpClient()->resetParameters();
     $defaultOptions = array('IdType' => 'ASIN', 'ResponseGroup' => 'BrowseNodeInfo');
     $options['BrowseNodeId'] = (string) $nodeId;
     $options = $this->_prepareOptions('BrowseNodeLookup', $options, $defaultOptions);
     $response = $client->restGet('/onca/xml', $options);
     if ($response->isError()) {
         /**
          * @see Zend_Service_Exception
          */
         throw new Zend_Service_Exception('An error occurred sending request. Status code: ' . $response->getStatus());
     }
     $dom = new DOMDocument();
     $dom->loadXML($response->getBody());
     self::_checkErrors($dom);
     return new Kwf_Service_Amazon_BrowseNode($dom);
 }
Exemplo n.º 13
0
 public function getChildData($parentData, $select = array())
 {
     Kwf_Benchmark::count('GenCards::getChildData');
     if (is_array($select)) {
         $select = new Kwf_Component_Select($select);
     }
     //es gibt exakt eine unterkomponente mit der id 'child'
     if ($select->hasPart(Kwf_Component_Select::WHERE_ID)) {
         if ($select->getPart(Kwf_Component_Select::WHERE_ID) != '-child') {
             return array();
         }
     }
     $ret = array();
     if (!$parentData) {
         if ($select->hasPart(Kwf_Component_Select::WHERE_COMPONENT_CLASSES)) {
             $cc = $select->getPart(Kwf_Component_Select::WHERE_COMPONENT_CLASSES);
             $componentValues = array_keys(array_intersect($this->_settings['component'], $cc));
             if (!$componentValues) {
                 throw new Kwf_Exception("no component classes found in this generator, should not have been clled");
             }
             reset($this->_settings['component']);
             if (in_array(current($this->_settings['component']), $cc)) {
                 throw new Kwf_Exception("can't get the first=default component without parentData as it might be not in the database");
             }
             $s = new Kwf_Model_Select();
             $s->whereEquals('component', $componentValues);
             if ($p = $select->getPart(Kwf_Component_Select::WHERE_CHILD_OF)) {
                 $ors = array(new Kwf_Model_Select_Expr_And(array(new Kwf_Model_Select_Expr_Like('component_id', $p->dbId . '%'), new Kwf_Model_Select_Expr_RegExp('component_id', '^' . $p->dbId . '[-_][^_]+$'))));
                 foreach ($this->_getPossibleIndirectDbIdShortcuts($p->componentClass) as $dbIdShortcut) {
                     $ors[] = new Kwf_Model_Select_Expr_StartsWith('component_id', $dbIdShortcut);
                 }
                 $s->where(new Kwf_Model_Select_Expr_Or($ors));
             }
             if ($subRoots = $select->getPart(Kwf_Component_Select::WHERE_SUBROOT)) {
                 foreach ($subRoots as $subRoot) {
                     $s->where(new Kwf_Model_Select_Expr_Like('component_id', $subRoot->dbId . '%'));
                 }
             }
             $data = $this->_getModel()->export(Kwf_Model_Abstract::FORMAT_ARRAY, $s, array('columns' => array('component_id')));
             $parentData = array();
             $s = new Kwf_Component_Select();
             $s->copyParts(array(Kwf_Component_Select::IGNORE_VISIBLE), $select);
             foreach ($data as $i) {
                 foreach (Kwf_Component_Data_Root::getInstance()->getComponentsByDbId($i['component_id'] . '-child', $s) as $d) {
                     if ($d->parent->componentClass == $this->_class) {
                         $ret[] = $d;
                     }
                 }
             }
         } else {
             throw new Kwf_Exception_NotYetImplemented();
         }
         return $ret;
     }
     $pData = $parentData;
     $parentDatas = is_array($parentData) ? $parentData : array($parentData);
     foreach ($this->_fetchKeys($pData, $select) as $key) {
         foreach ($parentDatas as $parentData) {
             $data = $this->_createData($parentData, $key, $select);
             if ($data) {
                 $ret[] = $data;
             }
         }
     }
     return $ret;
 }
Exemplo n.º 14
0
 public static function getComponentFile($class, $filename = '', $ext = 'php', $returnClass = false)
 {
     if (is_object($class)) {
         if ($class instanceof Kwf_Component_Abstract) {
             $class = get_class($class);
         } else {
             if ($class instanceof Kwf_Component_Data) {
                 $class = $class->componentClass;
             } else {
                 throw new Kwf_Exception("invalid class");
             }
         }
     }
     $files = Kwc_Abstract::getSetting($class, 'componentFiles');
     $key = false;
     //precomputed aus Kwf/Component/Abstract.php
     if ($ext == 'php' && $returnClass) {
         $key = $filename;
     } else {
         if ($ext == array('tpl', 'twig') && !$returnClass) {
             $key = $filename . '.tpl';
         }
     }
     if ($key) {
         Kwf_Benchmark::count('getComponentFile precomputed');
         if (isset($files[$key])) {
             return $files[$key];
         }
     }
     Kwf_Benchmark::count('getComponentFile slow');
     $ret = self::getComponentFiles($class, array(array('filename' => $filename, 'ext' => $ext, 'returnClass' => $returnClass)));
     return $ret[0];
 }
Exemplo n.º 15
0
 /**
  * @param $syncType Any of SYNC_ALWAYS, SYNC_AFTER_DELAY or SYNC_ONCE
  * @return array $ret An Array with keys
  *        'type'   => any of: SYNC_SELECT_TYPE_NOSYNC, SYNC_SELECT_TYPE_ALL, SYNC_SELECT_TYPE_SELECT
  *        'select' => the select object for syncing or null (depending on type)
  */
 protected function _getSynchronizeVars($syncType)
 {
     if ($this->_synchronizeDone) {
         if ($syncType !== self::SYNC_ALWAYS) {
             $this->_lockSync();
             //es wurde bereits synchronisiert
             return array('type' => self::SYNC_SELECT_TYPE_NOSYNC, 'select' => null);
         }
     }
     if ($this->_getMaxSyncDelay()) {
         if ($syncType === self::SYNC_AFTER_DELAY) {
             $lastSyncFile = $this->_getLastSyncFile();
             $lastSync = false;
             if (file_exists($lastSyncFile)) {
                 $lastSync = file_get_contents($lastSyncFile);
             }
             if ($lastSync && $lastSync + $this->_getMaxSyncDelay() > time()) {
                 $this->_lockSync();
                 //maxSyncDelay wurde noch nicht erreicht
                 return array('type' => self::SYNC_SELECT_TYPE_NOSYNC, 'select' => null);
             }
         }
     }
     $this->_synchronizeDone = true;
     //wegen endlosschleife schon hier
     Kwf_Benchmark::count('mirror sync');
     if (!$this->_syncTimeField) {
         // kein modified feld vorhanden, alle kopieren
         $ret = array('type' => self::SYNC_SELECT_TYPE_ALL, 'select' => null);
     } else {
         $syncField = $this->_syncTimeField;
         $proxyModel = $this->getProxyModel();
         $pr = $proxyModel->getRow($proxyModel->select()->order($syncField, 'DESC'));
         $cacheTimestamp = $pr ? $pr->{$syncField} : null;
         if ($cacheTimestamp && !preg_match('/^[0-9]{4,4}-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]$/', $cacheTimestamp)) {
             throw new Kwf_Exception("syncTimeField must be of type datetime (yyyy-mm-dd hh:mm:ss) when using mirror cache");
         }
         $sourceModel = $this->getSourceModel();
         if (!$cacheTimestamp) {
             // kein cache vorhanden, alle kopieren
             $ret = array('type' => self::SYNC_SELECT_TYPE_ALL, 'select' => null);
         } else {
             $select = $sourceModel->select();
             if ($sourceModel instanceof Kwf_Model_Service) {
                 if ($this->_syncTimeFieldIsUnique) {
                     $select->where(new Kwf_Model_Select_Expr_Higher($this->_syncTimeField, $cacheTimestamp));
                 } else {
                     $select->where(new Kwf_Model_Select_Expr_Or(array(new Kwf_Model_Select_Expr_Higher($this->_syncTimeField, $cacheTimestamp), new Kwf_Model_Select_Expr_Equals($this->_syncTimeField, $cacheTimestamp))));
                 }
             } else {
                 /**
                  * TODO: Sobald Service Kwf_DateTime versteht (>= KWF 1.11)
                  * oder höher ist, obige if-abfrage weg und nur das hier im else verwenden
                  */
                 if ($this->_syncTimeFieldIsUnique) {
                     $select->where(new Kwf_Model_Select_Expr_Higher($this->_syncTimeField, new Kwf_DateTime($cacheTimestamp)));
                 } else {
                     $select->where(new Kwf_Model_Select_Expr_Or(array(new Kwf_Model_Select_Expr_Higher($this->_syncTimeField, new Kwf_DateTime($cacheTimestamp)), new Kwf_Model_Select_Expr_Equal($this->_syncTimeField, new Kwf_DateTime($cacheTimestamp)))));
                 }
             }
             $ret = array('type' => self::SYNC_SELECT_TYPE_SELECT, 'select' => $select);
         }
     }
     if ($ret['type'] == self::SYNC_SELECT_TYPE_ALL) {
         //wenn alles importiert wird write lock bevor maxSyncFile geschrieben wird
         $this->_lockSync(true);
     } else {
         $this->_lockSync();
     }
     if ($this->_getMaxSyncDelay()) {
         //letzten sync zeitpunkt schreiben
         file_put_contents($this->_getLastSyncFile(), time());
     }
     return $ret;
 }
Exemplo n.º 16
0
 /**
  * Returns child components matching the given select
  *
  * @param array|Kwf_Component_Select
  * @return Kwf_Component_Data[]
  */
 public function getChildComponents($select = array())
 {
     $select = $this->_formatSelect($select);
     $sc = $select->getHash();
     if (isset($this->_childComponentsCache[$sc])) {
         Kwf_Benchmark::count('getChildComponents cached');
     } else {
         Kwf_Benchmark::count('getChildComponents uncached');
     }
     if (!isset($this->_childComponentsCache[$sc])) {
         $this->_childComponentsCache[$sc] = array();
         if ($select->getPart(Kwf_Component_Select::WHERE_COMPONENT_CLASSES) === array()) {
             return $this->_childComponentsCache[$sc];
             //vorzeitig abbrechen, da kommt sicher kein ergebnis
         }
         if ($select->hasPart(Kwf_Component_Select::LIMIT_COUNT)) {
             $limitCount = $select->getPart(Kwf_Component_Select::LIMIT_COUNT);
         } else {
             $limitCount = null;
         }
         $generators = Kwf_Component_Generator_Abstract::getOwnInstances($this, $select);
         $ret = $this->_getChildComponentsFromGenerators($generators, $select, $limitCount);
         if (is_null($limitCount) || count($ret) < $limitCount) {
             //wenn limit nicht erreicht, inherited generator auch noch fragen
             if (!is_null($limitCount)) {
                 $limitCount -= count($ret);
             }
             $generators = Kwf_Component_Generator_Abstract::getInheritedInstances($this, $select);
             $ret += $this->_getChildComponentsFromGenerators($generators, $select, $limitCount);
             //kein array_merge, da wuerden die keys verloren gehen - und die sind eh eindeutig
         }
         $this->_childComponentsCache[$sc] = $ret;
     }
     return $this->_childComponentsCache[$sc];
 }
Exemplo n.º 17
0
 private static function _getIndirectChildComponentClasses($class, $select, $cacheId)
 {
     static $ccc = array();
     $currentCacheId = 'iccc-' . md5($class . $cacheId);
     if (isset($ccc[$class . $cacheId])) {
         Kwf_Benchmark::count('iccc cache hit');
         return $ccc[$class . $cacheId];
     }
     $ret = Kwf_Cache_SimpleStatic::fetch($currentCacheId, $success);
     if ($success) {
         $ccc[$class . $cacheId] = $ret;
         Kwf_Benchmark::count('iccc cache semi-hit');
         return $ret;
     }
     Kwf_Benchmark::count('iccc cache miss', $class . ' ' . print_r($select->getParts(), true));
     $childConstraints = array('page' => false);
     $ccc[$class . $cacheId] = array();
     foreach (Kwc_Abstract::getChildComponentClasses($class, $childConstraints) as $childClass) {
         if (Kwc_Abstract::getChildComponentClasses($childClass, $select, $cacheId)) {
             $ccc[$class . $cacheId][] = $childClass;
             continue;
         }
         $classes = Kwc_Abstract::_getIndirectChildComponentClasses($childClass, $select, $cacheId);
         if ($classes) {
             $ccc[$class . $cacheId][] = $childClass;
         }
     }
     $ccc[$class . $cacheId] = array_unique(array_values($ccc[$class . $cacheId]));
     Kwf_Cache_SimpleStatic::add($currentCacheId, $ccc[$class . $cacheId]);
     return $ccc[$class . $cacheId];
 }
Exemplo n.º 18
0
 public function getChildData($parentDatas, $select = array())
 {
     Kwf_Benchmark::count('GenChained::getChildData');
     $ret = array();
     if (is_array($select)) {
         $select = new Kwf_Component_Select($select);
     }
     $chainedType = $this->getGeneratorFlag('chainedType');
     $slaveData = $select->getPart(Kwf_Component_Select::WHERE_CHILD_OF);
     while ($slaveData) {
         if (Kwc_Abstract::getFlag($slaveData->componentClass, 'chainedType') == $chainedType) {
             break;
         }
         $slaveData = $slaveData->parent;
     }
     $parentDataSelect = new Kwf_Component_Select();
     $parentDataSelect->copyParts(array('ignoreVisible'), $select);
     $parentDatas = is_array($parentDatas) ? $parentDatas : array($parentDatas);
     foreach ($parentDatas as $parentData) {
         foreach ($this->_getChainedChildComponents($parentData, $select) as $component) {
             $pData = array();
             if (!$parentData) {
                 if (!$slaveData) {
                     $pData = Kwc_Chained_Abstract_Component::getAllChainedByMaster($component->parent, $chainedType, $parentDataSelect);
                 } else {
                     $chainedComponent = Kwc_Chained_Abstract_Component::getChainedByMaster($component->parent, $slaveData, $chainedType, $parentDataSelect);
                     if ($chainedComponent) {
                         $pData = array($chainedComponent);
                     }
                 }
             } else {
                 $pData = array($parentData);
             }
             foreach ($pData as $d) {
                 $data = $this->_createData($d, $component, $select);
                 if ($data) {
                     $ret[] = $data;
                 }
             }
         }
     }
     return $ret;
 }
Exemplo n.º 19
0
 public function __construct($config)
 {
     $data['url'] = $config['url'];
     $encoding = false;
     if (substr($data['url'], 0, 7) == 'file://' || substr($data['url'], 0, 6) == 'php://') {
         $str = file_get_contents($data['url']);
     } else {
         $response = $config['model']->getHttpRequestor()->request($data['url']);
         if ($response->getStatusCode() != 200) {
             throw new Kwf_Exception("invalid status response from server: " . $response->getStatusCode() . " for '{$data['url']}'");
         }
         $str = $response->getBody();
     }
     if (!$str) {
         throw new Kwf_Exception("Can't load feed '{$data['url']}', response is empty");
     }
     $originalContent = $str;
     $str = trim($str);
     if (preg_match('#<?xml[^>]* encoding=["\']([^"\']*)["\']#', $str, $m)) {
         $encoding = trim(strtolower($m[1]));
         if ($encoding != 'utf8' && $encoding != 'utf-8') {
             try {
                 $str = iconv($encoding, 'utf-8', $str);
                 $str = preg_replace('#(<?xml[^>]* encoding=["\'])([^"\']*)(["\'])#', '\\1utf-8\\3', $str);
             } catch (Exception $e) {
             }
         }
     } else {
         if (isset($response)) {
             $ct = $response->getContentType();
             if ($ct) {
                 if (preg_match('#charset=([^;]*)#i', strtolower($ct), $m)) {
                     $encoding = trim($m[1]);
                     if ($encoding != 'utf8' && $encoding != 'utf-8') {
                         $str = iconv($encoding, 'utf-8', $str);
                     }
                 }
             }
         }
     }
     if (!$encoding) {
         try {
             $encoding = $config['model']->getDefaultEncoding();
             $str = iconv($encoding, 'utf-8', $str);
         } catch (Exception $e) {
         }
     }
     $entityLoaderWasDisabled = libxml_disable_entity_loader(true);
     $this->_xml = simplexml_load_string($str, 'SimpleXMLElement', LIBXML_NOERROR | LIBXML_NOWARNING);
     if (!$this->_xml) {
         //try with another encoding
         $this->_xml = simplexml_load_string(iconv('iso-8859-1', 'utf-8', $str), 'SimpleXMLElement', LIBXML_NOERROR | LIBXML_NOWARNING);
         if ($this->_xml) {
             $encoding = 'iso-8859-1';
         }
     }
     if (!$this->_xml) {
         if (class_exists('tidy')) {
             //maximum length; simply cut off, tidy will repair it properly
             $maxLength = 5 * 1024 * 1024;
             if (strlen($str) > $maxLength) {
                 $str = substr($str, 0, $maxLength);
             }
             $c = array('indent' => true, 'input-xml' => true, 'output-xml' => true, 'wrap' => '86', 'char-encoding' => 'utf8', 'newline' => 'LF');
             $tidy = new tidy();
             $tidy->parseString($str, $c, 'utf8');
             $tidy->cleanRepair();
             $str = $tidy->value;
             $str = preg_replace('#(<?xml[^>]* encoding=["\'])([^"\']*)(["\'])#', '\\1utf-8\\3', $str);
             $str = str_replace("", '', $str);
             $this->_xml = simplexml_load_string($str, 'SimpleXMLElement', LIBXML_NOERROR | LIBXML_NOWARNING);
         }
     }
     libxml_disable_entity_loader($entityLoaderWasDisabled);
     if (!$this->_xml) {
         throw new Kwf_Exception("Can't load feed: '{$data['url']}' " . $originalContent);
     }
     if ($this->_xml->channel) {
         $data['format'] = self::FORMAT_RSS;
     } else {
         if ($this->_xml->getName() == 'feed') {
             $data['format'] = self::FORMAT_ATOM;
         } else {
             throw new Kwf_Exception("Can't load feed '{$data['url']}', unknown format: " . $originalContent);
         }
     }
     $data['encoding'] = $encoding;
     $data['hub'] = null;
     if ($data['format'] == self::FORMAT_RSS) {
         $data['title'] = (string) $this->_xml->channel->title;
         $data['link'] = (string) $this->_xml->channel->link;
         $data['description'] = (string) $this->_xml->channel->description;
         //der hub ist im atom namespace in einem rss20 feed
         //also in diesen namespace wechseln
         foreach ($this->_xml->channel->children('http://www.w3.org/2005/Atom')->link as $link) {
             $link = $link->attributes('');
             //die attribute sind aber wida im default namespace, also wida rauswechseln
             if ($link['rel'] == 'hub') {
                 $data['hub'] = (string) $link['href'];
                 break;
             }
         }
     } else {
         $data['title'] = (string) $this->_xml->title;
         $data['link'] = null;
         foreach ($this->_xml->link as $link) {
             if (!$link['href']) {
                 continue;
             }
             if ($link['rel'] != 'self') {
                 $data['link'] = (string) $link['href'];
                 break;
             }
         }
         foreach ($this->_xml->link as $link) {
             if (!$link['href']) {
                 continue;
             }
             if ($link['rel'] == 'hub') {
                 $data['hub'] = (string) $link['href'];
                 break;
             }
         }
     }
     $config['data'] = $data;
     Kwf_Benchmark::count('loaded feed');
     parent::__construct($config);
 }
 /**
  * cache write - this is called when the script is about to finish, or when session_write_close() is called
  * data are written only when something has changed
  *
  * @param string $sessionId
  * @param string $data
  * @return bool
  */
 public function write($sessionId, $data)
 {
     $t = microtime(true);
     $expiration = $this->_lifeTime + time();
     //we store time of the db record expiration in the Memcache
     if ($this->_initSessionData !== $data) {
         Kwf_Registry::get('db')->query("REPLACE INTO kwf_sessions (sessionId, expiration, data) VALUES(?, ?, ?)", array($sessionId, $expiration, $data));
         $this->_memcacheSet($sessionId, $data);
     }
     Kwf_Benchmark::count('sessionhdl', 'read: ' . (microtime(true) - $t) * 1000 . 'ms');
     return true;
 }
Exemplo n.º 21
0
 protected function _getPageIds($parentData, $select)
 {
     if (!$parentData && ($p = $select->getPart(Kwf_Component_Select::WHERE_CHILD_OF))) {
         if ($p->getPage()) {
             $p = $p->getPage();
         }
         $parentData = $p;
     }
     $pageIds = array();
     if ($parentData && !$select->hasPart(Kwf_Component_Select::WHERE_ID)) {
         // diese Abfragen sind implizit recursive=true
         $parentId = $parentData->dbId;
         if ($select->getPart(Kwf_Component_Select::WHERE_HOME)) {
             $s = new Kwf_Model_Select();
             $s->whereEquals('is_home', true);
             $s->whereEquals('parent_subroot_id', $parentData->getSubroot()->dbId);
             //performance to look only in subroot - correct filterting done below
             Kwf_Benchmark::count('GenPage::query', 'home');
             $rows = $this->_getModel()->export(Kwf_Model_Interface::FORMAT_ARRAY, $s, array('columns' => array('id')));
             $homePages = array();
             foreach ($rows as $row) {
                 $homePages[] = $row['id'];
             }
             foreach ($homePages as $pageId) {
                 $pd = $this->_getPageData($pageId);
                 if (substr($pd['parent_id'], 0, strlen($parentId)) == $parentId) {
                     $pageIds[] = $pageId;
                     continue;
                 }
                 foreach ($pd['parent_ids'] as $pageParentId) {
                     if ($pageParentId == $parentId) {
                         $pageIds[] = $pageId;
                         break;
                     }
                 }
             }
         } else {
             if ($select->hasPart(Kwf_Component_Select::WHERE_FILENAME)) {
                 $filename = $select->getPart(Kwf_Component_Select::WHERE_FILENAME);
                 $cacheId = 'pcFnIds-' . $parentId . '-' . $filename;
                 $pageIds = Kwf_Cache_Simple::fetch($cacheId);
                 if ($pageIds === false) {
                     $s = new Kwf_Model_Select();
                     $s->whereEquals('filename', $filename);
                     if (is_numeric($parentId)) {
                         $s->whereEquals('parent_id', $parentId);
                     } else {
                         $s->where(new Kwf_Model_Select_Expr_Like('parent_id', $parentId . '%'));
                     }
                     Kwf_Benchmark::count('GenPage::query', 'filename');
                     $rows = $this->_getModel()->export(Kwf_Model_Interface::FORMAT_ARRAY, $s, array('columns' => array('id')));
                     $pageIds = array();
                     foreach ($rows as $row) {
                         $pageIds[] = $row['id'];
                     }
                     Kwf_Cache_Simple::add($cacheId, $pageIds);
                 }
             } else {
                 if ($select->hasPart(Kwf_Component_Select::WHERE_COMPONENT_CLASSES)) {
                     $selectClasses = $select->getPart(Kwf_Component_Select::WHERE_COMPONENT_CLASSES);
                     $keys = array();
                     foreach ($selectClasses as $selectClass) {
                         $key = array_search($selectClass, $this->_settings['component']);
                         if ($key) {
                             $keys[] = $key;
                         }
                     }
                     $s = new Kwf_Model_Select();
                     $s->whereEquals('component', array_unique($keys));
                     if (is_numeric($parentId)) {
                         $s->whereEquals('parent_id', $parentId);
                     } else {
                         $s->where(new Kwf_Model_Select_Expr_Like('parent_id', $parentId . '%'));
                     }
                     Kwf_Benchmark::count('GenPage::query', 'component');
                     $rows = $this->_getModel()->export(Kwf_Model_Interface::FORMAT_ARRAY, $s, array('columns' => array('id')));
                     foreach ($rows as $row) {
                         $pageIds[] = $row['id'];
                     }
                 } else {
                     $pageIds = $this->_getChildPageIds($parentId);
                 }
             }
         }
     } else {
         $pagesSelect = new Kwf_Model_Select();
         if ($id = $select->getPart(Kwf_Component_Select::WHERE_ID)) {
             //query only by id, no db query required
             $pageIds = array($id);
             if ($sr = $select->getPart(Kwf_Component_Select::WHERE_SUBROOT)) {
                 $pd = $this->_getPageData($id);
                 if ($pd['parent_subroot_id'] != $sr[0]->dbId) {
                     $pageIds = array();
                 }
             }
             if ($pageIds && $select->hasPart(Kwf_Component_Select::WHERE_COMPONENT_CLASSES)) {
                 $selectClasses = $select->getPart(Kwf_Component_Select::WHERE_COMPONENT_CLASSES);
                 $keys = array();
                 foreach ($selectClasses as $selectClass) {
                     $key = array_search($selectClass, $this->_settings['component']);
                     if ($key && !in_array($key, $keys)) {
                         $keys[] = $key;
                     }
                 }
                 $pd = $this->_getPageData($id);
                 if (!in_array($pd['component'], $keys)) {
                     $pageIds = array();
                 }
             }
             if ($pageIds && $select->getPart(Kwf_Component_Select::WHERE_HOME)) {
                 $pd = $this->_getPageData($id);
                 if (!$pd['is_home']) {
                     $pageIds = array();
                 }
             }
         } else {
             $benchmarkType = '';
             if ($select->hasPart(Kwf_Component_Select::WHERE_SUBROOT)) {
                 $subroot = $select->getPart(Kwf_Component_Select::WHERE_SUBROOT);
                 $subroot = $subroot[0];
                 $pagesSelect->whereEquals('parent_subroot_id', $subroot->dbId);
                 $benchmarkType .= 'subroot ';
             }
             if ($select->getPart(Kwf_Component_Select::WHERE_HOME)) {
                 $pagesSelect->whereEquals('is_home', true);
                 $benchmarkType .= 'home ';
             }
             if ($id = $select->getPart(Kwf_Component_Select::WHERE_ID)) {
                 $pagesSelect->whereEquals('id', $id);
                 $benchmarkType .= 'id ';
             }
             if ($select->hasPart(Kwf_Component_Select::WHERE_COMPONENT_CLASSES)) {
                 $selectClasses = $select->getPart(Kwf_Component_Select::WHERE_COMPONENT_CLASSES);
                 $keys = array();
                 foreach ($selectClasses as $selectClass) {
                     $key = array_search($selectClass, $this->_settings['component']);
                     if ($key && !in_array($key, $keys)) {
                         $keys[] = $key;
                     }
                 }
                 $pagesSelect->whereEquals('component', $keys);
                 $benchmarkType .= 'component ';
             }
             Kwf_Benchmark::count('GenPage::query', "noparent(" . trim($benchmarkType) . ")");
             $rows = $this->_getModel()->export(Kwf_Model_Interface::FORMAT_ARRAY, $pagesSelect, array('columns' => array('id')));
             $pageIds = array();
             foreach ($rows as $row) {
                 $pageIds[] = $row['id'];
             }
         }
         if ($parentData) {
             $parentId = $parentData->dbId;
             foreach ($pageIds as $k => $pageId) {
                 $match = false;
                 $pd = $this->_getPageData($pageId);
                 if (!$pd) {
                     continue;
                 }
                 if (substr($pd['parent_id'], 0, strlen($parentId)) == $parentId) {
                     $match = true;
                 }
                 if (!$match) {
                     foreach ($pd['parent_ids'] as $pageParentId) {
                         if ($pageParentId == $parentId) {
                             $match = true;
                             break;
                         }
                     }
                 }
                 if (!$match) {
                     unset($pageIds[$k]);
                 }
             }
         }
     }
     return $pageIds;
 }