Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

UGC API support #8

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions src/kSamsok.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
<?php
class kSamsok {
public $key;
public $ugcKey;
public $url = 'http://kulturarvsdata.se/ksamsok/api?';
public $ugcUrl = 'http://ugc.kulturarvsdata.se/UGC-hub/api?';

public function __construct($key) {
public function __construct($key, $ugcKey = false) {
$this->key = $key;
$this->ugcKey = $ugcKey;
// checks if API Key or request URL is bad(can also )
// check if URL does return a error
$testQuery = $this->url . 'x-api=' . $this->key . '&method=search&query=text%3D"test"&recordSchema=presentation';
$this->validXml($testQuery);

if ($this->ugcKey !== false) {
$testQuery = $this->url . 'x-api=' . $this->ugcKey . 'method=retrieve&scope=count&objectUri=all';
$this->validXml($testQuery);
}
}

// Checks if valid xml is returned, if not throw Exception and kill the script
Expand All @@ -27,6 +35,20 @@ protected function validXml($url) {
}
}

protected function validJson($url) {
try {
@$json = file_get_contents($url);

if ($json === false || json_decode($json) === null) {
throw new Exception('Bad API request. (' . $url . ')');
}
} catch(Exception $e) {
echo 'Caught Exception: ', $e->getMessage(), "\n";
// these are fatal errors so kill the script
die();
}
}

protected function prepareUrl($url) {
// replace withe space
$url = preg_replace('/\\s/', '%20', $url);
Expand Down Expand Up @@ -133,7 +155,7 @@ protected function parseRecord($record) {
}

protected function idFormat($id, $format = 'raw') {
// $format can be string 'xml' or string 'raw'
// $format can be string 'xml'/string 'raw'/string 'uri'

// if is the entire url strip it off
if (stripos($id, 'http://kulturarvsdata.se/') !== false) {
Expand Down Expand Up @@ -169,6 +191,14 @@ protected function idFormat($id, $format = 'raw') {
return substr_replace($id, '/xml', $formatLocation, 0);
}
}

if ($format === 'url') {
if (strpos($id,'http://kulturarvsdata.se/')) {
return $id;
} else {
return 'http://kulturarvsdata.se/' . $id;
}
}
}

public function search($text, $start, $hits, $images = false) {
Expand Down Expand Up @@ -311,4 +341,30 @@ public function searchHint($string, $count = '5') {
return false;
}
}

public function ugcObject($objectId) {
$objectId = $this->idFormat($objectId, 'url');

$urlQuery = $this->ugcUrl . 'x-api=' . $this->ugcKey . '&method=retrieve&scope=all&objectUri=' . $objectId . '&format=json';
$this->validJson($urlQuery);

return file_get_contents($urlQuery);
}

public function ugcCount($objectId) {
$objectId = $this->idFormat($objectId, 'url');

$urlQuery = $this->ugcUrl . 'x-api=' . $this->ugcKey . '&method=retrieve&scope=count&objectUri=' . $objectId . '&format=json';
$this->validJson($urlQuery);

return file_get_contents($urlQuery);
}

public function ugcSingleRelation($contentId) {

$urlQuery = $this->ugcUrl . 'x-api=' . $this->ugcKey . '&method=retrieve&objectUri=all&contentId=' . $contentId . '&scope=single&format=json';
$this->validJson($urlQuery);

return file_get_contents($urlQuery);
}
}