Example #1
0
 /**
  * Parse facets out of the response
  *
  * @param DOMDocument $dom
  * @return Facets
  */
 protected function extractFacets(\DOMDocument $dom)
 {
     $facets = new Search\Facets();
     $xml = simplexml_import_dom($dom->documentElement);
     // for now just the database hit counts
     $databases = $xml->Statistics->Statistic;
     if (count($databases) > 1) {
         $databases_facet_name = $this->config->getConfig("DATABASES_FACET_NAME", false, "Databases");
         $group = new Search\FacetGroup("databases");
         $group->name = "databases";
         $group->public = $databases_facet_name;
         $databases_array = array();
         foreach ($databases as $database) {
             $database_id = (string) $database->Database;
             $database_hits = (int) $database->Hits;
             // nix the empty ones
             if ($database_hits == 0) {
                 continue;
             }
             $databases_array[$database_id] = $database_hits;
         }
         // get 'em in reverse order
         arsort($databases_array);
         foreach ($databases_array as $database_id => $database_hits) {
             $facet = new Search\Facet();
             $facet->name = $this->config->getDatabaseName($database_id);
             $facet->count = Parser::number_format($database_hits);
             $facet->key = $database_id;
             $group->addFacet($facet);
         }
         $facets->addGroup($group);
     }
     return $facets;
 }
Example #2
0
 /**
  * Do the actual search and return results
  *
  * @param Query $search  search object
  * @param int $start     [optional] starting record number
  * @param int $max       [optional] max records
  * @param string $sort   [optional] sort order
  * @param bool $facets   [optional] whether to include facets
  *
  * @return Results
  */
 protected function doSearch(Search\Query $search, $start = 1, $max = 10, $sort = "", $facets = true)
 {
     $username = $search->getQueryTerm(0)->phrase;
     $label = $search->getLimit("label");
     $format = $search->getLimit("format");
     $results = new Search\ResultSet($this->config);
     $results->total = $this->datamap->totalRecords($username, $label->value, $format->value);
     // just the hit count please
     if ($max == 0) {
         return $results;
     }
     // no we want actual records too
     $records = array();
     if ($label->value != "") {
         $records = $this->datamap->getRecordsByLabel($username, $label->value, $sort, $start, $max);
     } elseif ($format->value != "") {
         $records = $this->datamap->getRecordsByFormat($username, $format->value, $sort, $start, $max);
     } else {
         $records = $this->datamap->getRecords($username, null, $sort, $start, $max);
     }
     // convert them into our model
     foreach ($records as $record) {
         $result = $this->createSearchResult($record);
         $results->addResult($result);
     }
     // facets
     $facets = new Search\Facets();
     // formats
     $formats = $this->datamap->getFormats($username);
     if (count($formats) > 0) {
         $group = new Search\FacetGroup();
         $group->name = "format";
         $group->public = "Formats";
         // @todo: i18n this?
         foreach ($formats as $format) {
             $facet = new Search\Facet();
             $facet->name = $format->format;
             $facet->count = $format->total;
             $group->addFacet($facet);
         }
         $facets->addGroup($group);
     }
     // labels
     $tags = $this->datamap->getTags($username);
     if (count($tags) > 0) {
         $group = new Search\FacetGroup();
         $group->name = "label";
         $group->public = "Labels";
         // @todo: i18n this?
         foreach ($tags as $tag) {
             $facet = new Search\Facet();
             $facet->name = $tag->label;
             $facet->count = $tag->total;
             $group->addFacet($facet);
         }
         $facets->addGroup($group);
     }
     $results->setFacets($facets);
     return $results;
 }
