private function setup_feeds_filters($search) { $query = AfsQuery::create_from_parameters(array()); $result = $search->execute($query); if (property_exists($result, 'replysets')) { foreach ($this->feeds_metadata as $metadata_feed => $metadata_helper) { $this->setup_feed_filters($result->replysets, $metadata_feed, $metadata_helper); } } }
/** @brief Constructs new instance of facet helper. * * @param $facet [in] root facet element. * @param $query [in] @a AfsQuery which has produced current reply. * @param $config [in] helper configuration object. */ public function __construct($facet, AfsQuery $query, AfsHelperConfiguration $config, $feed = null) { $this->id = $facet->id; if (property_exists($facet, 'labels') && !empty($facet->labels)) { $this->labels = $facet->labels; } if (property_exists($facet, 'tags')) { $this->tags = $facet->tags; } $this->layout = $facet->layout; $this->type = $facet->type; if (property_exists($facet, 'sticky') && 0 == strcmp('true', $facet->sticky)) { $this->sticky = true; } else { $this->sticky = false; } $facet_manager = $query->get_facet_manager(); $facet_manager->check_or_add_facet(new AfsFacet($this->id, $this->type, $this->layout)); $builder = new AfsFacetElementBuilder($facet_manager, $query); $this->elements = $builder->create_elements($this->id, $facet, $config, $feed); }
public function testKeepCustomParameters() { $query = AfsQuery::create_from_parameters(array("query" => "topic", "mycustomparameter" => "mycustomvalue")); $curlConnector = new SAI_CurlStub(); $mockBaseUrl = "localhost"; $aboutRequestOpts = array(CURLOPT_URL => "http://{$mockBaseUrl}/bo-ws/about"); $aboutResponse = <<<JSON { "x:type":"ws.response", "query":{ "x:type":"ws.response.query", "parameters":{ "x:type":"collection", "x:values":[ ] }, "properties":{ "x:type":"x:dynamic" } }, "result":{ "x:type":"bows.about", "boWsVersion":{ "x:type":"AfsVersion", "build":"3eaebfd1f1fe261780347cbc35bfbd65d613575e", "gen":"7.6", "major":"4", "minor":"0", "motto":"Pink Dolphin" }, "copyright":"Copyright (C) 1999-2013 Antidot" } } JSON; $response = <<<JSON {"header":{"query":{"userId":"user_5354ec142aa12","sessionId":"session_5354ec142aa4b","date":"2014-04-21T13:41:10+0200", "queryParam":[{"name":"afs:service","value":"71003"},{"name":"afs:status","value":"beta"},{"name":"afs:query","value":"topic"}, {"name":"afs:query@Book","value":"topic"},{"name":"afs:query@Topic","value":"topic"}, {"name":"afs:query@spellcheck","value":"topic"},{"name":"afs:output","value":"json,2"},{"name":"afs:output@Book","value":"json,2"}, {"name":"afs:output@Topic","value":"json,2"},{"name":"afs:output@spellcheck","value":"json,2"},{"name":"mycustomparameter","value":"mycustomvalue"}, {"name":"mycustomparameter@Book","value":"mycustomvalue"},{"name":"mycustomparameter@Topic","value":"mycustomvalue"}, {"name":"mycustomparameter@spellcheck","value":"mycustomvalue"},{"name":"afs:replies","value":"10"}, {"name":"afs:replies@Book","value":"10"},{"name":"afs:replies@Topic","value":"10"},{"name":"afs:replies@spellcheck", "value":"10"}],"mainCtx":{"textQuery":"topic"},"textQuery":"topic"},"user":{"requestMethod":"GET","agent":"Mozilla 5.0", "address":"127.0.0.1","output":{"format":"JSON","encoding":"gzip","charset":"UTF-8"}},"performance":{"durationMs":11},"info":{}}, "replySet":[{"meta":{"uri":"Book","totalItems":1,"totalItemsIsExact":true,"pageItems":1,"firstPageItem":1,"lastPageItem":1, "durationMs":1,"firstPaFId":147,"lastPaFId":147,"producer":"SEARCH"}, "facets":{"facet":[{"afs:t":"FacetTree","node":[{"key":"urn:dita:single_topic.ditamap", "labels":[{"lang":"EN","label":"urn:dita:single_topic.ditamap"},{"lang":"FR","label":"urn:dita:single_topic.ditamap"}], "items":1}],"layout":"TREE","type":"STRING","id":"BaseUri","labels":[{"lang":"EN","label":"BaseUri"}, {"lang":"FR","label":"BaseUri"}]},{"afs:t":"FacetTree","node":[{"key":"noditaval","labels":[{"lang":"EN","label":"noditaval"}, {"lang":"FR","label":"noditaval"}],"items":1}],"layout":"TREE","type":"STRING","id":"Ditaval", "labels":[{"lang":"EN","label":"Ditaval"},{"lang":"FR","label":"Ditaval"}]},{"afs:t":"FacetTree", "node":[{"key":"en","labels":[{"label":"English"}],"items":1}],"layout":"TREE","type":"STRING","id":"afs:lang", "labels":[{"label":"Language"}]}]},"content":{"reply":[{"docId":3,"uri":"urn:dita:single_topic.ditamap", "title":[{"afs:t":"KwicString","text":"Single "},{"afs:t":"KwicMatch","match":"Topic"}, {"afs:t":"KwicString","text":" Map"}],"relevance":{"rank":1},"layerReplies":{"reply":[{"layer":"USER_1", "reply":{"docId":3,"uri":"","clientData":[{"contents":{"meta":[],"label":"Single Topic Map","ditaval":"noditaval", "uri":"urn:dita:single_topic.ditamap"},"id":"ditaval","mimeType":"application\\/json"}]}}]}}]}}, {"meta":{"uri":"Topic","totalItems":1,"totalItemsIsExact":true,"pageItems":1,"firstPageItem":1,"lastPageItem":1,"durationMs":2, "firstPaFId":147,"lastPaFId":147,"producer":"SEARCH"},"facets":{"facet":[{"afs:t":"FacetTree", "node":[{"key":"others","labels":[{"lang":"EN","label":"others"},{"lang":"FR","label":"others"}],"items":1}], "layout":"TREE","type":"STRING","id":"Audience","labels":[{"lang":"EN","label":"Audience"},{"lang":"FR","label":"Audience"}]}, {"afs:t":"FacetTree","node":[{"key":"Single Topic Map","labels":[{"lang":"EN","label":"Single Topic Map"}, {"lang":"FR","label":"Single Topic Map"}],"items":1}],"layout":"TREE","type":"STRING","id":"Filter_By_Docs", "labels":[{"lang":"EN","label":"Filter by Documents"},{"lang":"FR","label":"Filtrer par Documents"}]}, {"afs:t":"FacetTree","node":[{"key":"en","labels":[{"label":"English"}],"items":1}],"layout":"TREE","type":"STRING", "id":"afs:lang","labels":[{"label":"Language"}]}]},"content":{"reply":[{"docId":2,"uri":"urn:dita:single_topic.dita", "title":[{"afs:t":"KwicString","text":"Titre du "},{"afs:t":"KwicMatch","match":"topic"}, {"afs:t":"KwicString","text":" unique"}],"abstract":[{"afs:t":"KwicString","text":"Ce "}, {"afs:t":"KwicMatch","match":"topic"},{"afs:t":"KwicString","text":" est seul dans sa ditamap et ceci devrait apparaitre dans le résumé."}], "relevance":{"rank":1},"layerReplies":{"reply":[{"layer":"USER_2","reply":{"docId":2,"uri":"", "clientData":[{"contents":{"book":{"uri":"urn:dita:single_topic.ditamap","label":"Single Topic Map"}, "topics":[{"uri":"urn:dita:single_topic.dita","label":"Titre du topic unique"}],"ditaval":"noditaval"}, "id":"breadcrumb","mimeType":"application\\/json"}]}}]}}]}}]} JSON; //Set BO response for AboutConnector $curlConnector->setResponse($aboutResponse, $aboutRequestOpts); //Set response for query $curlConnector->setResponse($response); $search = new AfsSearch($mockBaseUrl, '71003', AfsServiceStatus::STABLE, $curlConnector); $search->set_query($query); $coder = new AfsQueryCoder(); $search->set_query_coder($coder); $helper = $search->execute($query); $replysetHelper = $helper->get_replyset("Book"); $facetHelpers = $replysetHelper->get_facets(); //Make sure each link of facets contains custom parameter foreach ($facetHelpers as $facetHelper) { foreach ($facetHelper->get_elements() as $facetValueHelper) { $this->assertEquals(1, preg_match("/[&\\?]mycustomparameter=mycustomvalue[&\$]/", $facetValueHelper->link)); } } $this->assertEquals("mycustomvalue", $helper->get_query_parameter("mycustomparameter")); }
public function testAddClientData() { $query = new AfsQuery(); $ids = array(1, 2, 3); $id = 4; $query->add_client_data($id); $query->add_client_data($ids); $parameters = $query->get_parameters(); $clientData = $parameters['clientData']; $this->assertEquals(4, count($clientData)); for ($i = 1; $i <= 4; $i++) { $this->assertTrue(in_array($i, $clientData)); } }
/** @brief Creates full query from array of parameters. * * Unknown parameters are silently ignored. * * @param $params [in] structured array of parameters. * @return correctly initialized query. */ public static function create_from_parameters(array $params) { $result = AfsQuery::work_on_specific_parameters($params); $page = $result->get_page(); # page can be reset by some method calls foreach ($params as $param => $values) { $adder = 'add_' . $param; $setter = 'set_' . $param; if (strpos($param, 'filter') !== false) { $filter_array = explode('@', $param); if (count($filter_array) === 2) { $feed = $filter_array[1]; } else { $feed = null; } foreach ($values as $filter => $filter_values) { foreach ($filter_values as $value) { if (!is_null($feed)) { $result = $result->add_filter_on_feed($filter, array($value), $feed); } else { $result = $result->add_filter($filter, $value, $feed); } } } } elseif ($param == 'sort') { foreach ($values as $key => $value) { $result = $result->{$adder}($key, $value); } } elseif (is_object($result) && is_callable(array($result, $adder))) { foreach ($values as $value) { $result = $result->{$adder}($value); } } elseif (is_object($result) && is_callable(array($result, $setter))) { $result = $result->{$setter}($values); } else { // Store unknown parameter as a custom one $result->set_custom_parameter($param, $values); } } $result->page->set_value($page); return $result; }
/** * @brief Generate query from URL parameters. * * @param $params [in] * array of parameters. Usually set to $_GET. * @return @a AfsQuery correctly initialized. */ public function build_query(array $params) { $buffer = array(); foreach ($params as $param => $values) { $buffer[$param] = $this->coder($param, $values, 'decode'); } $query = AfsQuery::create_from_parameters($buffer); return $query; }
public function testFacetStrictOrder() { $query = new AfsQuery(); $facet_mgr = $query->get_facet_manager(); $sort = array('FOO', 'BAR'); $facet_mgr->set_facet_order($sort, AfsFacetOrder::STRICT); $this->qm->send($query); $params = $this->connector->get_parameters(); $this->assertTrue(array_key_exists('afs:facetOrder', $params)); $this->assertEquals(implode(',', $sort), $params['afs:facetOrder']); }
public function testIp() { global $_SERVER; $_SERVER = array('REMOTE_ADDR' => '127.0.0.1'); $connector = new SearchConnector('foo', new AfsService(42)); $query = new AfsQuery(); $url = $connector->build_url(null, $query->get_parameters()); $this->assertFalse(strpos($url, urlencode('afs:ip')) === False); }
public function testLazyFacetOrderingMoreFacetsDeclaredThanPresentInReply() { $input = json_decode('{ "header": { "query": { "userId": "afd070b6-4315-40cc-975d-747e28bf132a", "sessionId": "5bf5642d-a262-4608-9901-45aa6e87325d" }, "performance": { "durationMs": 666 } }, "replySet": [ { "meta": { "uri": "Test", "totalItems": 200, "totalItemsIsExact": true, "pageItems": 2, "firstPageItem": 3, "lastPageItem": 4, "durationMs": 42, "firstPaFId": 1, "lastPaFId": 1, "producer": "SEARCH" }, "facets": { "facet": [ { "afs:t": "FacetInterval", "interval": [ { "key": "[3 .. 6]", "items": 1 } ], "layout": "INTERVAL", "type": "DATE", "id": "Baz" }, { "afs:t": "FacetInterval", "interval": [ { "key": "[3 .. 6]", "items": 1 } ], "layout": "INTERVAL", "type": "INTEGER", "id": "Bar" }, { "afs:t": "FacetInterval", "interval": [ { "key": "[0 .. 3]", "items": 1 } ], "layout": "INTERVAL", "type": "REAL", "id": "Foo" } ] } } ] }'); $config = new AfsHelperConfiguration(); $query = new AfsQuery(); $facet_mgr = $query->get_facet_manager(); $facet_mgr->set_facet_order(array('Foo', 'Bal', 'Bar'), AfsFacetOrder::LAX); $this->assertTrue($facet_mgr->has_facet('Foo')); $this->assertTrue($facet_mgr->has_facet('Bal')); $this->assertTrue($facet_mgr->has_facet('Bar')); $this->assertFalse($facet_mgr->has_facet('Baz')); $helper = new AfsReplysetHelper($input->replySet[0], $query, $config); $this->assertEquals(AfsFacetType::REAL_TYPE, $facet_mgr->get_facet('Foo')->get_type()); $this->assertEquals(AfsFacetType::INTEGER_TYPE, $facet_mgr->get_facet('Bar')->get_type()); $facets = $helper->get_facets(); $this->assertEquals(3, count($facets)); $this->assertEquals('Foo', $facets[0]->get_id()); $this->assertEquals('Bar', $facets[1]->get_id()); $this->assertEquals('Baz', $facets[2]->get_id()); }
/** @internal * @brief Convert @a AfsQuery to arrays of parameters * * Array of parameters can be used to query AFS search engine. Each * key/value corresponds to parameter name and parameter value or array of * parameter values respectively. * You can easily get appropriate string with following call: * @code * $params = qm->convert_to_param($afs_query); * $string_params = array(); * foreach ($params as $k => $vs) { * if (is_array($vs)) { * foreach ($vs as $v) { * $string_params[] = urlencode($k) . '=' . urlencode($v); * } * } else { * $string_params[] = urlencode($k) . '=' . urlencode($vs); * } * } * $url_params = implode('&', $string_params); * @endcode * * * @param $query [in] @a AfsQuery to transform. * * @return string usable as URL parameter. * * @exception Exception when filter on unconfigured facet is used. */ private function convert_to_param(AfsQuery $query) { $params = array(); foreach ($query->get_parameters() as $param => $values) { $param_array = explode('@', $param); if (count($param_array) === 2) { $feed = $param_array[1]; } else { $feed = null; } $param_name = $param_array[0]; if ($param_name == 'filter') { foreach ($values as $facet => $ids) { $this->fill_in_filter($params, $this->format_filter($query, $facet, $ids), $feed); } } elseif ($param_name == 'sort') { if (!empty($values)) { foreach ($values as $name => $order) { $this->fill_in_sort($params, $this->format_sort($name, $order), $feed); } } } elseif ($param_name == 'advancedFilter') { foreach ($values as $value) { $this->fill_in_filter($params, $value, $feed); } } elseif ($param_name == 'nativeFunctionFilter') { foreach ($values as $value) { $this->fill_in_filter($params, $value, $feed); } } elseif ($param_name == 'nativeFunctionSort') { foreach ($values as $value) { $params['afs:sort'][] = $value; } } else { $this->fill_in_param($params, $param_name, $values, $feed); } } $params = array_merge($params, $query->get_custom_parameters()); return $params; }
/** * @expectedException Exception */ public function testNotOrchestratedRequest() { $query = AfsQuery::create_from_parameters(array("query" => "topic", "mycustomparameter" => "mycustomvalue")); $curlConnector = new SAI_CurlStub(); $mockBaseUrl = "localhost"; $aboutRequestOpts = array(CURLOPT_URL => "http://{$mockBaseUrl}/bo-ws/about"); $aboutResponse = <<<JSON { "x:type":"ws.response", "query":{ "x:type":"ws.response.query", "parameters":{ "x:type":"collection", "x:values":[ ] }, "properties":{ "x:type":"x:dynamic" } }, "result":{ "x:type":"bows.about", "boWsVersion":{ "x:type":"AfsVersion", "build":"3eaebfd1f1fe261780347cbc35bfbd65d613575e", "gen":"7.6", "major":"4", "minor":"0", "motto":"Pink Dolphin" }, "copyright":"Copyright (C) 1999-2013 Antidot" } } JSON; $response = <<<JSON {"header":{"query":{"userId":"b756a0de-e252-426f-8a24-66c4bbb96aa0","sessionId":"dd33b13c-dd9f-49a1-abe1-95e89140fb40","date":"2015-10-14T15:12:00+0000","queryParam":[{"name":"afs:service","value":"42"},{"name":"afs:status","value":"beta"},{"name":"afs:query","value":"vert"},{"name":"afs:output","value":"json,2"},{"name":"afs:facetDefault","value":"replies=100"},{"name":"afs:replies","value":"20"},{"name":"afs:sort","value":"disposite, DESC and afs:relevance, DESC"}],"mainCtx":{"textQuery":"vert"},"textQuery":"vert"},"user":{"requestMethod":"GET","agent":"Mozilla\\/5.0 (X11; Linux x86_64) AppleWebKit\\/537.36 (KHTML, like Gecko) Chrome\\/42.0.2311.90 Safari\\/537.36","address":"172.17.42.1","output":{"format":"JSON","encoding":"gzip","charset":"UTF-8"}},"performance":{"durationMs":5},"info":{}},"replySet":[{"meta":{"uri":"Catalog","totalItems":1,"totalItemsIsExact":true,"pageItems":1,"firstPageItem":1,"lastPageItem":1,"durationMs":1,"firstPaFId":3,"lastPaFId":3,"producer":"SEARCH"},"facets":{"facet":[{"afs:t":"FacetTree","node":[{"key":"fr","labels":[{"label":"Français"}],"items":1}],"layout":"TREE","type":"STRING","id":"afs:lang","labels":[{"label":"Language"}]}]},"content":{"reply":[{"docId":4,"uri":"3612531066812_fr","title":[{"afs:t":"KwicMatch","match":"vert"}],"relevance":{"rank":1},"clientData":[{"contents":"<product xmlns=\\"http:\\/\\/ref.antidot.net\\/store\\/afs#\\" xmlns:xsi=\\"http:\\/\\/www.w3.org\\/2001\\/XMLSchema-instance\\" id=\\"3612531066812\\" xml:lang=\\"fr\\" autocomplete=\\"on\\" xsi:schemaLocation=\\"http:\\/\\/ref.antidot.net\\/store\\/afs# http:\\/\\/ref.antidot.net\\/store\\/v4.1\\/xsd\\/product.xsd\\"><name xmlns=\\"http:\\/\\/ref.antidot.net\\/store\\/afs#\\">vert<\\/name><\\/product>","id":"main","mimeType":"text\\/xml"}]}]}}]} JSON; //Set BO response for AboutConnector $curlConnector->setResponse($aboutResponse, $aboutRequestOpts); //Set response for query $curlConnector->setResponse($response); $search = new AfsSearch($mockBaseUrl, '71003', AfsServiceStatus::STABLE, $curlConnector); $search->set_query($query); $coder = new AfsQueryCoder(); $search->set_query_coder($coder); $helper = $search->execute($query); $this->assertEquals($helper->is_orchestrated(), false); $helper->get_orchestration_type(); }
public function testFacetElementBuilderOnTreeNode() { $input = json_decode('{ "afs:t": "FacetTree", "node": [ { "key": "2010", "labels": [ { "lang": "FR", "region": "FR", "label": "2010" } ], "items": 24, "node": [ { "key": "2010-03", "labels": [ { "lang": "FR", "region": "FR", "label": "03" } ], "items": 14, "node": [ { "key": "2010-03-07", "labels": [ { "lang": "FR", "region": "FR", "label": "07" } ], "items": 4 } ] } ] } ], "layout": "TREE", "type": "DATE", "id": "TREE_DATE", "labels": [ { "label": "Tree date" } ] }'); $query = new AfsQuery(); $config = new AfsHelperConfiguration(); $config->set_helper_format(AfsHelperFormat::HELPERS); $facet_mgr = $query->get_facet_manager(); $facet_mgr->add_facet(new AfsFacet('TREE_DATE', AfsFacetType::DATE_TYPE)); $builder = new AfsFacetElementBuilder($facet_mgr, $query); $elems = $builder->create_elements('TREE_DATE', $input, $config); $this->assertEquals(count($elems), 1); $elem = reset($elems); $this->assertEquals($elem->label, '2010'); $this->assertEquals($elem->count, 24); $this->assertFalse($elem->active); $this->assertTrue($elem->query->has_filter('TREE_DATE', '"2010"')); $this->assertEquals(count($elem->values), 1); $elem = $elem->values[0]; $this->assertEquals($elem->label, '03'); $this->assertEquals($elem->count, 14); $this->assertFalse($elem->active); $this->assertTrue($elem->query->has_filter('TREE_DATE', '"2010-03"')); $this->assertEquals(count($elem->values), 1); $elem = $elem->values[0]; $this->assertEquals($elem->label, '07'); $this->assertEquals($elem->count, 4); $this->assertFalse($elem->active); $this->assertTrue($elem->query->has_filter('TREE_DATE', '"2010-03-07"')); $this->assertEquals(count($elem->values), 0); }