-
Notifications
You must be signed in to change notification settings - Fork 61
/
oai.php
382 lines (338 loc) · 17.2 KB
/
oai.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
require_once('oaiserver.php');
require_once('../config.php');
if (!file_exists("oai_config.php"))
{
die("oai-pmh is not available");
}
require_once('oai_config.php');
require_once('xerteobjects.php');
//require $xerte_toolkits_site->php_library_path . "template_library.php";
/**
* Identifier settings. It needs to have proper values to reflect the settings of the data provider.
* Is MUST be declared in this order
*
* - $identifyResponse['repositoryName'] : compulsory. A human readable name for the repository;
* - $identifyResponse['baseURL'] : compulsory. The base URL of the repository;
* - $identifyResponse['protocolVersion'] : compulsory. The version of the OAI-PMH supported by the repository;
* - $identifyResponse['earliestDatestamp'] : compulsory. A UTCdatetime that is the guaranteed lower limit of all datestamps recording changes, modifications, or deletions in the repository. A repository must not use datestamps lower than the one specified by the content of the earliestDatestamp element. earliestDatestamp must be expressed at the finest granularity supported by the repository.
* - $identifyResponse['deletedRecord'] : the manner in which the repository supports the notion of deleted records. Legitimate values are no ; transient ; persistent with meanings defined in the section on deletion.
* - $identifyResponse['granularity'] : the finest harvesting granularity supported by the repository. The legitimate values are YYYY-MM-DD and YYYY-MM-DDThh:mm:ssZ with meanings as defined in ISO8601.
*
*/
// based on original work from the PHP Laravel framework
if (!function_exists('str_contains')) {
function str_contains($haystack, $needle) {
return $needle !== '' && mb_strpos($haystack, $needle) !== false;
}
}
$identifyResponse = array();
$identifyResponse["repositoryName"] = $xerte_toolkits_site->name ;
$identifyResponse["baseURL"] = $xerte_toolkits_site->site_url . 'oai-pmh/oai.php'; //'http://198.199.108.242/~neis/oai_pmh/oai.php';
$identifyResponse["protocolVersion"] = '2.0';
$identifyResponse['adminEmail'] = $config['adminEmail']; //'danielneis@gmail.com';
$identifyResponse["earliestDatestamp"] = call_user_func('getEarliestDatestamp') . "T00:00:00Z";//'2013-01-01T12:00:00Z';
$identifyResponse["deletedRecord"] = 'persistent'; // How your repository handles deletions
// no: The repository does not maintain status about deletions.
// It MUST NOT reveal a deleted status.
// persistent: The repository persistently keeps track about deletions
// with no time limit. It MUST consistently reveal the status
// of a deleted record over time.
// transient: The repository does not guarantee that a list of deletions is
// maintained. It MAY reveal a deleted status for records.
$identifyResponse["granularity"] = 'YYYY-MM-DDThh:mm:ssZ';
/* unit tests ;) */
if (!isset($args)) {
$args = $_GET;
}
if (!isset($uri)) {
$uri = $xerte_toolkits_site->site_url . 'oai-pmh/oai.php';
}
$oai2 = new OAIServer($uri, $args, $identifyResponse,
array(
'ListMetadataFormats' =>
function($identifier = '') {
if (!empty($identifier) && $identifier != 'a.b.c') {
throw new OAIException('idDoesNotExist');
}
return
array(
'lom_ims' => array('metaDataPrefix'=>'lom_ims',
'schema'=>'http://www.imsglobal.org/xsd/imsmd_v1p2p4.xsd',
'metadataNamespace'=> 'http://www.imsglobal.org/xsd/imsmd_v1p2',
),
'oai_dc' => array('metadataPrefix'=>'oai_dc',
'schema'=>'http://www.openarchives.org/OAI/2.0/oai_dc.xsd',
'metadataNamespace'=>'http://www.openarchives.org/OAI/2.0/oai_dc/',
'record_prefix'=>'dc',
'record_namespace' => 'http://purl.org/dc/elements/1.1/'));
},
'ListSets' =>
function($resumptionToken = '') {
return
array (
array('setSpec'=>'class:collection', 'setName'=>'Collections'),
array('setSpec'=>'math', 'setName'=>'Mathematics') ,
array('setSpec'=>'phys', 'setName'=>'Physics'),
array('setSpec'=>'phdthesis', 'setName'=>'PHD Thesis',
'setDescription'=>
'<oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" '.
' xmlns:dc="http://purl.org/dc/elements/1.1/" '.
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '.
' xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ '.
' http://www.openarchives.org/OAI/2.0/oai_dc.xsd"> '.
' <dc:description>This set contains metadata describing '.
' electronic music recordings made during the 1950ies</dc:description> '.
' </oai_dc:dc>'));
},
'ListRecords' =>
function($metadataPrefix, $from, $until, $set = '', $count = false, $deliveredRecords = 0, $maxItems = 0){
if ($count) {
return 1;
}
if ($set != '') {
throw new OAIException('noSetHierarchy');
}
$records = call_user_func('getTemplates', $metadataPrefix,$from, $until);
return $records;
},
'GetRecord' =>
function($identifier, $metadataPrefix){
//TODO: Test whether identifier is in the right format and parse identifier to template_id
global $xerte_toolkits_site;
if(str_contains($identifier, $xerte_toolkits_site->site_url)) {
$parsed_identifier = explode($xerte_toolkits_site->site_url, $identifier)[1];
}
else{
throw new OAIException('idDoesNotExist');
}
$response_record = call_user_func(getSingleTemplate,$metadataPrefix, $parsed_identifier);
return $response_record;
},
)
);
$response = $oai2->response();
if (isset($return)) {
return $response;
} else {
$response->formatOutput = true;
$response->preserveWhiteSpace = false;
header('Content-Type: text/xml');
echo $response->saveXML();
}
function getEarliestDatestamp() {
global $xerte_toolkits_site;
$q = "select template_id,date_created from templatedetails ORDER BY date_created limit 1";
$result = db_query($q);
return $result[0]["date_created"];
}
function getSingleTemplate($metadataPrefix,$template_id) {
global $xerte_toolkits_site;
$prefix = $xerte_toolkits_site->database_table_prefix;
$q = "select td.template_id,
otd.template_framework,
otd.template_name as template_type,
otd.display_name as type_display_name,
td.template_name,
td.creator_id as owner_userid,
ld.username as owner_username,
concat(ld.firstname,' ',ld.surname) as owner,
td.date_created,
td.date_modified,
td.date_accessed,
td.number_of_uses,
td.access_to_whom,
td.extra_flags,
td.tsugi_published as lti_enabled,
td.tsugi_xapi_enabled as xapi_enabled
from {$prefix}templatedetails as td,
{$prefix}originaltemplatesdetails as otd,
{$prefix}logindetails as ld
where td.template_type_id=otd.template_type_id and td.creator_id=ld.login_id and td.access_to_whom = 'Public' and td.template_id = {$template_id}";
$response_template = db_query($q);
if (!$response_template) {
throw new OAIException('idDoesNotExist');
}
$tempMetaData = call_user_func(get_meta_data,$response_template[0]['template_id'],$response_template[0]["owner_username"],$response_template[0]["template_type"],$response_template[0]['owner']);
$response_record = call_user_func(makeRecordFromTemplate,$metadataPrefix,$response_template[0], $tempMetaData);
return $response_record;
}
function getTemplates($metadataPrefix,$from,$until) {
global $xerte_toolkits_site;
$prefix = $xerte_toolkits_site->database_table_prefix;
$q = "select td.template_id,
otd.template_framework,
otd.template_name as template_type,
otd.display_name as type_display_name,
td.template_name,
td.creator_id as owner_userid,
ld.username as owner_username,
concat(ld.firstname,' ',ld.surname) as owner,
td.date_created,
td.date_modified,
td.date_accessed,
td.number_of_uses,
td.access_to_whom,
td.extra_flags,
td.tsugi_published as lti_enabled,
td.tsugi_xapi_enabled as xapi_enabled
from {$prefix}templatedetails as td,
{$prefix}originaltemplatesdetails as otd,
{$prefix}logindetails as ld
where td.template_type_id=otd.template_type_id and td.creator_id=ld.login_id and td.access_to_whom = 'Public'";
if($from != "" && $until != ""){
$q = $q . "and td.date_modified between ? and ?";
$templates = db_query($q, array($from,$until));
}
else if($until != ""){
$q = $q . "and td.date_modified <= ?";
$templates = db_query($q, array($until));
}
else if($from != ""){
$q = $q . "and td.date_modified >= ?";
$templates = db_query($q, array($from));
}
else{
$templates = db_query($q);
}
//$response = new stdClass();
//$response->site_url = $xerte_toolkits_site->site_url;
//$response->site_name = $xerte_toolkits_site->site_name;
//To make sure the response templates are the same as the filled in metadata number
//$tmpTemplates = array();
$tmpRecords = array();
//get the oai status for all templates that at some point have been published
if($until != ""){
$q = "select template_id, status, timestamp from {$xerte_toolkits_site->database_table_prefix}oai_publish where audith_id IN (SELECT max(audith_id) from {$xerte_toolkits_site->database_table_prefix}oai_publish where timestamp < ? group by template_id)";
$publish_status = db_query($q, array($until));
}
else {
$q = "select template_id, status, timestamp from {$xerte_toolkits_site->database_table_prefix}oai_publish where audith_id IN (SELECT max(audith_id) from {$xerte_toolkits_site->database_table_prefix}oai_publish group by template_id)";
$publish_status = db_query($q);
}
$temp_added = array();
for($i=0;$i<count($templates);$i++)
{
$currentTemplate = $templates[$i];
$needle_key = array_search($currentTemplate['template_id'], array_column($publish_status, 'template_id'));
if ($needle_key !== false){
if (strcmp($publish_status[$needle_key]['status'], "published") == 0) {
$tempMetaData = call_user_func('get_meta_data',$currentTemplate['template_id'],$currentTemplate["owner_username"],$currentTemplate["template_type"], $currentTemplate['owner']);
if($tempMetaData->domain != 'unknown' and $tempMetaData->level != "unknown" and $tempMetaData->oaiPmhAgree){
$currentRecord = call_user_func('makeRecordFromTemplate',$metadataPrefix,$currentTemplate, $tempMetaData);
$tmpRecords[] = $currentRecord;
$temp_added[] = $needle_key;
}
}
}
}
//add all templates that have their sharing rights revoked
for($i=0;$i<count($templates);$i++) {
$currentTemplate = $templates[$i];
$needle_key = array_search($currentTemplate['template_id'], array_column($publish_status, 'template_id'));
if ($needle_key !== false){
if (!in_array($needle_key, $temp_added)) {
$record = array('identifier' => ($xerte_toolkits_site->site_url . $currentTemplate['template_id']),
'datestamp' => date($publish_status[$needle_key]['timestamp']),
'modified' => date($currentTemplate['date_modified']),
'deleted' => true);
$tmpRecords[] = $record;
}
}
}
//$response->templates = $tmpTemplates;
//$response->count = count($tmpTemplates);
return $tmpRecords;
};
function makeRecordFromTemplate($metadataPrefix,$template, $metadata){
global $xerte_toolkits_site;
if($metadataPrefix == "lom_ims") {
//get first publish time.
$q = "select timestamp from {$xerte_toolkits_site->database_table_prefix}oai_publish where template_id = ? and status = ? group by timestamp limit 1";
$params = array($template['template_id'], "published");
$first_publish_time = db_query_one($q, $params);
$record = array('identifier' => ($xerte_toolkits_site->site_url . $template['template_id']),
'datestamp' => date($first_publish_time["timestamp"]),
'modified' => date($template['date_modified']),
//'set' => 'class:activity',
'metadata' => array(
'container_name' => 'lom',
'container_attributes' => array(
'xmlns' => "http://www.imsglobal.org/xsd/imsmd_v1p2",
'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
'xsi:schemaLocation' =>
'http://www.imsglobal.org/xsd/imsmd_v1p2 http://www.imsglobal.org/xsd/imsmd_v1p2p4.xsd'
),
'general' => array(
//'title' => $template['template_name'],//'Testing records',
'title' => $metadata->name,//'Testing records',
'language' => explode("-", $metadata->language)[0],
'description' => $metadata->description,
),
'misc' => array(
'course' => $metadata->course,
'educational_code' => $metadata->educode,
'location' => ($xerte_toolkits_site->site_url . 'play.php?template_id=' . $template['template_id']),
),
'keywords' => explode("\n", $metadata->keywords),
'relation' => array(
'thumbnail' => $metadata->thumbnail,
'download' => $metadata->download
),
'lifecycle' => array(
'author' => $metadata->author,
'publisher' => $metadata->publisher,
'publishdate' => $first_publish_time["timestamp"],
),
'rights' => array(
'rights' => $metadata->rights,
'rightsId' => $metadata->rightsId,
'download' => $metadata->download
),
'classification' => array(
'domain_id' => $metadata->domainId,
'domain' => $metadata->domain,
'domain_source' => $metadata->domainSource,
'level' => $metadata->level,
'levelId' => $metadata->levelId,
),
));
if ($record['metadata']['relation']['download'])
{
$record['metadata']['relation']['download_url'] = $metadata->downloadUrl;
}
}
else if($metadataPrefix == "oai_dc"){
$record = array('identifier' => ($xerte_toolkits_site->site_url . $template['template_id']),
'datestamp' => date($template['date_modified']),
//'set' => 'class:activity',
'metadata' => array(
'container_name' => 'oai_dc:dc',
'container_attributes' => array(
'xmlns:oai_dc' => "http://www.openarchives.org/OAI/2.0/oai_dc/",
'xmlns:dc' => "http://purl.org/dc/elements/1.1/",
'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
'xsi:schemaLocation' =>
'http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd'
),
'fields' => array(
'dc:title' => $metadata->name,
'dc:creator' => $metadata->author,
'dc:subject' => $metadata->course,
'dc:description' =>$metadata->description,
'dc:publisher' => $metadata->publisher,
//'dc:contributor' => '',
'dc:date' => $template['date_modified'],
//'dc:type' => '',
//'dc:format' => '',
'dc:identifier' => ($xerte_toolkits_site->site_url . $template['template_id']),
'dc:source' => ($xerte_toolkits_site->site_url . 'play.php?template_id=' . $template['template_id']),
'dc:language' => explode("-", $metadata->language)[0],
//'dc:relation' => '',
//'dc:coverage' => '',
'dc:rights' => $metadata->rights,
)
));
}
return $record;
};