/** * Check spelling * * @param QueryTerms[] $query_terms */ public function checkSpelling(array $query_terms) { $registry = Registry::getInstance(); $app_id = $registry->getConfig('BING_ID', true); $suggestion = new Suggestion(); $client = Factory::getHttpClient(); // @todo: see if we can't collapse multiple terms into a single spellcheck query foreach ($query_terms as $term) { $query = $term->phrase; $query = urlencode(trim($query)); $correction = null; // get spell suggestion try { $url = "http://api.search.live.net/xml.aspx?Appid={$app_id}&sources=spell&query={$query}"; $response = $client->getUrl($url); // process it $xml = Parser::convertToDOMDocument($response); // echo header("Content-Type: text/xml"); echo $xml->saveXML(); exit; $suggestion_node = $xml->getElementsByTagName('Value')->item(0); if ($suggestion_node != null) { $correction = $suggestion_node->nodeValue; } } catch (\Exception $e) { trigger_error('Could not process spelling suggestion: ' . $e->getTraceAsString(), E_USER_WARNING); } // got one if ($correction != null) { $term->phrase = $suggestion_node->nodeValue; $suggestion->addTerm($term); } } return $suggestion; }
/** * Parse the primo response * * @param string $response * @return ResultSet */ public function parseResponse($response) { // load it $xml = Parser::convertToDOMDocument($response); // header("Content-type:text/xml"); echo $xml->saveXML(); exit; // check for errors $error = $xml->getElementsByTagName("ERROR")->item(0); if ($error != "") { throw new \Exception($error->getAttribute("MESSAGE")); } // set-up the result set $result_set = new Search\ResultSet($this->config); // total $docset = $xml->getElementsByTagName("DOCSET")->item(0); if ($docset == null) { throw new \Exception("Could not determine total number of records"); } $total = $docset->getAttribute("TOTALHITS"); $result_set->total = $total; // extract records foreach ($this->extractRecords($xml) as $xerxes_record) { $result_set->addRecord($xerxes_record); } // facets $facets = $this->extractFacets($xml); $result_set->setFacets($facets); return $result_set; }
public function loadXML($doc) { $id = null; $format = null; $score = null; $xml_data = ""; foreach ($doc->str as $str) { // marc record if ((string) $str["name"] == 'fullrecord') { $marc = trim((string) $str); // marc-xml if (substr($marc, 0, 5) == '<?xml') { $xml_data = $marc; } else { $marc = preg_replace('/#31;/', "", $marc); $marc = preg_replace('/#30;/', "", $marc); $marc_file = new \File_MARC($marc, \File_MARC::SOURCE_STRING); $marc_record = $marc_file->next(); $xml_data = $marc_record->toXML(); } } elseif ((string) $str["name"] == 'id') { $id = (string) $str; } } // format foreach ($doc->arr as $arr) { if ($arr["name"] == "format") { $format = (string) $arr->str; } } // score foreach ($doc->float as $float) { if ($float["name"] == "score") { $score = (string) $float; } } // load marc data $this->marc = new MarcRecord(); $this->marc->loadXML($xml_data); // save for later $this->document = Parser::convertToDOMDocument($doc); $this->serialized = $doc->asXml(); // process it $this->map(); $this->cleanup(); // add non-marc data $this->setRecordID($id); $this->setScore($score); $this->format()->setInternalFormat($format); $this->format()->setPublicFormat($format); // custom mappings if (class_exists('Local\\Model\\Solr\\Record')) { $local = new \Local\Model\Solr\Record(); $local->map($this, $this->marc); } }
/** * Serialize to XML * * @return array */ public function toXML() { $xml = Parser::convertToDOMDocument('<holding />'); foreach ($this->data as $name => $value) { $line = $xml->createElement('data', Parser::escapeXml($value)); $line->setAttribute('key', $name); $xml->documentElement->appendChild($line); } return $xml; }
/** * Parses a validation response from a CAS server to see if the returning CAS request is valid * * @param string $results xml or plain text response from cas server * @return bool true if valid, false otherwise * @exception throws exception if cannot parse response or invalid version */ private function isValid() { // values from the request $ticket = $this->request->getParam("ticket"); // configuration settings $configCasValidate = $this->registry->getConfig("CAS_VALIDATE", true); $configCasValidate = rtrim($configCasValidate, '/'); // figure out which type of response this is based on the service url $arrURL = explode("/", $configCasValidate); $service = array_pop($arrURL); // now get it! $url = $configCasValidate . "?ticket=" . $ticket . "&service=" . urlencode($this->validate_url); $client = Factory::getHttpClient(); $req = $client->get($url); $req->getCurlOptions()->set(CURLOPT_SSL_VERIFYHOST, false); $req->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, false); $response = $req->send(); $results = (string) $response->getBody(); // validate is plain text if ($service == "validate") { $message_array = explode("\n", $results); if (count($message_array) >= 2) { if ($message_array[0] == "yes") { return $message_array[1]; } } else { throw new \Exception("Could not parse CAS validation response."); } } elseif ($service == "serviceValidate" || $service == "proxyValidate") { // these are XML based $xml = Parser::convertToDOMDocument($results); $cas_namespace = "http://www.yale.edu/tp/cas"; $user = $xml->getElementsByTagNameNS($cas_namespace, "user")->item(0); $failure = $xml->getElementsByTagNameNS($cas_namespace, "authenticationFailure")->item(0); if ($user != null) { if ($user->nodeValue != "") { return $user->nodeValue; } else { throw new \Exception("CAS validation response missing username value"); } } elseif ($failure != null) { // see if error, rather than failed authentication if ($failure->getAttribute("code") == "INVALID_REQUEST") { throw new \Exception("Invalid request to CAS server: " . $failure->nodeValue); } } else { throw new \Exception("Could not parse CAS validation response."); } } else { throw new \Exception("Unsupported CAS version."); } // if we got this far, the request was invalid return false; }
/** * Get the display name of the group * * @param string $id group id * @return steing display name, or id if none found */ public function getGroupDisplayName($id) { $id = Parser::strtoupper($id); //case insensitive if (array_key_exists($id, $this->usergroups)) { $group = $this->usergroups[$id]; return (string) $group->display_name; } else { return $id; } }
public function hitsAction() { $total = $this->catalog->getTotal($this->query); // format it $total = Parser::number_format($total); // and tell the browser too $this->response->setVariable('hits', $total); // view template $this->response->setView('search/hits.xsl'); return $this->response; }
public function toXML() { $xml = Parser::convertToDOMDocument("<category />"); $xml->documentElement->setAttribute("name", $this->name); $xml->documentElement->setAttribute("normalized", $this->normalized); foreach ($this->subcategories as $subcategory) { $import = $xml->importNode($subcategory->toXML()->documentElement, true); $xml->documentElement->appendChild($import); } return $xml; }
public function toXML() { $xml = Parser::convertToDOMDocument("<subcategory />"); $xml->documentElement->setAttribute("name", $this->name); $xml->documentElement->setAttribute("sequence", $this->sequence); $xml->documentElement->setAttribute("id", $this->databases_id); foreach ($this->databases as $database) { $import = $xml->importNode($database->toXML()->documentElement, true); $xml->documentElement->appendChild($import); } return $xml; }
/** * Convert to EDS individual record syntax * * @param string $id * @return Url */ public function getRecordUrl($id) { if ($id == "") { throw new \DomainException('No record ID supplied'); } $database = Parser::removeRight($id, "-"); $id = Parser::removeLeft($id, "-"); // build request $url = $this->server . 'retrieve?'; $url .= 'dbid=' . $database; $url .= '&an=' . urlencode($id); $url .= '&includefacets=n'; return new Url($url, $this->headers); }
/** * Add a database to the local knowledgebase * * @param Database $objDatabase */ public function addDatabase(Database $objDatabase) { // load our data into xml object $xml = simplexml_load_string($objDatabase->data); // these fields have boolen values in metalib $boolean_fields = array("proxy", "searchable", "guest_access", "subscription", "sfx_suppress", "new_resource_expiry"); // normalize boolean values foreach ($xml->children() as $child) { $name = (string) $child->getName(); $value = (string) $child; if (in_array($name, $boolean_fields)) { $xml->{$name} = $this->convertMetalibBool($value); } } // remove empty nodes $dom = Parser::convertToDOMDocument($xml->asXML()); $xmlPath = new \DOMXPath($dom); $xmlNullNodes = $xmlPath->query('//*[not(node())]'); foreach ($xmlNullNodes as $node) { $node->parentNode->removeChild($node); } $objDatabase->data = $dom->saveXML(); // add the main database entries $this->doSimpleInsert("xerxes_databases", $objDatabase); // now also extract searchable fields so we can populate the search table // get fields from config foreach ($this->searchable_fields as $search_field) { $search_field = trim($search_field); foreach ($xml->{$search_field} as $field) { $searchable_terms = array(); foreach (explode(" ", (string) $field) as $term) { // only numbers and letters please $term = preg_replace('/[^a-zA-Z0-9]/', '', $term); $term = strtolower($term); // anything over 50 chars is likley a URL or something if (strlen($term) > 50) { continue; } array_push($searchable_terms, $term); } // remove duplicate terms $searchable_terms = array_unique($searchable_terms); // insert em $strSQL = "INSERT INTO xerxes_databases_search ( database_id, field, term ) " . "VALUES ( :metalib_id, :field, :term )"; foreach ($searchable_terms as $unique_term) { $this->insert($strSQL, array(":metalib_id" => $objDatabase->metalib_id, ":field" => $search_field, ":term" => $unique_term)); } } } }
/** * Convert to Ebsco individual record syntax * * @param string $id * @return Url */ public function getRecordUrl($id) { if (!strstr($id, "-")) { throw new \Exception("could not find record"); } // database and id come in on same value, so split 'em $database = Parser::removeRight($id, "-"); $id = Parser::removeLeft($id, "-"); // get results $query = "AN {$id}"; // construct url $url = $this->host . '/Search?' . 'prof=' . $this->username . '&pwd=' . $this->password . '&authType=&ipprof=' . '&query=' . urlencode($query) . "&db={$database}" . '&startrec=1&numrec=1&format=detailed'; return new Url($url); }
/** * Converts a string to a normalized (no-spaces, non-letters) string * * @param string $subject original string * @return string normalized string */ public static function normalize($region) { // this is influenced by the setlocale() call with category LC_CTYPE; see PopulateDatabases.php $normalized = iconv('UTF-8', 'ASCII//TRANSLIT', $region); $normalized = Parser::strtolower($normalized); $normalized = str_replace("&", "", $normalized); $normalized = str_replace("'", "", $normalized); $normalized = str_replace("+", "-", $normalized); $normalized = str_replace(" ", "-", $normalized); $normalized = Parser::preg_replace('/\\W/', "-", $normalized); while (strstr($normalized, "--")) { $normalized = str_replace("--", "-", $normalized); } return $normalized; }
/** * Load data from XML */ public function loadXML($xml) { $this->document = Parser::convertToDOMDocument($xml); $this->xpath = new \DOMXPath($this->document); // test to see what profile the context object is using // set namespace accordingly if ($this->document->getElementsByTagNameNS("info:ofi/fmt:xml:xsd:book", "book")->item(0) != null) { $this->xpath->registerNamespace("rft", "info:ofi/fmt:xml:xsd:book"); } elseif ($this->document->getElementsByTagNameNS("info:ofi/fmt:xml:xsd:dissertation", "dissertation")->item(0) != null) { $this->xpath->registerNamespace("rft", "info:ofi/fmt:xml:xsd:dissertation"); } elseif ($this->document->getElementsByTagNameNS("info:ofi/fmt:xml:xsd", "journal")->item(0) != null) { $this->xpath->registerNamespace("rft", "info:ofi/fmt:xml:xsd"); } else { $this->xpath->registerNamespace("rft", "info:ofi/fmt:xml:xsd:journal"); } $this->map(); $this->cleanup(); }
public function hitsAction() { // create an identifier for this search $id = $this->helper->getQueryID(); // see if one exists in session already $total = $this->request->getSessionData($id); // nope if ($total == null) { // so do a search (just the hit count) // and cache the hit total $total = $this->engine->getHits($this->query); $this->request->setSessionData($id, (string) $total); } // format it $total = Parser::number_format($total); // and tell the browser too $this->data["hits"] = $total; return $this->data; }
public function getRecommendations(Xerxes\Record $xerxes_record, $min_relevance = 0, $max_records = 10) { $bx_records = array(); // now get the open url $open_url = $xerxes_record->getOpenURL(null, $this->sid); $open_url = str_replace('genre=unknown', 'genre=article', $open_url); // send it to bx service $url = $this->url . "/recommender/openurl?token=" . $this->token . "&{$open_url}" . "&res_dat=source=global&threshold={$min_relevance}&maxRecords={$max_records}"; try { $client = Factory::getHttpClient(); $client->setUri($url); $client->setOptions(array('timeout' => 4)); $xml = $client->send()->getBody(); if ($xml == "") { throw new \Exception("No response from bx service"); } } catch (\Exception $e) { // just issue the exception as a warning trigger_error("Could not get result from bX service: " . $e->getTraceAsString(), E_USER_WARNING); return $bx_records; } // header("Content-type: text/xml"); echo $xml; exit; $doc = Parser::convertToDOMDocument($xml); $xpath = new \DOMXPath($doc); $xpath->registerNamespace("ctx", "info:ofi/fmt:xml:xsd:ctx"); $records = $xpath->query("//ctx:context-object"); foreach ($records as $record) { $bx_record = new Record(); $bx_record->loadXML($record); array_push($bx_records, $bx_record); } if (count($bx_records) > 0) { // first one is the record we want to find recommendations for // so skip it; any others are actual recommendations array_shift($bx_records); } return $bx_records; }
/** * Load from MARC-XML source * * @param string|DOMNode|DOMDocument $node */ public function loadXML($node = null) { if ($node != null) { // make sure we have a DOMDocument $this->document = Parser::convertToDOMDocument($node); // extract the three data types $leader = $this->document->getElementsByTagName("leader"); $control_fields = $this->document->getElementsByTagName("controlfield"); $data_fields = $this->document->getElementsByTagName("datafield"); // leader $this->leader = new Leader($leader->item(0)); // control fields foreach ($control_fields as $control_field) { $controlfield = new ControlField($control_field); array_push($this->_controlfields, $controlfield); } // data fields foreach ($data_fields as $data_field) { $datafield = new DataField($data_field); array_push($this->_datafields, $datafield); } } }
/** * Create a normalize category name (lowercase, just alpha and dashes) * from supplied name * * @param string $name [optional] will otherwise use name property */ public function setNormalizedFromName($name = null) { if ($name == null) { $name = $this->name; } // convert accented character and the like to just ascii equivalent // this is influenced by the setlocale() call with category LC_CTYPE $this->normalized = iconv('UTF-8', 'ASCII//TRANSLIT', $name); $this->normalized = Parser::strtolower($this->normalized); // strip out weird characters $this->normalized = str_replace("&", "", $this->normalized); $this->normalized = str_replace("'", "", $this->normalized); // convert these to dashes $this->normalized = str_replace("+", "-", $this->normalized); $this->normalized = str_replace(" ", "-", $this->normalized); // now any other non-word character to a dash $this->normalized = Parser::preg_replace('/\\W/', "-", $this->normalized); // pair multiple dashes down to one while (strstr($this->normalized, "--")) { $this->normalized = str_replace("--", "-", $this->normalized); } return $this; }
/** * Return an array of the phrase with all terms AND'd, while * preserving boolean operators and quoted phrases * * @return array */ public function normalizedArray($phrase = "") { if ($phrase == "") { $phrase = $this->phrase; } $bolQuote = false; // flags the start and end of a quoted phrase $arrWords = array(); // the query broken into a word array $arrFinal = array(); // final array of words $strQuote = ""; // quoted phrase // strip extra spaces while (strstr($phrase, " ")) { $phrase = str_replace(" ", " ", $phrase); } // split words into an array $arrWords = explode(" ", $phrase); // cycle thru each word in the query for ($x = 0; $x < count($arrWords); $x++) { if ($bolQuote == true) { // we are inside of a quoted phrase $strQuote .= " " . $arrWords[$x]; if (strpos($arrWords[$x], "\"") !== false) { // the end of a quoted phrase $bolQuote = false; if ($x + 1 < count($arrWords)) { if ($arrWords[$x + 1] != "and" && $arrWords[$x + 1] != "or" && $arrWords[$x + 1] != "not") { // the next word is not a boolean operator, // so AND the current one array_push($arrFinal, $strQuote); array_push($arrFinal, "AND"); } else { array_push($arrFinal, $strQuote); } } else { array_push($arrFinal, $strQuote); } $strQuote = ""; } } elseif ($bolQuote == false && strpos($arrWords[$x], "\"") !== false) { // this is the start of a quoted phrase $strQuote .= " " . $arrWords[$x]; $bolQuote = true; } elseif ($arrWords[$x] == "and" || $arrWords[$x] == "or" || $arrWords[$x] == "not") { // the current word is a boolean operator array_push($arrFinal, Parser::strtoupper($arrWords[$x])); } else { if ($x + 1 < count($arrWords)) { if ($arrWords[$x + 1] != "and" && $arrWords[$x + 1] != "or" && $arrWords[$x + 1] != "not") { // the next word is not a boolean operator, // so AND the current one array_push($arrFinal, $arrWords[$x]); array_push($arrFinal, "AND"); } else { array_push($arrFinal, $arrWords[$x]); } } else { array_push($arrFinal, $arrWords[$x]); } } } // single quoted phrase if (count($arrFinal) == 0 && $strQuote != "") { array_push($arrFinal, $strQuote); } return $arrFinal; }
/** * Return an individual record * * @param string record identifier * @return ResultSet */ protected function doGetRecord($id) { $results = new Search\ResultSet($this->config); // get the record from the database $record = $this->datamap->getRecordByID($id); // no record found? if ($record == null) { $results->total = 0; return $results; } // got one $results->total = 1; $result = $this->createSearchResult($record); // corrupted record, look out if ($result->corrupted == true) { $fixed = false; $data = $record->marc; // go back to the original search engine and fetch it again $class_name = 'Application\\Model\\' . ucfirst($result->source) . '\\Engine'; if (class_exists($class_name)) { try { $engine = new $class_name(); $new_results = $engine->getRecord($result->original_id); if ($new_results->total > 0) { $result = $new_results->getRecord(0); $fixed = true; } } catch (NotFoundException $e) { if (strstr($data, 'Xerxes_TransRecord')) { $data = '<?xml version="1.0"?>' . Parser::removeLeft($data, '<?xml version="1.0"?>'); $data = Parser::removeRight($data, '</xerxes_record>') . '</xerxes_record>'; $xerxes_record = new Xerxes\Record(); $xerxes_record->loadXML($data); $record->xerxes_record = $xerxes_record; // recreate the result, since we need the openurl and such $result = $this->createSearchResult($record); $fixed = true; } } } elseif (strstr($data, 'Xerxes_MetalibRecord')) { $data = '<?xml' . Parser::removeLeft($data, '<?xml'); $data = Parser::removeRight($data, '</x_server_response>') . '</x_server_response>'; $xerxes_record = new \Xerxes_MetalibRecord(); $xerxes_record->loadXML($data); $record->xerxes_record = $xerxes_record; // recreate the result, since we need the openurl and such $result = $this->createSearchResult($record); $fixed = true; } if ($fixed == false) { throw new \Exception('Sorry, this record has been corrupted'); } } // if a catalog record, fetch holdings if ($record->xerxes_record instanceof Solr\Record) { try { $engine = new Solr\Engine(); $solr_results = $engine->getRecord($result->original_id); $holdings = $solr_results->getRecord(0)->getHoldings(); $result->setHoldings($holdings); } catch (\Exception $e) { trigger_error('saved records holdings lookup: ' . $e->getMessage(), E_USER_WARNING); } } $results->addResult($result); return $results; }
/** * Serialize to XML */ public function toXML() { $xml = new \SimpleXMLElement('<link />'); if ($this->isFullText()) { $xml->addAttribute("type", "full"); $xml->addAttribute("format", $this->getType()); } else { $xml->addAttribute("type", $this->getType()); } $xml->display = $this->getDisplay(); $xml->url = $this->getURL(); return Parser::convertToDOMDocument($xml); }
public function map() { // score $this->score = (string) $this->document->documentElement->getAttribute("RANK"); // record data $record = $this->document->documentElement->getElementsByTagName("record")->item(0); // print $this->document->saveXML(); $control = $this->getElement($record, "control"); $display = $this->getElement($record, "display"); $links = $this->getElement($record, "links"); $search = $this->getElement($record, "search"); $sort = $this->getElement($record, "sort"); $addata = $this->getElement($record, "addata"); $facets = $this->getElement($record, "facets"); $sourceid = ""; if ($control != null) { $sourceid = $this->getElementValue($control, "sourceid"); } if ($display != null) { // database name $this->database_name = $this->getElementValue($display, "source"); $this->database_name = strip_tags($this->database_name); // journal $this->journal = $this->getElementValue($display, "ispartof"); // snippet $this->snippet = $this->getElementValue($display, "snippet"); $this->snippet = strip_tags($this->snippet); // description $this->abstract = $this->getElementValue($display, "description"); $this->abstract = strip_tags($this->abstract); // language $this->language = $this->getElementValue($display, "language"); // peer reviewed $peer_reviewed = $this->getElementValue($display, 'lds50'); if ($peer_reviewed == 'peer_reviewed') { $this->refereed = true; } } if ($search != null) { // record id $this->record_id = $this->getElementValue($search, "recordid"); // year $this->year = $this->getElementValue($search, "creationdate"); // issn $issn = $this->getElementValue($search, "issn"); $issn = preg_replace('/\\D/', "", $issn); array_push($this->issns, $issn); // authors $authors = $this->getElementValues($search, "creatorcontrib"); foreach ($authors as $author) { array_push($this->authors, new Xerxes\Record\Author($author, null, "personal")); } // format $format = $this->getElementValue($search, "rsrctype"); $this->format()->setInternalFormat($format); // create a readable display $format_display = self::createReadableLabel($format); $this->format()->setPublicFormat($format_display); } // article data if ($addata != null) { $this->journal_title = $this->start_page = $this->getElementValue($addata, "jtitle"); $this->volume = $this->getElementValue($addata, "volume"); $this->issue = $this->getElementValue($addata, "issue"); $this->start_page = $this->getElementValue($addata, "spage"); $this->end_page = $this->getElementValue($addata, "epage"); // primo's own ris type $ris_type = $this->getElementValue($addata, 'ristype'); if ($ris_type != "") { $this->format()->setNormalizedFormat($ris_type); } // abstract $abstract = $this->getElementValue($addata, "abstract"); if ($this->abstract == "") { $this->abstract = strip_tags($abstract); } } // subjects if ($facets != null) { $topics = $this->getElementValues($facets, "topic"); foreach ($topics as $topic) { $subject_object = new Xerxes\Record\Subject(); $subject_object->value = $topic; $subject_object->display = $topic; array_push($this->subjects, $subject_object); } } // title if ($sort != null) { $this->title = $this->getElementValue($sort, "title"); } // direct link $backlink = $this->getElementValue($links, "backlink"); if ($backlink != "") { $backlink = Parser::removeLeft($backlink, '$$U'); $url = Parser::removeRight($backlink, '$$E'); $message = Parser::removeLeft($backlink, '$$E'); $link = new Link($url); $link->setType(Link::ONLINE); $this->links[] = $link; } // Gale title clean-up, because for some reason unknown to man they put weird // notes and junk at the end of the title. so remove them here and add them to notes. if (stristr($sourceid, "gale") || stristr($sourceid, "muse")) { $gale_regex = '/\\(([^)]*)\\)/'; $matches = array(); if (preg_match_all($gale_regex, $this->title, $matches) != 0) { $this->title = preg_replace($gale_regex, "", $this->title); foreach ($matches[1] as $match) { array_push($this->notes, $match); } } if (strpos($this->abstract, 'the full-text of this article') !== false) { $this->abstract = Parser::removeLeft($this->abstract, 'Abstract:'); } } }
/** * Extracts the MARC data from the HTML response and converts it to MARC-XML * * @param string $marc marc data as string * @return DOMDocument marc-xml document */ protected function extractMarc($response) { $xml = Parser::convertToDOMDocument("<record xmlns=\"http://www.loc.gov/MARC21/slim\" />"); $marc = ""; // marc data as text $arrTags = array(); // array to hold each MARC tag if (!stristr($response, "<pre>")) { // didn't find a record return $xml; } // parse out MARC data $marc = Parser::removeLeft($response, "<pre>"); $marc = Parser::removeRight($marc, "</pre>"); // remove break-tabs for easier parsing $marc = str_replace(" \n ", " ", $marc); $marc = str_replace("\n ", " ", $marc); $marc = trim($marc); // assign the marc values to the array based on Unix LF as delimiter $arrTags = explode("\n", $marc); foreach ($arrTags as $strTag) { // assign tag # and identifiers $strTagNumber = substr($strTag, 0, 3); $strId1 = substr($strTag, 4, 1); $strId2 = substr($strTag, 5, 1); // assign data and clean it up $data = substr($strTag, 7); // only convert all data to utf8 if told to do so, // but always do it to the leader, since it has mangled chars if ($this->convert_to_utf8 == true || $strTagNumber == "LEA") { if (function_exists("mb_convert_encoding")) { $data = mb_convert_encoding($data, "UTF-8"); } else { $data = utf8_encode($data); } } $data = Parser::escapeXml($data); $data = trim($data); if ($strTagNumber == "LEA") { // leader $objLeader = $xml->createElementNS($this->marc_ns, "leader", $data); $xml->documentElement->appendChild($objLeader); } elseif ($strTagNumber == "REC") { // Pseudo-MARC "REC" data field to store the INNOPAC // bibliographic record number in subfield a. $objRecNum = $xml->createElementNS($this->marc_ns, "datafield"); $objRecNum->setAttribute("tag", "REC"); $objRecNum->setAttribute("ind1", ' '); $objRecNum->setAttribute("ind2", ' '); $objRecNumSub = $xml->createElementNS($this->marc_ns, "subfield", strtolower($data)); $objRecNumSub->setAttribute("code", 'a'); $objRecNum->appendChild($objRecNumSub); $xml->documentElement->appendChild($objRecNum); } elseif ((int) $strTagNumber <= 8) { // control fields $objControlField = $xml->createElementNS($this->marc_ns, "controlfield", $data); $objControlField->setAttribute("tag", $strTagNumber); $xml->documentElement->appendChild($objControlField); } else { // data fields $objDataField = $xml->createElementNS($this->marc_ns, "datafield"); $objDataField->setAttribute("tag", $strTagNumber); $objDataField->setAttribute("ind1", $strId1); $objDataField->setAttribute("ind2", $strId2); // if first character is not a pipe symbol, then this is the default |a subfield // so make that explicit for the array if (substr($data, 0, 1) != "|") { $data = "|a " . $data; } // split the subfield data on the pipe and add them in using the first // character after the delimiter as the subfield code $arrSubFields = explode("|", $data); foreach ($arrSubFields as $strSubField) { if ($strSubField != "") { $code = substr($strSubField, 0, 1); $data = trim(substr($strSubField, 1)); // check for a url, in which case we need to ensure there are no spaces; // which can happen on the wrap of the data in the marc display if (strlen($data) > 4) { if (substr($data, 0, 4) == "http") { $data = str_replace(" ", "", $data); } } $objSubField = $xml->createElementNS($this->marc_ns, "subfield", $data); $objSubField->setAttribute("code", $code); $objDataField->appendChild($objSubField); } } $xml->documentElement->appendChild($objDataField); } } return $xml; }
/** * Is the user inside the local ip range */ public function isInLocalIpRange() { return Parser::isIpAddrInRanges($this->ip_address, $this->ip_range); }
public function extractFormat($data_fields) { if (is_array($data_fields)) { $data_fields = implode(" ", $data_fields); // combine them into a string } $data_fields = Parser::strtolower($data_fields); if (strstr($data_fields, 'dissertation')) { return self::Thesis; } if (strstr($data_fields, 'proceeding')) { return self::ConferenceProceeding; } if (strstr($data_fields, 'conference')) { return self::ConferencePaper; } if (strstr($data_fields, 'hearing')) { return self::Hearing; } if (strstr($data_fields, 'working')) { return self::UnpublishedWork; } if (strstr($data_fields, 'book review') || strstr($data_fields, 'review-book')) { return self::BookReview; } if (strstr($data_fields, 'film review') || strstr($data_fields, 'film-book')) { return self::Review; } if (strstr("{$data_fields} ", 'review ')) { return self::Review; } if (strstr($data_fields, 'book art') || strstr($data_fields, 'book ch') || strstr($data_fields, 'chapter')) { return self::BookSection; } if (strstr($data_fields, 'journal')) { return self::Article; } if (strstr($data_fields, 'periodical') || strstr($data_fields, 'serial')) { return self::Article; } if (strstr($data_fields, 'book')) { return self::Book; } if (strstr($data_fields, 'pamphlet')) { return self::Pamphlet; } if (strstr($data_fields, 'essay')) { return self::Article; } if (strstr($data_fields, 'article')) { return self::Article; } // if we got this far, just return unknown return self::Unknown; }
/** * Append an item to the xml * * @param DOMDocument $xml * @param string $id * @param mixed $value */ private function appendElement(&$xml, $id, $value) { $new = $xml->createElement($id, Parser::escapeXml($value)); $xml->documentElement->appendChild($new); }
public function getOriginalXML($bolString = false) { // convert original (JSON-based) array to xml $this->document = Parser::convertToDOMDocument('<original />'); Parser::addToXML($this->document, 'record', $this->original_array); return parent::getOriginalXML($bolString); }
/** * 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; }
/** * Add a facet * * @param Facet $facets */ public function addFacet(Facet $facet) { $facet->count_display = Parser::number_format($facet->count); array_push($this->facets, $facet); }
/** * Add links to the query object limits * * @param Query $query */ public function addQueryLinks(Query $query) { // we have to pass in the query object here rather than take // the property above because adding the links doesn't seem // to reflect back in the main object, even though they should // be references, maybe because limit objects are in an array? // search option links $search = $this->registry->getConfig('search'); if ($search instanceof \SimpleXMLElement) { $controller_map = $this->request->getControllerMap(); foreach ($search->xpath("//option") as $option) { // format the number // is this the current tab? if ($controller_map->getControllerName() == (string) $option["id"] && ($this->request->getParam('source') == (string) $option["source"] || (string) $option["source"] == '')) { $option->addAttribute('current', "1"); } // url $params = $query->extractSearchParams(); $params['controller'] = $controller_map->getUrlAlias((string) $option["id"]); $params['action'] = "results"; $params['source'] = (string) $option["source"]; $url = $this->request->url_for($params); $option->addAttribute('url', $url); // cached search hit count? foreach ($this->request->getAllSessionData() as $session_id => $session_value) { // does this value in the cache have the save id as our tab? $id = str_replace("_" . $query->getHash(), "", $session_id); if ($id == (string) $option["id"]) { // yup, so add it $option->addAttribute('hits', Parser::number_format($session_value)); } } } $this->registry->setConfig('search', $search); } // links to remove facets foreach ($query->getLimits() as $limit) { $params = $this->currentParams(); $params = Parser::removeFromArray($params, $limit->field, $limit->value); $limit->remove_url = $this->request->url_for($params); } }