setQuery('SELECT `value` FROM `#__openaire` WHERE `key` = \'thriftConnectorHost\''); $host = $db -> loadResult(); $db -> setQuery('SELECT `value` FROM `#__openaire` WHERE `key` = \'thriftConnectorPort\''); $port = $db -> loadResult(); // Create a thrift connection (Boiler plate) $socket = new TSocket($host, $port); $socket->setRecvTimeout(10000); $this->transport = new TBufferedTransport($socket); $protocol = new TBinaryProtocol($this->transport); $this->client = new OpenAireConnectorClient($protocol); } function getSelectedDocuments() { $session =& JFactory::getSession(); $docs = $session->get('selectedDocuments', array()); $ret = array(); foreach ($docs as $doc) $ret[] = unserialize($doc); return $ret; } public function addSelectDocument($doc) { $session =& JFactory::getSession(); $selected = $session->get('selectedDocuments'); if (isset($selected)) { foreach ($selected as $sdoc) { if (unserialize($sdoc)->claimInfo == $doc->claimInfo && unserialize($sdoc)->claimSource == $doc->claimSource) { return; } } } $selected[] = serialize($doc); $session->set('selectedDocuments', $selected); } public function unselectDocument($docId) { $session =& JFactory::getSession(); $selected = $session->get('selectedDocuments'); unset($selected[$docId]); $selected = array_values($selected); $session->set('selectedDocuments', $selected); } function identifyDoi($doi) { $params = array( "noredirect" => "true", "pid" => "antleb@di.uoa.gr", "format" => "unixref", "id" => "doi:".trim($doi)); $url = "http://www.crossref.org/openurl"; $resp = trim($this->load($url, $params)); $doc = $this->parseCrossrefResponse($resp); if ($doc != null) { $doc->claimSource = 'doi'; $doc->claimInfo = $doi; } return $doc; } public function identifyORCID($orcidId) { $url = "http://pub.orcid.org/".$orcidId."/orcid-works"; $params = array(); $resp = trim($this->load($url, $params)); $docs = $this->parseORCIDResponse($resp); foreach ($docs as $doc) { $doc->claimSource = 'orcid'; $doc->claimInfo = $orcidId.'_'.$doc->claimInfo; } return $docs; } function identifyArxiv($arxivId) { $params = array("id_list" => $arxivId); $url = "http://export.arxiv.org/api/query"; $resp = trim($this->load($url, $params)); $doc = $this->parseArxivResponse($resp); if ($doc != null) { $doc->claimSource = 'arxiv'; $doc->claimInfo = $arxivId; } return $doc; } function identifyPubmed($pubmedId) { $params = array( "db" => "pubmed", "id" => $pubmedId, "retmode" => "xml"); $url = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?"; $resp = trim($this->load($url, $params)); $doc = $this->parsePubmedResponse($resp); if ($doc != null) { $doc->claimSource = 'pubmed'; $doc->claimInfo = $pubmedId; } return $doc; } public function reset() { $session =& JFactory::getSession(); $session->set('selectedDocuments', null); $session->set('selectedProjects', null); } public function addSelectedProject($projectInfo) { $projects = unserialize(JFactory::getSession()->get('selectedProjects', serialize(array()))); $projects[$projectInfo->projectId] = $projectInfo; JFactory::getSession()->set('selectedProjects', serialize($projects)); } public function removeSelectedProject($projectId) { $projects = unserialize(JFactory::getSession()->get('selectedProjects', serialize(array()))); if ($projects != null) { unset($projects[$projectId]); } JFactory::getSession()->set('selectedProjects', serialize($projects)); } public function getSelectedProjects() { $projects = unserialize(JFactory::getSession()->get('selectedProjects', serialize(array()))); return $projects; } public function getClaimedDocuments() { $email = JFactory::getUser()->email; // Open up the connection $this->transport->open(); // submit the docs $list = $this->client->getUserClaims($email); $this->transport->close(); return $list; } public function getDocumentsByProject($grantAgreementNumber, $includePending = false) { // Open up the connection $this->transport->open(); // submit the docs $list = $this->client->getProjectDocuments($grantAgreementNumber, $includePending); $this->transport->close(); return $list; } public function getAllClaimedDocuments($from, $to, $orderBy = "title") { // Open up the connection $this->transport->open(); // submit the docs $list = $this->client->getAllClaims($from, $to, $orderBy); $this->transport->close(); return $list; } public function submitClaimedDocuments() { $selectedDocuments = $this->getSelectedDocuments(); $userInfo = $this->getCurrentUserInfo(); $projects = array(); foreach ($this->getSelectedProjects() as $proj) { $projects[] = $proj->projectId; } // Open up the connection $this->transport->open(); // submit the docs foreach ($selectedDocuments as $doc) { $vars = get_object_vars($doc); $vars['authors'] = array(); $vars['projects'] = array(); foreach ($doc->authors as $author) $vars['authors'][] = new AuthorInfo(get_object_vars($author)); $doc = new Document($vars); // JLog :: getInstance()->addEntry(array('level' => 'debug', 'comment' => (var_export($doc, true)))); $id = $this->client->insertDocument($doc->claimSource, $doc->claimInfo); $this->client->updateDocument($id, $doc); $this->client->claimDocument($id, $userInfo, $projects); } $this->transport->close(); } private function getCurrentUserInfo() { $juser = JFactory::getUser(); return new UserInfo(array( "email" => $juser->email, "lastName" => $juser->name)); } public function updateDocument($id, $doc) { // Open up the connection $this->transport->open(); // submit the docs $this->client->updateDocument($id, $doc); $this->transport->close(); } public function claimDocument($documentId, $projects) { $user = $this->getCurrentUserInfo(); // Open up the connection $this->transport->open(); // submit the docs $this->client->claimDocument($documentId, $user, $projects); $this->transport->close(); } public function rejectDocument($documentId, $projectId) { $user = $this->getCurrentUserInfo(); // Open up the connection $this->transport->open(); // submit the docs $this->client->rejectDocument($documentId, $user, $projectId); $this->transport->close(); } public function deleteClaim($docId) { // Open up the connection $this->transport->open(); // submit the docs $this->client->unclaimDocument($docId); $this->transport->close(); } public function parseCrossrefResponse($xml) { $xmldoc = new SimpleXMLElement(str_replace('xmlns=', 'ns=', $xml)); if (count ($xmldoc->xpath("/doi_records")) > 0 && count($xmldoc->xpath("/doi_records/doi_record/crossref/error")) == 0) { $doc = new Document; $res = $xmldoc->xpath('//publication_date/year'); while(list( , $node) = each($res)) { $doc->publicationDate = (string) $node; } $res = $xmldoc->xpath("//contributors/person_name[@contributor_role='author']"); $authors = array(); while(list( , $node) = each($res)) { $authors[] = new AuthorInfo(array("name"=>((string)$node->given_name), "surname"=>((string)$node->surname))); } $doc->authors = $authors; $res = $xmldoc->xpath('//titles/title'); while(list( , $node) = each($res)) { $doc->title = (string) $node; } $res = $xmldoc->xpath('//doi_data/resource'); while(list( , $node) = each($res)) { $doc->url = (string) $node; } //JLog :: getInstance()->addEntry(array('level' => 'debug', 'comment' => (var_export($doc, true)))); return $doc; } else { return null; } } private function parseORCIDResponse($xml) { $res = array(); $xmldoc = new SimpleXMLElement(str_replace('xmlns=', 'ns=', $xml)); if (count($xmldoc->xpath("//orcid-work")) > 0) { $docs = $xmldoc->xpath("//orcid-work"); while (list( , $docxml) = each($docs)) { if (count($docxml->xpath('work-external-identifiers/work-external-identifier[work-external-identifier-type = "doi"]/work-external-identifier-id')) > 0) { $doi = $docxml->xpath('work-external-identifiers/work-external-identifier[work-external-identifier-type = "doi"]/work-external-identifier-id'); $doi = (string) $doi[0][0]; //JLog :: getInstance()->addEntry(array('level' => 'debug', 'comment' => ((string) $docxml->attributes()))); $doc = $this->identifyDoi($doi); if ($doc != null) { $doc->claimSource = 'orcid'; $doc->claimInfo = (string) $docxml->attributes(); $res[] = $doc; } } else { $doc = new Document(); $foo = $docxml->xpath('work-title/title/text()'); $doc->title = (string) $foo[0]; $foo = $docxml->xpath('publication-date/year/text()'); $doc->publicationDate = (string) $foo[0]; $foo = $docxml->xpath('url/text()'); $doc->url = (string) $foo[0]; $doc->claimSource = 'orcid'; $doc->claimInfo = (string) $docxml->attributes(); $res[] = $doc; $authors = $docxml->xpath('contributor[contributor-attributes/contributor-role = "author"]/credit-name'); JLog :: getInstance()->addEntry(array('level' => 'debug', 'comment' => (var_export($authors, true)))); if (count($authors) > 0) { $authorList = array(); while(list( , $node) = each($authors)) { // $authorList[] = new AuthorInfo(array("name" => ""), "surname" => ((string)$node->credit-name))); } $doc->authors = $authorList; } } } } return $res; } public function parsePubmedResponse($xml) { $xmldoc = new SimpleXMLElement(str_replace('xmlns=', 'ns=', $xml)); if (count ($xmldoc->xpath("//Article")) > 0) { $doc = new Document; $res = $xmldoc->xpath('//ArticleDate'); while(list( , $node) = each($res)) { $date = ((string) $node->Year); $date .= '/'.((string) $node->Month); $date .= '/'.((string) $node->Day); $doc->publicationDate = $date; } $res = $xmldoc->xpath("//Article//Author"); $authors = array(); while(list( , $node) = each($res)) { $authors[] = new AuthorInfo(array("name"=>((string)$node->ForeName), "surname"=>((string)$node->LastName))); } $doc->authors = $authors; $res = $xmldoc->xpath('//ArticleTitle'); while(list( , $node) = each($res)) { $doc->title = (string) $node; } $res = $xmldoc->xpath('//Article/Abstract'); while(list( , $node) = each($res)) { $doc->description = ((string) $node->AbstractText); } $res = $xmldoc->xpath('//PubmedData/ArticleIdList/ArticleId[@IdType="pubmed"]'); while(list( , $node) = each($res)) { $doc->url = 'http://www.ncbi.nlm.nih.gov/pubmed/'.((string) $node); } $doc->repositoryName = 'UK PubMed Central'; return $doc; } else { return null; } } public function parseArxivResponse($xml) { $xmldoc = new SimpleXMLElement(str_replace('xmlns=', 'ns=', $xml)); if (count ($xmldoc->xpath("//feed/entry")) > 0) { $doc = new Document; $res = $xmldoc->xpath('//entry/published'); while(list( , $node) = each($res)) { $doc->publicationDate = (string) $node; } $res = $xmldoc->xpath("//entry/author"); $authors = array(); while(list( , $node) = each($res)) { $authors[] = new AuthorInfo(array("name"=>((string)$node->name))); } $doc->authors = $authors; $res = $xmldoc->xpath('//entry/title'); while(list( , $node) = each($res)) { $doc->title = (string) $node; } $res = $xmldoc->xpath('//entry/id'); while(list( , $node) = each($res)) { $doc->url = (string) $node; } $res = $xmldoc->xpath('//entry/summary'); while(list( , $node) = each($res)) { $doc->dDescription = (string) $node; } return $doc; } else { return null; } } public function parseSearchResult($fields) { $doc = new Document; $doc->claimSource = 'driver'; foreach ($fields as $field) { switch (trim($field['name'])) { case 'id': $doc->claimInfo = trim($field['values'][0]['displayValue']); break; case '${title}': if (count($field['values']) > 0) { $doc->title = $field['values'][0]['displayValue']; if ($field['values'][0]['type'] == 'url' && $field['values'][0]['urlType'] == 'external') $doc->url = $field['values'][0]['href']; } else { $doc->title = "-"; } break; case 'Author(s)': $authors = array(); foreach ($field['values'] as $value) { $authors[] = new AuthorInfo(array("name"=>trim($value['displayValue']))); } $doc->authors = $authors; break; // multiple value fields case 'Publisher(s)': foreach ($field['values'] as $value) { $doc->publisher = trim($value['displayValue']); } break; case 'Subject(s)': foreach ($field['values'] as $value) { $doc->subjects = trim($value['displayValue']); } break; // single value fields case 'Language(s)': if (count($field['values']) > 0) $doc->language = trim($field['values'][0]['displayValue']); else $doc->language = "-"; break; case 'Description': if (count($field['values']) > 0) $doc->description = trim($field['values'][0]['displayValue']); else $doc->description = "-"; break; case 'Repository': if (count($field['values']) > 0) $doc->repositoryName = trim($field['values'][0]['displayValue']); else $doc->repositoryName = "-"; break; case 'Publication Date(s)': if (count($field['values']) > 0) $doc->publicationDate = trim($field['values'][0]['displayValue']); else $doc->publicationDate = "-"; break; } } return $doc; } private function load($url, $params) { $cache = & JFactory::getCache('com_openaire', 'callback'); $conf =& JFactory::getConfig(); $cacheactive = $conf->getValue('config.caching'); $cache->setCaching(1); //enable caching $result = $cache->call('load', $url, $params); $cache->setCaching($cacheactive); return $result; } } ?>