-
Notifications
You must be signed in to change notification settings - Fork 5
/
reference_redirector.functions.php
279 lines (246 loc) · 6.21 KB
/
reference_redirector.functions.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
define('DEFAULT_URL', 'http://mek.oszk.hu/');
define('CSV_DIR', 'reference_redirector_csv');
define('LN', "\n");
/**
* Resolve URLs based on 'Káldos-convention'
*/
function resolve_url($fullpath) {
$parts = explode('/', $fullpath);
switch(strtolower($parts[0])) {
case 'rmny':
$url = get_rmny_url($parts);
break;
case 'rmk':
$url = get_rmk_url($parts);
break;
case 'ms':
$url = get_ms_url($parts);
break;
default:
$url = DEFAULT_URL;
}
return $url;
}
/**
* Get the URL of an RMNy page
*/
function get_rmny_url($parts) {
if (count($parts) < 2) {
return DEFAULT_URL;
}
$book_id = $parts[1];
$volume_id = '';
$page_id = '';
if (count($parts) == 4) {
$volume_id = $parts[2];
$page_id = $parts[3];
}
else if (count($parts) == 3) {
$page_id = $parts[2];
}
while (preg_match('/^\d{1,3}\D?$/', $book_id)) {
$book_id = '0' . $book_id;
}
$mek_dir = rmny2mek($book_id);
return get_page_url($mek_dir, $volume_id, $page_id);
}
/**
* Get the URL of an RMK page
*/
function get_rmk_url($parts) {
if (count($parts) < 3) {
return DEFAULT_URL;
}
$rmk_volume = $parts[1];
$book_id = $parts[2];
$volume_id = '';
$page_id = '';
if (count($parts) == 5) {
$volume_id = $parts[3];
$page_id = $parts[4];
}
else if (count($parts) == 4) {
$rmk_volume = 1;
$page_id = $parts[3];
}
if (strtoupper($rmk_volume) == 'I') {
$rmk_volume = 1;
}
else if (strtoupper($rmk_volume) == 'II') {
$rmk_volume = 2;
}
else if (strtoupper($rmk_volume) == 'III') {
$rmk_volume = 3;
}
while (preg_match('/^\d{1,3}\D?$/', $book_id)) {
$book_id = '0' . $book_id;
}
$mek_dir = rmk2mek($rmk_volume . '/' . $book_id);
return get_page_url($mek_dir, $volume_id, $page_id);
}
/**
* Get the URL of an RMK page
*/
function get_ms_url($parts) {
if (count($parts) < 4) {
return DEFAULT_URL;
}
$country = $parts[1];
$library = $parts[2];
$collection = '';
$book_id = '';
$volume_id = '';
$page_id = '';
if (count($parts) == 7) {
$collection = $parts[3];
$book_id = $parts[4];
$volume_id = $parts[5];
$page_id = $parts[6];
}
else if (count($parts) == 6) {
$collection = $parts[3];
$book_id = $parts[4];
$page_id = $parts[5];
}
else if (count($parts) == 5) {
$collection = $parts[3];
$book_id = $parts[4];
}
if (strtoupper($volume_id) == 'I') {
$volume_id = 1;
}
else if (strtoupper($volume_id) == 'II') {
$volume_id = 2;
}
else if (strtoupper($volume_id) == 'III') {
$volume_id = 3;
}
while (preg_match('/^\d{1,3}\D?$/', $book_id)) {
$book_id = '0' . $book_id;
}
$mek_dir = ms2mek(sprintf("%s/%s/%s/%s",
strtoupper($country), strtoupper($library), strtoupper($collection), $book_id));
return get_page_url($mek_dir, $volume_id, $page_id);
}
/**
* Get MEK ID by RMNy ID
*/
function rmny2mek($rmny_id) {
$record = get_record_by_key(CSV_DIR . '/rmny.csv', $rmny_id);
return $record[1];
}
/**
* Get MEK ID by RMK ID
*/
function rmk2mek($rmk_id) {
$record = get_record_by_key(CSV_DIR . '/rmk.csv', $rmk_id);
return $record[1];
}
/**
* Get MEK ID by MS ID
*/
function ms2mek($rmk_id) {
$record = get_record_by_key(CSV_DIR . '/ms.csv', $rmk_id);
return $record[1];
}
/**
* Returns a record in CSV file based on a key.
*
* @param $csv_file (String)
* Relative path of a CSV file
* @param $identifier (String)
* The key of the record we are looking for
*
* @return (Array or FALSE)
* The record as (non associative) array. If it doesn't find it, returns FALSE
*/
function get_record_by_key($csv_file, $identifier) {
$result = FALSE;
$handle = fopen($csv_file, "r");
$alternativeIdentifier = '';
if (preg_match('/A$/', $identifier)) {
$alternativeIdentifier = preg_replace('/A$/', '', $identifier);
}
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$id = $data[0];
if ($id == $identifier || ($alternativeIdentifier != '' && $id == $alternativeIdentifier)) {
$result = $data;
break;
}
}
fclose($handle);
return $result;
}
/**
* Transform the volume number and page number into a canonical form, where the page
* number is a four number and A or B letter. E.g. 1, 3 becomes 1/0003A.
*
* @param $volume_id (String)
* A volume number of an old book
* @param $page_id (String)
* A page number of identifier of an old book
*
* @return (String or FALSE)
* The canonical form of the page number. It is used as key in CSV. If the page
* number doesn't fit into a pattern, it returns FALSE.
*/
function get_csv_identifier($volume_id, $page_id) {
$identifier = FALSE;
if (preg_match('/^(\d+|\d+[A-Zacfhklmnopq][\dA])([rRvVaAbBC]?)$/', $page_id, $matches)) {
$num = $matches[1];
$alpha = $matches[2];
while (strlen($num) < 4) {
$num = '0' . $num;
}
if (empty($alpha) && $num != '0000') {
$alpha = 'A';
}
else {
$alpha = strtoupper($alpha);
if ($alpha == 'R') {
$alpha = 'A';
}
else if ($alpha == 'V') {
$alpha = 'B';
}
}
$identifier = (isset($volume_id) && !empty($volume_id) ? $volume_id . '/' : '') . $num . $alpha;
}
return $identifier;
}
/**
* Finds out the URL of a page in an old book. It uses a CSV file, which contains only
* the referred pages, not all pages.
*
* @param $mek_dir (String)
* A relative MEK directory path, like 08800/08838
* @param $volume_id (String)
* A volume number of an old book
* @param $page_id (String)
* A page number of identifier of an old book
*
* @return (String)
* The URL of that page inside MEK or of the MEK home page
*/
function get_page_url($mek_dir, $volume_id, $page_id) {
if ($mek_dir === FALSE) {
return DEFAULT_URL;
}
$mek_id = substr($mek_dir, -5);
$csv_file = CSV_DIR . '/' . $mek_id . '.csv';
if (!file_exists($csv_file)) {
return DEFAULT_URL . $mek_dir;
}
$identifier = get_csv_identifier($volume_id, $page_id);
if ($identifier === FALSE) {
return DEFAULT_URL . $mek_dir;
}
$mek_page = get_record_by_key($csv_file, $identifier);
if (!empty($mek_page)) {
return DEFAULT_URL . $mek_dir . '/html/' . $mek_page[1] . '.html';
}
else {
return DEFAULT_URL . $mek_dir;
}
}