Пример #1
0
 /**
  * Checks if the Phraseanet session is still valid. Creates a new one if required.
  *
  * @return PhraseaEngine
  * @throws \RuntimeException
  * @throws \Exception_InternalServerError
  */
 private function checkSession()
 {
     if (!$this->app['authentication']->getUser()) {
         throw new \RuntimeException('Phrasea currently support only authenticated queries');
     }
     if (!phrasea_open_session($this->app['session']->get('phrasea_session_id'), $this->app['authentication']->getUser()->getId())) {
         if (!($ses_id = phrasea_create_session((string) $this->app['authentication']->getUser()->getId()))) {
             throw new \Exception_InternalServerError('Unable to create phrasea session');
         }
         $this->app['session']->set('phrasea_session_id', $ses_id);
     }
     return $this;
 }
Пример #2
0
 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;
 }