forked from UoEMainLibrary/ASRestAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostGenreSubjectToAS.php
209 lines (168 loc) · 5.74 KB
/
postGenreSubjectToAS.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/**
* Created by JetBrains PhpStorm.
* User: cknowles - University of Edinburgh
* Date: 08/01/2014
* Time: 14:22
*/
//$url = "http://localhost:8089/";
//$response = file_get_contents($url);
//echo $response;
//Example using CURL for Get
class Data {
public $title = "";
public $vocabulary = "";
public $external_ids = "";
public $source = "";
public $authority_id = "";
public $created_by = "";
//'created_on' => ,
public $last_modified_by = "";
public $user_mtime = "";
public $terms= "";
}
class Term {
public $term_type = "";
public $term = "";
public $vocabulary = "";
}
//not sure if needed
$username = 'xxxx';
$password = 'xxxx';
$filename = "/Users/cknowles/Documents/CRCSubjectCSV/updated/cms_auth_genr.csv";
//start session
//start_session();
$session_id = loginToAS($username, $password);
//$_SESSION['TOKEN']=$session_id;
print_r($session_id);
echo '***************';
readInCSV($filename, $session_id);
function loginToAS($username, $password)
{
$headers = array(
'Accept: application/json',
'Content-Type: application/json'
);
$service_url = 'http://localhost:8089/users/admin/login?password=admin';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_URL, $service_url);
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occurred during curl exec. Additional info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occurred: ' . $decoded->response->errormessage);
}
echo 'LOGIN: response ok!';
//echo print_r($curl_response);
//echo '====================';
//echo print_r($decoded);
//get session out of response to use in every call
$session_id = $decoded->session;
//echo print_r($session_id);
return $session_id;
}
function readInCSV($csvFile, $session_id)
{
if(!file_exists($csvFile) || !is_readable($csvFile))
return FALSE;
$file_handle = fopen($csvFile, 'r');
$delimiter = ',';
$enclosure = '"';
$escape = '\\';
while ($line = fgets($file_handle)) {
$line_as_arr = str_getcsv( $line , $delimiter , $enclosure, $escape);
echo (count($line_as_arr));
if(count($line_as_arr) == 13)
{
//ignore lines set to delete/suppress
if ($line_as_arr[12] != 'y')
{
//echo($line_as_arr[12]);
createSubject($line_as_arr, $session_id);
}
}
else{
echo("Error processing line =".$line);
}
}
fclose($file_handle);
}
function createSubject($line_as_arr, $session_id)
{
echo 'create subject';
//example from CRC database
//0 1 2 3 4 5 6 7 8 9 10 11 12
//id,"term","use_for","source","other","ext_id","notes","created_for","created_by","created_on","last_edited","last_edited_by","suppress"
$term = new Term();
$term->term = $line_as_arr[1];
$term->term_type = "genre_form";
$term->vocabulary = "/vocabularies/1";
$data = new Data();
$data->title = $line_as_arr[1];
$data->vocabulary = "/vocabularies/1";
$data->external_ids =array();
$data->source = $line_as_arr[3];
$data->authority_id = "gen_".$line_as_arr[0];
//add notes
$notes = "";
if (!empty($line_as_arr[5])){
$notes = $notes . "External Id = " . $line_as_arr[5] . ",";
}
if (!empty($line_as_arr[6])) {
$notes = $notes . "Notes = " . $line_as_arr[6]. ",";
}
if (!empty($line_as_arr[7])) {
$notes = $notes . "Created For = " . $line_as_arr[7]. ",";
}
if (!empty($line_as_arr[2])) {
$notes = $notes . "Use For = " . $line_as_arr[2]. ",";
}
if (strlen($notes) > 0){
$data->scope_note = trim($notes, ",");
}
$data->terms = array($term);
echo json_encode($data);
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
'X-ArchivesSpace-Session: '.$session_id
);
$service_url = 'http://localhost:8089/subjects';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_URL, $service_url);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occurred during curl exec. Additional info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occurred: ' . $decoded->response->errormessage);
}
echo 'response ok!';
echo print_r($curl_response);
//echo '====================';
//echo print_r($decoded);
}