Example #3
0
 /**
  * Parse facets out of the response
  *
  * @param DOMDocument $dom  pazpar2 XML
  * @return Facets
  */
 protected function extractFacets(\DOMDocument $dom)
 {
     $facets = new Search\Facets();
     //echo $dom->saveXML();
     $groups = $dom->getElementsByTagName("list");
     if ($groups->length > 0) {
         // we'll pass the facets into an array so we can control both which
         // ones appear and in what order in the Xerxes config
         $facet_group_array = array();
         foreach ($groups as $facet_group) {
             $facet_values = $facet_group->getElementsByTagName("term");
             // if only one entry, then all the results have this same facet,
             // so no sense even showing this as a limit
             if ($facet_values->length <= 1) {
                 continue;
             }
             $group_internal_name = $facet_group->getAttribute("name");
             $facet_group_array[$group_internal_name] = $facet_values;
         }
         // now take the order of the facets as defined in xerxes config
         foreach ($this->conf()->getFacets() as $group_internal_name => $facet_values) {
             // we defined it, but it's not in the pazpar2 response
             if (!array_key_exists($group_internal_name, $facet_group_array)) {
                 continue;
             }
             $group = new Search\FacetGroup();
             $group->name = $group_internal_name;
             $group->public = $this->conf()->getFacetPublicName($group_internal_name);
             // get the actual facets out of the array above
             $facet_values = $facet_group_array[$group_internal_name];
             // and put them in their own array so we can mess with them
             $facet_array = array();
             foreach ($facet_values as $facet_value) {
                 $name = $facet_value->getElementsByTagName("name")->item(0)->nodeValue;
                 // pz2 returns authors with a trailing comma
                 // sometime also get unwanted fullstop
                 $name = trim($name, ",. ");
                 $counts = $facet_value->getElementsByTagName("frequency");
                 $count = $counts->item(0)->nodeValue;
                 $facet_array[$name] = $count;
             }
             // sort facets into descending order of frequency
             arsort($facet_array);
             // assume not date
             $group = $this->conf()->getFacetObjects($group, $group_internal_name, $facet_array);
             $facets->addGroup($group);
         }
     }
     return $facets;
 }
Example #4
0
 /**
  * Parse facets out of the response
  *
  * @param array $summon_results
  * @return Facets
  */
 protected function extractFacets(array $summon_results)
 {
     $facets = new Search\Facets();
     $facet_fields = array();
     if (array_key_exists('facetFields', $summon_results)) {
         $facet_fields = $summon_results['facetFields'];
     }
     if (array_key_exists('rangeFacetFields', $summon_results)) {
         foreach ($summon_results['rangeFacetFields'] as $range_facet) {
             $facet_fields[] = $range_facet;
         }
     }
     if (count($facet_fields) > 0) {
         // @todo: figure out how to factor out some of this to parent class
         // take them in the order defined in config
         foreach ($this->config->getFacets() as $group_internal_name => $config) {
             foreach ($facet_fields as $facetFields) {
                 if ($facetFields["displayName"] == $group_internal_name) {
                     $group = new Search\FacetGroup();
                     $group->name = $facetFields["displayName"];
                     $group->type = (string) $config["type"];
                     if ($config['display'] == 'false') {
                         $group->display = 'false';
                     }
                     $facets->addGroup($group);
                     // date type
                     if ((string) $config["type"] == "date") {
                         foreach ($facetFields["counts"] as $counts) {
                             $start_date = $counts['range']['minValue'];
                             $end_date = $counts['range']['maxValue'];
                             $count = $counts["count"];
                             $facet = new Search\Facet();
                             $facet->name = "{$start_date}-{$end_date}";
                             $facet->count = $counts["count"];
                             $facet->key = "{$start_date}:{$end_date}";
                             $facet->is_date = true;
                             $facet->timestamp = strtotime("1/1/{$start_date}") * 1000;
                             $date_facets[] = $facet;
                         }
                         // since summon returns date ranges whether they get 0 hits or not
                         // we need to trim the ones at the front and end that are 0 count
                         for ($i = 0; $i < count($date_facets); $i++) {
                             $date_facet = $date_facets[$i];
                             if ((int) $date_facet->count == 0) {
                                 $date_facets[$i] = null;
                             } else {
                                 break;
                             }
                         }
                         $date_facets = array_values(array_filter($date_facets));
                         for ($i = count($date_facets) - 1; $i >= 0; $i--) {
                             $date_facet = $date_facets[$i];
                             if ($date_facet->count == 0) {
                                 unset($date_facets[$i]);
                             } else {
                                 break;
                             }
                         }
                         foreach ($date_facets as $date_facet) {
                             $group->addFacet($date_facet);
                         }
                     } else {
                         foreach ($facetFields["counts"] as $counts) {
                             // skip excluded facets
                             if ($group->name == 'ContentType' && in_array($counts["value"], $this->query->getExcludedFormats())) {
                                 continue;
                             }
                             $facet = new Search\Facet();
                             $facet->name = $counts["value"];
                             $facet->count = $counts["count"];
                             if ($counts['isNegated'] == '1') {
                                 $facet->is_excluded = 1;
                             }
                             $group->addFacet($facet);
                         }
                     }
                 }
             }
         }
     }
     return $facets;
 }
