Skip to content

Commit

Permalink
modules now loaded through autoloader (and dm renamed dmno)
Browse files Browse the repository at this point in the history
  • Loading branch information
lokal-profil committed Oct 8, 2013
1 parent 18fa7ff commit 6dbd0c8
Show file tree
Hide file tree
Showing 6 changed files with 389 additions and 367 deletions.
368 changes: 13 additions & 355 deletions Modules.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php

/* -------------------------------------------------------------------------------- */
/**
* Base module
* For implemented modules see the /modules directory
*/
abstract class modul {
protected static $short_name; //short parseable name of the datasource
public static $long_name; //name of datasource
public static $plain_name; //short but descriptive
public static $info_link; //url to more information for datasource
public static $data_license; //license for the data (excluding images)
protected static $service_url; //The address for the api
protected $thumb_width; //The address for the api
public $items; //array of replsponded items
public static $supported_types;//array of types supported by make_query
protected static $short_name; //short parseable name of the datasource
public static $long_name; //name of datasource
public static $plain_name; //short but descriptive
public static $info_link; //url to more information for datasource
public static $data_license; //license for the data (excluding images)
protected static $service_url; //The address for the api
protected $thumb_width; //The width of the requested thumbnail (if requested)
public $items; //array of response items
public static $supported_types; //array of types supported by make_query

/** Construct the class, giving thumb_width in px */
abstract public function __construct($thumb_width);
Expand All @@ -37,348 +39,4 @@ abstract public function make_query($type, $value);
abstract public function process_response($response);
}

/**
* --------------------------------------------------------------------------------
* Description of dataset
*/
class odok extends modul {
protected static $short_name = 'odok';
public static $long_name = 'ÖDOK - Public art in Sweden';
public static $plain_name = 'ÖDOK'; //short but descriptive
public static $info_link = 'http://offentligkonst.se';
protected static $service_url = 'http://wlpa.wikimedia.se/odok-bot/api.php';
public static $data_license = 'ODbL';
public static $supported_types = array('artist', 'title', 'place');

public function __construct($thumb_width) {//these should not be instance properties
$this->thumb_width = $thumb_width;
}

/** Construct and return the query */
public function make_query($type, $value) {
$queryUrl = self::$service_url . '?action=get&format=json&limit=100';
switch ($type) {
case 'artist':
$queryUrl .= '&artist=' . urlencode($value);
break;
case 'title':
$queryUrl .= '&title=' . urlencode($value);
break;
case 'place':
$queryUrl .= '&address=' . urlencode($value); //Should return a warnign that it doesn't search on district/city/municipality/county
break;
default:
$queryUrl = NULL;
break;
}
return $queryUrl;
}

/** Process the returned response and fill internal parameters */
public function process_response($response) {
$json = json_decode($response, true);

$totalResults = count($json['body']);
$success = $json['head'];
if ($totalResults < 1) {
$this->items = Array();
$success = $json['head']['status'];
if ($success == 0) {
return $json['head']['error_number'] . ": " . $json['head']['error_message'];
}
} else {
$arr = Array();
foreach ($json['body'] as $key => $value){
$a = $value['hit'];
//Make media separately
if (empty($a['image'])){
$media = array( "mediatype" => 'none');
} else {
$media = array(
"mediatype" => 'image',
"thumb" => 'https://commons.wikimedia.org/w/thumb.php?f=' . $a['image'] . '&width=' . $this->thumb_width,
"medialink" => self::getImageFromCommons($a['image']),
"medialic" => NULL,
"byline" => 'See <a href="https://commons.wikimedia.org/wiki/File:' . $a['image'] .'">the image page on Wikimedia Commons</a>.'
);
}
//Make place separately
$place = '';
if (!empty($a['address'])){
$place .= $a['address'];
if (!empty($a['district'])){
$place .= ', ' . $a['district'];
}
} elseif (!empty($a['district'])){
$place .= $a['district'];
}
//Make main object
$item = array(
"id" => $a['id'],
"title" => empty($a['title']) ? NULL : $a['title'],
"artist" => empty($a['artist']) ? NULL : $a['artist'], //wikidata?
"year" => empty($a['year']) ? NULL : $a['year'],
"material" => empty($a['material']) ? NULL : $a['material'],
"place" => empty($place) ? NULL : $place,
"geodata" => array(
"lat" => $a['lat'],
"lon" => $a['lon'],
),
"media" => $media,
"text" => array(
"fulltext" => empty($a['descr']) ? NULL : $a['descr'],
"textlic" => 'CC BY-SA 3.0', //as info is from Wikipedia
"byline" => 'Description from <a href="https://sv.wikipedia.org/">Wikipedia</a> / <a href="https://creativecommons.org/licenses/by-sa/3.0/deed.en">CC BY-SA 3.0</a>.'
),
"meta" => array(
"module" => self::$short_name,
"datalic" => self::$data_license,
"byline" => '<a href="' . self::$info_link . '">' . self::$long_name . '</a> /' . self::$data_license
)
);
array_push($arr, $item);
}
$this->items = $arr;
}
return; //Null
}

/*
* Given the filename on Commons this returns the url of the full image
* From: https://fisheye.toolserver.org/browse/erfgoed/api/includes/CommonFunctions.php
*/
private static function getImageFromCommons($filename) {
if ($filename) {
$filename = ucfirst($filename);
$filename = str_replace(' ', '_', $filename);
$md5hash=md5($filename);
$url = 'https://upload.wikimedia.org/wikipedia/commons/' . $md5hash[0] . '/' . $md5hash[0] . $md5hash[1] . '/' . $filename;
return $url;
}
}
}

