Skip to content

Commit

Permalink
[PURR][#2583]Replace OFR API with ROR API for organization query
Browse files Browse the repository at this point in the history
  • Loading branch information
kuang5 authored and nkissebe committed Nov 13, 2024
1 parent 173bfae commit 6d71fdc
Show file tree
Hide file tree
Showing 8 changed files with 424 additions and 363 deletions.
141 changes: 75 additions & 66 deletions core/components/com_members/admin/controllers/members.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,7 @@ public function saveTask()
// If RoR Api is turned off because of failed API or if key doesn't exist, don't retrieve list from Api.
$useRorApi = \Component::params('com_members')->get('rorApi');
if (isset($profile['organization']) && !empty($profile['organization']) && $useRorApi) {
$id = $this->getOrganizationId($profile['organization']);
$profile['orgid'] = $id;
$profile['orgid'] = $this->getOrganizationId($profile['organization']);
}

foreach ($profile as $key => $data)
Expand Down Expand Up @@ -1716,47 +1715,56 @@ public function checkpassTask()
/**
* Perform querying of research organization based on the input value
*
* @return array matched research organization names
* @return array or false matched research organization names
*/
public function getOrganizationsTask(){
$term = trim(Request::getString('term', ''));
$term = \Components\Members\Helpers\Utility::escapeSpecialChars($term);

$verNum = \Component::params('com_members')->get('rorApiVersion');

if (!empty($verNum))
{
$queryURL = "https://api.ror.org/$verNum/organizations?query.advanced=names.value:" . urlencode($term);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $queryURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

if (strpos($term, ' ') !== false){
$term = str_replace(' ', '+', $term);
}

$queryURL = 'https://api.ror.org/organizations?query=' . $term;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $queryURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

if (!$result){
return false;
}
$result = curl_exec($ch);

$info = curl_getinfo($ch);
if (!$result){
return false;
}

$code = $info['http_code'];
$info = curl_getinfo($ch);

if (($code != 201) && ($code != 200)){
return false;
}
$code = $info['http_code'];

$organizations = [];
if (($code != 201) && ($code != 200)){
return false;
}

$resultObj = json_decode($result);
$organizations = [];

foreach ($resultObj->items as $orgObj){
$organizations[] = $orgObj->name;
}
$resultObj = json_decode($result);

foreach ($resultObj->items as $orgObj)
{
foreach ($orgObj->names as $nameObj)
{
if ($nameObj->lang == "en" && !in_array($nameObj->value, $organizations))
{
$organizations[] = $nameObj->value;
}
}
}

curl_close($ch);
curl_close($ch);

echo json_encode($organizations);
exit();
echo json_encode($organizations);
exit();
}
}

/**
Expand All @@ -1767,47 +1775,48 @@ public function getOrganizationsTask(){
*/
public function getOrganizationId($organization){
$org = trim($organization);
$id = "none";

if (strpos($org, ' ') !== false){
$org = str_replace(' ', '+', $org);
}

$queryURL = "https://api.ror.org/organizations?query=" . $org;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $queryURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

if (!$result){
return false;
}

$info = curl_getinfo($ch);
$orgQry = \Components\Members\Helpers\Utility::escapeSpecialChars($org);

$verNum = \Component::params('com_members')->get('rorApiVersion');

if (!empty($verNum))
{
$queryURL = "https://api.ror.org/$verNum/organizations?query.advanced=names.value:" . urlencode($orgQry);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $queryURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$code = $info['http_code'];
$result = curl_exec($ch);

if (($code != 201) && ($code != 200)){
return false;
}
if (!$result){
return false;
}

$resultObj = json_decode($result);
$info = curl_getinfo($ch);

$org = str_replace('+', ' ', $org);
$code = $info['http_code'];

foreach ($resultObj->items as $orgObj){
if ($org == $orgObj->name){
$id = $orgObj->id;
break;
}
}
if (($code != 201) && ($code != 200)){
return false;
}

curl_close($ch);
$resultObj = json_decode($result);

return $id;
foreach ($resultObj->items as $orgObj)
{
foreach ($orgObj->names as $nameObj)
{
if (strcmp($nameObj->value, $org) == 0)
{
curl_close($ch);
return $orgObj->id;
}
}
}

curl_close($ch);
return "";
}
}


}
1 change: 1 addition & 0 deletions core/components/com_members/config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@
<option value="0">No</option>
<option value="1">Yes</option>
</field>
<field name="rorApiVersion" type="text" menu="hide" default="" label="Research Organization Registry API Version Number" description="The version number of Research Organization Registry API, such as 'v2'." />
<field name="@spacer" type="spacer" default="" label="" description="" />
<field name="orcid_service" type="list" default="public" label="ORCID Service" description="Select the service to use for ORCID. Sandbox is for testing and debugging purposes. The Public service only allows for searching records, new ID creation is not allowed.">
<option value="public">Public (search only)</option>
Expand Down
36 changes: 36 additions & 0 deletions core/components/com_members/helpers/utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,40 @@ public static function isActiveCode($code)

return ($result) ? true : false;
}

/**
* Escape the special characters that might exist in the organization names on Research Organization Registry
*
* @param string $term
* @return string
*/
public static function escapeSpecialChars($term)
{
$special_chars = [
"+" => "\+",
"-" => "\-",
"=" => "\=",
"&&" => "\&&",
"||" => "\||",
">" => "\>",
"<" => "\<",
"!" => "\!",
"(" => "\(",
")" => "\)",
"{" => "\{",
"}" => "\}",
"[" => "\[",
"]" => "\]",
"^" => "\^",
'"' => '\"',
"~" => "\~",
"*" => "\*",
"?" => "\?",
":" => "\:",
"\\" => "\\\\",
"/" => "\/"
];

return str_replace(array_keys($special_chars), array_values($special_chars), $term);
}
}
Loading

0 comments on commit 6d71fdc

Please sign in to comment.