Example #5
0
 /**
  * Parse facets out of the response
  *
  * @param array $summon_results
  * @return Facets
  */
 protected function extractFacets($summon_results)
 {
     $facets = new Search\Facets();
     $facet_fields = array();
     if (array_key_exists('facetFields', $summon_results)) {
         $facet_fields = $summon_results['facetFields'];
     }
     if (array_key_exists('rangeFacetFields', $summon_results)) {
         foreach ($summon_results['rangeFacetFields'] as $range_facet) {
             $facet_fields[] = $range_facet;
         }
     }
     if (count($facet_fields) > 0) {
         // @todo: figure out how to factor out some of this to parent class
         // take them in the order defined in config
         foreach ($this->config->getFacets() as $group_internal_name => $config) {
             foreach ($facet_fields as $facetFields) {
                 if ($facetFields["displayName"] == $group_internal_name) {
                     $group = new Search\FacetGroup();
                     $group->name = $facetFields["displayName"];
                     $group->public = $this->config->getFacetPublicName($facetFields["displayName"]);
                     if ($config['display'] == 'false') {
                         $group->display = 'false';
                     }
                     $facets->addGroup($group);
                     // date type
                     if ((string) $config["type"] == "date") {
                         foreach ($facetFields["counts"] as $counts) {
                             $start_date = $counts['range']['minValue'];
                             $end_date = $counts['range']['maxValue'];
                             $facet = new Search\Facet();
                             $facet->name = "{$start_date}-{$end_date}";
                             $facet->count = $counts["count"];
                             $facet->key = "{$start_date}:{$end_date}";
                             $facet->is_date = true;
                             $group->addFacet($facet);
                         }
                     } else {
                         foreach ($facetFields["counts"] as $counts) {
                             // skip excluded facets
                             if ($group->name == 'ContentType' && in_array($counts["value"], $this->formats_exclude)) {
                                 continue;
                             }
                             $facet = new Search\Facet();
                             $facet->name = $counts["value"];
                             $facet->count = $counts["count"];
                             if ($counts['isNegated'] == '1') {
                                 $facet->is_excluded = 1;
                             }
                             $group->addFacet($facet);
                         }
                     }
                 }
             }
         }
     }
     return $facets;
 }
Example #6
0
 /**
  * Parse facets out of the response
  *
  * @param DOMDocument $dom 	Primo XML
  * @return Facets
  */
 protected function extractFacets(\DOMDocument $dom)
 {
     $facets = new Search\Facets();
     // echo $dom->saveXML();
     $groups = $dom->getElementsByTagName("FACET");
     if ($groups->length > 0) {
         // we'll pass the facets into an array so we can control both which
         // ones appear and in what order in the Xerxes config
         $facet_group_array = array();
         foreach ($groups as $facet_group) {
             $facet_values = $facet_group->getElementsByTagName("FACET_VALUES");
             // if only one entry, then all the results have this same facet,
             // so no sense even showing this as a limit
             if ($facet_values->length <= 1) {
                 continue;
             }
             $group_internal_name = $facet_group->getAttribute("NAME");
             $facet_group_array[$group_internal_name] = $facet_values;
         }
         // now take the order of the facets as defined in xerxes config
         foreach ($this->config->getFacets() as $group_internal_name => $facet_values) {
             // we defined it, but it's not in the primo response
             if (!array_key_exists($group_internal_name, $facet_group_array)) {
                 continue;
             }
             $group = new Search\FacetGroup();
             $group->name = $group_internal_name;
             $group->public = $this->config->getFacetPublicName($group_internal_name);
             // get the actual facets out of the array above
             $facet_values = $facet_group_array[$group_internal_name];
             // and put them in their own array so we can mess with them
             $facet_array = array();
             foreach ($facet_values as $facet_value) {
                 $key = $facet_value->getAttribute("KEY");
                 $value = $facet_value->getAttribute("VALUE");
                 $facet_array[$key] = $value;
             }
             // date
             $decade_display = array();
             $is_date = $this->config->isDateType($group_internal_name);
             if ($is_date == true) {
                 $date_arrays = $group->luceneDateToDecade($facet_array);
                 $decade_display = $date_arrays["display"];
                 $facet_array = $date_arrays["decades"];
             } else {
                 // not a date, sort by hit count
                 arsort($facet_array);
             }
             // now make them into group facet objects
             foreach ($facet_array as $key => $value) {
                 $public_value = $this->config->getValuePublicName($group_internal_name, $key);
                 $facet = new Search\Facet();
                 $facet->name = $public_value;
                 $facet->count = $value;
                 // dates are different
                 if ($is_date == true) {
                     $facet->name = $decade_display[$key];
                     $facet->is_date = true;
                     $facet->key = $key;
                 }
                 $group->addFacet($facet);
             }
             $facets->addGroup($group);
         }
     }
     return $facets;
 }
