Skip to content

Commit

Permalink
Move makeHttpRequest function out of arSolrPlugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
melaniekung committed Aug 21, 2024
1 parent 915ad3e commit 218c2d6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
15 changes: 15 additions & 0 deletions lib/helper/QubitHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -818,3 +818,18 @@ function render_search_result_date($date)
return Qubit::renderDateStartEnd($displayDate, $startDate, $endDate);
}
}

function makeHttpRequest($url, $method = 'GET', $body = null)
{
$curlSession = curl_init($url);
if ($body) {
curl_setopt($curlSession, CURLOPT_POST, 1);
curl_setopt($curlSession, CURLOPT_POSTFIELDS, $body);
}
curl_setopt($curlSession, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curlSession);
curl_close($curlSession);

return json_decode($result);
}
39 changes: 13 additions & 26 deletions plugins/arSolrPlugin/lib/arSolrPlugin.class.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

require 'lib/helper/QubitHelper.php';

/*
* This file is part of the Access to Memory (AtoM) software.
*
Expand Down Expand Up @@ -113,7 +115,7 @@ public function flush()
try {
$url = $this->solrBaseUrl.'/solr/'.$this->solrClientOptions['collection'].'/update/';
$query = '{"delete": {"query": "*:*"}}';
arSolrPlugin::makeHttpRequest($url, 'POST', $query);
makeHttpRequest($url, 'POST', $query);
} catch (Exception $e) {
}

Expand Down Expand Up @@ -237,7 +239,7 @@ public function addDocument($data, $type)
}

$url = $this->solrBaseUrl.'/solr/'.$this->solrClientOptions['collection'].'/update/json/docs';
$response = arSolrPlugin::makeHttpRequest($url, 'POST', json_encode([
$response = makeHttpRequest($url, 'POST', json_encode([
$type => $data,
]));

Expand All @@ -260,33 +262,18 @@ public function getSolrCollection()
public function search($query, $type)
{
$url = $this->getSolrUrl().'/solr/'.$this->getSolrCollection().'/query';
$response = arSolrPlugin::makeHttpRequest($url, 'POST', json_encode($query->getQueryParams()));
$response = makeHttpRequest($url, 'POST', json_encode($query->getQueryParams()));

return new arSolrResultSet($response);
}

public static function makeHttpRequest($url, $method = 'GET', $body = null)
{
$curlSession = curl_init($url);
if ($body) {
curl_setopt($curlSession, CURLOPT_POST, 1);
curl_setopt($curlSession, CURLOPT_POSTFIELDS, $body);
}
curl_setopt($curlSession, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curlSession);
curl_close($curlSession);

return json_decode($result);
}

/**
* Initialize Solr index if it does not exist.
*/
protected function initialize()
{
$url = $this->solrBaseUrl.'/solr/admin/collections?action=LIST';
$response = arSolrPlugin::makeHttpRequest($url);
$response = makeHttpRequest($url);

if (false !== array_search($this->solrClientOptions['collection'], $response->collections)) {
$this->log('Collection found. Not initializing');
Expand All @@ -295,14 +282,14 @@ protected function initialize()

$this->log('Creating Solr Collection');
$url = $this->solrBaseUrl.'/solr/admin/collections?action=CREATE&name='.$this->solrClientOptions['collection'].'&numShards=2&replicationFactor=1&wt=json';
arSolrPlugin::makeHttpRequest($url);
makeHttpRequest($url);

$topLevelProperties = [];
$subProperties = [];

$url = $this->solrBaseUrl.'/api/collections/'.$this->solrClientOptions['collection'].'/config/';
$updateDefaultHandler = '{"update-requesthandler": {"name": "/select", "class": "solr.SearchHandler", "defaults": {"echoParams": "explicit"}}}';
arSolrPlugin::makeHttpRequest($url, 'POST', $updateDefaultHandler);
makeHttpRequest($url, 'POST', $updateDefaultHandler);

// Load and normalize mappings
$this->loadAndNormalizeMappings();
Expand Down Expand Up @@ -349,7 +336,7 @@ private function addAutoCompleteFields()
}
$addCopyField .= substr($copyField, 0, -1).'}';
$url = $this->solrBaseUrl.'/api/collections/'.$this->solrClientOptions['collection'].'/schema/';
arSolrPlugin::makeHttpRequest($url, 'POST', $addCopyField);
makeHttpRequest($url, 'POST', $addCopyField);
}
}

Expand Down Expand Up @@ -382,8 +369,8 @@ private function addAutoCompleteConfigs()
}
}
}';
arSolrPlugin::makeHttpRequest($url, 'POST', $addSearchComponent);
arSolrPlugin::makeHttpRequest($url, 'POST', $addRequestHandler);
makeHttpRequest($url, 'POST', $addSearchComponent);
makeHttpRequest($url, 'POST', $addRequestHandler);
}
}

Expand Down Expand Up @@ -556,14 +543,14 @@ private function setAnalyzers()
]];

$url = $this->solrBaseUrl.'/solr/'.$this->solrClientOptions['collection'].'/schema/';
arSolrPlugin::makeHttpRequest($url, 'POST', json_encode($query));
makeHttpRequest($url, 'POST', json_encode($query));
}
}

private function addFieldsToType($query)
{
$url = $this->solrBaseUrl.'/solr/'.$this->solrClientOptions['collection'].'/schema/';
arSolrPlugin::makeHttpRequest($url, 'POST', $query);
makeHttpRequest($url, 'POST', $query);
}

private function loadAndNormalizeMappings()
Expand Down

0 comments on commit 218c2d6

Please sign in to comment.