cache = JCache :: getInstance();
$this -> http = new JHttp();
$parameters = JComponentHelper :: getParams('com_openaire');
$socket = new TSocket($parameters -> get('thriftHost'), intval($parameters -> get('thriftPort')));
$socket -> setRecvTimeout(intval($parameters -> get('thriftTimeout')));
$this -> transport = new TBufferedTransport($socket);
$this -> client = new OpenAIREConnectorClient(new TBinaryProtocol($this -> transport));
$this -> transport -> open();
}
// Close Thrift transport
public function __destruct() {
$this -> transport -> close();
}
// Get the selected projects stored in session.
// return the selected projects (array) or NULL if any errors occur
public function getSelectedProjects($suffix = self :: CLAIM) {
try {
$projects = unserialize(JFactory :: getSession() -> get(self :: SELECTED_PROJECTS . $suffix, serialize(array())));
JLog :: add('Retrieved ' . count($projects) . ' selected projects', JLog :: INFO, self :: LOG);
return $projects;
} catch (Exception $e) {
JLog :: add('Error retrieving selected projects: ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
return NULL;
}
}
// Add a project to the selected projects stored in session.
// $project the project to add
public function addSelectedProject($project, $suffix = self :: CLAIM) {
try {
$session = JFactory :: getSession();
$projects = unserialize($session -> get(self :: SELECTED_PROJECTS . $suffix, serialize(array())));
$projects[$project -> id] = $project;
$session -> set(self :: SELECTED_PROJECTS . $suffix, serialize($projects));
JLog :: add('Added project ' . $project -> id . ' to selected projects', JLog :: INFO, self :: LOG);
} catch (Exception $e) {
JLog :: add('Error adding project ' . $project -> id . ' to selected projects: ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
}
}
// Remove a project from the selected projects stored in session.
// $projectId the identifier of the project to remove
public function removeSelectedProject($projectId, $suffix = self :: CLAIM) {
try {
$session = JFactory :: getSession();
$projects = unserialize($session -> get(self :: SELECTED_PROJECTS . $suffix, serialize(array())));
unset($projects[$projectId]);
$session -> set(self :: SELECTED_PROJECTS . $suffix, serialize($projects));
JLog :: add('Removed project ' . $projectId . ' from selected projects', JLog :: INFO, self :: LOG);
} catch (Exception $e) {
JLog :: add('Error removing project ' . $projectId . ' from selected projects: ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
}
}
//empties selected concepts
public function emptySelectedProjects($suffix = self :: CLAIM) {
try {
$session = JFactory :: getSession();
$session -> set(self :: SELECTED_PROJECTS . $suffix, serialize(array()));
} catch (Exception $e) {
;
}
}
//empties selected publications
public function emptySelectedPublications($suffix = self :: CLAIM) {
try {
$session = JFactory :: getSession();
$session -> set(self :: SELECTED_PUBLICATIONS . $suffix, serialize(array()));
} catch (Exception $e) {
;
}
}
//Get concept using id
//returns concept object or error
public function getConcept($conceptId, $withChildren = false){
$contexts = $this->getContexts();
$concept = new JObject();
$concept->context = new JObject();
$concept->category = new JObject();
$concept->concepts = array();
$concept->id = $conceptId;
$data = explode("::", $conceptId);
if (count($data) > 1){
list ($context, $category) = $data;
$category = $context . "::" . $category;
}
else{
$context = $data[0];
$category = null;
}
if (!isset($contexts[$context]))
return NULL;
$concept->context->id = $context;
$concept->context->name = $contexts[$context]->name;
if ($category == null){
$concept->category = null;
$concept->path = $this->getConceptPath($concept);
return $concept;
}
if (!isset($contexts[$context]->categories[$category]))
return NULL;
$concept->category->id = $category;
$concept->category->name = $contexts[$context]->categories[$category]->name;
$concepts = $contexts[$context]->categories[$category]->concepts;
$conceptId2 = $category;
for ($i = 2; $i < count($data); $i++){
$conceptId2 = $conceptId2 . "::" . $data[$i];
$concept_s = new JObject();
$concept_s -> id = $conceptId2;
$concept_s -> name = $concepts[$conceptId2] -> name;
$concepts = isset($concepts[$conceptId2] -> concepts)?$concepts[$conceptId2] -> concepts:array();
$concept_s -> concepts = $concepts;
$concept -> concepts[] = $concept_s;
}
$concept->path = $this->getConceptPath($concept);
return $concept;
}
//Get concept using id
//returns concept object or error
public function getConceptPath($concept){
$path = $concept->context->name . ($concept->category != null?" > " . $concept->category->name:"");
$concept_parts = array();
foreach ($concept -> concepts as $concept_part){
$concept_parts[] = $concept_part -> name;
}
$path = $path . (count($concept_parts)?" > " . implode(" > ", $concept_parts):"");
return $path;
}
// Get the selected concepts stored in session.
// return the selected concepts (array) or NULL if any errors occur
public function getSelectedConcepts($suffix = self :: CLAIM) {
try {
$concepts = unserialize(JFactory :: getSession() -> get(self :: SELECTED_CONCEPTS . $suffix, serialize(array())));
JLog :: add('Retrieved ' . count($concepts) . ' selected concepts', JLog :: INFO, self :: LOG);
return $concepts;
} catch (Exception $e) {
JLog :: add('Error retrieving selected concepts: ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
return NULL;
}
}
// Add a project to the selected projects stored in session.
// $project the project to add
public function addSelectedConcept($concept, $suffix = self :: CLAIM) {
try {
$session = JFactory :: getSession();
$concepts = unserialize($session -> get(self :: SELECTED_CONCEPTS . $suffix, serialize(array())));
$concepts[$concept -> id] = $concept;
$session -> set(self :: SELECTED_CONCEPTS . $suffix, serialize($concepts));
JLog :: add('Added project ' . $concept -> id . ' to selected projects', JLog :: INFO, self :: LOG);
} catch (Exception $e) {
JLog :: add('Error adding project ' . $concept -> id . ' to selected projects: ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
}
}
// Remove a project from the selected projects stored in session.
// $projectId the identifier of the project to remove
public function removeSelectedConcept($conceptId, $suffix = self :: CLAIM) {
try {
$session = JFactory :: getSession();
$concepts = unserialize($session -> get(self :: SELECTED_CONCEPTS . $suffix, serialize(array())));
unset($concepts[$conceptId]);
$session -> set(self :: SELECTED_CONCEPTS . $suffix, serialize($concepts));
JLog :: add('Removed project ' . $conceptId . ' from selected projects', JLog :: INFO, self :: LOG);
} catch (Exception $e) {
JLog :: add('Error removing project ' . $conceptId . ' from selected projects: ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
}
}
//empties selected concepts
public function emptySelectedConcepts($suffix = self :: CLAIM) {
try {
$session = JFactory :: getSession();
$session -> set(self :: SELECTED_CONCEPTS . $suffix, serialize(array()));
} catch (Exception $e) {
;
}
}
// Get the selected publications stored in session.
// return the selected publications (arary) or NULL if any errors occur
public function getSelectedPublications($suffix = self :: CLAIM) {
try {
$publications = unserialize(JFactory :: getSession() -> get(self :: SELECTED_PUBLICATIONS . $suffix, serialize(array())));
JLog :: add('Retrieved ' . count($publications) . ' selected publications', JLog :: INFO, self :: LOG);
return $publications;
} catch (Exception $e) {
JLog :: add('Error retrieving selected publications: ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
return NULL;
}
}
// Add a publication to the selected publications stored in session.
// $publication the publication to add
public function addSelectedPublication($publication, $suffix = self :: CLAIM) {
try {
$session = JFactory :: getSession();
$publications = unserialize($session -> get(self :: SELECTED_PUBLICATIONS . $suffix, serialize(array())));
$publications[$publication -> source . $publication -> id] = $publication;
$session -> set(self :: SELECTED_PUBLICATIONS . $suffix, serialize($publications));
JLog :: add('Added ' . $publication -> source . ' publication ' . $publication -> id . ' to selected publications', JLog :: INFO, self :: LOG);
} catch (Exception $e) {
JLog :: add('Error adding ' . $publication -> source . ' publication ' . $publication -> id . ' to selected publications: ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
}
}
// Remove a publication from the selected publications stored in session.
// $source the source of the publication to remove
// $publicationId the identifier of the publication to remove
public function removeSelectedPublication($source, $publicationId, $suffix = self :: CLAIM) {
try {
$session = JFactory :: getSession();
$publications = unserialize($session -> get(self :: SELECTED_PUBLICATIONS . $suffix, serialize(array())));
unset($publications[$source . $publicationId]);
$session -> set(self :: SELECTED_PUBLICATIONS. $suffix, serialize($publications));
JLog :: add('Removed ' . $source . ' publication ' . $publicationId . ' from selected publications', JLog :: INFO, self :: LOG);
} catch (Exception $e) {
JLog :: add('Error removing ' . $source . ' publication ' . $publicationId . ' from selected publications: ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
}
}
// Claim all the selected publications.
// $user the user to claim publications for
public function claimSelectedPublications($user, $mode = self :: CLAIM, $activePublication = NULL, $suffix = "", $activePublicationType = "publication") {
try {
$projects = array_map(function ($project) {
$project2 = new Project();
$project2 -> projectId = $project -> id;
$project2 -> type = NULL;
return $project2;}, $this -> getSelectedProjects($mode.$suffix));
$concepts = array_map(function ($concept) {return $concept -> id;}, $this -> getSelectedConcepts($mode.$suffix));
if ($mode != self :: CLAIMINLINE){
$selectedPublications = $this -> getSelectedPublications($mode.$suffix);
}
else {
$selectedPublications = array($activePublication);
}
$dcSource = NULL;
$subjects = array();
$category = NULL;
foreach ($selectedPublications as $publication) {
$authors = array_map(function ($author) {
$a = array();
$a['id'] = $author -> id;
$a['firstName'] = $author -> firstName;
$a['lastName'] = $author -> lastName;
if (($author -> lastName == NULL) && ($author -> fullName != NULL))
$a['lastName'] = $author -> fullName;
return new Author($a);
}, $publication -> authors);
$doi = (($publication -> source == self :: DOI) || ($publication -> source == self :: DATACITE)) ? $publication -> id : NULL;
if ($mode !== self :: CLAIMINLINE and $publication -> source !== "openaire"){
$docId = $this -> client -> insertDocument($publication -> id, $publication -> source, $publication -> title, $publication -> description, isset($publication -> license)?$publication -> license:"", $publication -> embargoEndDate, $authors, $publication -> url, $dcSource, $user -> email, $subjects, $publication -> year, $publication -> publisher, $publication -> language, $category, $doi);
}else{
$docId = $publication->id;
}
foreach ($concepts as $concept)
$this -> client -> insertConcept($docId, $publication -> source, $concept, $user -> email);
foreach ($projects as $project){
$this -> client -> insertProjectRelation($docId, $publication -> source, $project, $user -> email);
}
if ($activePublication != NULL && $mode === self::CLAIMINLINE2){
$this -> client -> insertResultRelation($activePublication -> id, $activePublicationType,
$activePublication -> source, $docId, $publication -> claim_type, $user -> email);
}
}
if ($this -> cache -> getCaching()){
$cacheId = self :: CLAIMED_PUBLICATIONS_CACHE_ID . '.' . $user -> username;
$this -> cache ->remove($cacheId);
}
JLog :: add('Claimed ' . count($selectedPublications) . ' publications (user: ' . $user -> username . ')', JLog :: INFO, self :: LOG);
return true;
} catch (Exception $e) {
JLog :: add('Error claiming selected publications (user: ' . $user -> username . '): ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
return false;
}
}
// Get the publications claimed by a user using caching if enabled.
// $user the user whose claimed publications to retrieve
// return an array of publications
public function getClaimedPublications($user) {
/* if ($this -> cache -> getCaching()) {
$cacheId = self :: CLAIMED_PUBLICATIONS_CACHE_ID . '.' . $user -> username;
$publications = $this -> cache -> get($cacheId, self :: CACHE_GROUP);
if ($publications === FALSE) {
$publications = $this -> _getClaimedPublications($user);
if ($publications !== NULL)
$this -> cache -> store($publications, $cacheId, self :: CACHE_GROUP);
}
} else
*/ $publications = $this -> _getClaimedPublications($user);
return $publications;
}
// Get the publications claimed by a user using caching if enabled.
// $user the user whose claimed publications to retrieve
// return an array of publications
public function getAllClaimedPublications($from, $to) {
/*if ($this -> cache -> getCaching()) {
$cacheId = self :: CLAIMED_PUBLICATIONS_CACHE_ID . '.' . $from . "." . $to;
$publications = $this -> cache -> get($cacheId, self :: CACHE_GROUP);
if ($publications === FALSE) {
$publications = $this -> _getAllClaimedPublications($from, $to);
if ($publications !== NULL)
$this -> cache -> store($publications, $cacheId, self :: CACHE_GROUP);
}
} else*/
$publications = $this -> _getAllClaimedPublicationsExtended($from, $to);
return $publications;
}
// Search for a publication DOI using caching if enabled.
// $doi the publication DOI to search for
// return an array of results
public function searchDOI($doi, $page, $size) {
if ($this -> cache -> getCaching()) {
$cacheId = self :: SEARCH_DOI_CACHE_ID . '.' . $doi;
$results = $this -> cache -> get($cacheId, self :: CACHE_GROUP);
if ($results === FALSE) {
$results = $this -> _searchDOI($doi, $page, $size);
if ($results !== NULL)
$this -> cache -> store($results, $cacheId, self :: CACHE_GROUP);
}
} else
$results = $this -> _searchDOI($doi, $page, $size);
return $results;
}
// Search for a dataset DOI using caching if enabled.
// $doi the dataset DOI to search for
// return a result (object) containing publications and total
public function searchDataCite($doi, $page, $size) {
if ($this -> cache -> getCaching()) {
$cacheId = self :: SEARCH_DATASET_CACHE_ID . '.' . $doi;
$results = $this -> cache -> get($cacheId, self :: CACHE_GROUP);
if ($results === FALSE) {
$results = $this -> _searchDataCite($doi, $page, $size);
if ($results !== NULL)
$this -> cache -> store($results, $cacheId, self :: CACHE_GROUP);
}
} else
$results = $this -> _searchDataCite($doi, $page, $size);
return $results;
}
// Search for publications in ORCID using caching if enabled.
// $doi the ORCID id to search for
// $page the result page to retrieve
// $size the result page size to use
// return a result (object) containing datasets and total
public function searchORCID($orcid, $page, $size) {
if ($this -> cache -> getCaching()) {
$cacheId = self :: SEARCH_ORCID_CACHE_ID . '.' . $orcid . '.' . $page . '.' . $size;
$results = $this -> cache -> get($cacheId, self :: CACHE_GROUP);
if ($results === FALSE) {
$results = $this -> _searchORCID($orcid, $page, $size);
if ($results !== NULL)
$this -> cache -> store($results, $cacheId, self :: CACHE_GROUP);
}
} else
$results = $this -> _searchORCID($orcid, $page, $size);
return $results;
}
// Get the publications claimed by a user.
// $user the user whose claimed publications to retrieve
// return an array of publications or NULL if any errors occur
private function _getClaimedPublications($user) {
try {
$locale = JFactory :: getLanguage()->getTag();
$claimedPublications = array();
$claimedPublicationsGrouped = array();
$time = microtime(TRUE);
$claims = $this -> client -> getClaimedPublications($user -> email);
foreach ($claims as $claim){
$parsedClaim = $this -> parseClaimedPublication($claim);
if ($parsedClaim == null)
continue;
$claimedPublications[] = $parsedClaim;
}
$resultsToGet = array();
$projectsToGet = array();
$claimedDocuments = array();
foreach ($claimedPublications as $claimedPublication){
if ($claimedPublication -> publication != null and $claimedPublication -> publication -> id != null)
$claimedDocuments[$claimedPublication->publicationId] = $claimedPublication -> publication;
if ($claimedPublication -> projectId !== null){
$projectsToGet[] = $claimedPublication -> projectId;
}
if ($claimedPublication -> targetPublicationId !== null){
$resultsToGet[] = $claimedPublication -> targetPublicationId;
}
if ($claimedPublication -> targetDatasetId !== null){
$resultsToGet[] = $claimedPublication -> targetDatasetId;
}
if (($claimedPublication -> publication == null || ($claimedPublication -> publication != null and $claimedPublication -> publication -> id == null))){
$resultsToGet[] = $claimedPublication -> publicationId;
}
if ($claimedPublication -> publication == null && $claimedPublication -> type == "dataset"){
$resultsToGet[] = $claimedPublication -> publicationId;
}
}
$resultsToGet = array_unique($resultsToGet);
$projectsToGet = array_unique($projectsToGet);
$searchModel = new OpenAireModelSearch();
$results = $searchModel->getResults($resultsToGet, $locale);
$projects = $searchModel->getProjects($projectsToGet, $locale);
$keydResults = array();
$keydProjects = array();
foreach ($results as $result)
$keydResults[$result->id] = $result;
foreach ($projects as $project)
$keydProjects[$project->id] = $project;
foreach ($claimedPublications as $claimedPublication){
if (!isset($claimedPublicationsGrouped[$claimedPublication -> publicationId])){
if ($claimedPublication -> publication == null || $claimedPublication -> publication -> id == null){
$publication = isset($keydResults[$claimedPublication -> publicationId])?$keydResults[$claimedPublication -> publicationId]:null;
}
else{
$publication = $claimedPublication -> publication;
}
if ($publication == null || $publication -> id == null)
continue;
$claimedPublicationsGrouped[$claimedPublication -> publicationId] = new stdClass();
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> publicationId = $claimedPublication -> publicationId;
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> date = $claimedPublication -> date;
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> publication = $publication;
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> publications = array();
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> datasets = array();
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> projects = array();
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> concepts = array();
}
if ($claimedPublicationsGrouped[$claimedPublication -> publicationId] -> date < $claimedPublication -> date)
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> date = $claimedPublication -> date;
if ($claimedPublication -> publication != null){
$claimedConcepts = array();
foreach ($claimedPublication -> publication -> concepts as $concept){
$concept = $this->getConcept($concept);
$claimedConcept = new stdClass();
$claimedConcept->id = $concept->id;
$claimedConcept->title = $concept->path;
$claimedConcept->date = $claimedPublication->date;
$claimedConcepts[] = $claimedConcept;
}
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> concepts = array_merge($claimedPublicationsGrouped[$claimedPublication -> publicationId] -> concepts, $claimedConcepts);
}
if ($claimedPublication -> targetPublicationId !== null){
$claimedPublication->title = isset($keydResults[$claimedPublication -> targetPublicationId ])?$keydResults[$claimedPublication -> targetPublicationId ]->title:null;
if ($claimedPublication->title == null){
$claimedPublication->title = isset($claimedDocuments[$claimedPublication -> targetDatasetId ])?$claimedDocuments[$claimedPublication -> targetDatasetId ]->title:null;
}
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> publications[] = $claimedPublication;
}
if ($claimedPublication -> targetDatasetId !== null){
$claimedPublication->title = isset($keydResults[$claimedPublication -> targetDatasetId ])?$keydResults[$claimedPublication -> targetDatasetId ]->title:null;
if ($claimedPublication->title == null){
$claimedPublication->title = isset($claimedDocuments[$claimedPublication -> targetDatasetId ])?$claimedDocuments[$claimedPublication -> targetDatasetId ]->title:null;
}
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> datasets[] = $claimedPublication;
}
if ($claimedPublication -> projectId !== null){
$claimedPublication->title = isset($keydProjects[$claimedPublication -> projectId ])?$keydProjects[$claimedPublication -> projectId ]->title:null;
$claimedPublication->acronym = isset($keydProjects[$claimedPublication -> projectId ])?$keydProjects[$claimedPublication -> projectId ]->acronym:null;
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> projects[] = $claimedPublication;
}
}
JLog :: add('Retrieved ' . count($claimedPublications) . ' claimed publications (user: ' . $user -> username . ') in ' . (microtime(TRUE) - $time) . ' s', JLog :: INFO, self :: LOG);
return $claimedPublicationsGrouped;
} catch (Exception $e) {
JLog :: add('Error retrieving claimed publications (user: ' . $user -> username . '): ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
return NULL;
}
}
public function _getAllClaimedPublications($from, $to) {
try {
// submit the docs
$claimedPublications = array();
$time1 = strtotime(str_replace("/", "-", $from)) * 1000;
$time2 = strtotime(str_replace("/", "-", $to)) * 1000;
$list = $this->client ->getAllClaimedPublications($time1, $time2);
foreach ($list as $claim){
$claimedPublications[] = $this -> parseClaimedPublication($claim);
}
}
catch (Exception $e) {
JLog :: add('Error retrieving all claims (from: ' . $from . ', to: '. $to .'): ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
return NULL;
}
return $claimedPublications;
}
// Get the publications claimed
// return an array of publications or NULL if any errors occur
private function _getAllClaimedPublicationsExtended($from, $to) {
try {
$locale = JFactory :: getLanguage()->getTag();
$claimedPublications = array();
$claimedPublicationsExtended = array();
$time = microtime(TRUE);
$time1 = strtotime(str_replace("/", "-", $from)) * 1000;
$time2 = strtotime(str_replace("/", "-", $to)) * 1000;
$claims = $this->client ->getAllClaimedPublications($time1, $time2);
foreach ($claims as $claim){
$parsedClaim = $this -> parseClaimedPublication($claim);
if ($parsedClaim == null)
continue;
$claimedPublications[] = $parsedClaim;
}
$resultsToGet = array();
$projectsToGet = array();
$claimedDocuments = array();
foreach ($claimedPublications as $claimedPublication){
if ($claimedPublication -> publication != null and $claimedPublication -> publication -> id != null)
$claimedDocuments[$claimedPublication->publicationId] = $claimedPublication -> publication;
if ($claimedPublication -> projectId !== null){
$projectsToGet[] = $claimedPublication -> projectId;
}
if ($claimedPublication -> targetPublicationId !== null){
$resultsToGet[] = $claimedPublication -> targetPublicationId;
}
if ($claimedPublication -> targetDatasetId !== null){
$resultsToGet[] = $claimedPublication -> targetDatasetId;
}
if (($claimedPublication -> publication == null || ($claimedPublication -> publication != null and $claimedPublication -> publication -> id == null))){
$resultsToGet[] = $claimedPublication -> publicationId;
}
if ($claimedPublication -> publication == null && $claimedPublication -> type == "dataset"){
$resultsToGet[] = $claimedPublication -> publicationId;
}
}
$resultsToGet = array_unique($resultsToGet);
$projectsToGet = array_unique($projectsToGet);
$searchModel = new OpenAireModelSearch();
$results = $searchModel->getResults($resultsToGet, $locale);
$projects = $searchModel->getProjects($projectsToGet, $locale);
$keydResults = array();
$keydProjects = array();
foreach ($results as $result)
$keydResults[$result->id] = $result;
foreach ($projects as $project)
$keydProjects[$project->id] = $project;
foreach ($claimedPublications as $claimedPublication){
/* if ($claimedPublication -> publication == null || $claimedPublication -> publication -> id == null){
//print_r($claimedPublication -> publication -> concepts);
$claimedPublication -> publication = isset($keydResults[$claimedPublication -> publicationId])?$keydResults[$claimedPublication -> publicationId]:null;
}
*/
$claimedPublication->targetConceptId=null;
if ($claimedPublication -> publication != null && isset($claimedPublication -> publication -> concepts )){
$claimedConcepts = array();
foreach ($claimedPublication -> publication -> concepts as $concept){
$concept = $this->getConcept($concept);
$claimedConcept = new stdClass();
$claimedConcept->id = $concept->id;
$claimedConcept->title = $concept->path;
$claimedConcept->date = $claimedPublication->date;
$claimedConcepts[] = $claimedConcept;
$claimedPublication->title=$concept->path;
$claimedPublication->targetConceptId= $concept->id;
}
$claimedPublication -> concepts= $claimedConcepts;
}
if ($claimedPublication -> targetPublicationId !== null){
$claimedPublication->title = isset($keydResults[$claimedPublication -> targetPublicationId ])?$keydResults[$claimedPublication -> targetPublicationId ]->title:null;
if ($claimedPublication->title == null){
$claimedPublication->title = isset($claimedDocuments[$claimedPublication -> targetDatasetId ])?$claimedDocuments[$claimedPublication -> targetDatasetId ]->title:null;
}
}
if ($claimedPublication -> targetDatasetId !== null){
$claimedPublication->title = isset($keydResults[$claimedPublication -> targetDatasetId ])?$keydResults[$claimedPublication -> targetDatasetId ]->title:null;
if ($claimedPublication->title == null){
$claimedPublication->title = isset($claimedDocuments[$claimedPublication -> targetDatasetId ])?$claimedDocuments[$claimedPublication -> targetDatasetId ]->title:null;
}
}
if ($claimedPublication -> projectId !== null){
$claimedPublication->title = isset($keydProjects[$claimedPublication -> projectId ])?$keydProjects[$claimedPublication -> projectId ]->title:null;
$claimedPublication->acronym = isset($keydProjects[$claimedPublication -> projectId ])?$keydProjects[$claimedPublication -> projectId ]->acronym:null;
}
if ($claimedPublication -> publication == null || $claimedPublication -> publication -> id == null){
//print_r($claimedPublication -> publication -> concepts);
$claimedPublication -> publication = isset($keydResults[$claimedPublication -> publicationId])?$keydResults[$claimedPublication -> publicationId]:null;
}
}
JLog :: add('Retrieved ' . count($claimedPublications) . ' claimed publications in ' . (microtime(TRUE) - $time) . ' s', JLog :: INFO, self :: LOG);
return $claimedPublications;
} catch (Exception $e) {
JLog :: add('Error retrieving claimed publications (user: ' . $user -> username . '): ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
return NULL;
}
}
// Search for a publication DOI.
// $doi the publication DOI to search for
// return a result (object) containing publications and total
private function _searchDOI($doi, $page, $size) {
try {
$result = new JObject();
$result -> publications = array();
$result -> totalPublications = 0;
$result -> totalDatasets = 0;
$dois = explode(" ", preg_replace('/\s+/', ' ',$doi));
$unique_dois =array_slice( array_unique($dois),0,10);
$pattern1 ='#\b(10[.][0-9]{4,}(?:[.][0-9]+)*/(?:(?!["&\'<>])\S)+)\b#';
$pattern2 ='#\b(10[.][0-9]{4,}(?:[.][0-9]+)*/(?:(?!["&\'<>])[[:graph:]])+)\b#';
if (preg_match($pattern1,$unique_dois[0])||preg_match($pattern2,$unique_dois[0])){
if(count($dois)>10){
$result = new JObject();
$result->publications = array();
$result->totalPublications = '-';
$result->totalDatasets = 0;
$result->maxDoisExceeded = true;
return $result;
}
JLog :: add('It;s a DOI!!!!!!!!! '.$dois[0] , JLog :: INFO, self :: LOG);
JLog :: add('Searching for DOIs ' , JLog :: INFO, self :: LOG);
foreach($unique_dois as $doi_){
JLog :: add('DOI::: '.$doi_ , JLog :: INFO, self :: LOG);
$publication = $this->_searchSingleDOI($doi_);
if ($publication != null) {
$result->publications[] = $publication;
}
}
}else{
JLog :: add('It;s NOOOOT a DOI!!!!!!!!! '.$dois[0] , JLog :: INFO, self :: LOG);
}
$result -> totalPublications = count( $result -> publications);
if($result -> totalPublications ===0){
//.'&rows='.$size.'&offset='.$page
$request = "http://api.crossref.org/works?query=" . urlencode( trim($doi))."&rows=".$size."&offset=".($page-1)*$size;
JLog :: add('Requesting ' . $request, JLog :: INFO, self :: LOG);
$time = microtime(TRUE);
$response = $this -> http -> get($request);
JLog :: add('Received response in ' . (microtime(TRUE) - $time) . ' s', JLog :: INFO, self :: LOG);
if ($response == NULL)
throw new Exception('no HTTP response');
if ($response -> code != self :: HTTP_OK)
throw new Exception('HTTP response code ' . $response -> code);
$res = array();
$res = json_decode(str_replace("-", "_", $response -> body));
if(isset($res -> status)&&$res -> status === "ok" && isset($res -> message)){
foreach($res ->message -> items as $item){
$publication = $this -> _parseJsonDOI($item);
if($publication!=null){
$result -> publications[] = $publication;
}
}
$result -> totalPublications = $res -> message ->total_results;
/*if(isset($size)&&isset($page)){
$result -> publications = array_slice($result -> publications, ($page - 1) * $size, $size);
}*/
}
}
return $result;
} catch (Exception $e) {
JLog :: add('Error performing DOI search (doi: ' . $doi . '): ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
$result = new JObject();
$result -> publications = array();
$result -> totalPublications = 0;
$result -> totalDatasets = 0;
JLog :: add('error parsing DOI record', JLog :: INFO, self :: LOG);
return $result;
}
}
public function searchSingleDOI($doi) {
if ($this -> cache -> getCaching()) {
$cacheId = self :: SEARCH_DOI_CACHE_ID . '.' . $doi;
$results = $this -> cache -> get($cacheId, self :: CACHE_GROUP);
if ($results === FALSE) {
$results = $this -> _searchSingleDOI($doi);
if ($results !== NULL)
$this -> cache -> store($results, $cacheId, self :: CACHE_GROUP);
}
} else
$results = $this -> _searchSingleDOI($doi);
return $results;
}
private function _searchSingleDOI($doi) {
try {
$request = self :: CROSSREF_API_DOI . urlencode( trim($doi));
JLog :: add('Requesting ' . $request, JLog :: INFO, self :: LOG);
$time = microtime(TRUE);
$response = $this -> http -> get($request);
JLog :: add('Received response in ' . (microtime(TRUE) - $time) . ' s', JLog :: INFO, self :: LOG);
if ($response == NULL)
throw new Exception('no HTTP response');
if ($response -> code != self :: HTTP_OK)
throw new Exception('HTTP response code ' . $response -> code);
$res = array();
$res = json_decode(str_replace("-", "_", $response -> body));
if(isset($res -> status) && $res -> status === "ok" && isset($res -> message)){
$publication = $this -> _parseJsonDOI($res -> message);
return $publication;
}
} catch (Exception $e) {
JLog :: add('Error performing DOI search (doi: ' . $doi . '): ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
$result = new JObject();
$result -> publications = array();
$result -> totalPublications = 0;
$result -> totalDatasets = 0;
JLog :: add('error parsing DOI record', JLog :: INFO, self :: LOG);
return null;
}
}
private function _parseJsonDOI($message) {
$publication = new JObject();
$publication -> id = isset($message -> DOI)?$message -> DOI:NULL;
$publication -> source = self :: DOI;
$publication -> url = isset($message -> URL)?$message -> URL:NULL;
$publication -> accessMode = NULL;
$publication -> datasources = array();
$publication -> title = (isset($message -> title) && isset($message -> title[0]))?$message -> title[0]:NULL;
$publication -> authors = array();
if(isset($message -> author)){
foreach ($message -> author as $author_item) {
$author = new JObject();
$author -> id = NULL;
$author -> lastName = isset($author_item-> family) ? trim($author_item -> family):NULL;
$author -> firstName = isset($author_item -> given) ? trim($author_item -> given):NULL;
$author -> fullName = NULL;
if (($author -> id != NULL) || ($author -> lastName != NULL) || ($author-> firstName != NULL) || ($author -> fullName != NULL))
$publication -> authors[] = $author;
}
}
$publication -> publisher =isset($message -> publisher)?$message -> publisher:NULL;
$publication -> year = (isset($message -> issued )&&isset($message -> issued -> date_parts[0]))?
((isset($message -> issued -> date_parts[0][0]))?$message -> issued -> date_parts[0][0]:NULL):NULL;
$publication -> language = NULL;
$publication -> projects = array();
$publication -> embargoEndDate = NULL;
$publication -> description = NULL;
if (($publication -> id != NULL)&&( ($publication -> url != NULL) || ($publication -> title != NULL) || ($publication -> authors != NULL) || ($publication -> year != NULL))) {
return $publication;
}
return NULL;
}
// Search for a dataset DOI using caching if enabled.
// $doi the dataset DOI to search for
// return a result (object) containing datasets and total
private function _searchDataCiteWithDoi($doi) {
try {
$request = self :: DATACITE_URL . urlencode(trim($doi));
JLog :: add('Requesting ' . $request, JLog :: INFO, self :: LOG);
$time = microtime(TRUE);
$response = $this -> http -> get($request);
JLog :: add('Received response in ' . (microtime(TRUE) - $time) . ' s', JLog :: INFO, self :: LOG);
if ($response== NULL)
throw new Exception('no HTTP response');
/*if ($response -> code == self :: HTTP_NOT_FOUND) { // no dataset found; just return empty result
$result = new JObject();
$result -> datasets = array();
$result -> totalDatasets = 0;
JLog :: add('Retrieved 0 DataCite records in ' . (microtime(TRUE) - $time) . ' s', JLog :: INFO, self :: LOG);
return $result;
}*/
if ($response -> code != self :: HTTP_OK)
throw new Exception('HTTP response code ' . $response -> code);
$document = new DOMDocument();
$document -> recover = TRUE;
if ($document -> loadXML(trim($response -> body)) == FALSE)
throw new Exception('invalid XML response');
$xpath = new DOMXPath($document);
if ((($descriptionNodes = $xpath -> query('/rdf:RDF/rdf:Description')) == FALSE) || (($descriptionNode = $descriptionNodes -> item(0)) == NULL))
throw new Exception('error parsing DataCite record');
if (($idNodes = $xpath -> query('./j.0:identifier', $descriptionNode)) == FALSE)
throw new Exception('error parsing DataCite record');
if (($titleNodes = $xpath -> query('./j.0:title', $descriptionNode)) == FALSE)
throw new Exception('error parsing DataCite record');
if (($authorNodes = $xpath -> query('./j.0:creator', $descriptionNode)) == FALSE)
throw new Exception('error parsing DataCite record');
if (($yearNodes = $xpath -> query('./j.0:date', $descriptionNode)) == FALSE)
throw new Exception('error parsing DataCite record');
if (($publisherNodes = $xpath -> query('./j.0:publisher', $descriptionNode)) == FALSE)
throw new Exception('error parsing DataCite record');
$dataset = new JObject();
$dataset -> id = (($idNode = $idNodes -> item(0)) == NULL) ? NULL : trim($idNode -> nodeValue);
$dataset -> source = self :: DATACITE;
$dataset -> url = self :: DOI_URL . urlencode(trim($doi));
$dataset -> accessMode = NULL;
$dataset -> datasources = array();
$dataset -> title = (($titleNode = $titleNodes -> item(0)) == NULL) ? NULL : trim($titleNode -> nodeValue);
$dataset -> authors = array();
$dataset -> publisher = (($publisherNode = $publisherNodes -> item(0)) == NULL) ? NULL : trim($publisherNode -> nodeValue);
$dataset -> year = (($yearNode = $yearNodes -> item(0)) == NULL) ? NULL : intval(trim($yearNode -> nodeValue));
$dataset -> language = NULL;
$dataset -> projects = array();
$dataset -> embargoEndDate = NULL;
$dataset -> description = NULL;
foreach ($authorNodes as $authorNode) {
$author = new JObject();
$author -> id = NULL;
$author -> lastName = NULL;
$author -> firstName = NULL;
$author -> fullName = trim($authorNode -> nodeValue);
if ($author -> fullName != NULL)
$dataset -> authors[] = $author;
}
/*$result = new JObject();
$result -> datasets = array();
$result -> totalDatasets = 0;
if (($dataset -> id != NULL) || ($dataset -> url != NULL) || ($dataset -> title != NULL) || ($dataset -> authors != NULL) || ($dataset -> year != NULL)) {
$result -> datasets[] = $dataset;
$result -> totalDatasets = 1;
}*/
JLog :: add('Retrieved DataCite record in ' . (microtime(TRUE) - $time) . ' s', JLog :: INFO, self :: LOG);
return $dataset;
} catch (Exception $e) {
JLog :: add('Error performing DataCite search (doi: ' . $doi . '): ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
return NULL;
}
}
private function _searchDataCite($doi, $page, $size) {
$result = new JObject();
$result->datasets = array();
$result->totalPublications = 0;
$result->totalDatasets = 0;
try {
$dois = explode(" ", preg_replace('/\s+/', ' ',$doi));
$unique_dois =array_slice( array_unique($dois),0,10);
$pattern1 = '#\b(10[.][0-9]{4,}(?:[.][0-9]+)*/(?:(?!["&\'<>])\S)+)\b#';
$pattern2 = '#\b(10[.][0-9]{4,}(?:[.][0-9]+)*/(?:(?!["&\'<>])[[:graph:]])+)\b#';
if (preg_match($pattern1, $unique_dois[0]) || preg_match($pattern2, $unique_dois[0])) {
if(count($dois)>10){
$result = new JObject();
$result->datasets = array();
$result->totalPublications = 0;
$result->totalDatasets = '-';
$result->maxDoisExceeded = true;
return $result;
}
JLog :: add('It;s a dataset DOI!!!!!!!!! ' . $dois[0], JLog :: INFO, self :: LOG);
JLog :: add('Searching for DOIs in Datacite', JLog :: INFO, self :: LOG);
foreach ($unique_dois as $doi_) {
JLog :: add('DOI::: ' . $doi_, JLog :: INFO, self :: LOG);
$dataset = $this->_searchDataCiteWithDoi($doi_);
if ($dataset != null) {
$result->datasets[] = $dataset;
}
}
}else{
JLog :: add('It;sNOOOOOOOT a dataset DOI!!!!!!!!! ' . $dois[0], JLog :: INFO, self :: LOG);
}
$result->totalDatasets = count($result->datasets);
if($result->totalDatasets!==0){
return $result;
}
$request = self :: DATACITE_URL_KEYWORD. urlencode(trim($doi)).self :: DATACITE_URL_KEYWORD_REST."&rows=".$size."&start=".($page-1)*$size;
JLog :: add('Requesting ' . $request, JLog :: INFO, self :: LOG);
$time = microtime(TRUE);
$response = $this -> http -> get($request);
JLog :: add('Received response in ' . (microtime(TRUE) - $time) . ' s', JLog :: INFO, self :: LOG);
if ($response== NULL)
throw new Exception('no HTTP response');
if ($response -> code == self :: HTTP_NOT_FOUND) { // no dataset found; just return empty result
$result = new JObject();
//$result -> datasets = array();
$result -> totalDatasets = 0;
$result -> totalPublications = 0;
JLog :: add('Retrieved 0 DataCite records in ' . (microtime(TRUE) - $time) . ' s', JLog :: INFO, self :: LOG);
return $result;
}
if ($response -> code != self :: HTTP_OK)
throw new Exception('HTTP response code ' . $response -> code);
$document = new DOMDocument();
$document -> recover = TRUE;
if ($document -> loadXML(trim($response -> body)) == FALSE)
throw new Exception('invalid XML response');
$xpath = new DOMXPath($document);
if ((($numFoundNodes = $xpath -> query('/response/result[@name = "response"]/@numFound')) == FALSE) || (($numFoundNode = $numFoundNodes -> item(0)) == NULL))
throw new Exception('error parsing DataCite record');
$numFound=((($numFoundNode)) == NULL) ? NULL : trim($numFoundNode -> nodeValue);
if ((($resultNodes = $xpath -> query('/response/result/doc')) == FALSE) || (($resultNode = $resultNodes -> item(0)) == NULL))
throw new Exception('error parsing DataCite record');
foreach ($resultNodes as $resultNode) {
if (($idNodes = $xpath -> query('./str[@name = "doi"]/text()', $resultNode)) == FALSE)
throw new Exception('error parsing DataCite record');
if (($titleNodes = $xpath -> query('./arr[@name="title"]/str/text()', $resultNode)) == FALSE)
throw new Exception('error parsing DataCite record');
if (($authorNodes = $xpath -> query('./arr[@name = "creator"]/str/text()', $resultNode)) == FALSE)
throw new Exception('error parsing DataCite record');
if (($publisherNodes = $xpath -> query('./str[@name = "publisher"]/text()', $resultNode)) == FALSE)
throw new Exception('error parsing DataCite record');
if (($yearNodes = $xpath -> query('./date[@name = "created"]/text()', $resultNode)) == FALSE)
throw new Exception('error parsing DataCite record');
$dataset = new JObject();
$dataset -> id = (($idNode = $idNodes -> item(0)) == NULL) ? NULL : trim($idNode -> nodeValue);
$dataset -> source = self :: DATACITE;
$dataset -> url = self :: DOI_URL . urlencode(trim($dataset -> id));
$dataset -> accessMode = NULL;
$dataset -> datasources = array();
$dataset -> title = (($titleNode = $titleNodes -> item(0)) == NULL) ? NULL : trim($titleNode -> nodeValue);
$dataset -> authors = array();
$dataset -> publisher = (($publisherNode = $publisherNodes -> item(0)) == NULL) ? NULL : trim($publisherNode -> nodeValue);
$dataset -> year = (($yearNode = $yearNodes -> item(0)) == NULL) ? NULL : substr(trim($yearNode -> nodeValue),0,4);
$dataset -> language = NULL;
$dataset -> projects = array();
$dataset -> embargoEndDate = NULL;
$dataset -> description = NULL;
/*echo("
Title:".$dataset -> title);
echo("
Id:".$dataset -> id);
echo("
publisher:".$dataset -> publisher);
echo("
year:".$dataset -> year);*/
foreach ($authorNodes as $authorNode) {
$authorName= $authorNode -> nodeValue;
$author = new JObject();
$author -> id = NULL;
$author -> lastName = NULL;
$author -> firstName = NULL;
$author -> fullName = trim($authorName );
//echo("
author:".$author -> fullName);
if ($author -> fullName != NULL)
$dataset -> authors[] = $author;
}
if (($dataset -> id != NULL) || ($dataset -> url != NULL) || ($dataset -> title != NULL) || ($dataset -> authors != NULL) || ($dataset -> year != NULL)) {
$result -> datasets[] = $dataset;
}
}
//
$result -> totalDatasets =$numFound ;
/* if(isset($size)&&isset($page)){
$result -> datasets = array_slice($result -> datasets, ($page - 1) * $size, $size);
}*/
JLog :: add('Retrieved DataCite '.count($result -> datasets).' records in ' . (microtime(TRUE) - $time) . ' s', JLog :: INFO, self :: LOG);
$result -> totalPublications = 0;
return $result;
} catch (Exception $e) {
JLog :: add('Error performing DataCite search (doi: ' . $doi . '): ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
$result = new JObject();
//$result -> datasets = array();
$result -> totalPublications = 0;
$result -> totalDatasets = 0;
return $result;
}
}
// Parse a journal out of a DOI record.
// $doi the DOI of the record
// $xpath the DOMXPath to parse
// $journalNode the journal node to start from
// return a result (object) containing publications and total
private function parseJournal($doi, $xpath, $journalNode) {
if (($resourceNodes = $xpath -> query('./journal_article/doi_data/resource', $journalNode)) == FALSE)
throw new Exception('error parsing DOI record');
if (($titleNodes = $xpath -> query('./journal_article/titles/title', $journalNode)) == FALSE)
throw new Exception('error parsing DOI record');
if (($contributorNodes = $xpath -> query('./journal_article/contributors', $journalNode)) == FALSE)
throw new Exception('error parsing DOI record');
if (($yearNodes = $xpath -> query('./journal_article/publication_date/year', $journalNode)) == FALSE)
throw new Exception('error parsing DOI record');
$publication = new JObject();
$publication -> id = $doi;
$publication -> source = self :: DOI;
$publication -> url = (($resourceNode = $resourceNodes -> item(0)) == NULL) ? NULL : trim($resourceNode -> nodeValue);
$publication -> accessMode = NULL;
$publication -> datasources = array();
$publication -> title = (($titleNode = $titleNodes -> item(0)) == NULL) ? NULL : trim($titleNode -> nodeValue);
$publication -> authors = $this -> parseContributors($xpath, $contributorNodes);
$publication -> publisher = NULL;
$publication -> year = (($yearNode = $yearNodes -> item(0)) == NULL) ? NULL : intval(trim($yearNode -> nodeValue));
$publication -> language = NULL;
$publication -> projects = array();
$publication -> embargoEndDate = NULL;
$publication -> description = NULL;
$result = new JObject();
$result -> publications = array();
$result -> totalPublications = 0;
if (($publication -> id != NULL) || ($publication -> url != NULL) || ($publication -> title != NULL) || ($publication -> authors != NULL) || ($publication -> year != NULL)) {
$result -> publications[] = $publication;
$result -> totalPublications = 1;
}
return $result;
}
// Parse a book out of a DOI record.
// $doi the DOI of the record
// $xpath the DOMXPath to parse
// $bookNode the book node to start from
// return a result (object) containing publications and total
private function parseBook($doi, $xpath, $bookNode) {
if (($bookMetadataNodes = $xpath -> query('./book_metadata', $bookNode)) == FALSE)
throw new Exception('error parsing DOI record');
if (($bookSeriesMetadataNodes = $xpath -> query('./book_series_metadata', $bookNode)) == FALSE)
throw new Exception('error parsing DOI record');
if (($bookSetMetadataNodes = $xpath -> query('./book_set_metadata', $bookNode)) == FALSE)
throw new Exception('error parsing DOI record');
if ((($metadataNode = $bookMetadataNodes -> item(0)) == NULL) && (($metadataNode = $bookSeriesMetadataNodes -> item(0)) == NULL) && (($metadataNode = $bookSetMetadataNodes -> item(0)) == NULL))
throw new Exception('error parsing DOI record');
if (($resourceNodes = $xpath -> query('./doi_data/resource', $metadataNode)) == FALSE)
throw new Exception('error parsing DOI record');
if (($titleNodes = $xpath -> query('./titles/title', $metadataNode)) == FALSE)
throw new Exception('error parsing DOI record');
if (($contributorNodes = $xpath -> query('./contributors', $metadataNode)) == FALSE)
throw new Exception('error parsing DOI record');
if (($yearNodes = $xpath -> query('./publication_date/year', $metadataNode)) == FALSE)
throw new Exception('error parsing DOI record');
$publication = new JObject();
$publication -> id = $doi;
$publication -> source = self :: DOI;
$publication -> url = (($resourceNode = $resourceNodes -> item(0)) == NULL) ? NULL : trim($resourceNode -> nodeValue);
$publication -> accessMode = NULL;
$publication -> datasources = array();
$publication -> title = (($titleNode = $titleNodes -> item(0)) == NULL) ? NULL : trim($titleNode -> nodeValue);
$publication -> authors = $this -> parseContributors($xpath, $contributorNodes);
$publication -> publisher = NULL;
$publication -> year = (($yearNode = $yearNodes -> item(0)) == NULL) ? NULL : intval(trim($yearNode -> nodeValue));
$publication -> language = NULL;
$publication -> projects = array();
$publication -> embargoEndDate = NULL;
$publication -> description = NULL;
$result = new JObject();
$result -> publications = array();
$result -> totalPublications = 0;
if (($publication -> id != NULL) || ($publication -> url != NULL) || ($publication -> title != NULL) || ($publication -> authors != NULL) || ($publication -> year != NULL)) {
$result -> publications[] = $publication;
$result -> totalPublications = 1;
}
return $result;
}
// Parse a Conference out of a DOI record.
// $doi the DOI of the record
// $xpath the DOMXPath to parse
// $conferenceNode the book node to start from
// return a result (object) containing publications and total
private function parseConference($doi, $xpath, $conferenceNode) {
if (($eventMetadata = $xpath -> query('./event_metadata', $conferenceNode)) == FALSE)
throw new Exception('error parsing DOI record');
if (($proceedingsMetadata = $xpath -> query('./proceedings_metadata', $conferenceNode)) == FALSE)
throw new Exception('error parsing DOI record');
if (($conferencePaperNodes = $xpath -> query('./conference_paper', $conferenceNode)) == FALSE)
throw new Exception('error parsing DOI record');
if (($resourceNodes = $xpath -> query('./conference_paper/doi_data/resource', $conferenceNode)) == FALSE)
throw new Exception('error parsing DOI record');
if (($titleNodes = $xpath -> query('./conference_paper/titles/title', $conferenceNode)) == FALSE)
throw new Exception('error parsing DOI record');
if (($contributorNodes = $xpath -> query('./conference_paper/contributors', $conferenceNode)) == FALSE)
throw new Exception('error parsing DOI record');
if (($yearNodes = $xpath -> query('./conference_paper/publication_date/year', $conferenceNode)) == FALSE)
throw new Exception('error parsing DOI record');
$publication = new JObject();
$publication -> id = $doi;
$publication -> source = self :: DOI;
$publication -> url = (($resourceNode = $resourceNodes -> item(0)) == NULL) ? NULL : trim($resourceNode -> nodeValue);
$publication -> accessMode = NULL;
$publication -> datasources = array();
$publication -> title = (($titleNode = $titleNodes -> item(0)) == NULL) ? NULL : trim($titleNode -> nodeValue);
$publication -> authors = $this -> parseContributors($xpath, $contributorNodes);
$publication -> publisher = NULL;
$publication -> year = (($yearNode = $yearNodes -> item(0)) == NULL) ? NULL : intval(trim($yearNode -> nodeValue));
$publication -> language = NULL;
$publication -> projects = array();
$publication -> embargoEndDate = NULL;
$publication -> description = NULL;
$result = new JObject();
$result -> publications = array();
$result -> totalPublications = 0;
if (($publication -> id != NULL) || ($publication -> url != NULL) || ($publication -> title != NULL) || ($publication -> authors != NULL) || ($publication -> year != NULL)) {
$result -> publications[] = $publication;
$result -> totalPublications = 1;
}
return $result;
}
// Parse contributor metadata out of a DOI record.
// $doi the DOI of the record
// $xpath the DOMXPath to parse
// $contributorNodes the contributor nodes to start from
// return authors (array)
private function parseContributors($xpath, $contributorNodes) {
$authors = array();
foreach ($contributorNodes as $contributorNode) {
if (($personNameNodes = $xpath -> query('./person_name', $contributorNode)) == FALSE)
throw new Exception('error parsing DOI record');
if (($organizationNodes = $xpath -> query('./organization', $contributorNode)) == FALSE)
throw new Exception('error parsing DOI record');
foreach ($personNameNodes as $personNameNode) {
if (($surnameNodes = $xpath -> query('./surname', $personNameNode)) == FALSE)
throw new Exception('error parsing DOI record');
if (($givenNameNodes = $xpath -> query('./given_name', $personNameNode)) == FALSE)
throw new Exception('error parsing DOI record');
$author = new JObject();
$author -> id = NULL;
$author -> lastName = (($surnameNode = $surnameNodes -> item(0)) == NULL) ? NULL : trim($surnameNode -> nodeValue);
$author -> firstName = (($givenNameNode = $givenNameNodes -> item(0)) == NULL) ? NULL : trim($givenNameNode -> nodeValue);
$author -> fullName = NULL;
if (($author -> id != NULL) || ($author -> lastName != NULL) || ($author -> firstName != NULL) || ($author -> fullName != NULL))
$authors[] = $author;
}
foreach ($organizationNodes as $organizationNode) {
$author = new JObject();
$author -> id = NULL;
$author -> lastName = NULL;
$author -> firstName = NULL;
$author -> fullName = trim($organizationNode -> nodeValue);
if (($author -> id != NULL) || ($author -> lastName != NULL) || ($author -> firstName != NULL) || ($author -> fullName != NULL))
$authors[] = $author;
}
}
return $authors;
}
// Search ORCID.
// $orcid the ORCID id to search for
// $page the result page to retrieve
// $size the result page size to use
// return an array of results or NULL if any errors occur
/*private function _searchORCID($orcid, $page, $size) {
try {
$time = microtime(TRUE);
$authorRequest = self :: ORCID_URL_PREFIX . trim($orcid) . self :: ORCID_URL_AUTHOR_SUFFIX;
JLog :: add('Requesting ' . $authorRequest, JLog :: INFO, self :: LOG);
$authorResponse = $this -> http -> get($authorRequest);
JLog :: add('Received response in ' . (microtime(TRUE) - $time) . ' s');
if ($authorResponse == NULL)
throw new Exception('no HTTP response');
if ($authorResponse -> code != self :: HTTP_OK)
throw new Exception('HTTP response code ' . $authorResponse -> code);
$authorDocument = new DOMDocument();
$authorDocument -> recover = TRUE;
if ($authorDocument -> loadXML(trim($authorResponse -> body)) == FALSE)
throw new Exception('invalid XML response');
$authorXpath = new DOMXPath($authorDocument);
if ($authorXpath -> registerNamespace('orcid', $authorDocument -> lookupNamespaceUri(NULL)) == FALSE)
throw new Exception('error parsing ORCID bio record');
if (($familyNameNodes = $authorXpath -> query('/orcid:orcid-message/orcid:orcid-profile/orcid:orcid-bio/orcid:personal-details/orcid:family-name')) == FALSE)
throw new Exception('error parsing ORCID bio record');
if (($givenNamesNodes = $authorXpath -> query('/orcid:orcid-message/orcid:orcid-profile/orcid:orcid-bio/orcid:personal-details/orcid:given-names')) == FALSE)
throw new Exception('error parsing ORCID bio record');
$publicationsRequest = self :: ORCID_URL_PREFIX . trim($orcid) . self :: ORCID_URL_PUBLICATIONS_SUFFIX;
JLog :: add('Requesting ' . $publicationsRequest, JLog :: INFO, self :: LOG);
if (($publicationsResponse = $this -> http -> get($publicationsRequest)) == NULL)
throw new Exception('no HTTP response');
if ($publicationsResponse -> code != self :: HTTP_OK)
throw new Exception('HTTP response code ' . $publicationsResponse -> code);
$publicationsDocument = new DOMDocument();
$publicationsDocument -> recover = TRUE;
if ($publicationsDocument -> loadXML(trim($publicationsResponse -> body)) == FALSE)
throw new Exception('invalid XML response');
$publicationsXpath = new DOMXPath($publicationsDocument);
if ($publicationsXpath -> registerNamespace('orcid', $publicationsDocument -> lookupNamespaceUri(NULL)) == FALSE)
throw new Exception('error parsing ORCID works record');
if (($orcidWorkNodes = $publicationsXpath -> query('/orcid:orcid-message/orcid:orcid-profile/orcid:orcid-activities/orcid:orcid-works/orcid:orcid-work')) == FALSE)
throw new Exception('error ORCID works record');
$author = new JObject();
$author -> id = NULL;
$author -> lastName = (($familyNameNode = $familyNameNodes -> item(0)) == NULL) ? NULL : trim($familyNameNode -> nodeValue);
$author -> firstName = (($givenNamesNode = $givenNamesNodes ->item(0)) == NULL) ? NULL : trim($givenNamesNode -> nodeValue);
$author -> fullName = NULL;
$result = new JObject();
$result -> publications = array();
foreach ($orcidWorkNodes as $orcidWorkNode) {
if (($putCodeNodes = $publicationsXpath -> query('./@put-code', $orcidWorkNode)) == FALSE)
throw new Exception('error ORCID works record');
if (($urlNodes = $publicationsXpath -> query('./orcid:url', $orcidWorkNode)) == FALSE)
throw new Exception('error ORCID works record');
if (($titleNodes = $publicationsXpath -> query('./orcid:work-title/orcid:title', $orcidWorkNode)) == FALSE)
throw new Exception('error ORCID works record');
if (($yearNodes = $publicationsXpath -> query('./orcid:publication-date/orcid:year', $orcidWorkNode)) == FALSE)
throw new Exception('error ORCID works record');
if (($shortDescriptionNodes = $publicationsXpath -> query('./orcid:short-description', $orcidWorkNode)) == FALSE)
throw new Exception('error ORCID works record');
if (($doiNodes = $publicationsXpath -> query('./orcid:work-external-identifiers/orcid:work-external-identifier[orcid:work-external-identifier-type = "doi"]/orcid:work-external-identifier-id', $orcidWorkNode)) == FALSE)
throw new Exception('error ORCID works record');
$publication = new JObject();
$publication -> id = (($putCodeNode = $putCodeNodes -> item(0)) == NULL) ? NULL : ($orcid . '-' . trim($putCodeNode -> nodeValue));
$publication -> source = self :: ORCID;
$publication -> url = (($urlNode = $urlNodes -> item(0)) == NULL) ? NULL : trim($urlNode -> nodeValue);
$publication -> accessMode = NULL;
$publication -> datasources = array();
$publication -> title = (($titleNode = $titleNodes -> item(0)) == NULL) ? NULL : trim($titleNode -> nodeValue);
$publication -> authors = array();
$publication -> publisher = NULL;
$publication -> year = (($yearNode = $yearNodes -> item(0)) == NULL) ? NULL : intval(trim($yearNode -> nodeValue));
$publication -> language = NULL;
$publication -> projects = array();
$publication -> embargoEndDate = NULL;
$publication -> description = (($shortDescriptionNode = $shortDescriptionNodes -> item(0)) == NULL) ? NULL : trim($shortDescriptionNode -> nodeValue);
if (($author -> lastName != NULL) || ($author -> firstName != NULL))
$publication -> authors[] = $author;
if ((($doiNode = $doiNodes -> item(0)) != NULL) && (($doiResult = $this -> searchDOI(trim($doiNode -> nodeValue))) != NULL) && ($doiResult -> totalPublications > 0)) { // resolve via DOI
if ($publication -> url == NULL) // set URL if missing
$publication -> url = $doiResult -> publications[0] -> url;
if ($publication -> title == NULL) // set title if missing
$publication -> title = $doiResult -> publications[0] -> title;
foreach ($doiResult -> publications[0] -> authors as $doiAuthor) { // merge authors
if ((count($publication -> authors) == 0) || ($doiAuthor -> id != $publication -> authors[0] -> id) || ($doiAuthor -> lastName != $publication -> authors[0] -> lastName) || ($doiAuthor -> firstName != $publication -> authors[0] -> firstName) || ($doiAuthor -> fullName != $publication -> authors[0] -> fullName))
$publication -> authors[] = $doiAuthor;
}
if ($publication -> year == NULL) // set year if missing
$publication -> year = $doiResult -> publications[0] -> year;
}
if (($publication -> id != NULL) || ($publication -> title != NULL) || ($publication -> authors != NULL) || ($publication -> year != NULL) || ($publication -> description != NULL))
$result -> publications[] = $publication;
}
$result -> totalPublications = count($result -> publications);
$result -> publications = array_slice($result -> publications, ($page - 1) * $size, $size);
JLog :: add('ORCID search retrieved ' . count($result -> publications) . ' publications in ' . (microtime(TRUE) - $time) . ' s (ORCID: ' . $orcid . ', page: ' . $page . ', size: ' . $size . ')', JLog :: INFO, self :: LOG);
return $result;
} catch (Exception $e) {
JLog :: add('Error performing ORCID search (ORCID: ' . $orcid . ', page: ' . $page . ', size: ' . $size . '): ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
return NULL;
}
}*/
private function _searchORCID($orcid, $page, $size) {
try {
$time = microtime(TRUE);
$authorRequest = self :: ORCID_URL_PREFIX . trim($orcid) . self :: ORCID_URL_AUTHOR_SUFFIX;
JLog :: add('Requesting ' . $authorRequest, JLog :: INFO, self :: LOG);
$authorResponse = $this -> http -> get($authorRequest);
JLog :: add('Received response in ' . (microtime(TRUE) - $time) . ' s');
$author = new JObject();
$result = new JObject();
if ($authorResponse != NULL&&$authorResponse -> code===self :: HTTP_OK){
/*if ($authorResponse -> code != self :: HTTP_OK)
throw new Exception('HTTP response code ' . $authorResponse -> code);*/
$authorDocument = new DOMDocument();
$authorDocument -> recover = TRUE;
if ($authorDocument -> loadXML(trim($authorResponse -> body)) == FALSE)
throw new Exception('invalid XML response');
$authorXpath = new DOMXPath($authorDocument);
if ($authorXpath -> registerNamespace('orcid', $authorDocument -> lookupNamespaceUri(NULL)) == FALSE)
throw new Exception('error parsing ORCID bio record');
if (($familyNameNodes = $authorXpath -> query('/orcid:orcid-message/orcid:orcid-profile/orcid:orcid-bio/orcid:personal-details/orcid:family-name')) == FALSE)
throw new Exception('error parsing ORCID bio record');
if (($givenNamesNodes = $authorXpath -> query('/orcid:orcid-message/orcid:orcid-profile/orcid:orcid-bio/orcid:personal-details/orcid:given-names')) == FALSE)
throw new Exception('error parsing ORCID bio record');
$author -> id = $orcid;
$author -> lastName = (($familyNameNode = $familyNameNodes -> item(0)) == NULL) ? NULL : trim($familyNameNode -> nodeValue);
$author -> firstName = (($givenNamesNode = $givenNamesNodes ->item(0)) == NULL) ? NULL : trim($givenNamesNode -> nodeValue);
$author -> fullName = NULL;
}else{
$time = microtime(TRUE);
$orcid=str_replace(" ","+",$orcid);
$authorRequest = 'http://pub.orcid.org/search/orcid-bio?q='. trim($orcid);
JLog :: add('Requesting ' . $authorRequest, JLog :: INFO, self :: LOG);
$authorResponse = $this -> http -> get($authorRequest);
JLog :: add('Received response in ' . (microtime(TRUE) - $time) . ' s');
if ($authorResponse -> code != self :: HTTP_OK)
throw new Exception('HTTP response code ' . $authorResponse -> code);
$authorDocument = new DOMDocument();
$authorDocument -> recover = TRUE;
if ($authorDocument -> loadXML(trim($authorResponse -> body)) == FALSE)
throw new Exception('invalid XML response');
$authorXpath = new DOMXPath($authorDocument);
if ($authorXpath -> registerNamespace('orcid', $authorDocument -> lookupNamespaceUri(NULL)) == FALSE)
throw new Exception('error parsing ORCID bio record');
if (($resultNodes = $authorXpath -> query('/orcid:orcid-message/orcid:orcid-search-results/orcid:orcid-search-result')) == FALSE)
throw new Exception('error parsing ORCID bio record');
$authors = array();
foreach ($resultNodes as $resultNode) {
if (($familyNameNodes = $authorXpath -> query('./orcid:orcid-profile/orcid:orcid-bio/orcid:personal-details/orcid:family-name',$resultNode)) == FALSE)
throw new Exception('error parsing ORCID bio record');
if (($givenNamesNodes = $authorXpath -> query('./orcid:orcid-profile/orcid:orcid-bio/orcid:personal-details/orcid:given-names',$resultNode)) == FALSE)
throw new Exception('error parsing ORCID bio record');
if (($idNodes = $authorXpath -> query('./orcid:orcid-profile/orcid:orcid-identifier/orcid:path',$resultNode)) == FALSE)
throw new Exception('error parsing ORCID bio record');
if (($relevanceNodes = $authorXpath -> query('./orcid:relevancy-score',$resultNode)) == FALSE)
throw new Exception('error parsing ORCID bio record');
$tempAuthor = new JObject();
$tempAuthor -> id = (($idNode = $idNodes -> item(0)) == NULL) ? NULL : trim($idNode -> nodeValue);
$tempAuthor -> lastName = (($familyNameNode = $familyNameNodes -> item(0)) == NULL) ? NULL : trim($familyNameNode -> nodeValue);
$tempAuthor -> firstName = (($givenNamesNode = $givenNamesNodes ->item(0)) == NULL) ? NULL : trim($givenNamesNode -> nodeValue);
$tempAuthor -> fullName = NULL;
$relevance = (($relevanceNode = $relevanceNodes ->item(0)) == NULL) ? NULL : trim($relevanceNode -> nodeValue);
$authors[] = $tempAuthor;
}
$author= $authors[0];
$authors = array_slice($authors, 0, 10);
$result ->alternativeAuthors =$authors;
}
$publicationsRequest = self :: ORCID_URL_PREFIX . trim(((isset($author -> id)&&$author -> id!=null)?$author -> id:$orcid)) . self :: ORCID_URL_PUBLICATIONS_SUFFIX;
JLog :: add('Requesting ' . $publicationsRequest, JLog :: INFO, self :: LOG);
if (($publicationsResponse = $this -> http -> get($publicationsRequest)) == NULL)
throw new Exception('no HTTP response');
if ($publicationsResponse -> code != self :: HTTP_OK)
throw new Exception('HTTP response code ' . $publicationsResponse -> code);
$publicationsDocument = new DOMDocument();
$publicationsDocument -> recover = TRUE;
if ($publicationsDocument -> loadXML(trim($publicationsResponse -> body)) == FALSE)
throw new Exception('invalid XML response');
$publicationsXpath = new DOMXPath($publicationsDocument);
if ($publicationsXpath -> registerNamespace('orcid', $publicationsDocument -> lookupNamespaceUri(NULL)) == FALSE)
throw new Exception('error parsing ORCID works record');
if (($orcidWorkNodes = $publicationsXpath -> query('/orcid:orcid-message/orcid:orcid-profile/orcid:orcid-activities/orcid:orcid-works/orcid:orcid-work')) == FALSE)
throw new Exception('error ORCID works record');
$result -> publications = array();
foreach ($orcidWorkNodes as $orcidWorkNode) {
if (($putCodeNodes = $publicationsXpath -> query('./@put-code', $orcidWorkNode)) == FALSE)
throw new Exception('error ORCID works record');
if (($urlNodes = $publicationsXpath -> query('./orcid:url', $orcidWorkNode)) == FALSE)
throw new Exception('error ORCID works record');
if (($titleNodes = $publicationsXpath -> query('./orcid:work-title/orcid:title', $orcidWorkNode)) == FALSE)
throw new Exception('error ORCID works record');
if (($yearNodes = $publicationsXpath -> query('./orcid:publication-date/orcid:year', $orcidWorkNode)) == FALSE)
throw new Exception('error ORCID works record');
if (($shortDescriptionNodes = $publicationsXpath -> query('./orcid:short-description', $orcidWorkNode)) == FALSE)
throw new Exception('error ORCID works record');
if (($doiNodes = $publicationsXpath -> query('./orcid:work-external-identifiers/orcid:work-external-identifier[orcid:work-external-identifier-type = "doi"]/orcid:work-external-identifier-id', $orcidWorkNode)) == FALSE)
throw new Exception('error ORCID works record');
$publication = new JObject();
$publication -> id = (($putCodeNode = $putCodeNodes -> item(0)) == NULL) ? NULL : ($author -> id . '-' . trim($putCodeNode -> nodeValue));
$publication -> source = self :: ORCID;
$publication -> url = (($urlNode = $urlNodes -> item(0)) == NULL) ? NULL : trim($urlNode -> nodeValue);
$publication -> accessMode = NULL;
$publication -> datasources = array();
$publication -> title = (($titleNode = $titleNodes -> item(0)) == NULL) ? NULL : trim($titleNode -> nodeValue);
$publication -> authors = array();
$publication -> publisher = NULL;
$publication -> year = (($yearNode = $yearNodes -> item(0)) == NULL) ? NULL : intval(trim($yearNode -> nodeValue));
$publication -> language = NULL;
$publication -> projects = array();
$publication -> embargoEndDate = NULL;
$publication -> description = (($shortDescriptionNode = $shortDescriptionNodes -> item(0)) == NULL) ? NULL : trim($shortDescriptionNode -> nodeValue);
if (($author -> lastName != NULL) || ($author -> firstName != NULL))
$publication -> authors[] = $author;
if ((($doiNode = $doiNodes -> item(0)) != NULL) && (($doiResult = $this -> searchSingleDOI(trim($doiNode -> nodeValue))) != NULL)) { // resolve via DOI
if ($publication -> url == NULL) // set URL if missing
$publication -> url = $doiResult -> url;
if ($publication -> title == NULL) // set title if missing
$publication -> title = $doiResult -> title;
foreach ($doiResult -> authors as $doiAuthor) { // merge authors
if ((count($publication -> authors) == 0) || ($doiAuthor -> id != $publication -> authors[0] -> id) || ($doiAuthor -> lastName != $publication -> authors[0] -> lastName) || ($doiAuthor -> firstName != $publication -> authors[0] -> firstName) || ($doiAuthor -> fullName != $publication -> authors[0] -> fullName))
$publication -> authors[] = $doiAuthor;
}
if ($publication -> year == NULL) // set year if missing
$publication -> year = $doiResult -> year;
}
if (($publication -> id != NULL) || ($publication -> title != NULL) || ($publication -> authors != NULL) || ($publication -> year != NULL) || ($publication -> description != NULL))
$result -> publications[] = $publication;
}
$result -> totalPublications = count($result -> publications);
if($result -> totalPublications >($page - 1) * $size){
$result -> publications = array_slice($result -> publications, ($page - 1) * $size, $size);
}
$result -> totalDatasets = 0;
$result -> author = $author;
JLog :: add('ORCID search retrieved ' . count($result -> publications) . ' publications in ' . (microtime(TRUE) - $time) . ' s (ORCID: ' . $orcid . ', page: ' . $page . ', size: ' . $size . ')', JLog :: INFO, self :: LOG);
return $result;
} catch (Exception $e) {
JLog :: add('Error performing ORCID search (ORCID: ' . $orcid . ', page: ' . $page . ', size: ' . $size . '): ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
$result = new JObject();
$result -> totalPublications = 0;
$result -> totalDatasets = 0;
$result -> author = $author;
return $result;
}
}
private function parseClaimedPublication($claim) {
$claimedPublication = new JObject();
$claimedPublication -> date = $claim -> date;
$claimedPublication -> claimType=$claim -> type;
$claimedPublication -> claimId=isset($claim -> id)?$claim -> id:'';
$document = new DOMDocument();
$document -> recover = TRUE;
if ($document -> loadXML(trim($claim -> xml)) == FALSE)
throw new Exception('invalid XML response');
$xPath = new DOMXPath($document);
switch ($claim -> type) {
case self :: RELATION:
if ((($relationNodes = $xPath -> query('/RELATIONS/RELATION')) == FALSE) || (($relationNode = $relationNodes -> item(0)) == NULL))
throw new Exception('error parsing relation');
if ((($sourceNodes = $xPath -> query('./@source', $relationNode)) == FALSE) || (($sourceNode = $sourceNodes -> item(0)) == NULL))
throw new Exception('error parsing relation source');
if ((($targetNodes = $xPath -> query('./@target', $relationNode)) == FALSE) || (($targetNode = $targetNodes -> item(0)) == NULL))
throw new Exception('error parsing relation target');
if ((($relationTypeNodes = $xPath -> query('./@type', $relationNode)) == FALSE) || (($relationTypeNode = $relationTypeNodes -> item(0)) == NULL))
throw new Exception('error parsing relation type');
$claimedPublication -> projectId = NULL;
$claimedPublication -> targetDatasetId = NULL;
$claimedPublication -> targetPublicationId = NULL;
$claimedPublication -> date = $claim -> date;
$claimedPublication -> type = "publication";
if ($relationTypeNode -> nodeValue == "resultProject"){
$claimedPublication -> projectId = trim(str_replace("40|", "", $targetNode -> nodeValue));
$claimedPublication -> type = "publication";
}
else if ($relationTypeNode -> nodeValue == "resultResult_publicationpublication_isRelatedTo"){
$sourceNode -> nodeValue = str_replace("50|", "", $sourceNode -> nodeValue);
$claimedPublication -> targetPublicationId = trim(str_replace("50|", "", $targetNode -> nodeValue));
$claimedPublication -> type = "publication";
}
else if ($relationTypeNode -> nodeValue == "resultResult_publicationdataset_isRelatedTo"){
$sourceNode -> nodeValue = str_replace("50|", "", $sourceNode -> nodeValue);
$claimedPublication -> targetDatasetId = trim(str_replace("50|", "", $targetNode -> nodeValue));
$claimedPublication -> type = "publication";
}
else if ($relationTypeNode -> nodeValue == "resultResult_datasetpublication_isRelatedTo"){// openaire dataset me publ
$sourceNode -> nodeValue = str_replace("50|", "", $sourceNode -> nodeValue);
$claimedPublication -> targetPublicationId = trim(str_replace("50|", "", $targetNode -> nodeValue));
$claimedPublication -> type = "dataset";
}
else if ($relationTypeNode -> nodeValue == "resultResult_datasetdataset_isRelatedTo"){// openaire dataset me ext dataset
$sourceNode -> nodeValue = str_replace("50|", "", $sourceNode -> nodeValue);
$claimedPublication -> targetDatasetId = trim(str_replace("50|", "", $targetNode -> nodeValue));
$claimedPublication -> type = "dataset";
}
else {
return null;
}
$claimedPublication -> publication = NULL;
$claimedPublication -> publicationId = str_replace("50|", "",trim($sourceNode -> nodeValue));
$claimedPublication -> userEmail = $claim -> userEmail;
break;
case self :: DMF:
$claimedPublication -> targetDatasetId = NULL;
$claimedPublication -> targetPublicationId = NULL;
$claimedPublication -> date = $claim -> date;
$claimedPublication -> publication = $this -> parseDMF($xPath);
$claimedPublication -> publicationId = $claim->resultid;
$claimedPublication -> projectId = NULL;
$claimedPublication -> userEmail = $claim -> userEmail;
}
return $claimedPublication;
}
private function parseDMF($xpath) {
$xpath -> registerNamespace('openaire', 'http://namespace.openaire.eu/');
$xpath -> registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
$xpath -> registerNamespace('oaf', 'http://namespace.openaire.eu/oaf');
$xpath -> registerNamespace('dr', 'http://www.driver-repository.eu/namespace/dr');
$conceptNodes = $xpath -> query('/record/openaire:metadata/oaf:concept/@id');
$publication = new JObject();
$publication -> id = NULL;
$publication -> source = NULL;
$publication -> languages = array();
$publication -> url = NULL;
$publication -> authors = array();
$publication -> subjects = array();
$publication -> concepts = array();
$publication -> embargoEndDate = NULL;
$publication -> datasources = array();
$publication -> collectedFrom = array();
$publication -> sources = array();
$publication -> projects = array();
$publication -> pids = array();
$publication -> relatedPublications = array();
$publication -> relatedDatasets = array();
$publication -> externalPublications = array();
$publication -> externalDatasets = array();
if (($identifierNodes = $xpath -> query('/record/openaire:metadata/dc:identifier|/record/openaire:metadata/oaf:identifier')) == FALSE){
//throw new Exception('error parsing DMF');
}
if (($accessModeNodes = $xpath -> query('/record/openaire:metadata/oaf:accessrights/text()')) == FALSE){
//throw new Exception('error parsing DMF');
}
if (($titleNodes = $xpath -> query('/record/openaire:metadata/dc:title/text()')) == FALSE){
//throw new Exception('error parsing DMF');
}
if (($authorNodes = $xpath -> query('/record/openaire:metadata/dc:creator/text()')) == FALSE){
//throw new Exception('error parsing DMF');
}
if (($dateNodes = $xpath -> query('/record/openaire:metadata/dc:dateAccepted/text()')) == FALSE){
//throw new Exception('error parsing DMF');
}
if (($publisherNodes = $xpath -> query('/record/openaire:metadata/dc:publisher/text()')) != FALSE){
$publication -> publisher = (($publisherNode = $publisherNodes -> item(0)) == NULL) ? NULL : trim($publisherNode -> nodeValue);
}
if (($languageNodes = $xpath -> query('/record/openaire:metadata/dc:language/text()')) != FALSE){
foreach ($languageNodes as $languageNode) {
if ((($language = trim($languageNode -> nodeValue)) != NULL) && ($language != self :: UNDETERMINED))
$publication -> languages[] = $language;
}
}
if (($typeNodes = $xpath -> query('/record/openaire:metadata/dr:CObjCategory/text()')) != FALSE){
$publication -> type = (($typeNode = $typeNodes -> item(0)) == NULL) ? NULL : trim($typeNode -> nodeValue);
}
if (($descriptionNodes = $xpath -> query('/record/openaire:metadata/dc:description/text()')) != FALSE){
$publication -> description = (($descriptionNode = $descriptionNodes -> item(0)) == NULL) ? NULL : trim($descriptionNode -> nodeValue);
}
if (($datasourceNodes = $xpath -> query('/record/openaire:metadata/oaf:hostedBy')) != FALSE){
foreach ($datasourceNodes as $datasourceNode) {
if (($idNodes = $xpath -> query('./@id', $datasourceNode)) == FALSE){
//throw new Exception('error parsing DMF');
}
if (($nameNodes = $xpath -> query('./@name', $datasourceNode)) == FALSE){
//throw new Exception('error parsing DMF');
}
$datasource = new JObject();
$datasource -> id = (($idNode = $idNodes -> item(0)) == NULL) ? NULL : trim($idNode -> nodeValue);
$datasource -> name = (($nameNode = $nameNodes -> item(0)) == NULL) ? NULL : trim($nameNode -> nodeValue);
$datasource -> url = NULL;
$datasource -> accessMode = NULL;
if (($datasource -> id != NULL) || ($datasource -> name != NULL))
$publication -> datasources[] = $datasource;
}
}
if (($collectedFromNodes = $xpath -> query('/record/openaire:metadata/oaf:collectedFrom')) != FALSE){
foreach ($collectedFromNodes as $collectedFromNode) {
if (($idNodes = $xpath -> query('./@id', $collectedFromNode)) == FALSE){
//throw new Exception('error parsing DMF');
}
if (($nameNodes = $xpath -> query('./@name', $collectedFromNode)) == FALSE){
//throw new Exception('error parsing DMF');
}
$collectedFrom = new JObject();
$collectedFrom -> id = (($idNode = $idNodes -> item(0)) == NULL) ? NULL : trim($idNode -> nodeValue);
$collectedFrom -> name = (($nameNode = $nameNodes -> item(0)) == NULL) ? NULL : trim($nameNode -> nodeValue);
if (($collectedFrom -> id != NULL) || ($collectedFrom -> name != NULL))
$publication -> collectedFrom[] = $collectedFrom;
}
}
if (($sourceNodes = $xpath -> query('/record/openaire:metadata/dc:source/text()')) != FALSE){
foreach ($sourceNodes as $sourceNode) {
if (($source = trim($sourceNode -> nodeValue)) != NULL)
$publication -> sources[] = $source;
}
}
if (($projectNodes = $xpath -> query('/record/openaire:metadata/oaf:projectid/text()')) != FALSE){
foreach ($projectNodes as $projectNode) {
$project = new JObject();
$project -> id = trim($projectNode -> nodeValue);
$project -> acronym = NULL;
$project -> title = NULL;
$project -> code = NULL;
if ($project -> id != NULL)
$publication -> projects[] = $project;
}
}
$publication -> accessMode = (($accessModeNode = $accessModeNodes -> item(0)) == NULL) ? NULL : trim($accessModeNode -> nodeValue);
$publication -> title = (($titleNode = $titleNodes -> item(0)) == NULL) ? NULL : trim($titleNode -> nodeValue);
$publication -> year = (($dateNode = $dateNodes -> item(0)) == NULL) ? NULL : intval(trim($dateNode -> nodeValue));
$publication -> date = ($dateNode == NULL) ? NULL : strtotime(trim($dateNode -> nodeValue));
if ($conceptNodes !== false){
foreach ($conceptNodes as $conceptNode) {
$publication -> concepts[] = trim($conceptNode -> nodeValue);
}
}
foreach ($authorNodes as $authorNode) {
if (($idNodes = $xpath -> query('./field[@name = "personId"]/@value', $authorNode)) == FALSE){
// throw new Exception('error parsing publication');
}
if (($fullNameNodes = $xpath -> query('./field[@name = "fullname"]/@value', $authorNode)) == FALSE){
// throw new Exception('error parsing publication');
}
if (($rankingNodes = $xpath -> query('./field[@name = "ranking"]/@value', $authorNode)) == FALSE){
//throw new Exception('error parsing publication');
}
$author = new JObject();
$author -> id = NULL;
$author -> lastName = NULL;
$author -> firstName = NULL;
$author -> fullName = trim($authorNode -> nodeValue);
$author -> ranking = 0;
if ($author -> fullName != NULL)
$publication -> authors[] = $author;
usort($publication -> authors, function ($author1, $author2) {return $author1 -> ranking - $author2 -> ranking;});
}
foreach ($identifierNodes as $identifierNode) {
$pid = new JObject();
$pid -> clazz=NULL;
$pid -> value=NULL;
if (($typeNodes = $xpath -> query('./@identifierType', $identifierNode)) != FALSE)
$pid -> clazz = (($typeNode = $typeNodes -> item(0)) == NULL) ? NULL : trim($typeNode -> nodeValue);
if (($textNodes = $xpath -> query('./text()', $identifierNode)) != FALSE)
$pid -> value = (($textNode = $textNodes -> item(0)) == NULL) ? NULL : trim($textNode -> nodeValue);
if ($pid -> value != NULL) {
if ($pid -> clazz == NULL && strpos($pid -> value , 'http') !==false){
$publication -> url = $pid -> value;
}else if ($pid -> clazz == NULL && strpos($pid -> value , 'http')===false ){
$publication -> id = $pid -> value;
$publication -> source = 'other';
$publication -> pids[] = $pid;
}
else if($pid -> clazz != NULL){
$publication -> id = $pid -> value;
$publication -> source = $pid -> clazz;
$publication -> pids[] = $pid;
}
}
}
return $publication;
}
private function _parseContextsConcepts($conceptNodes, &$parent, $xpath){
foreach ($conceptNodes as $conceptNode) {
if (($includeNodes = $xpath -> query('./@claim', $conceptNode)) == FALSE)
throw new Exception('error parsing publication');
if (($idNodes = $xpath -> query('./@id', $conceptNode)) == FALSE)
throw new Exception('error parsing publication');
if (($nameNodes = $xpath -> query('./@label', $conceptNode)) == FALSE)
throw new Exception('error parsing publication');
if (($subConceptNodes = $xpath -> query('./concept', $conceptNode)) == FALSE)
throw new Exception('error parsing publication');
$concept = new JObject();
$concept -> id = (($idNode = $idNodes -> item(0)) == NULL) ? NULL : trim($idNode -> nodeValue);
$concept -> name = (($nameNode = $nameNodes -> item(0)) == NULL) ? NULL : trim($nameNode -> nodeValue);
$concept -> claim = (($includeNode = $includeNodes -> item(0)) == NULL) ? NULL : trim($includeNode -> nodeValue);
if ($concept -> claim != NULL){
$concept -> claim = ($concept -> claim == "true") ? true : false;
}
$concept -> concepts = array();
$this->_parseContextsConcepts($subConceptNodes, $concept -> concepts, $xpath);
if (($concept -> id != NULL) || ($concept -> name != NULL))
$parent[$concept -> id] = $concept;
}
}
public function parseContexts($context_source){
$document = new DOMDocument();
$document -> recover = TRUE;
if ($document -> loadXML($context_source) == FALSE)
throw new Exception('invalid XML response');
$xpath = new DOMXPath($document);
if ((($configurationNodes = $xpath -> query('./BODY/CONFIGURATION')) == FALSE) || (($configurationNode = $configurationNodes -> item(0)) == NULL))
throw new Exception('error parsing contexts');
if (($contextNodes = $xpath -> query('./context', $configurationNode)) == FALSE)
throw new Exception('error parsing contexts');
$contexts = array();
foreach ($contextNodes as $contextNode) {
if (($includeNodes = $xpath -> query('./@claim', $contextNode)) == FALSE)
throw new Exception('error parsing publication');
if (($idNodes = $xpath -> query('./@id', $contextNode)) == FALSE)
throw new Exception('error parsing publication');
if (($nameNodes = $xpath -> query('./@label', $contextNode)) == FALSE)
throw new Exception('error parsing publication');
if (($categoryNodes = $xpath -> query('./category', $contextNode)) == FALSE)
throw new Exception('error parsing publication');
$context = new JObject();
$context -> id = (($idNode = $idNodes -> item(0)) == NULL) ? NULL : trim($idNode -> nodeValue);
$context -> name = (($nameNode = $nameNodes -> item(0)) == NULL) ? NULL : trim($nameNode -> nodeValue);
$context -> claim = (($includeNode = $includeNodes -> item(0)) == NULL) ? NULL : trim($includeNode -> nodeValue);
if ($context -> claim != NULL){
$context -> claim = ($context -> claim == "true") ? true : false;
}
$context -> categories = array();
foreach ($categoryNodes as $categoryNode) {
if (($includeNodes = $xpath -> query('./@claim', $categoryNode)) == FALSE)
throw new Exception('error parsing publication');
if (($idNodes = $xpath -> query('./@id', $categoryNode)) == FALSE)
throw new Exception('error parsing publication');
if (($nameNodes = $xpath -> query('./@label', $categoryNode)) == FALSE)
throw new Exception('error parsing publication');
if (($conceptNodes = $xpath -> query('./concept', $categoryNode)) == FALSE)
throw new Exception('error parsing publication');
$category = new JObject();
$category -> id = (($idNode = $idNodes -> item(0)) == NULL) ? NULL : trim($idNode -> nodeValue);
$category -> name = (($nameNode = $nameNodes -> item(0)) == NULL) ? NULL : trim($nameNode -> nodeValue);
$category -> claim = (($includeNode = $includeNodes -> item(0)) == NULL) ? NULL : trim($includeNode -> nodeValue);
if ($category -> claim != NULL){
$category -> claim = ($category -> claim == "true") ? true : false;
}
$category -> concepts = array();
$this->_parseContextsConcepts($conceptNodes, $category -> concepts, $xpath);
if (($category -> id != NULL) || ($category -> name != NULL) || ($category -> concepts != NULL))
$context -> categories[$category -> id] = $category;
}
if (($context -> id != NULL) || ($context -> name != NULL) || ($context -> categories != NULL))
$contexts[$context -> id] = $context;
}
return $contexts;
}
public function getContexts() {
try {
$time = microtime(TRUE);
$contexts = $this -> client -> getContexts();
$contexts = $this -> parseContexts($contexts[0]);
JLog :: add('Retrieved ' . count($contexts) . ' contexts in ' . (microtime(TRUE) - $time) . ' s', JLog :: INFO, self :: LOG);
return $contexts;
} catch (Exception $e) {
JLog :: add('Error retrieving contexts ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
return NULL;
}
}
// Get the publications claimed by a user using caching if enabled.
// $user the user whose claimed publications to retrieve
// return an array of publications
public function getClaimedDocsByUser($user) {
try {
$time = microtime(TRUE);
$claimedDocs = $this -> client -> getClaimedPublications($user -> email);
$publications = $this -> parseClaimedDocs($claimedDocs);
JLog :: add('Retrieved ' . count($publications) . ' claimed publications (user: ' . $user -> username . ') in ' . (microtime(TRUE) - $time) . ' s', JLog :: INFO, self :: LOG);
return $publications;
} catch (Exception $e) {
JLog :: add('Error retrieving claimed publications (user: ' . $user -> username . '): ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
return NULL;
}
}
// Get the publications claimed by a user using caching if enabled.
// $user the user whose claimed publications to retrieve
// return an array of publications
public function getClaimedDocsByTime($from, $to) {
try {
$time = microtime(TRUE);
$time1 = strtotime(str_replace("/", "-", $from)) * 1000;
$time2 = (strtotime(str_replace("/", "-", $to.' 23:59:59' ))) * 1000;
$claimedDocs = $this->client ->getAllClaimedPublications($time1, $time2);
$publications = $this -> parseClaimedDocs($claimedDocs);
JLog :: add('Retrieved ' . count($claimedDocs) .' Parsed ' . count($publications) . ' claimed publications(from: ' . $from . ', to: '. $to .') in ' . (microtime(TRUE) - $time) . ' s', JLog :: INFO, self :: LOG);
JLog :: add('Time1 : '.strtotime(str_replace("/", "-", $from)).' from: ' . $time1 . ', to :'. strtotime(str_replace("/", "-", $to)).' time2: '. $time2 .') time: '.$time , JLog :: INFO, self :: LOG);
return $publications;
} catch (Exception $e) {
JLog :: add('Error retrieving all claims (from: ' . $from . ', to: '. $to .'): ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
return NULL;
}
}
public function deleteClaim($email, $id) {
try {
$this->client ->deleteClaim($email, $id);
JLog :: add('Deleting claim', JLog :: INFO, self :: LOG);
} catch (Exception $e) {
JLog :: add('Error deleting claim ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
}
}
public function parseClaimedDocs($claimedDocs ) {
$locale = JFactory :: getLanguage()->getTag();
$claimedPublications = array();
$claimedPublicationsGrouped = array();
$time = microtime(TRUE);
foreach ($claimedDocs as $claim){
$parsedClaim = $this -> parseClaimedPublication($claim);
if ($parsedClaim == null){
continue;
}
$claimedPublications[] = $parsedClaim;
}
$resultsToGet = array();
$projectsToGet = array();
$claimedDocuments = array();
foreach ($claimedPublications as $claimedPublication){
if ($claimedPublication -> publication != null and $claimedPublication -> publication -> id != null){
$claimedDocuments[$claimedPublication->publicationId] = $claimedPublication -> publication;
}
if ($claimedPublication -> projectId !== null){
$projectsToGet[] = $claimedPublication -> projectId;
}
if ($claimedPublication -> targetPublicationId !== null){
$resultsToGet[] = $claimedPublication -> targetPublicationId;
}
if ($claimedPublication -> targetDatasetId !== null){
$resultsToGet[] = $claimedPublication -> targetDatasetId;
}
if (($claimedPublication -> publication == null || ($claimedPublication -> publication != null and $claimedPublication -> publication -> id == null))){
$resultsToGet[] = $claimedPublication -> publicationId;
}
if ($claimedPublication -> publication == null && $claimedPublication -> type == "dataset"){
$resultsToGet[] = $claimedPublication -> publicationId;
}
}
$resultsToGet = array_unique($resultsToGet);
$projectsToGet = array_unique($projectsToGet);
$searchModel = new OpenAireModelSearch();
$results = $searchModel->getResults($resultsToGet, $locale);
$projects = $searchModel->getProjects($projectsToGet, $locale);
$keydResults = array();
$keydProjects = array();
foreach ($results as $result){
$keydResults[$result->id] = $result;
}
foreach ($projects as $project)
$keydProjects[$project->id] = $project;
foreach ($claimedPublications as $claimedPublication){
if (!isset($claimedPublicationsGrouped[$claimedPublication -> publicationId])){
if ($claimedPublication -> publication == null || $claimedPublication -> publication -> id == null||isset($keydResults[$claimedPublication -> publicationId])){
$publication = isset($keydResults[$claimedPublication -> publicationId])?$keydResults[$claimedPublication -> publicationId]:null;
}
else{
$publication = $claimedPublication -> publication;
}
if ($publication == null )
continue;
$claimedPublicationsGrouped[$claimedPublication -> publicationId] = new stdClass();
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> publicationId = $claimedPublication -> publicationId;
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> date = $claimedPublication -> date;
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> publication = $publication;
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> publications = array();
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> datasets = array();
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> projects = array();
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> concepts = array();
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> type = isset($claimedPublication -> type)?$claimedPublication -> type:'Publication';
//$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> claimType = $claimedPublication -> claimType;
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> publication -> userEmail = $claimedPublication -> userEmail;
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> publication -> claimId = $claimedPublication -> claimId;
}
if ($claimedPublicationsGrouped[$claimedPublication -> publicationId] -> date < $claimedPublication -> date)
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> date = $claimedPublication -> date;
if ($claimedPublication -> publication != null){
// $claimedPublicationsGrouped[$claimedPublication -> publicationId] -> publication=(isset($keydResults[$claimedPublication -> publicationId])&&($keydResults[$claimedPublication -> publicationId]!==null))? $keydResults[$claimedPublication -> publicationId]:$claimedPublication -> publication;
$claimedConcepts = array();
foreach ($claimedPublication -> publication -> concepts as $concept){
$concept = $this->getConcept($concept);
if(!isset( $concept)){
$concept = new stdClass();
}
$claimedConcept = new stdClass();
$claimedConcept->id = isset($concept->id)?$concept->id:'';
$claimedConcept->title = isset($concept->path)?$concept->path:'';
$claimedConcept->date = $claimedPublication->date;
$claimedConcept-> userEmail = $claimedPublication -> userEmail;
$claimedConcept -> claimId = $claimedPublication -> claimId;
//$claimedConcepts[] = $claimedConcept;
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> concepts[]=$claimedConcept;
}
//$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> concepts = array_merge($claimedPublicationsGrouped[$claimedPublication -> publicationId] -> concepts, $claimedConcepts);
}
if ($claimedPublication -> targetPublicationId !== null){
$claimedPublication->title = isset($keydResults[$claimedPublication -> targetPublicationId ])?$keydResults[$claimedPublication -> targetPublicationId ]->title:null;
if ($claimedPublication->title == null){
$claimedPublication->title = isset($claimedDocuments[$claimedPublication -> targetPublicationId ])?$claimedDocuments[$claimedPublication -> targetPublicationId ]->title:null;
}
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> publications[] = $claimedPublication;
}
if ($claimedPublication -> targetDatasetId !== null){
$claimedPublication->title = isset($keydResults[$claimedPublication -> targetDatasetId ])?$keydResults[$claimedPublication -> targetDatasetId ]->title:null;
if ($claimedPublication->title == null){
$claimedPublication->title = isset($claimedDocuments[$claimedPublication -> targetDatasetId ])?$claimedDocuments[$claimedPublication -> targetDatasetId ]->title:null;
}
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> datasets[] = $claimedPublication;
}
if ($claimedPublication -> projectId !== null){
$claimedPublication->title = isset($keydProjects[$claimedPublication -> projectId ])?$keydProjects[$claimedPublication -> projectId ]->title:null;
$claimedPublication->acronym = isset($keydProjects[$claimedPublication -> projectId ])?$keydProjects[$claimedPublication -> projectId ]->acronym:null;
$claimedPublicationsGrouped[$claimedPublication -> publicationId] -> projects[] = $claimedPublication;
}
}
return $claimedPublicationsGrouped;
}
}