Example #7
0
 /**
  * Parse facets out of the response
  *
  * @param Json $json
  * @return Facets
  */
 protected function extractFacets(Json $json)
 {
     if (array_key_exists('debug', $_GET)) {
         print_r($json);
         exit;
     }
     $facets = new Search\Facets();
     $facet_fields = $json->extractData('SearchResult/AvailableFacets');
     // @todo: figure out how to factor out some of this to parent class
     // take them in the order defined in config
     foreach ($this->config->getFacets() as $group_internal_name => $config) {
         foreach ($facet_fields as $facetFields) {
             if ($facetFields["Id"] == $group_internal_name) {
                 $group = new Search\FacetGroup();
                 $group->name = $facetFields['Id'];
                 $group->public = $facetFields['Label'];
                 if ($config['display'] == 'false') {
                     $group->display = 'false';
                 }
                 $facets->addGroup($group);
                 foreach ($facetFields['AvailableFacetValues'] as $counts) {
                     $facet = new Search\Facet();
                     $facet->name = $counts["Value"];
                     $facet->count = $counts["Count"];
                     $group->addFacet($facet);
                 }
             }
         }
     }
     return $facets;
 }
Example #8
0
 /**
  * Parse facets out of the response
  *
  * @param array $summon_results
  * @return Facets
  */
 protected function extractFacets($summon_results, $total)
 {
     $facets = new Search\Facets();
     if (array_key_exists("facetFields", $summon_results)) {
         // @todo: figure out how to factor out some of this to parent class
         // take them in the order defined in config
         foreach ($this->config->getFacets() as $group_internal_name => $config) {
             foreach ($summon_results["facetFields"] as $facetFields) {
                 if ($facetFields["displayName"] == $group_internal_name) {
                     $group = new Search\FacetGroup();
                     $group->name = $facetFields["displayName"];
                     $group->public = $this->config->getFacetPublicName($facetFields["displayName"]);
                     $facets->addGroup($group);
                     // choice type
                     if ((string) $config["type"] == "choice") {
                         foreach ($config->choice as $choice) {
                             foreach ($facetFields["counts"] as $counts) {
                                 if ($counts["value"] == (string) $choice["internal"]) {
                                     $facet = new Search\Facet();
                                     $facet->name = (string) $choice["public"];
                                     $facet->count = $counts["count"];
                                     $facet->key = $counts["value"];
                                     $group->addFacet($facet);
                                 }
                             }
                         }
                     } else {
                         foreach ($facetFields["counts"] as $counts) {
                             // skip excluded facets
                             if ($group->name == 'ContentType' && in_array($counts["value"], $this->formats_exclude)) {
                                 continue;
                             }
                             $facet = new Search\Facet();
                             $facet->name = $counts["value"];
                             $facet->count = $counts["count"];
                             $group->addFacet($facet);
                         }
                     }
                 }
             }
         }
     }
     return $facets;
 }
Example #9
0
 /**
  * Extract facets from the Solr response
  * 
  * @param simplexml	$xml	solr response
  * @return Facets, null if none
  */
 protected function extractFacets($xml)
 {
     $facets = new Search\Facets();
     $groups = $xml->xpath("//lst[@name='facet_fields']/lst");
     if ($groups !== false && count($groups) > 0) {
         foreach ($groups as $facet_group) {
             // if only one entry, then all the results have this same facet,
             // so no sense even showing this as a limit
             $count = count($facet_group->int);
             if ($count <= 1) {
                 continue;
             }
             $group_internal_name = (string) $facet_group["name"];
             $group = new Search\FacetGroup();
             $group->name = $group_internal_name;
             $group->public = $this->config->getFacetPublicName($group_internal_name);
             // put facets into an array
             $facet_array = array();
             foreach ($facet_group->int as $int) {
                 $facet_array[(string) $int["name"]] = (int) $int;
             }
             // date
             $decade_display = array();
             $is_date = $this->config->isDateType($group_internal_name);
             if ($is_date == true) {
                 $date_arrays = $group->luceneDateToDecade($facet_array);
                 $decade_display = $date_arrays["display"];
                 $facet_array = $date_arrays["decades"];
             }
             foreach ($facet_array as $key => $value) {
                 $facet = new Search\Facet();
                 $facet->name = $key;
                 $facet->count = $value;
                 // dates are different
                 if ($is_date == true) {
                     $facet->name = $decade_display[$key];
                     $facet->is_date = true;
                     $facet->key = $key;
                 }
                 $group->addFacet($facet);
             }
             $facets->addGroup($group);
         }
     }
     return $facets;
 }
