-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckan_converter.class.php
170 lines (158 loc) · 5.28 KB
/
ckan_converter.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* ckan_converter class
*
* @copyright 2013 Luca Gioppo
* @license GPLv2
* @author Luca Gioppo
* @created 28/11/2013
* @version 0.1
*/
include ('wso2api.class.php');
/**
*
* @author Luca Gioppo
*
*/
class Ckan_converter {
private $wso2api;
private $debug = false;
private $arrAllowedProviders;
/**
* Class constructor
*
* @param string $api_server
* @param string $user
* @param string $password
* @param string $debug
*/
function __construct($api_server, $user = 'admin', $password = 'admin', $debug = false) {
$this->wso2api = new Wso2API ( $api_server, $user, $password, $debug );
$this->debug = $debug;
$this->getAllowedProviders();
if ($this->debug) {
error_log ( print_r ( 'api_server: ' . $api_server, TRUE ) );
error_log ( print_r ( 'user: ' . $user, TRUE ) );
error_log ( print_r ( 'password: ' . $password, TRUE ) );
error_log ( print_r ( 'arrAllowedProviders: ' . $this->arrAllowedProviders, TRUE ) );
}
}
/**
* return the packages in CKAN format
*
* @return array
*/
function getPackages() {
$apis = array ();
$ret = $this->wso2api->get_all_api_list ();
if ($ret) {
foreach ( $this->wso2api->response->apis as $value ) {
if ($value->status == 'PUBLISHED') {
foreach ( $this->arrAllowedProviders as $provider ) {
if($provider == $value->provider){
$finishname = $value->name . ':' . $value->version . ':' . $value->provider;
$apis [] = $finishname;
break;
}
}
}
}
} else {
echo 'Error message' . $this->wso2api->error_message;
echo 'Error message code' . $this->wso2api->error_code;
}
return $apis;
}
/**
* return the detail of the CKAN resource
*
* @param string $apiname
* @param string $apiversion
* @param string $apiprovider
* @return array
*/
function getAPI($apiname, $apiversion, $apiprovider, $serverapistore) {
// variables for holding the whole API data
$thePackage;
$theAPI;
$swagger;
// getting all the API to get the correct one
$retList = $this->wso2api->get_all_api_list ();
if ($retList) {
foreach ( $this->wso2api->response->apis as $value ) {
if (($value->name == $apiname) && ($value->version == $apiversion) && ($value->provider == $apiprovider)) {
$thePackage = $value;
if ($this->debug)
error_log ( 'Response: ' . print_r ( $thePackage, TRUE ) );
}
}
} else {
echo 'Error message' . $this->wso2api->error_message;
echo 'Error message code' . $this->wso2api->error_code;
}
// getting the API detail
$retDetail = $this->wso2api->get_api_info ( $apiprovider, $apiname, $apiversion );
if ($retDetail) {
if ($this->debug)
error_log ( 'API: ' . print_r ( $this->wso2api->response, TRUE ) );
$theAPI = $this->wso2api->response;
} else {
echo 'Error message' . $this->wso2api->error_message;
echo 'Error message code' . $this->wso2api->error_code;
}
// getting swagger
$retSwagger = $this->wso2api->get_api_swagger ( $apiname, $apiversion );
if ($retSwagger) {
if ($this->debug)
error_log ( 'Swagger: ' . print_r ( $this->wso2api->response, TRUE ) );
$swagger = $this->wso2api->response;
} else {
echo 'Error message' . $this->wso2api->error_message;
echo 'Error message code' . $this->wso2api->error_code;
}
$ckanapi = array ();
$ckanapi ["license_title"] = empty($swagger->info->license)?'':$swagger->info->license;
$ckanapi ["mantainer"] = $theAPI->api->techOwner;
$ckanapi ["mantainer_email"] = $theAPI->api->techOwnerMail;
$ckanapi ["id"] = $apiname . ':' . $apiversion . ':' . $apiprovider;
$ckanapi ["metadata_created"] = date ( "d-m-Y H:i:s", $theAPI->api->lastUpdated / 1000 );
$ckanapi ["relationships"] = array ();
$ckanapi ["license"] = empty($swagger->info->license)?'':$swagger->info->license;
$ckanapi ["metadata_modified"] = date ( "d-m-Y H:i:s", $theAPI->api->lastUpdated / 1000 );
$ckanapi ["author"] = $theAPI->api->bizOwner;
$ckanapi ["author_email"] = $theAPI->api->bizOwnerMail;
$ckanapi ["version"] = $apiversion;
$ckanapi ["licence_id"] = '';
$ckanapi ["type"] = null;
$ckanapi ["groups"] = array ();
$ckanapi ["tags"] = split ( ',', $theAPI->api->tags );
$ckanapi ["tracking_summary"] = array (
"total" => 0,
"summary" => 0
);
$ckanapi ["resources"] = '';
$ckanapi ["name"] = $apiname;
$ckanapi ["isopen"] = true;
$ckanapi ["notes_rendered"] = '';
$ckanapi ["url"] = (empty($swagger->basePath)?'':$swagger->basePath) . '/' . $apiname . '/' . $apiversion;
$ckanapi ["url"] = 'http://' . $serverapistore . '/apis/info?name%3D' . $apiname . '%26version%3D' . $apiversion . "%26provider%3D" . $apiprovider;
$ckanapi ["ckan_url"] = '';
$ckanapi ["notes"] = $theAPI->api->description;
$ckanapi ["title"] = $apiname;
$ckanapi ["ratings_average"] = null;
$ckanapi ["extras"] = array (
// "release-notes" => '',
"topic" => "",
// "publish-date" => '' ,
// "language" => empty($swagger->info->language)?'':$swagger->info->language
);
$ckanapi ["rating_count"] = '';
$ckanapi ["revision_id"] = '';
return $ckanapi;
}
function getAllowedProviders(){
$str = file_get_contents('./allowedProviders.json');
$this->arrAllowedProviders = json_decode($str, true); // decode the JSON into an associative array
}
}
?>