/** * 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; }
/** * 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; }
/** * 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); }
/** * Paging element * * @param int $total total # of hits for query * @param int $start start value for the page * @param int $max maximum number of results to show * * @return DOMDocument formatted paging navigation */ public function pager($total, $start, $max) { if ($total < 1) { return null; } $objXml = new \DOMDocument(); $objXml->loadXML("<pager />"); $base_record = 1; // starting record in any result set $page_number = 1; // starting page number in any result set $bolShowFirst = false; // show the first page when you get past page 10 if ($start == 0) { $start = 1; } $current_page = ($start - 1) / $max + 1; // calculates the current selected page $bottom_range = $current_page - 5; // used to show a range of pages $top_range = $current_page + 5; // used to show a range of pages $total_pages = ceil($total / $max); // calculates the total number of pages // for pages 1-10 show just 1-10 (or whatever records per page) if ($bottom_range < 5) { $bottom_range = 0; } if ($current_page < $max) { $top_range = 10; } else { $bolShowFirst = true; } // chop the top pages as we reach the end range if ($top_range > $total_pages) { $top_range = $total_pages; } // see if we even need a pager if ($total > $max) { // show first page if ($bolShowFirst == true) { $objPage = $objXml->createElement("page", "1"); $params = $this->currentParams(); $params["start"] = 1; $link = $this->request->url_for($params); $objPage->setAttribute("link", Parser::escapeXml($link)); $objPage->setAttribute("type", "first"); $objXml->documentElement->appendChild($objPage); } // create pages and links while ($base_record <= $total) { if ($page_number >= $bottom_range && $page_number <= $top_range) { if ($current_page == $page_number) { $objPage = $objXml->createElement("page", $page_number); $objPage->setAttribute("here", "true"); $objXml->documentElement->appendChild($objPage); } else { $objPage = $objXml->createElement("page", $page_number); $params = $this->currentParams(); $params["start"] = $base_record; $link = $this->request->url_for($params); $objPage->setAttribute("link", Parser::escapeXml($link)); $objXml->documentElement->appendChild($objPage); } } $page_number++; $base_record += $max; } $next = $start + $max; if ($next <= $total) { $objPage = $objXml->createElement("page", ""); // element to hold the text_results_next label $params = $this->currentParams(); $params["start"] = $next; $link = $this->request->url_for($params); $objPage->setAttribute("link", Parser::escapeXml($link)); $objPage->setAttribute("type", "next"); $objXml->documentElement->appendChild($objPage); } } return $objXml; }
/** * Add global array as xml to request xml document * * @param DOMDocument $xml [by reference] request xml document * @param DOMNode $objAppend [by reference] node to append values to * @param array $arrValues global array */ private function addElement(&$xml, &$objAppend, $arrValues) { foreach ($arrValues as $key => $value) { // @todo: change this to 'data' element and fix xslt // need to make sure the xml element has a valid name // and not something crazy with spaces or commas, etc. $strSafeKey = Parser::strtolower(preg_replace('/\\W/', '_', $key)); if (is_array($value)) { foreach ($value as $strKey => $strValue) { $objElement = $xml->createElement($strSafeKey); $objElement->setAttribute('original_key', $key); $objElement->setAttribute("key", $strKey); $objAppend->appendChild($objElement); if (is_array($strValue)) { // multi-dimensional arrays will be recursively added $this->addElement($xml, $objElement, $strValue); } else { $objElement->nodeValue = Parser::escapeXml($strValue); } } } else { $objElement = $xml->createElement($strSafeKey, Parser::escapeXml($value)); $objElement->setAttribute('original_key', $key); $objAppend->appendChild($objElement); } } }
/** * Serialize to XML * * @return DOMDocument */ public function toXML() { $objXml = new \DOMDocument(); $objXml->loadXML("<xerxes_record />"); $properties = $this->getProperties(); #### special handling // normalized title $title_normalized = $this->getTitle(true); if ($title_normalized != "") { $properties['title_normalized'] = $title_normalized; } // journal title $journal_title = $this->getJournalTitle(true); if ($journal_title != "") { $properties['journal_title'] = $journal_title; } // primary author $primary_author = $this->getPrimaryAuthor(true); if ($primary_author != "") { $properties['primary_author'] = $primary_author; } // full-text indicator if ($this->hasFullText()) { $properties['full_text_bool'] = 1; } // authors if (count($this->authors) > 0) { $objAuthors = $objXml->createElement("authors"); $x = 1; foreach ($this->authors as $objXerxesAuthor) { $objAuthor = $objXml->createElement("author"); $objAuthor->setAttribute("type", $objXerxesAuthor->type); if ($objXerxesAuthor->additional == true) { $objAuthor->setAttribute("additional", "true"); } if ($objXerxesAuthor->last_name != "") { $objAuthorLast = $objXml->createElement("aulast", Parser::escapeXml($objXerxesAuthor->last_name)); $objAuthor->appendChild($objAuthorLast); } if ($objXerxesAuthor->first_name != "") { $objAuthorFirst = $objXml->createElement("aufirst", Parser::escapeXml($objXerxesAuthor->first_name)); $objAuthor->appendChild($objAuthorFirst); } if ($objXerxesAuthor->init != "") { $objAuthorInit = $objXml->createElement("auinit", Parser::escapeXml($objXerxesAuthor->init)); $objAuthor->appendChild($objAuthorInit); } if ($objXerxesAuthor->name != "") { $objAuthorCorp = $objXml->createElement("aucorp", Parser::escapeXml($objXerxesAuthor->name)); $objAuthor->appendChild($objAuthorCorp); } if ($objXerxesAuthor->display != "") { $objAuthorDisplay = $objXml->createElement("display", Parser::escapeXml($objXerxesAuthor->display)); $objAuthor->appendChild($objAuthorDisplay); } $objAuthor->setAttribute("rank", $x); if ($x == 1 && $this->editor == true) { $objAuthor->setAttribute("editor", "true"); } $objAuthors->appendChild($objAuthor); $x++; } $objXml->documentElement->appendChild($objAuthors); } // standard numbers if (count($this->issns) > 0 || count($this->isbns) > 0 || $this->govdoc_number != "" || $this->gpo_number != "" || $this->oclc_number != "") { $objStandard = $objXml->createElement("standard_numbers"); if (count($this->issns) > 0) { foreach ($this->issns as $strIssn) { $objIssn = $objXml->createElement("issn", Parser::escapeXml($strIssn)); $objStandard->appendChild($objIssn); } } if (count($this->isbns) > 0) { foreach ($this->isbns as $strIsbn) { $objIssn = $objXml->createElement("isbn", Parser::escapeXml($strIsbn)); $objStandard->appendChild($objIssn); } } if ($this->govdoc_number != "") { $objGovDoc = $objXml->createElement("gpo", Parser::escapeXml($this->govdoc_number)); $objStandard->appendChild($objGovDoc); } if ($this->gpo_number != "") { $objGPO = $objXml->createElement("govdoc", Parser::escapeXml($this->gpo_number)); $objStandard->appendChild($objGPO); } if ($this->oclc_number != "") { $objOCLC = $objXml->createElement("oclc", Parser::escapeXml($this->oclc_number)); $objStandard->appendChild($objOCLC); } $objXml->documentElement->appendChild($objStandard); } ## basic elements foreach ($properties as $key => $value) { // these are utility variables if ($key == "utility" || in_array($key, $this->utility)) { continue; } // these we handled these above if ($key == "authors" || $key == "isbns" || $key == "issns" || $key == "govdoc_number" || $key == "gpo_number" || $key == "oclc_number") { continue; } // otherwise, create a new node Parser::addToXML($objXml, $key, $value); } return $objXml; }
/** * 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 = Parser::escapeXml($this->getDisplay()); $xml->url = $this->getURL(); return Parser::convertToDOMDocument($xml); }