public function onCollectionCreate(CollectionCreateEvent $event)
 {
     $sql = 'SELECT u.usr_id, c.session_id
             FROM (usr u, Sessions s, basusr b)
             LEFT JOIN cache c ON (c.usr_id = u.usr_id)
             WHERE u.model_of = 0 AND u.usr_login NOT LIKE "(#deleted%"
               AND b.base_id = :base_id AND b.usr_id = u.usr_id AND b.actif=1
               AND s.usr_id = u.usr_id';
     $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
     $stmt->execute([':base_id' => $event->getCollection()->get_base_id()]);
     $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
     $stmt->closeCursor();
     $initialized = false;
     foreach ($rows as $row) {
         $user = $this->app['manipulator.user']->getRepository()->find($row['usr_id']);
         $this->app['acl']->get($user)->inject_rights();
         if (null !== $row['session_id']) {
             if (!$initialized) {
                 $this->app['phraseanet.SE']->initialize();
                 $initialized = true;
             }
             phrasea_clear_cache($row['session_id']);
             phrasea_close_session($row['session_id']);
         }
     }
 }
Exemple #2
0
 /**
  * {@inheritdoc}
  */
 public function query($query, $offset, $perPage, SearchEngineOptions $options = null)
 {
     if (null === $options) {
         $options = new SearchEngineOptions();
     }
     $this->initialize();
     $this->checkSession();
     $this->clearAllCache(new \DateTime('-1 hour'));
     assert(is_int($offset));
     assert($offset >= 0);
     assert(is_int($perPage));
     if (trim($query) === '') {
         $query = "all";
     }
     if ($options->getRecordType()) {
         $query .= ' AND recordtype=' . $options->getRecordType();
     }
     $sql = 'SELECT query, query_time, duration, total FROM cache WHERE session_id = :ses_id';
     $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
     $stmt->execute([':ses_id' => $this->app['session']->get('phrasea_session_id')]);
     $row = $stmt->fetch(\PDO::FETCH_ASSOC);
     $stmt->closeCursor();
     $date_obj = new \DateTime('-10 min');
     $date_quest = new \DateTime($row['query_time']);
     if ($query != $row['query']) {
         $this->resetCacheNextQuery = true;
     }
     if ($date_obj > $date_quest) {
         $this->resetCacheNextQuery = true;
     }
     if ($this->resetCacheNextQuery === true) {
         phrasea_clear_cache($this->app['session']->get('phrasea_session_id'));
         $this->addQuery($query, $options);
         $this->executeQuery($query, $options);
         $sql = 'SELECT query, query_time, duration, total FROM cache WHERE session_id = :ses_id';
         $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
         $stmt->execute([':ses_id' => $this->app['session']->get('phrasea_session_id')]);
         $row = $stmt->fetch(\PDO::FETCH_ASSOC);
         $stmt->closeCursor();
     } else {
         /**
          * @todo clean this in DB
          */
         $this->total_available = $this->total_results = $this->app['session']->get('phrasea_engine_n_results');
     }
     $res = phrasea_fetch_results($this->app['session']->get('phrasea_session_id'), $offset + 1, $perPage, false);
     $rs = [];
     $error = $this->app->trans('Unable to execute query');
     if (isset($res['results']) && is_array($res['results'])) {
         $rs = $res['results'];
         $error = '';
     }
     $resultNumber = $offset;
     $records = new ArrayCollection();
     foreach ($rs as $data) {
         try {
             $records->add(new \record_adapter($this->app, \phrasea::sbasFromBas($this->app, $data['base_id']), $data['record_id'], $resultNumber));
         } catch (\Exception $e) {
         }
         $resultNumber++;
     }
     $propositions = $this->getPropositions();
     $suggestions = $this->getSuggestions($query);
     return new SearchEngineResult($records, $query, $row['duration'], $offset, $row['total'], $row['total'], $error, '', $suggestions, $propositions, '');
 }
 protected function doExecute(InputInterface $input, OutputInterface $output)
 {
     if (!extension_loaded('phrasea2')) {
         $output->writeln("<error>Missing Extension php-phrasea.</error>");
         return 1;
     }
     $usrId = $input->getArgument('usr_id');
     if (null === ($TestUser = $this->container['repo.users']->find($usrId))) {
         $output->writeln("<error>Wrong user !</error>");
         return 1;
     }
     $output->writeln(sprintf("\nWill do the check with user <info>%s</info> (%s)\n", $TestUser->getDisplayName(), $TestUser->getEmail()));
     $output->writeln("PHRASEA FUNCTIONS");
     foreach (get_extension_funcs("phrasea2") as $function) {
         $output->writeln("<info>{$function}</info>");
     }
     $configuration = $this->getService('configuration');
     $connexion = $configuration['main']['database'];
     $hostname = $connexion['host'];
     $port = $connexion['port'];
     $user = $connexion['user'];
     $password = $connexion['password'];
     $dbname = $connexion['dbname'];
     $output->writeln("\n-- phrasea_conn --");
     if (phrasea_conn($hostname, $port, $user, $password, $dbname) !== true) {
         $output->writeln("<error>Failed ! </error> got no connection");
         return 1;
     } else {
         $output->writeln("<info>Succes ! </info> got connection");
     }
     $output->writeln("");
     $output->writeln("\n-- phrasea_info --");
     foreach (phrasea_info() as $key => $value) {
         $output->writeln("\t{$key} => {$value}");
     }
     $output->writeln("");
     $output->writeln("\n-- phrasea_create_session --");
     $sessid = phrasea_create_session((string) $TestUser->getId());
     if (ctype_digit((string) $sessid)) {
         $output->writeln("<info>Succes ! </info> got session id {$sessid}");
     } else {
         $output->writeln("<error>Failed ! </error> got no session id");
         return 1;
     }
     $output->writeln("\n-- phrasea_open_session --");
     $phSession = phrasea_open_session($sessid, $usrId);
     if ($phSession) {
         $output->writeln("<info>Succes ! </info> got session ");
     } else {
         $output->writeln("<error>Failed ! </error> got no session ");
         return 1;
     }
     $output->writeln("\n-- phrasea_clear_cache --");
     $ret = phrasea_clear_cache($sessid);
     if ($sessid) {
         $output->writeln("<info>Succes ! </info> got session ");
     } else {
         $output->writeln("<error>Failed ! </error> got no session ");
         return 1;
     }
     $tbases = [];
     foreach ($phSession["bases"] as $phbase) {
         $tcoll = [];
         foreach ($phbase["collections"] as $coll) {
             $tcoll[] = 0 + $coll["base_id"];
         }
         if (sizeof($tcoll) > 0) {
             $kbase = "S" . $phbase["sbas_id"];
             $tbases[$kbase] = [];
             $tbases[$kbase]["sbas_id"] = $phbase["sbas_id"];
             $tbases[$kbase]["searchcoll"] = $tcoll;
             $tbases[$kbase]["mask_xor"] = $tbases[$kbase]["mask_and"] = 0;
             $qp = new PhraseaEngineQueryParser($this->container);
             $treeq = $qp->parsequery($input->getOption('query'));
             $arrayq = $qp->makequery($treeq);
             $tbases[$kbase]["arrayq"] = $arrayq;
         }
     }
     $output->writeln("\n-- phrasea_query --");
     $nbanswers = 0;
     foreach ($tbases as $kb => $base) {
         $tbases[$kb]["results"] = NULL;
         $ret = phrasea_query2($phSession["session_id"], $base["sbas_id"], $base["searchcoll"], $base["arrayq"], $this->container['conf']->get(['main', 'key']), $usrId, false, PHRASEA_MULTIDOC_DOCONLY, '', []);
         if ($ret) {
             $output->writeln("<info>Succes ! </info> got result on sbas_id " . $base["sbas_id"]);
         } else {
             $output->writeln("<error>Failed ! </error> No results on sbas_id " . $base["sbas_id"]);
             return 1;
         }
         $tbases[$kb]["results"] = $ret;
         $nbanswers += $tbases[$kb]["results"]["nbanswers"];
     }
     $output->writeln("Got a total of <info>{$nbanswers}</info> answers");
     $output->writeln("\n-- phrasea_fetch_results --");
     $rs = phrasea_fetch_results($sessid, $usrId, 1, true, '[[em]]', '[[/em]]');
     if ($rs) {
         $output->writeln("<info>Succes ! </info> got result ");
     } else {
         $output->writeln("<error>Failed ! </error> got no result ");
         return 1;
     }
     // test disconnected mode if available
     // prepare the test before closing session
     if (function_exists("phrasea_public_query")) {
         // fill an array for each sbas to query
         $tbases = [];
         foreach ($phSession["bases"] as $phbase) {
             // fill an array of collections to query for this sbas
             $tcoll = [];
             foreach ($phbase["collections"] as $coll) {
                 $tcoll[] = 0 + $coll["base_id"];
             }
             if (sizeof($tcoll) > 0) {
                 // parse the query for this sbas
                 $qp = new PhraseaEngineQueryParser($this->container);
                 $treeq = $qp->parsequery($input->getOption('query'));
                 $arrayq = $qp->makequery($treeq);
                 $tbases["S" . $phbase["sbas_id"]] = ["sbas_id" => $phbase["sbas_id"], "searchcoll" => $tcoll, "arrayq" => $arrayq];
             }
         }
     }
     $output->writeln("\n-- phrasea_close_session --");
     $rs = phrasea_close_session($sessid);
     if ($rs) {
         $output->writeln("<info>Succes ! </info> closed ! ");
     } else {
         $output->writeln("<error>Failed ! </error> not closed ");
         return 1;
     }
     // session is closed, test disconnected mode if available
     if (function_exists("phrasea_public_query")) {
         $output->writeln("\n-- phrasea_public_query(...0, 5,...) --");
         $ret = phrasea_public_query($tbases, PHRASEA_MULTIDOC_DOCONLY, '', [], '', 0, 5, true);
         if (is_array($ret) && array_key_exists("results", $ret) && is_array($ret["results"])) {
             $output->writeln(sprintf("<info>Succes ! </info> returned %d answers", count($ret["results"])));
         } else {
             $output->writeln("<error>Failed ! </error>");
             return 1;
         }
         foreach ($ret['results'] as $result) {
             $sbid = $result["sbid"];
             $rid = $result["rid"];
             $q = $tbases["S" . $sbid]["arrayq"];
             // query tree
             $h = phrasea_highlight($sbid, $rid, $q, "", false);
             $output->writeln(sprintf("\n-- phrasea_highlight(%d, %d,...) --", $sbid, $rid));
             if (is_array($h) && array_key_exists("results", $h) && is_array($h["results"]) && count($h["results"]) == 1 && array_key_exists("sbid", $h["results"][0]) && $h["results"][0]["sbid"] == $sbid && array_key_exists("rid", $h["results"][0]) && $h["results"][0]["rid"] == $rid && array_key_exists("spots", $h["results"][0]) && is_array($h["results"][0]["spots"])) {
                 $output->writeln(sprintf("<info>Succes ! </info> sbid=%d, rid=%d (%d spots)", $sbid, $h["results"][0]["rid"], count($h["results"][0]["spots"])));
             } else {
                 $output->writeln("<error>Failed ! </error>");
                 return 1;
             }
         }
     }
     // disconnected mode
     return 0;
 }