mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-19 07:53:14 +00:00
[SearchEngine] Finalize search engine refactor
This commit is contained in:
@@ -17,16 +17,16 @@ use Alchemy\Phrasea\SearchEngine\SearchEngineOptions;
|
|||||||
use Alchemy\Phrasea\SearchEngine\SearchEngineResult;
|
use Alchemy\Phrasea\SearchEngine\SearchEngineResult;
|
||||||
use Alchemy\Phrasea\Exception\RuntimeException;
|
use Alchemy\Phrasea\Exception\RuntimeException;
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
|
||||||
|
|
||||||
class PhraseaEngine implements SearchEngineInterface
|
class PhraseaEngine implements SearchEngineInterface
|
||||||
{
|
{
|
||||||
private static $initialized = false;
|
private $initialized;
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @var SearchEngineOptions
|
* @var SearchEngineOptions
|
||||||
*/
|
*/
|
||||||
private $options;
|
private $options;
|
||||||
|
private $app;
|
||||||
private $queries = array();
|
private $queries = array();
|
||||||
private $arrayq = array();
|
private $arrayq = array();
|
||||||
private $colls = array();
|
private $colls = array();
|
||||||
@@ -44,6 +44,39 @@ class PhraseaEngine implements SearchEngineInterface
|
|||||||
$this->options = new SearchEngineOptions();
|
$this->options = new SearchEngineOptions();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function initialize()
|
||||||
|
{
|
||||||
|
if ($this->initialized) {
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
$choosenConnexion = $this->app['phraseanet.configuration']->getPhraseanet()->get('database');
|
||||||
|
|
||||||
|
$connexion = $this->app['phraseanet.configuration']->getConnexion($choosenConnexion);
|
||||||
|
|
||||||
|
$hostname = $connexion->get('host');
|
||||||
|
$port = (int) $connexion->get('port');
|
||||||
|
$user = $connexion->get('user');
|
||||||
|
$password = $connexion->get('password');
|
||||||
|
$dbname = $connexion->get('dbname');
|
||||||
|
|
||||||
|
if (!extension_loaded('phrasea2')) {
|
||||||
|
throw new RuntimeException('Phrasea extension is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!function_exists('phrasea_conn')) {
|
||||||
|
throw new RuntimeException('Phrasea extension requires upgrade');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (phrasea_conn($hostname, $port, $user, $password, $dbname) !== true) {
|
||||||
|
throw new RuntimeException('Unable to initialize Phrasea connection');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->initialized = true;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
private function checkSession()
|
private function checkSession()
|
||||||
{
|
{
|
||||||
if (!$this->app['phraseanet.user']) {
|
if (!$this->app['phraseanet.user']) {
|
||||||
@@ -58,16 +91,6 @@ class PhraseaEngine implements SearchEngineInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function initialize()
|
|
||||||
{
|
|
||||||
if(!self::$initialized) {
|
|
||||||
|
|
||||||
\phrasea::start($this->app['phraseanet.configuration']);
|
|
||||||
|
|
||||||
self::$initialized = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
@@ -90,7 +113,6 @@ class PhraseaEngine implements SearchEngineInterface
|
|||||||
return $this->configurationPanel;
|
return $this->configurationPanel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
@@ -462,7 +484,7 @@ class PhraseaEngine implements SearchEngineInterface
|
|||||||
if ($status) {
|
if ($status) {
|
||||||
$requestStat = 'xxxx';
|
$requestStat = 'xxxx';
|
||||||
|
|
||||||
for ($i = 4; ($i <= 64); $i ++ ) {
|
for ($i = 4; ($i <= 32); $i++) {
|
||||||
if (!isset($status[$i])) {
|
if (!isset($status[$i])) {
|
||||||
$requestStat = 'x' . $requestStat;
|
$requestStat = 'x' . $requestStat;
|
||||||
continue;
|
continue;
|
||||||
@@ -485,7 +507,9 @@ class PhraseaEngine implements SearchEngineInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($this->options->fields()) {
|
if ($this->options->fields()) {
|
||||||
$this->queries[$sbas] .= ' IN (' . implode(' OR ', array_map(function(\databox_field $field){return $field->get_name();},$this->options->fields())) . ')';
|
$this->queries[$sbas] .= ' IN (' . implode(' OR ', array_map(function(\databox_field $field) {
|
||||||
|
return $field->get_name();
|
||||||
|
}, $this->options->fields())) . ')';
|
||||||
}
|
}
|
||||||
if (($this->options->getMinDate() || $this->options->getMaxDate()) && $this->options->getDateFields()) {
|
if (($this->options->getMinDate() || $this->options->getMaxDate()) && $this->options->getDateFields()) {
|
||||||
if ($this->options->getMinDate()) {
|
if ($this->options->getMinDate()) {
|
||||||
@@ -562,5 +586,39 @@ class PhraseaEngine implements SearchEngineInterface
|
|||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
public function clearCache()
|
||||||
|
{
|
||||||
|
if ($this->app['session']->has('phrasea_session_id')) {
|
||||||
|
$this->initialize();
|
||||||
|
phrasea_close_session($this->app['session']->get('phrasea_session_id'));
|
||||||
|
$this->app['session']->remove('phrasea_session_id');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
public function clearAllCache(\DateTime $date = null)
|
||||||
|
{
|
||||||
|
if (!$date) {
|
||||||
|
$date = new \DateTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
$sql = "SELECT session_id FROM cache WHERE lastaccess <= :date";
|
||||||
|
|
||||||
|
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
|
||||||
|
$stmt->execute(array(':date' => $date->format(DATE_ISO8601)));
|
||||||
|
$rs = $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||||
|
$stmt->closeCursor();
|
||||||
|
|
||||||
|
foreach ($rs as $row) {
|
||||||
|
phrasea_close_session($row['session_id']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -808,5 +808,21 @@ class SphinxSearchEngine implements SearchEngineInterface
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
public function clearCache()
|
||||||
|
{
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
public function clearAllCache(\DateTime $date = null)
|
||||||
|
{
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -145,6 +145,10 @@ class SphinxSearchEngineTest extends SearchEngineAbstractTest
|
|||||||
$appbox = self::$DI['app']['phraseanet.appbox'];
|
$appbox = self::$DI['app']['phraseanet.appbox'];
|
||||||
self::$searchEngine->buildSuggestions($appbox->get_databoxes(), self::$config, 0);
|
self::$searchEngine->buildSuggestions($appbox->get_databoxes(), self::$config, 0);
|
||||||
|
|
||||||
|
$process = new Process($indexer . ' --all --rotate -c ' . self::$config);
|
||||||
|
$process->run();
|
||||||
|
usleep(500000);
|
||||||
|
|
||||||
$suggestions = self::$searchEngine->autoComplete('jean');
|
$suggestions = self::$searchEngine->autoComplete('jean');
|
||||||
$this->assertInstanceOf('\\Doctrine\\Common\\Collections\\ArrayCollection', $suggestions);
|
$this->assertInstanceOf('\\Doctrine\\Common\\Collections\\ArrayCollection', $suggestions);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user