-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi_bhl_name.php
158 lines (119 loc) · 3.1 KB
/
api_bhl_name.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
<?php
// BHL API to get distribution of names in BHL
error_reporting(E_ALL);
require_once (dirname(__FILE__) . '/lib.php');
require_once (dirname(__FILE__) . '/api_utils.php');
//----------------------------------------------------------------------------------------
function default_display()
{
echo "hi";
}
//----------------------------------------------------------------------------------------
// http://www.php.net/manual/en/function.str-getcsv.php#95132
function csv_to_array($csv, $delimiter = ',', $enclosure = '"', $escape = '\\', $terminator = "\n") {
$r = array();
$rows = explode($terminator,trim($csv));
$names = array_shift($rows);
$names = str_getcsv($names,$delimiter,$enclosure,$escape);
$nc = count($names);
foreach ($rows as $row) {
if (trim($row)) {
$values = str_getcsv($row,$delimiter,$enclosure,$escape);
if (!$values) $values = array_fill(0,$nc,null);
$r[] = array_combine($names,$values);
}
}
return $r;
}
//----------------------------------------------------------------------------------------
function display_bhl_name($name, $callback = '')
{
$obj = new stdclass;
$obj->hits = array();
$obj->query = $name;
// Add status
$obj->status = 404;
$url = 'https://www.biodiversitylibrary.org/Services/NameListDownloadService.ashx?type=c&name=' . urlencode($name) . '&lang=';
$csv = get($url);
$r = csv_to_array($csv);
$hits = array();
foreach ($r as $row)
{
if (is_array($row))
{
if (isset($row['Url']))
{
$PageID = preg_replace('/http[s]?:\/\/www.biodiversitylibrary.org\/page\//', '', $row['Url']);
//$id = $row['Title'] . $row['Volume'];
$id = $PageID;
if (!isset($hits[$id]))
{
$hit = new stdclass;
$hit->PageID = $PageID;
$hit->identifiers = array();
$hit->title = $row['Title'];
$info = new stdclass;
// to do:
//parse_bhl_date($row['Volume'], $info);
if (isset($info->start))
{
$hit->year = $info->start;
}
else
{
$hit->year = $row['Date'];
}
$identifier = new stdclass;
$identifier->type = 'bhl';
$identifier->id = $PageID;
$hit->identifiers[] = $identifier;
$hits[$id] = $hit;
}
}
}
}
// sort
$keys = array();
$years = array();
foreach ($hits as $k => $hit)
{
$keys[] = $k;
$years[] = $hit->year;
}
array_multisort($years, SORT_NUMERIC, $keys);
$obj->hits = array();
foreach ($keys as $k) {
$obj->hits[$k] = $hits[$k];
}
if (count($obj->hits) > 0)
{
$obj->status = 200;
}
api_output($obj, $callback);
}
//--------------------------------------------------------------------------------------------------
function main()
{
$callback = '';
$handled = false;
// If no query parameters
if (count($_GET) == 0)
{
default_display();
exit(0);
}
if (isset($_GET['callback']))
{
$callback = $_GET['callback'];
}
if (!$handled)
{
if (isset($_GET['name']))
{
display_bhl_name($_GET['name'], $callback);
$handled = true;
}
}
}
main();
?>