Skip to content

Commit

Permalink
Initial import as standalone lib
Browse files Browse the repository at this point in the history
  • Loading branch information
rsinger committed Mar 26, 2014
0 parents commit 11c6723
Show file tree
Hide file tree
Showing 6 changed files with 575 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

.idea/
atlassian-ide-plugin.xml
vendor/

composer.lock
26 changes: 26 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "talis/SRUClient-php",
"description": "This is a php client library for SRU (http://www.loc.gov/standards/sru/)",
"keywords": ["sru", "php", "client library"],
"homepage": "https://github.com/talis/SRUclient-php",
"type": "library",
"license": "MIT",
"authors":[
{
"name":"Ross Singer",
"email": "[email protected]",
"homepage":"http://engineering.talis.com"
}
],
"require":{
"php": ">=5.3.0",
"phpunit/phpunit": ">=3.7.0@stable",
"monolog/monolog": ">=1.5.0",
"guzzle/guzzle":"~3.7"
},
"autoload":{
"psr-0": {
"SRU": "src"
}
}
}
353 changes: 353 additions & 0 deletions src/SRU/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,353 @@
<?php

namespace SRU;

class Client
{
/**
* Base URL of SRU service
* @var string
*/
private $baseUrl;

/**
* Default record schema for requests
* @var string
*/
private $defaultRecordSchema;

/**
* Default max number of records to return
* @var int
*/
private $defaultMaximumRecords = 10;

/**
* Default HTTP method to use for requests
* @var string
*/
private $defaultHttpMethod;

/**
* Default SRU standard version to request
* @var string
*/
private $defaultSRUVersion = "1.1";

/**
* @var \Guzzle\Http\Client
*/
private $httpClient;

/**
* @param string $baseUrl The base URL of the SRU service
* @param array $options An array of options for the SRU service ('recordSchema', 'maximumRecords', 'httpMethod', 'version')
*/
function __construct($baseUrl, $options = array())
{
$this->baseUrl = $baseUrl;
if (isset($options['recordSchema']))
{
$this->defaultRecordSchema = $options['recordSchema'];
}
if(isset($options['maximumRecords']))
{
$this->defaultMaximumRecords = $options['maximumRecords'];
}
if(isset($options['httpMethod']))
{
$this->defaultHttpMethod = $options['httpMethod'];
}
if(isset($options['version']))
{
$this->defaultSRUVersion = $options['version'];
}
}

/**
* Returns a DOMDocument of the explain operation response (or a string if $raw is true)
* @param bool $raw
* @return \DOMDocument|string
*/
public function explain($raw = false)
{
$explainResponse = $this->fetch(array("version" => $this->getDefaultSRUVersion(), "operation" => "explain"));
if ($raw == true)
{
$explain = $explainResponse->getBody();
}
else
{
$explain = new \DOMDocument();
$explain->loadXML($explainResponse->getBody());
}

return ($explain);
}

/**
* Alias for searchRetrieve()
*
* @param string $query
* @param array $options
* @param bool $raw
* @return SearchRetrieveResponse|string
*/
public function search($query, $options = array(), $raw = false)
{
return ($this->searchRetrieve($query, $options, $raw));
}

/**
* Performs a searchRetrieve operation and returns a SearchRetrieveResponse object or a string is $raw is true
*
* @param string $query The CQL query string
* @param array $options The query options ('version', 'maximumRecords', 'startRecord', 'recordSchema', 'recordPacking')
* @param bool $raw If true, returns the response as a string
* @return SearchRetrieveResponse|string
*/
public function searchRetrieve($query, $options = array(), $raw = false)
{
$options['operation'] = 'searchRetrieve';
$options["query"] = $query;
$options["version"] = isset($options['version']) ? $options['version'] : $this->getDefaultSRUVersion();
$options['maximumRecords'] = isset($options['maximumRecords']) ? $options['maximumRecords'] : $this->defaultMaximumRecords;
$options['startRecord'] = isset($options['startRecord']) ? $options['startRecord'] : 1;
if (isset($options['recordSchema']) || $this->defaultRecordSchema)
{
$options['recordSchema'] = isset($options['recordSchema']) ? $options['recordSchema'] : $this->defaultRecordSchema;
}
$options['recordPacking'] = isset($options['recordPacking']) ? $options['recordPacking'] : "xml";
$searchRetrieveResponse = $this->fetch($options);
if ($raw == true)
{
$searchRetrieve = $searchRetrieveResponse->getBody();
}
else
{
$searchXML = new \DOMDocument();
$searchXML->loadXML($searchRetrieveResponse->getBody());

$searchRetrieve = new SearchRetrieveResponse($searchXML);
}

return ($searchRetrieve);
}

/**
* Performs a scan operation and returns a ScanResponse object or a string is $raw is true
*
* @param string $query The CQL query string
* @param array $options The query options ('version', 'maximumTerms', 'scanClause')
* @param bool $raw If true, returns the response as a string
* @return SearchRetrieveResponse|string
*/
public function scan($scanClause, $options = array(), $raw = false)
{
$options['operation'] = 'scan';
$options["scanClause"] = $scanClause;
$options["version"] = isset($options['version']) ? $options['version'] : $this->getDefaultSRUVersion();
$options['maximumTerms'] = isset($options['maximumTerms']) ? $options['maximumTerms'] : $this->defaultMaximumRecords;

$scanResponse = $this->fetch($options);
if ($raw == true)
{
$scan = $scanResponse->getBody();
}
else
{
$scan = new ScanResponse($scanResponse->getBody());
}
return ($scan);
}

/**
* Returns the supported recordSchema identifiers as defined in the explain response
* @return array
*/
public function recordSchemas()
{
$explain = $this->explain();
$xpath = new \DOMXPath($explain);
$xpath->registerNamespace("zs", "http://www.loc.gov/zing/srw/");
$xpath->registerNamespace("ex", "http://explain.z3950.org/dtd/2.0/");

$nodes = $xpath->query("/zs:explainResponse/zs:record/zs:recordData/ex:explain/ex:schemaInfo/ex:schema");
$schemas = array();
foreach ($nodes as $node)
{
$schemas[$node->getAttribute("name")] = array("identifier" => $node->getAttribute("identifier"));
$titleList = $node->getElementsByTagName("title");
foreach ($titleList as $title)
{
$schemas[$node->getAttribute("name")]["title"] = $title->nodeValue;
}
}
return ($schemas);
}

/**
* Returns the supported indexes as defined in the explain response
* @return array
*/
public function indexes()
{
$explain = $this->explain();
$xpath = new \DOMXPath($explain);
$xpath->registerNamespace("zs", "http://www.loc.gov/zing/srw/");
$xpath->registerNamespace("ex", "http://explain.z3950.org/dtd/2.0/");

$nodes = $xpath->query("/zs:explainResponse/zs:record/zs:recordData/ex:explain/ex:indexInfo/ex:index/ex:map/ex:name");
$indexes = array();
foreach ($nodes as $node)
{
$idx = array("set" => $node->getAttribute("set"), "name" => $node->nodeValue);
$nodeList = $node->parentNode->parentNode->getElementsByTagName('title');
foreach ($nodeList as $title)
{
$idx['title'] = $title->nodeValue;
}
$indexes[$node->getAttribute("set") . "." . $node->nodeValue] = $idx;
}
return ($indexes);
}

/**
* Performs the HTTP request based on the defined request method
* @param array $args
* @param array $options
* @return \Guzzle\Http\Message\Response
*/
protected function fetch(array $args = NULL, array $options = array())
{
$client = $this->getHttpClient();

if($this->defaultHttpMethod == 'GET')
{
if(empty($args))
{
$url = $this->getBaseUrl();
} else {
$url = $this->getBaseUrl() . "?" . http_build_query($args);
}

$request = $client->get($url, $args, $options);
$response = $request->send();

return $response;
} else
{
$request = $client->post($this->getBaseUrl(), array(), $args, $options);
$response = $request->send();

return $response;
}
}

/**
* Returns the base URL of the SRU service
* @return string
*/
public function getBaseUrl()
{
return $this->baseUrl;
}

/**
* Sets the base URL of the SRU service
* @param string $baseUrl
*/
public function setBaseUrl($baseUrl)
{
$this->baseUrl = $baseUrl;
}

/**
* Returns the default record schema
*
* @return string
*/
public function getDefaultRecordSchema()
{
return $this->defaultRecordSchema;
}

/**
* Sets the default record schema for searchRetrieve requests
*
* @param string $defaultRecordSchema
*/
public function setDefaultRecordSchema($defaultRecordSchema)
{
$this->defaultRecordSchema = $defaultRecordSchema;
}

/**
* Returns the default maximum number of records to return in a searchRetrieve request
* @return int
*/
public function getDefaultMaximumRecords()
{
return $this->defaultMaximumRecords;
}

/**
* Sets the default maximum number of records to return in a searchRetrieve request
* @param int $defaultMaximumRecords
*/
public function setDefaultMaximumRecords($defaultMaximumRecords)
{
$this->defaultMaximumRecords = $defaultMaximumRecords;
}

/**
* Returns the HTTP method client will use with SRU service by default
*
* @return string
*/
public function getDefaultHttpMethod()
{
return $this->defaultHttpMethod;
}

/**
* Sets the HTTP method the client will use with the SRU service by default
* @param string $defaultHttpMethod
*/
public function setDefaultHttpMethod($defaultHttpMethod)
{
$this->defaultHttpMethod = $defaultHttpMethod;
}

/**
* Lazy loader for the GuzzleClient
* @return \Guzzle\Http\Client
*/
protected function getHttpClient()
{
if (!$this->httpClient)
{
$this->httpClient = new \Guzzle\Http\Client();
}
return $this->httpClient;
}

/**
* @return string
*/
public function getDefaultSRUVersion()
{
return $this->defaultSRUVersion;
}

/**
* @param string $defaultSRUVersion
*/
public function setDefaultSRUVersion($defaultSRUVersion)
{
$this->defaultSRUVersion = $defaultSRUVersion;
}

}

?>
Loading

0 comments on commit 11c6723

Please sign in to comment.