Example #10
0
 /**
  * Extract facets from the Solr response
  * 
  * @param simplexml	$xml	solr response
  * @return Facets           null if none
  */
 protected function extractFacets($xml)
 {
     $facets = new Search\Facets();
     $groups = $xml->xpath("//lst[@name='facet_fields']/lst");
     if ($groups !== false && count($groups) > 0) {
         foreach ($groups as $facet_group) {
             // if only one entry, then all the results have this same facet,
             // so no sense even showing this as a limit
             $count = count($facet_group->int);
             if ($count <= 1) {
                 continue;
             }
             $group_internal_name = (string) $facet_group["name"];
             $group = new Search\FacetGroup();
             $group->name = $group_internal_name;
             // put facets into an array
             $facet_array = array();
             foreach ($facet_group->int as $int) {
                 $facet_array[(string) $int["name"]] = (int) $int;
             }
             $is_date = $this->config->isDateType($group_internal_name);
             foreach ($facet_array as $key => $value) {
                 $facet = new Search\Facet();
                 $facet->name = $key;
                 $facet->count = $value;
                 if ($is_date == true) {
                     $facet->is_date = true;
                     $facet->timestamp = strtotime("1/1/{$key}") * 1000;
                 }
                 $group->addFacet($facet);
             }
             $facets->addGroup($group);
         }
     }
     return $facets;
 }
Example #11
0
 /**
  * Parse facets out of the response
  *
  * @param DOMDocument $dom 	Primo XML
  * @return Facets
  */
 protected function extractFacets(\DOMDocument $dom)
 {
     $facets = new Search\Facets();
     $id = $this->query->getHash();
     // header("Content-type: text/plain");	print_r($this->query->getLimits());
     // see if this is in the cache
     $xml = $this->cache->get($id);
     if ($xml == "") {
         $this->cache->set($id, $dom->saveXML());
     } else {
         $dom = new \DOMDocument();
         $dom->loadXML($xml);
     }
     $groups = $dom->getElementsByTagName("FACET");
     // otherwise, grab the response
     if ($groups->length > 0) {
         // we'll pass the facets into an array so we can control both which
         // ones appear and in what order in the Xerxes config
         $facet_group_array = array();
         foreach ($groups as $facet_group) {
             $facet_values = $facet_group->getElementsByTagName("FACET_VALUES");
             // if only one entry, then all the results have this same facet,
             // so no sense even showing this as a limit
             if ($facet_values->length <= 1) {
                 continue;
             }
             $group_internal_name = $facet_group->getAttribute("NAME");
             $facet_group_array[$group_internal_name] = $facet_values;
         }
         // now take the order of the facets as defined in xerxes config
         foreach ($this->config->getFacets() as $group_internal_name => $facet_values) {
             // we defined it, but it's not in the primo response
             if (!array_key_exists($group_internal_name, $facet_group_array)) {
                 continue;
             }
             $group = new Search\FacetGroup();
             $group->name = $group_internal_name;
             // get the actual facets out of the array above
             $facet_values = $facet_group_array[$group_internal_name];
             // and put them in their own array so we can mess with them
             $facet_array = array();
             foreach ($facet_values as $facet_value) {
                 $key = $facet_value->getAttribute("KEY");
                 $value = $facet_value->getAttribute("VALUE");
                 $facet_array[$key] = $value;
             }
             // date
             $decade_display = array();
             $is_date = $this->config->isDateType($group_internal_name);
             if ($is_date == true) {
                 $date_arrays = $group->luceneDateToDecade($facet_array);
                 $decade_display = $date_arrays["display"];
                 $facet_array = $date_arrays["decades"];
             } elseif ($group->name != "tlevel") {
                 // not a date, sort by hit count
                 arsort($facet_array);
             }
             // now make them into group facet objects
             foreach ($facet_array as $key => $value) {
                 $facet = new Search\Facet();
                 $facet->count = $value;
                 $facet->name = $key;
                 if ($group->name == "tlevel" || $group->name == "pfilter") {
                     $facet->name = str_replace('_', ' ', $facet->name);
                     $facet->name = ucwords($facet->name);
                 }
                 // is this an excluded facet?
                 foreach ($this->query->getLimits() as $limit) {
                     if ($limit->field == $group->name && $limit->value == $key && $limit->boolean == "NOT") {
                         $facet->is_excluded = 1;
                     }
                 }
                 // dates are different
                 if ($is_date == true) {
                     $facet->name = $decade_display[$key];
                     $facet->is_date = true;
                     $facet->key = $key;
                 }
                 $group->addFacet($facet);
             }
             $facets->addGroup($group);
         }
     }
     return $facets;
 }