This repository was archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabel.php
More file actions
257 lines (236 loc) · 9.99 KB
/
label.php
File metadata and controls
257 lines (236 loc) · 9.99 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
/**
* Displays form for submitting list of barcodes
*
* @author Jared Howland <sirsi@jaredhowland.com>
* @version 2014-04-15
* @since 2014-02-22
*
*/
require_once 'config.php';
// Get all barcodes entered into form and put them into an array
$item_ids = explode("\n",
$_REQUEST['barcodes']); // Access is limited to BYU's IP range so I am carelessly not cleaning the input before using it
// Get data for each barcode using SIRSI API calls
$data = get_data($item_ids);
// Format the data to only include necessary information for our application
$clean_data = format_data($data);
// Give output to template for display to user
$html = array('title' => 'Home', 'html' => $clean_data);
template::display('report.tmpl', $html);
/***************************
* FUNCTIONS *
***************************/
/**
* Constructs API call(s) and returns data for all barcodes
*
* @param array Array with all barcodes entered into form
*
* @return array Array with just relevant data from API calls included
**/
function get_data($item_ids)
{
// Loop through all barcodes
// Sirsi technically allows aggregating multiple calls but does not return all necessary information for each returned result for this to be useful for us
foreach ($item_ids AS $item_id) {
$item_id = trim($item_id);
// Create search parameters for Sirsi API
$parameters = 'marcEntryFilter=ALL&includeItemInfo=true&includeCallNumberSummary=true&json=true&itemID=' . $item_id;
// Construct URL for Sirsi API call
$url = config::API_BASE_URL . config::API_SEARCH_URL . $parameters . '&clientID=' . config::CLIENT_ID;
// Get the results of the API call
$data = get_json($url);
if ($data == '') {
echo '<p>There was a problem connecting to the server. Please try again later.</p>';
die();
}
// Sirsi spits out bad JSON if there is an error
// Correct the invalid JSON if there is an error
if (config::BAD_FAULT_JSON) {
$data = str_replace('faultResponse', '"faultResponse"', $data);
}
// Convert the object to a PHP array
$data = json_decode($data, true);
// Store all API call results in one array
$all_data[] = array('item_id' => $item_id, 'data' => $data);
}
return $all_data;
}
/**
* Extracts relevant information from results of all API calls
*
* @param array Array with data from all API calls
*
* @return array Array with just relevant data from API calls included
**/
function format_data($data)
{
foreach ($data AS $record) {
$item_id = $record['item_id'];
$title_info = $record['data']['TitleInfo'][0];
// If no result found
if (empty($title_info)) {
$all_records[] = array('error' => 'No item found for ' . $item_id . '.');
// If result is found
} else {
$title_id = $title_info['titleID'];
// Current and home locations and call number
foreach ($title_info['CallInfo'] AS $CallInfo) {
$record_call_number = $CallInfo['callNumber'];
foreach ($CallInfo['ItemInfo'] AS $ItemInfo) {
$record_item_id = $ItemInfo['itemID'];
if ($record_item_id == $item_id) {
$call_number = $record_call_number;
$current_location = $ItemInfo['currentLocationID'];
$home_location = $ItemInfo['homeLocationID'];
}
// Indicate whether the item is a SERIAL because base call number
// does not include subfield z (can remove once SIRSI updates APIs
// to match documentation)
if ($ItemInfo['itemTypeID'] == 'SERIAL') {
$call_number = $record_call_number . '<br/><span class="warning">(SERIAL)</span>';
} // Non-BOOK items have a different location for call number data
elseif ($ItemInfo['itemTypeID'] != 'BOOK') {
$call_number = $record_call_number;
}
}
}
// MARC record data
$marc = $title_info['BibliographicInfo']['MarcEntryInfo'];
foreach ($marc AS $field) {
$label = $field['label'];
$entryID = $field['entryID'];
$indicators = $field['indicators'];
$text = $field['text'];
$unformattedText = $field['unformattedText'];
if ($entryID == '001') {
$control_number = $text;
}
if ($entryID == '020') {
$isbn = $text;
}
if ($entryID == '100') {
$author = $text;
}
if ($entryID == '245') {
$title = $text;
}
// Vernacular title
if ($entryID == '880' && strpos($unformattedText, '|6245-01|') !== false) {
$linked_title = $text;
}
if ($entryID == '260') {
$publisher = $text;
$pub_array = explode('|c', $unformattedText);
$pub_date = trim($pub_array[1], '-. ');
}
}
$all_records[] = array(
'item_id' => $item_id,
'title_id' => $title_id,
'current_location' => $current_location,
'home_location' => $home_location,
'call_number' => $call_number,
'control_number' => $control_number,
'isbn' => $isbn,
'author' => $author,
'title' => $title,
'linked_title' => $linked_title,
'pub_date' => $pub_date,
'publisher' => $publisher
);
// Reset all variables for next item being searched
$item_id = null;
$title_id = null;
$current_location = null;
$home_location = null;
$call_number = null;
$control_number = null;
$isbn = null;
$author = null;
$title = null;
$linked_title = null;
$pub_date = null;
$publisher = null;
}
}
return $all_records;
}
/**
* Uses CURL to grab the JSON from SIRSI
* From http://25labs.com/alternative-for-file_get_contents-using-curl/
*
* @param string URL of API call
*
* @return string Data returned by API call
**/
function get_json($url)
{
$curl = curl_init();
// $userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36';
$userAgent = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)';
curl_setopt($curl, CURLOPT_URL,
$url); //The URL to fetch. This can also be set when initializing a session with curl_init().
curl_setopt($curl, CURLOPT_RETURNTRANSFER,
true); //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 150); //The number of seconds to wait while trying to connect.
curl_setopt($curl, CURLOPT_USERAGENT,
$userAgent); //The contents of the "User-Agent: " header to be used in a HTTP request.
curl_setopt($curl, CURLOPT_FAILONERROR,
true); //To fail silently if the HTTP code returned is greater than or equal to 400.
curl_setopt($curl, CURLOPT_FOLLOWLOCATION,
true); //To follow any "Location: " header that the server sends as part of the HTTP header.
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //Do not verify peer’s certificate
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true); //Force new connection rather than using cache
curl_setopt($curl, CURLOPT_AUTOREFERER,
true); //To automatically set the Referer: field in requests where it follows a Location: redirect.
curl_setopt($curl, CURLOPT_TIMEOUT, 10); //The maximum number of seconds to allow cURL functions to execute.
// If not production, give verbose feedback on curl problems
if (config::DEVELOPMENT) {
curl_setopt($curl, CURLOPT_VERBOSE, true);
$verbose = fopen('php://temp', 'rw+');
curl_setopt($curl, CURLOPT_STDERR, $verbose);
}
// Execute query
$contents = curl_exec($curl);
// Report errors if they occur
curl_error_report($curl, $verbose);
curl_close($curl);
return $contents;
}
/**
* Sends CURL error message to user when needed (and only when in development)
* From http://25labs.com/alternative-for-file_get_contents-using-curl/
*
* @param mixed
* string If there is a result when making CURL call, will return data
* boolean If there is no result when making CURL call, will return FALSE
*
* @return null If error exists, prints error directly to user and kills everything
* Otherwise, it does not return anything
**/
function curl_error_report($curl, $verbose = null)
{
if (curl_errno($curl)) {
if (config::DEVELOPMENT) {
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";
$curlVersion = curl_version();
extract(curl_getinfo($curl));
$metrics = <<<EOD
URL: $url
Code: $http_code ($redirect_count redirect(s) in $redirect_time secs)
Content: $content_type Size: $download_content_length (Own: $size_download) Filetime: $filetime
Time: $total_time Start @ $starttransfer_time (DNS: $namelookup_time Connect: $connect_time Request: $pretransfer_time)
Speed: Down: $speed_download (avg.) Up: $speed_upload (avg.)
Curl: v{$curlVersion['version']}
EOD;
echo '<pre>' . $metrics . '</pre>';
}
echo 'cURL Error: ' . curl_errno($curl) . ': ' . curl_error($curl) . '<br/>';
curl_close($curl);
die();
}
return;
}