/** * Get the defined public names of a set of facet values for media * This function name is defined in configuration file pazpar2.xml * * @param Search\FacetGroup $group * @param string $internal_group group internal id * @param array $keys pz2 facet key:count array * * @return Search\FacetGroup Populated facetgroup object */ public function getFacetObjects($group, $internal_group, $keys) { // populate the name map if required $query = "//config[@name='facet_fields']/facet[@internal='{$internal_group}']/@name_filter"; $do_filter = (bool) $this->xml->xpath($query); if ($do_filter) { $function = 'get' . ucfirst($internal_group) . 'Map'; $params[0] = $group; $params[1] = array_keys($keys); $map = call_user_func_array(array($this, $function), $params); } foreach ($keys as $key => $value) { $facet = new Search\Facet(); if ($do_filter) { $facet->key = $key; $facet->name = $map[$key]; } else { $facet->name = $key; } $facet->count = $value; $group->addFacet($facet); } return $group; }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }