function addItemEntryByItemUnique($db, $item_unique, $app_root) { $dom = new DOMDocument('1.0', 'utf-8'); $doc = new Dase_DBO_ItemAtom($db); $doc->unique_id = $item_unique; if ($doc->findOne()) { $xml = $doc->doc; $xml = str_replace('{APP_ROOT}', $app_root, $xml); $dom->loadXml($xml); $e = $dom->getElementsByTagNameNS(Dase_Atom::$ns['atom'], 'entry'); $root = $e->item(0); $root = $this->dom->importNode($root, true); $entry = new Dase_Atom_Entry_Item($this->dom, $root); $this->_entries[] = $entry; return $entry; } }
public function getResultsAsItemAtom() { $app_root = $this->request->app_root; $dom = new DOMDocument('1.0', 'utf-8'); $dom->loadXml($this->_getSearchResults()); $url = $this->_cleanUpUrl($this->request->getUrl()); $total = 0; foreach ($dom->getElementsByTagName('result') as $el) { if ('response' == $el->getAttribute('name')) { $total = $el->getAttribute('numFound'); } } $id = $app_root . '/search/' . md5($url); $updated = date(DATE_ATOM); //todo: probably the q param preg_match('/.*(\\?|&|&)q=([^&]+)/i', urldecode($this->solr_search_url), $matches); //solr escaped " fix $query = stripslashes(htmlspecialchars($matches[2])); $num = $this->num; $previous = 0; $next = 0; if ($num < $total) { $next = $num + 1; } if ($num > 1) { $previous = $num - 1; } //todo: optimize!! no need to get results again $ids = $this->getResultsAsIds(); if (!$this->uid && count($ids)) { $this->uid = $ids[0]; } $next_uid = ''; $prev_uid = ''; $key = array_search($this->uid, $ids); if (false === $key) { //this would be the case when the uid points to a record //not actually in the search results. $total = 0; } else { if ($key + 1 < $total) { $next_uid = $ids[$key + 1]; } if ($key > 0) { $prev_uid = $ids[$key - 1]; } } $feed = <<<EOD <feed xmlns="http://www.w3.org/2005/Atom" \t xmlns:thr="http://purl.org/syndication/thread/1.0"> <author> \t<name>DASe (Digital Archive Services)</name> \t<uri>http://daseproject.org</uri> \t<email>admin@daseproject.org</email> </author> <title>DASe Search Result</title> <link rel="alternate" title="Search Result" href="{$url}" type="text/html"/> <updated>{$updated}</updated> <category term="searchitem" scheme="http://daseproject.org/category/feedtype"/> <id>{$id}</id> <totalResults xmlns="http://a9.com/-/spec/opensearch/1.1/">{$total}</totalResults> <startIndex xmlns="http://a9.com/-/spec/opensearch/1.1/">{$this->start}</startIndex> <itemsPerPage xmlns="http://a9.com/-/spec/opensearch/1.1/">{$this->max}</itemsPerPage> <Query xmlns="http://a9.com/-/spec/opensearch/1.1/" role="request" searchTerms="{$query}"/> EOD; //omit format param $item_request_url = preg_replace('/(\\?|&|&)format=\\w+/i', '', $this->request->url); //omit num param $item_request_url = preg_replace('/(\\?|&|&)num=\\w+/i', '', $item_request_url); //omit uid param $item_request_url = preg_replace('/(\\?|&|&)uid=[^&]*/i', '', $item_request_url); $item_request_url = htmlspecialchars($item_request_url); if ($next_uid) { $next_url = $item_request_url . '&num=' . $next . '&uid=' . $next_uid; $feed .= "\n <link rel=\"next\" href=\"{$next_url}\"/>"; } if ($prev_uid) { $previous_url = $item_request_url . '&num=' . $previous . '&uid=' . $prev_uid; $feed .= "\n <link rel=\"previous\" href=\"{$previous_url}\"/>"; } $search_request_url = str_replace('search/item', 'search', $this->request->url); //omit format param $search_request_url = preg_replace('/(\\?|&|&)format=\\w+/i', '', $search_request_url); //omit num param $search_request_url = preg_replace('/(\\?|&|&)num=\\w+/i', '', $search_request_url); //omit uid param $search_request_url = preg_replace('/(\\?|&|&)uid=[^&]*/i', '', $search_request_url); $search_request_url = htmlspecialchars($search_request_url); if ($total) { $doc = new Dase_DBO_ItemAtom($this->db); $doc->unique_id = $this->uid; $doc->findOne(); $entry = $doc->doc; $added = <<<EOD <link rel="up" href="{$search_request_url}"/> <category term="{$num}" scheme="http://daseproject.org/category/position"/> EOD; $entry = str_replace('<author>', $added . "\n <author>", $entry); $feed .= $entry; } $feed .= "</feed>"; $feed = str_replace('{APP_ROOT}', $app_root, $feed); return $feed; }
function storeDoc($type = "") { $app_root = "{APP_ROOT}"; if (!$type || 'json' == $type) { $doc = new Dase_DBO_ItemJson($this->db); $doc->unique_id = $this->getUnique(); if ($doc->findOne()) { $doc->updated = date(DATE_ATOM); $doc->doc = $this->buildJson($app_root); $doc->update(); } else { $doc->updated = date(DATE_ATOM); $doc->doc = $this->buildJson($app_root); $doc->insert(); } } if (!$type || 'atom' == $type) { $entry = new Dase_Atom_Entry_Item(); $entry = $this->injectAtomEntryData($entry, $app_root); $doc = new Dase_DBO_ItemAtom($this->db); $doc->unique_id = $this->getUnique(); if ($doc->findOne()) { $doc->updated = date(DATE_ATOM); //passing in entry root prevent xml declaration $doc->doc = $entry->asXml($entry->root); $doc->update(); } else { $doc->updated = date(DATE_ATOM); $doc->doc = $entry->asXml($entry->root); $doc->insert(); } } }