public function saveAction() { $values = Zend_Json::decode($this->getParam("data")); // convert all special characters to their entities so the xml writer can put it into the file $values = array_htmlspecialchars($values); try { $sphinx_config = new SphinxSearch_Config(); $sphinx_config->writeSphinxConfig(); $plugin_config = new SphinxSearch_Config_Plugin(); $config_data = $plugin_config->getData(); $config_data["path"]["pid"] = $values["sphinxsearch.path_pid"]; $config_data["path"]["querylog"] = $values["sphinxsearch.path_querylog"]; $config_data["path"]["log"] = $values["sphinxsearch.path_logfile"]; $config_data["path"]["indexer"] = $values["sphinxsearch.path_indexer"]; $config_data["path"]["phpcli"] = $values["sphinxsearch.path_phpcli"]; $config_data["path"]["searchd"] = $values["sphinxsearch.path_searchd"]; $config_data["indexer"]["period"] = $values["sphinxsearch.indexer_period"]; $config_data["indexer"]["runwithmaintenance"] = $values["sphinxsearch.indexer_maintenance"] == "true" ? "true" : "false"; $config_data["indexer"]["onchange"] = $values["sphinxsearch.indexer_onchange"]; $config_data["documents"]["use_i18n"] = $values["sphinxsearch.documents_i18n"] == "true" ? "true" : "false"; $config_data["searchd"]["port"] = $values["sphinxsearch.searchd_port"]; $plugin_config->setData($config_data); $plugin_config->save(); $this->_helper->json(array("success" => true)); } catch (Exception $e) { $this->_helper->json(false); } }
/** * Returns an instance of SphinxSearch_Config_Plugin * * Singleton pattern implementation * * @return SphinxSearch_Config_Plugin */ public static function getInstance() { if (null === self::$_instance) { self::$_instance = new self(); } return self::$_instance; }
public function __construct($query) { $this->query = $query; $max_results = intval(SphinxSearch_Config_Plugin::getValue("results", "maxresults")); $this->limit = $max_results; $SphinxClient = new SphinxClient(); $this->SphinxClient = $SphinxClient; $SphinxClient->SetMatchMode(SPH_MATCH_EXTENDED2); $SphinxClient->SetSortMode(SPH_SORT_EXTENDED, "@weight DESC"); $SphinxClient->setServer("localhost", SphinxSearch_Config_Plugin::getValue("searchd", "port")); // Sphinx Client is to always return everything - it's just IDs // Paginator is then to cast the necessary Items, this can be done // with offset/limit $SphinxClient->setLimits(0, $max_results, $max_results); }
public static function queryDocument($query, $params = array()) { if (trim($query) == "") { return array(); } $sphinx_config = SphinxSearch_Config::getInstance(); $documents_config = $sphinx_config->getDocumentsAsArray(); $SphinxClient = new SphinxClient(); $entries = array(); $language = "all"; if (SphinxSearch_Config_Plugin::getValue("documents", "use_i18n") == "true") { if (array_key_exists("language", $params)) { $language = $params["language"]; } else { $locale = Zend_Registry::get("Zend_Locale"); $language = $locale->getLanguage(); } } $field_weights = array(); $indexes = array(); foreach ($documents_config as $document_name => $document_properties) { $indexes[] = "idx_document_" . $document_name . "_" . $language; foreach ($document_properties["elements"] as $field_name => $field_config) { if (array_key_exists("weight", $field_config) && intval($field_config["weight"]) > 0) { $field_weights[$field_name] = intval($field_config["weight"]); } } } if (sizeof($field_weights) > 0) { $SphinxClient->setFieldWeights($field_weights); } $search_result = $SphinxClient->Query($query, implode(", ", $indexes)); if ($search_result === false) { throw new Exception($SphinxClient->GetLastError()); } if ($search_result["total_found"] > 0) { foreach ($search_result["matches"] as $id => $meta) { $entries[] = array("result" => Document::getById($id), "id" => $id, "meta" => $meta, "type" => "document"); } } return $entries; }
public static function runIndexer($index_name = null) { if (is_null($index_name)) { $index_name = "--all"; } $lockfile = SPHINX_VAR . DIRECTORY_SEPARATOR . "lock.txt"; $output = array(); try { $indexer = SphinxSearch_Config_Plugin::getValue("path", "indexer"); } catch (Exception $e) { $indexer = "/usr/bin/indexer"; } if (!(is_file($indexer) && is_executable($indexer))) { $message = "SphinxSearch Indexer could not be executed at " . $indexer; logger::err($message); $output[] = $message; $return_var = 1; } else { $fp = @fopen($lockfile, "a+"); if ($fp === false) { $message = "SphinxSearch Indexer could open lockfile " . $lockfile; logger::err($message); $output[] = $message; $return_var = 1; } else { if (flock($fp, LOCK_EX | LOCK_NB)) { logger::debug("SphinxSearch Indexer locked with " . $lockfile); ftruncate($fp, 0); fwrite($fp, getmypid()); fflush($fp); $return_var = null; exec("{$indexer} --config " . SPHINX_VAR . DIRECTORY_SEPARATOR . "sphinx.conf " . $index_name . (self::isSearchdRunning() ? " --rotate " : ""), $output, $return_var); if ($return_var == 0) { SphinxSearch_Config_Plugin::setValue("indexer", "lastrun", time()); } flock($fp, LOCK_UN); // release the lock logger::debug("SphinxSearch Indexer unlocked" . $lockfile); } else { $message = "SphinxSearch Indexer is not executed: locked with " . $lockfile; logger::err($message); $output[] = $message; $return_var = 1; } fclose($fp); } } return array("output" => implode("\n", $output), "return_var" => $return_var); }
public function writeSphinxConfig() { $pimcore_config = Pimcore_Config::getSystemConfig(); $params = $pimcore_config->database->params; $db_host = $params->host; $db_user = $params->username; $db_password = $params->password; $db_name = $params->dbname; $db_port = $params->port; $index_path = SPHINX_VAR . DIRECTORY_SEPARATOR . "index"; $cli_path = PIMCORE_PLUGINS_PATH . DIRECTORY_SEPARATOR . "SphinxSearch" . DIRECTORY_SEPARATOR . "cli"; $pid_path = SphinxSearch_Config_Plugin::getValue("path", "pid"); $logfile_path = SphinxSearch_Config_Plugin::getValue("path", "log"); $querylog_path = SphinxSearch_Config_Plugin::getValue("path", "querylog"); $port = SphinxSearch_Config_Plugin::getValue("searchd", "port"); if (substr($pid_path, 0, 1) != DIRECTORY_SEPARATOR) { $pid_path = PIMCORE_DOCUMENT_ROOT . DIRECTORY_SEPARATOR . $pid_path; } if (substr($logfile_path, 0, 1) != DIRECTORY_SEPARATOR) { $logfile_path = PIMCORE_DOCUMENT_ROOT . DIRECTORY_SEPARATOR . $logfile_path; } if (substr($querylog_path, 0, 1) != DIRECTORY_SEPARATOR) { $querylog_path = PIMCORE_DOCUMENT_ROOT . DIRECTORY_SEPARATOR . $querylog_path; } $config = <<<EOL indexer { mem_limit = 32M } searchd { port = {$port} log = {$logfile_path} query_log = {$querylog_path} read_timeout = 5 max_children = 30 pid_file = {$pid_path} max_matches = 1000 seamless_rotate = 1 preopen_indexes = 0 unlink_old = 1 } EOL; $indexer = SphinxSearch_Config_Plugin::getValue("path", "phpcli") . " " . $cli_path . DIRECTORY_SEPARATOR . "index_documents.php"; $documents_config = $this->getDocumentsAsArray(); $languages = array("all"); if (SphinxSearch_Config_Plugin::getValue("documents", "use_i18n") == "true") { $languages = Pimcore_Tool::getValidLanguages(); } foreach ($languages as $lang) { foreach ($documents_config as $document_name => $document_properties) { $source_name = "document_" . $document_name . "_" . $lang; $index_name = "idx_document_" . $document_name . "_" . $lang; $document_index_path = $index_path . DIRECTORY_SEPARATOR . "document_" . $document_name . "_" . $lang; $config .= <<<EOL source {$source_name} { type = xmlpipe2 xmlpipe_command = {$indexer} -d {$document_name} -l {$lang} } index {$index_name} { source = {$source_name} path = {$document_index_path} charset_type = utf-8 } EOL; } } foreach ($this->getClasses()->children() as $class) { /** * @var $class SimpleXMLElement */ $class_name = $class->getName(); $object_class = Object_Class::getByName($class_name); $fields = array("oo_id", "o_creationDate", "o_modificationDate", "o_published", "o_type"); $attributes = array("o_creationDate" => "sql_attr_timestamp", "o_modificationDate" => "sql_attr_timestamp", "o_published" => "sql_field_string", "o_type" => "sql_field_string"); foreach ($class->children() as $field) { $fields[] = $field->attributes()->name; if ($field->attributes()->field_type) { $attributes[$field->attributes()->name->__toString()] = $field->attributes()->field_type; } } $fields = "`" . implode("`,`", $fields) . "`"; $attributes_definition = ""; foreach ($attributes as $key => $value) { $attributes_definition .= " {$value} = {$key}\n"; // Yes, really $value first } // Do we have localized fields? if ($object_class->getFieldDefinition("localizedfields")) { $pimcore_languages = Pimcore_Tool::getValidLanguages(); foreach ($pimcore_languages as $lang) { $source_class_name = $class_name . "_" . $lang; $table = "object_localized_" . $object_class->getId() . "_" . $lang; $config .= <<<EOL source {$source_class_name} { type = mysql sql_host = {$db_host} sql_user = {$db_user} sql_pass = {$db_password} sql_db = {$db_name} sql_port = {$db_port} # optional, default is 3306 sql_query = SELECT {$fields} FROM {$table} sql_query_info = SELECT oo_id FROM {$table} WHERE oo_id=\$id {$attributes_definition} } index idx_{$source_class_name} { source = {$source_class_name} path = {$index_path}/{$source_class_name} docinfo = extern charset_type = utf-8 min_word_len = 1 min_prefix_len = 0 min_infix_len = 1 } EOL; } } else { $table = "object_" . $object_class->getId(); $document_index_path = $index_path . DIRECTORY_SEPARATOR . $class_name; $config .= <<<EOL source {$class_name} { type = mysql sql_host = {$db_host} sql_user = {$db_user} sql_pass = {$db_password} sql_db = {$db_name} sql_port = {$db_port} # optional, default is 3306 sql_query = SELECT {$fields} FROM {$table} sql_query_info = SELECT oo_id FROM {$table} WHERE oo_id=\$id {$attributes_definition} } index idx_{$class_name} { source = {$class_name} path = {$document_index_path} docinfo = extern charset_type = utf-8 min_word_len = 1 min_prefix_len = 0 min_infix_len = 1 } EOL; } } if (is_dir(SPHINX_VAR . DIRECTORY_SEPARATOR . "sphinx.conf.d")) { foreach (glob(SPHINX_VAR . DIRECTORY_SEPARATOR . "sphinx.conf.d/*.conf") as $filename) { $config .= file_get_contents($filename); } } file_put_contents(SPHINX_VAR . DIRECTORY_SEPARATOR . "sphinx.conf", $config); }