/** * Rebuild shopware search index * Loop through all tables / columns that should be considered in search * Write keywords to desired table * @throws Enlight_Exception */ public function buildSearchIndex() { // Set time of last cache rebuild $sql = ' SET @parent = (SELECT id FROM s_core_config_elements WHERE name = \'fuzzysearchlastupdate\'); DELETE FROM `s_core_config_values` WHERE element_id = @parent; INSERT INTO `s_core_config_values` (`element_id`, `shop_id`, `value`) VALUES (@parent, 1, CONCAT(\'s:\', LENGTH(NOW()), \':"\', NOW(), \'";\')); '; $this->database->exec($sql); // Truncate search index table $this->database->query('TRUNCATE TABLE `s_search_index`'); // Get a list of all tables and columns in this tables that should be processed by search /** * Example return: * tableID | table | where | referenz_table | fieldIDs | fields | foreign_key * 1 | s_articles | NULL | NULL | 3,4 | name, keywords | NULL * 2 | s_categories | NULL | s_articles_categories | 1,2 | metakeywords, description | categoryID */ $tables = $this->getSearchInvolvedTables(); if (!empty($tables)) { foreach ($tables as $table) { // Set primary key $table['elementID'] = empty($table['foreign_key']) && $table['table'] != 's_articles' ? 'articleID' : 'id'; // Build sql query to fetch values from this table $sql = 'SELECT ' . $table['elementID'] . ' as id, ' . $table['fields'] . ' FROM ' . $table['table']; // If any where condition is set, add to query if (!empty($table['where'])) $sql .= 'WHERE ' . $table['where']; // Get all fields & values from current table $getTableKeywords = $this->database->fetchAll($sql); // If no result, return if (empty($getTableKeywords)) { continue; } // Build array from columns fieldIDs and fields $fields = array_combine(explode(', ', $table["fieldIDs"]), explode(', ', $table["fields"])); $keywords = array(); $sql_index = array(); // Go through every row of result foreach ($getTableKeywords as $currentRow => $row) { // Go through every column of result foreach ($fields as $fieldID => $field) { // Split string from column into keywords $field_keywords = $this->getKeywordsFromString($row[$field]); if (empty($field_keywords)) { continue; } foreach ($field_keywords as &$keyword) { $keyword = $this->database->quote($keyword); $keywords[] = $keyword; } // SQL-queries to fill s_search_index $sql_index[] = 'SELECT sk.id as keywordID, ' . $row['id'] . ' as elementID, ' . $fieldID . ' as fieldID ' . 'FROM s_search_keywords sk ' . 'WHERE sk.keyword IN (' . implode(', ', $field_keywords) . ')'; } // If no new keywords were found, proceed with next table if (empty($keywords)) { continue; } // If last row or more then 5000 keywords fetched, write results to index if ($currentRow == count($getTableKeywords) - 1 || count($keywords) > 5000) { $keywords = array_unique($keywords); // Remove duplicates $sql_keywords = 'INSERT IGNORE INTO `s_search_keywords` (`keyword`) VALUES'; $sql_keywords .= ' (' . implode('), (', $keywords) . ')'; // Insert Keywords try { $this->database->query($sql_keywords); } catch (PDOException $e) { throw new Enlight_Exception($e->getMessage()); } $keywords = array(); // Update index try { $sql_index = implode("\n\nUNION ALL\n\n", $sql_index); $sql_index = "INSERT DELAYED IGNORE INTO s_search_index (keywordID, elementID, fieldID)\n\n" . $sql_index; $this->database->query($sql_index); $sql_index = array(); } catch (PDOException $e) { throw new Enlight_Exception($e->getMessage()); } } } } } // Cleanup index from invalid entries $this->cleanupIndex(); // Cleanup keywords from invalid entries $this->cleanupKeywords(); }
/** * Constructor */ protected function __construct($blnWithDbh = true, Zend_Config_Xml &$sysConfig, Zend_Config_Xml &$modConfig, Zend_Config_Xml &$webConfig) { /** * set sys config object */ $this->sysConfig = $sysConfig; /** * set modules config object */ $this->modConfig = $modConfig; /** * set website config object */ $this->webConfig = $webConfig; /** * initialize Zend_Log */ $this->logger = new Zend_Log(); /** * create logfile extension for file writer */ if (isset($_SESSION["sesUserName"]) && isset($_SERVER['REMOTE_ADDR'])) { $strLogFileExtension = $_SESSION["sesUserName"] . '_' . $_SERVER['REMOTE_ADDR']; } else { if (isset($_SERVER['REMOTE_ADDR'])) { $strLogFileExtension = $_SERVER['REMOTE_ADDR']; } else { $strLogFileExtension = 'local'; } } /** * create log file writer */ $writer = new Zend_Log_Writer_Stream(GLOBAL_ROOT_PATH . $this->sysConfig->logger->path . 'log_' . date('Ymd') . '_' . $strLogFileExtension . '.log'); $this->logger->addWriter($writer); /** * set log priority */ $filter = new Zend_Log_Filter_Priority((int) $this->sysConfig->logger->priority); $this->logger->addFilter($filter); if ($blnWithDbh == true) { /** * initialize the ZEND DB Connection * do lazy connection binding, so db connection will be established on first use with dbh->getConnection() */ try { $pdoParams = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true); $dbhParameters = array('host' => $this->sysConfig->database->params->host, 'username' => $this->sysConfig->database->params->username, 'password' => $this->sysConfig->database->params->password, 'dbname' => $this->sysConfig->database->params->dbname, 'driver_options' => $pdoParams); $this->dbh = Zend_Db::factory($this->sysConfig->database->adapter, $dbhParameters); $this->dbh->getConnection(); $this->dbh->exec('SET CHARACTER SET ' . $this->sysConfig->encoding->db); Zend_Db_Table::setDefaultAdapter($this->dbh); } catch (Zend_Db_Adapter_Exception $exc) { $this->logger->err($exc); header('Location: http://' . $this->sysConfig->hostname); die; } catch (Zend_Exception $exc) { $this->logger->err($exc); header('Location: http://' . $this->sysConfig->hostname); die; } //$this->initGuiTexts(); } }
/** * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. */ protected function tearDown() { $this->db->exec('DROP TABLE IF EXISTS s_crontab '); }
/** * Constructor */ protected function __construct($blnWithDbh = true, Zend_Config_Xml &$sysConfig, Zend_Config_Xml &$zooConfig, Zend_Config_Xml &$config) { /** * set sys config object */ $this->sysConfig = $sysConfig; /** * set modules config object */ $this->zooConfig = $zooConfig; /** * set website config object */ $this->config = $config; /** * initialize Zend_Log */ $this->logger = new Zend_Log(); /** * initialize Zend_Session_Namespace */ $this->objCoreSession = new Zend_Session_Namespace('Core'); /** * create logfile extension for file writer */ $strLogFileExtension = ''; if ($this->sysConfig->logger->priority > Zend_Log::ERR) { if (isset($_SESSION["sesUserName"]) && isset($_SERVER['REMOTE_ADDR'])) { $strLogFileExtension = '_' . $_SESSION["sesUserName"] . '_' . $_SERVER['REMOTE_ADDR']; } else { if (isset($_SERVER['REMOTE_ADDR'])) { $strLogFileExtension = '_' . $_SERVER['REMOTE_ADDR']; } else { $strLogFileExtension = '_local'; } } } /** * create log file writer */ $writer = new Zend_Log_Writer_Stream(GLOBAL_ROOT_PATH . $this->sysConfig->logger->path . 'log_' . date('Ymd') . $strLogFileExtension . '.log'); $this->logger->addWriter($writer); /** * set log priority */ $filter = new Zend_Log_Filter_Priority((int) $this->sysConfig->logger->priority); $this->logger->addFilter($filter); /** * get language and set translate object */ $this->logger->info('get language from ... '); if (isset($_GET['language'])) { $this->logger->info('GET'); $this->strLanguageCode = trim($_GET['language'], '/'); foreach ($this->config->languages->language->toArray() as $arrLanguage) { if (array_key_exists('code', $arrLanguage) && $arrLanguage['code'] == strtolower($this->strLanguageCode)) { $this->intLanguageId = $arrLanguage['id']; break; } } if ($this->intLanguageId == null) { if (isset($this->objCoreSession->languageId)) { $this->logger->info('SESSION'); $this->intLanguageId = $this->objCoreSession->languageId; $this->strLanguageCode = $this->objCoreSession->languageCode; } else { $this->logger->info('DEFAULT'); $this->blnIsDefaultLanguage = true; $this->intLanguageId = $this->sysConfig->languages->default->id; $this->strLanguageCode = $this->sysConfig->languages->default->code; } } } else { if (isset($_SERVER['REQUEST_URI']) && preg_match('/^\\/[a-zA-Z\\-]{2,5}\\//', $_SERVER['REQUEST_URI'])) { $this->logger->info('URI'); preg_match('/^\\/[a-zA-Z\\-]{2,5}\\//', $_SERVER['REQUEST_URI'], $arrMatches); $this->strLanguageCode = trim($arrMatches[0], '/'); foreach ($this->config->languages->language->toArray() as $arrLanguage) { if (array_key_exists('code', $arrLanguage) && $arrLanguage['code'] == strtolower($this->strLanguageCode)) { $this->intLanguageId = $arrLanguage['id']; break; } } if ($this->intLanguageId == null) { if (isset($this->objCoreSession->languageId)) { $this->logger->info('SESSION'); $this->intLanguageId = $this->objCoreSession->languageId; $this->strLanguageCode = $this->objCoreSession->languageCode; } else { $this->logger->info('DEFAULT'); $this->blnIsDefaultLanguage = true; $this->intLanguageId = $this->sysConfig->languages->default->id; $this->strLanguageCode = $this->sysConfig->languages->default->code; } } } else { if (isset($this->objCoreSession->languageId)) { $this->logger->info('SESSION'); $this->intLanguageId = $this->objCoreSession->languageId; $this->strLanguageCode = $this->objCoreSession->languageCode; } else { $this->logger->info('DEFAULT'); $this->blnIsDefaultLanguage = true; $this->intLanguageId = $this->sysConfig->languages->default->id; $this->strLanguageCode = $this->sysConfig->languages->default->code; } } } /** * set up zoolu translate obj */ $this->intZooluLanguageId = Zend_Auth::getInstance()->hasIdentity() ? Zend_Auth::getInstance()->getIdentity()->languageId : $this->intLanguageId; $this->strZooluLanguageCode = Zend_Auth::getInstance()->hasIdentity() ? Zend_Auth::getInstance()->getIdentity()->languageCode : $this->strLanguageCode; if (file_exists(GLOBAL_ROOT_PATH . 'application/zoolu/language/zoolu-' . $this->strZooluLanguageCode . '.mo')) { $this->translate = new HtmlTranslate('gettext', GLOBAL_ROOT_PATH . 'application/zoolu/language/zoolu-' . $this->strZooluLanguageCode . '.mo'); } else { $this->translate = new HtmlTranslate('gettext', GLOBAL_ROOT_PATH . 'application/zoolu/language/zoolu-' . $this->zooConfig->languages->default->code . '.mo'); } // update session language $this->updateSessionLanguage(); if ($blnWithDbh == true) { /** * initialize the ZEND DB Connection * do lazy connection binding, so db connection will be established on first use with dbh->getConnection() */ try { $pdoParams = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true); $dbhParameters = array('host' => $this->sysConfig->database->params->host, 'username' => $this->sysConfig->database->params->username, 'password' => $this->sysConfig->database->params->password, 'dbname' => $this->sysConfig->database->params->dbname, 'driver_options' => $pdoParams); $this->dbh = Zend_Db::factory($this->sysConfig->database->adapter, $dbhParameters); if ($this->sysConfig->logger->priority == Zend_Log::DEBUG) { $this->dbh->getProfiler()->setEnabled(true); } $this->dbh->getConnection(); $this->dbh->exec('SET CHARACTER SET ' . $this->sysConfig->encoding->db); Zend_Db_Table::setDefaultAdapter($this->dbh); /** * using a default metadata cache for all table objects * * set up the cache */ $arrFrontendOptions = array('automatic_serialization' => true); /** * memcache server configuration */ $arrServer = array('host' => Zend_Cache_Backend_Memcached::DEFAULT_HOST, 'port' => Zend_Cache_Backend_Memcached::DEFAULT_PORT, 'persistent' => Zend_Cache_Backend_Memcached::DEFAULT_PERSISTENT); $arrBackendOptions = array('cache_dir' => GLOBAL_ROOT_PATH . $this->sysConfig->path->cache->tables); $objCache = Zend_Cache::factory('Core', 'File', $arrFrontendOptions, $arrBackendOptions); /** * set the cache to be used with all table objects */ Zend_Db_Table_Abstract::setDefaultMetadataCache($objCache); } catch (Zend_Db_Adapter_Exception $exc) { $this->logger->err($exc); header('Location: http://' . $this->sysConfig->hostname); die; } catch (Zend_Exception $exc) { $this->logger->err($exc); header('Location: http://' . $this->sysConfig->hostname); die; } } }