/**
* --------------------------------------------------------------------------------
* Description of dataset
*/
class voreskunst extends modul {
protected static $short_name = 'voreskunst';
public static $long_name = 'Vores Kunst - Kulturstyrelsen';
public static $plain_name = 'Vores Kunst'; //short but descriptive
public static $info_link = 'http://vores.kunst.dk/';
protected static $service_url = 'http://kunstpaastedet.dk/wsd/search/';
public static $data_license = 'CC-0'; //http://www.kulturstyrelsen.dk/kulturarv/databaser/rettigheder-til-data/
public static $supported_types = array('artist', 'title', 'place');

public function __construct($thumb_width) {
$this->thumb_width = $thumb_width;
}

/** Construct and return the query */
public function make_query($type, $value) {
$queryUrl = self::$service_url;
switch ($type) {
case 'artist':
$queryUrl .= 'artist/' . urlencode($value);
break;
case 'title':
$queryUrl .= 'keyword/' . urlencode($value); //Should return a warning clarifying it's a free text search. Reduce relevance
break;
case 'place':
//test if zipcode
if (strlen($value)==4 and is_numeric($value)){
$queryUrl .= 'zipcode/' . urlencode($value);
} else {
$queryUrl .= 'keyword/' . urlencode($value); //Should return a warning clarifying it's a free text search. Reduce relevance
}
break;
default:
$queryUrl = NULL;
break;
}
return $queryUrl;
}

/** Process the returned response and fill internal parameters */
public function process_response($response) {
$json = json_decode($response, true);

reset($json);
$first_key = key($json);

if (!is_array($json[$first_key])) {
$this->items = Array();
return $json[$first_key];
} else {
$arr = Array();
foreach ($json[$first_key] as $a){
//Make place separately
$place = '';
if (!empty($a['location address'])){
$place .= $a['location address'];
if (!empty($a['location name'])){
$place .= ', ' . $a['location name'];
}
} elseif (!empty($a['location name'])){
$place .= $a['location name'];
}
//Make main object
$item = array(
"id" => $a['object_id'],
"title" => empty($a['title']) ? NULL : $a['title'],
"artist" => empty($a['artist']) ? NULL : $a['artist'],
"year" => empty($a['date']) ? NULL : $a['date'],
"material" => NULL,
"place" => empty($place) ? NULL : $place,
"geodata" => array(
"lat" => $a['latitude'],
"lon" => $a['longitude'],
),
"media" => array(
"mediatype" => 'none' //As $a['primary_image'] points to a dispatcher
//"medialic" => 'CC BY-NC-ND 2.5 DK'
),
"text" => array(
"fulltext" => empty($a['primary_image']) ? NULL : 'You can <a href="' . $a['primary_image'] . '">download an image of this artwork</a>.',
"textlic" => NULL,
"byline" => NULL
),
"meta" => array(
"module" => self::$short_name,
"datalic" => self::$data_license,
"byline" => '<a href="' . self::$info_link . '">' . self::$long_name . '</a> /' . self::$data_license
)
);
array_push($arr, $item);
}
$this->items = $arr;
}
return; //Null
}
}

