public function testXquery_escape()
 {
     $string = 'Eléphant ';
     $this->assertEquals($string, thesaurus::xquery_escape($string));
     $string = '&é"\'(-è_ çà)=ù*!:;,?./§%µ+°0987654321';
     $this->assertEquals('&é"'(-è_ çà)=ù*!:;,?./§%µ+°0987654321', thesaurus::xquery_escape($string));
 }
Exemple #2
0
 public function SearchTermJson(Application $app, Request $request)
 {
     if (null === ($lng = $request->get('lng'))) {
         $data = explode('_', $app['locale']);
         if (count($data) > 0) {
             $lng = $data[0];
         }
     }
     $html = '';
     $sbid = (int) $request->get('sbid');
     try {
         $databox = $app['phraseanet.appbox']->get_databox($sbid);
         $html = "" . '<LI id="TX_P.' . $sbid . '.T" class="expandable">' . "\n";
         $html .= "\t" . '<div class="hitarea expandable-hitarea"></div>' . "\n";
         $html .= "\t" . '<span>' . \phrasea::sbas_labels($sbid, $app) . '</span>' . "\n";
         if ($request->get('t')) {
             if ($request->get('field') != '') {
                 $domth = $databox->get_dom_thesaurus();
                 $dom_struct = $databox->get_dom_structure();
             } else {
                 $domth = $databox->get_dom_thesaurus();
             }
             $q = null;
             if ($request->get('field') != '') {
                 // search only in the branch(es) linked to this field
                 if ($dom_struct) {
                     $xpath = new \DOMXPath($dom_struct);
                     if ($znode = $xpath->query('/record/description/' . $request->get('field'))->item(0)) {
                         $q = '(' . $znode->getAttribute('tbranch') . ')';
                     }
                 }
             } else {
                 // search in the whole thesaurus
                 $q = '/thesaurus';
             }
             if ($q !== null && $domth) {
                 $xpath = new \DOMXPath($domth);
                 $t = $this->splitTermAndContext($request->get('t'));
                 $q2 = 'starts-with(@w, \'' . \thesaurus::xquery_escape($app['unicode']->remove_indexer_chars($t[0])) . '\')';
                 if ($t[1]) {
                     $q2 .= ' and starts-with(@k, \'' . \thesaurus::xquery_escape($app['unicode']->remove_indexer_chars($t[1])) . '\')';
                 }
                 $q2 = '//sy[' . $q2 . ' and @lng=\'' . $lng . '\']';
                 $q .= $q2;
                 $nodes = $xpath->query($q);
                 for ($i = 0; $i < $nodes->length; $i++) {
                     $nodes->item($i)->setAttribute('bold', '1');
                     for ($n = $nodes->item($i)->parentNode; $n && $n->nodeType == XML_ELEMENT_NODE && $n->nodeName == 'te'; $n = $n->parentNode) {
                         $n->setAttribute('open', '1');
                     }
                 }
                 $this->getHTMLTerm($sbid, $lng, $domth->documentElement, $html);
             }
         } else {
             $html .= "\t" . '<ul style="display: none;">loading</ul>' . "\n";
         }
         $html .= "" . '</LI>' . "\n";
     } catch (\Exception $e) {
     }
     return $app->json(['parm' => ['sbid' => $request->get('sbid'), 't' => $request->get('t'), 'field' => $request->get('field'), 'lng' => $request->get('lng'), 'debug' => $request->get('debug')], 'html' => $html]);
 }
Exemple #3
0
 private function doSearchCandidate(Application $app, $bid, $pid, $t, $k, $piv)
 {
     $ret = new \DOMDocument("1.0", "UTF-8");
     $ret->standalone = true;
     $ret->preserveWhiteSpace = false;
     $root = $ret->appendChild($ret->createElement("result"));
     $root->appendChild($ret->createCDATASection(var_export(["bid" => $bid, "pid" => $pid, "t" => $t, "k" => $k, "piv" => $piv], true)));
     $ctlist = $root->appendChild($ret->createElement("candidates_list"));
     try {
         $databox = $app['phraseanet.appbox']->get_databox((int) $bid);
         $domstruct = $databox->get_dom_structure();
         $domth = $databox->get_dom_thesaurus();
         $domct = $databox->get_dom_cterms();
         if ($domstruct && $domth && $domct) {
             $xpathth = new \DOMXPath($domth);
             $xpathct = new \DOMXPath($domct);
             // on cherche les champs d'ou peut provenir un candidat, en fct de l'endroit oe on veut inserer le nouveau terme
             $fields = array();
             $xpathstruct = new \DOMXPath($domstruct);
             $nodes = $xpathstruct->query("/record/description/*[@tbranch]");
             for ($i = 0; $i < $nodes->length; $i++) {
                 $fieldname = $nodes->item($i)->nodeName;
                 $tbranch = $nodes->item($i)->getAttribute("tbranch");
                 if ($pid != "") {
                     $q = "(" . $tbranch . ")/descendant-or-self::te[@id='" . $pid . "']";
                 } else {
                     $q = "(" . $tbranch . ")/descendant-or-self::te[not(@id)]";
                 }
                 $fields[$fieldname] = ["name" => $fieldname, "tbranch" => $tbranch, "cid" => null, "sourceok" => false];
                 if (!$tbranch) {
                     continue;
                 }
                 $l = $xpathth->query($q)->length;
                 if ($l > 0) {
                     // le pt d'insertion du nvo terme se trouve dans la tbranch du champ,
                     // donc ce champ peut etre source de candidats
                     $fields[$fieldname]["sourceok"] = true;
                 } else {
                     // le pt d'insertion du nvo terme ne se trouve PAS dans la tbranch du champ,
                     // donc ce champ ne peut pas etre source de candidats
                 }
             }
             // on considere que la source 'deleted' est toujours valide
             $fields["[deleted]"] = ["name" => $app->trans('thesaurus:: corbeille'), "tbranch" => null, "cid" => null, "sourceok" => true];
             if (count($fields) > 0) {
                 $q = "@w='" . \thesaurus::xquery_escape($app['unicode']->remove_indexer_chars($t)) . "'";
                 if ($k) {
                     if ($k != "*") {
                         $q .= " and @k='" . \thesaurus::xquery_escape($app['unicode']->remove_indexer_chars($k)) . "'";
                     }
                 } else {
                     $q .= " and not(@k)";
                 }
                 $q = "/cterms//te[./sy[{$q}]]";
                 $nodes = $xpathct->query($q);
                 // le terme peut etre present dans plusieurs candidats
                 for ($i = 0; $i < $nodes->length; $i++) {
                     // on a trouve le terme dans les candidats, mais en provenance de quel champ ?.. on remonte au champ candidat
                     for ($n = $nodes->item($i)->parentNode; $n && $n->parentNode && $n->parentNode->nodeName != "cterms"; $n = $n->parentNode) {
                     }
                     if ($n && array_key_exists($f = $n->getAttribute("field"), $fields)) {
                         $fields[$f]["cid"] = $nodes->item($i)->getAttribute("id");
                     }
                 }
             }
             foreach ($fields as $kfield => $field) {
                 if ($field["cid"] === null) {
                     continue;
                 }
                 $ct = $ctlist->appendChild($ret->createElement("ct"));
                 $ct->setAttribute("field", $field["name"]);
                 $ct->setAttribute("sourceok", $field["sourceok"] ? "1" : "0");
                 if ($field["cid"] !== null) {
                     $ct->setAttribute("id", $field["cid"]);
                 }
             }
         }
     } catch (\Exception $e) {
     }
     return $ret;
 }