/* -------------------------------------------------------------------------------- */
class dm extends modul {
protected static $short_name = 'dmno';
public static $long_name = 'Digitalt museum';
public static $plain_name = 'Digitalt museum'; //short but descriptive
public static $info_link = 'http://digitaltmuseum.no';
protected static $service_url = 'http://kulturnett2.delving.org/organizations/kulturnett/api/search';
public static $data_license = '';
public static $supported_types = array('artist', 'title');

public function __construct($thumb_width) {//these should not be instance properties
$this->thumb_width = $thumb_width;
}

/** Construct and return the query */
public function make_query($type, $value) {
$queryUrl = self::$service_url . '?format=json&query=';
switch ($type) {
case 'artist':
$queryUrl .= 'delving_creator:' . urlencode($value);
break;
case 'title':
$queryUrl .= 'delving_title:' . urlencode($value);
break;
default:
$queryUrl = NULL;
break;
}
return $queryUrl;
}

/** Process the returned response and fill internal parameters */
public function process_response($response) {
$json = json_decode($response, true);
$totalResults = count($json['result']['items']);
$success = $json['result'];
if ($totalResults < 1) {
$this->items = Array();
$success = $json['head']['status'];
if ($success == 0) {
return "Unknown error";
}
} else {
$arr = Array();
foreach ($json['result']['items'] as $value){
error_log(print_r($value, true));
$a = $value['item']['fields']['delving_thumbnail'][0];
//Make media separately
if (empty($a)){
$media = array( "mediatype" => 'none');
} else {
$media = array(
"mediatype" => 'image',
"thumb" => $a,
"medialink" => $a,
"medialic" => $value['item']['fields']['europeana_rights'][0],
"byline" => 'See <a href="' . $value['item']['fields']['delving_landingPage'][0] .'">the image page on ' . $value['item']['fields']['delving_provider'][0] . '</a>.'
);
}


$a = $value['item']['fields'];

//Make place separately
$place = '';
if (!empty($a['abm_namedPlace'][0])){
$place .= $a['abm_namedPlace'][0];
}

if (!empty($a['abm_municipality'][0])){
if(!empty($place)) {
$place .= ', ';
}
$place .= $a['abm_municipality'][0];
}

if(!empty($a['abm_county'])) {
if(!empty($place)) {
$place .= ', ';
}
$place .= $a['abm_county'][0];
}

// Handle geopos
$lat = NULL;
$long = NULL;
if($a['delving_hasGeoHash'][0] == 'true' && !empty($a['delving_geohash'][0])) {
list($lat, $long) = explode(',', $a['delving_geohash'][0]);
}

//Make main object
$item = array(
"id" => $a['dc_identifier'],
"title" => empty($a['delving_title'][0]) ? NULL : $a['delving_title'][0],
"artist" => empty($a['delving_creator'][0]) ? NULL : $a['delving_creator'][0], //wikidata?
"year" => empty($a['dcterms_created'][0]) ? NULL : $a['dcterms_created'],
"material" => empty($a['dc_medium'][0]) ? NULL : $a['dc_medium'][0],
"place" => empty($place) ? NULL : $place,
"geodata" => array(
"lat" => $lat,
"lon" => $long,
),
"media" => $media,
"text" => array(
"fulltext" => empty($a['delving_description'][0]) ? NULL : $a['delving_description'][0],
"textlic" => NULL, //as info is from Wikipedia
"byline" => NULL
),
"meta" => array(
"module" => self::$short_name,
"datalic" => self::$data_license,
"byline" => '<a href="' . self::$info_link . '">' . self::$long_name . '</a> /' . self::$data_license
)
);
array_push($arr, $item);
}
$this->items = $arr;
}
return; //Null
}

}

?>
Loading

0 comments on commit 6dbd0c8

Please sign in to comment.