-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlistrecords.php
182 lines (160 loc) · 5.28 KB
/
listrecords.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
<?php
/**
* \file
* \brief Response to Verb ListRecords
*
* Lists records according to conditions. If there are too many, a resumptionToken is generated.
* - If a request comes with a resumptionToken and is still valid, read it and send back records.
* - Otherwise, set up a query with conditions such as: 'metadataPrefix', 'from', 'until', 'set'.
* Only 'metadataPrefix' is compulsory. All conditions are accessible through global array variable <B>$args</B> by keywords.
*/
debug_message("\nI am debuging". __FILE__) ;
// Resume previous session?
if (isset($args['resumptionToken'])) {
if (!file_exists(TOKEN_PREFIX.$args['resumptionToken'])) {
$errors[] = oai_error('badResumptionToken', '', $args['resumptionToken']);
} else {
$readings = readResumToken(TOKEN_PREFIX.$args['resumptionToken']);
if ($readings == false) {
$errors[] = oai_error('badResumptionToken', '', $args['resumptionToken']);
} else {
debug_var_dump('readings',$readings);
list($deliveredrecords, $extquery, $metadataPrefix) = $readings;
}
}
} else { // no, we start a new session
$deliveredrecords = 0;
$extquery = '';
$metadataPrefix = $args['metadataPrefix'];
if (isset($args['from'])) {
$from = checkDateFormat($args['from']);
$extquery .= fromQuery($from);
}
if (isset($args['until'])) {
$until = checkDateFormat($args['until']);
$extquery .= untilQuery($until);
}
if (isset($args['set'])) {
if (is_array($SETS)) {
$extquery .= setQuery($args['set']);
} else {
$errors[] = oai_error('noSetHierarchy');
}
}
}
if (!empty($errors)) {
oai_exit();
}
// Load the handler
if (is_array($METADATAFORMATS[$metadataPrefix])
&& isset($METADATAFORMATS[$metadataPrefix]['myhandler'])) {
$inc_record = $METADATAFORMATS[$metadataPrefix]['myhandler'];
include($inc_record);
} else {
$errors[] = oai_error('cannotDisseminateFormat', 'metadataPrefix', $metadataPrefix);
}
if (!empty($errors)) {
oai_exit();
}
if (empty($errors)) {
$query = selectallQuery($metadataPrefix) . $extquery;
debug_message("Query: $query") ;
$res = $db->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$r = $res->execute();
if ($r===false) {
if (SHOW_QUERY_ERROR) {
echo __FILE__.','.__LINE__."<br />";
echo "Query: $query<br />\n";
print_r($db->errorInfo());
exit();
} else {
$errors[] = oai_error('noRecordsMatch');
}
} else {
$r = $res->setFetchMode(PDO::FETCH_ASSOC);
if ($r===false) {
exit("FetchMode is not supported");
}
$num_rows = rowCount($metadataPrefix, $extquery, $db);
if ($num_rows==0) {
echo "Cannot find records: $query\n";
$errors[] = oai_error('noRecordsMatch');
}
}
}
if (!empty($errors)) {
oai_exit();
}
// Will we need a new ResumptionToken?
if($args['verb']=='ListRecords') {
$maxItems = MAXRECORDS;
} elseif($args['verb']=='ListIdentifiers') {
$maxItems = MAXIDS;
} else {
exit("Check ".__FILE__." ".__LINE__.", there is something wrong.");
}
$maxrec = min($num_rows - $deliveredrecords, $maxItems);
if ($num_rows - $deliveredrecords > $maxItems) {
$cursor = (int)$deliveredrecords + $maxItems;
$restoken = createResumToken($cursor, $extquery, $metadataPrefix);
$expirationdatetime = gmstrftime('%Y-%m-%dT%TZ', time()+TOKEN_VALID);
}
// Last delivery, return empty ResumptionToken
elseif (isset($args['resumptionToken'])) {
$restoken = $args['resumptionToken']; // just used as an indicator
unset($expirationdatetime);
}
if (isset($args['resumptionToken'])) {
debug_message("Try to resume because a resumptionToken supplied.") ;
$record = $res->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, $deliveredrecords);
}
// Record counter
$countrec = 0;
// Publish a batch to $maxrec number of records
$outputObj = new ANDS_Response_XML($args);
while ($countrec++ < $maxrec) {
$record = $res->fetch(PDO::FETCH_ASSOC);
if ($record===false) {
if (SHOW_QUERY_ERROR) {
echo __FILE__.",". __LINE__."<br />";
print_r($db->errorInfo());
exit();
}
}
$identifier = $oaiprefix.$record[$SQL['identifier']];
$datestamp = formatDatestamp($record[$SQL['datestamp']]);
$setspec = $record[$SQL['set']];
// debug_var_dump('record', $record);
if (isset($record[$SQL['deleted']]) && ($record[$SQL['deleted']] === true) &&
($deletedRecord == 'transient' || $deletedRecord == 'persistent')) {
$status_deleted = TRUE;
} else {
$status_deleted = FALSE;
}
//debug_var_dump('status_deleted', $status_deleted);
if($args['verb']=='ListRecords') {
$cur_record = $outputObj->create_record();
$cur_header = $outputObj->create_header($identifier, $datestamp,$setspec,$cur_record);
// return the metadata record itself
if (!$status_deleted) {
debug_var_dump('inc_record',$inc_record);
create_metadata($outputObj, $cur_record, $identifier, $setspec, $db);
}
} else { // for ListIdentifiers, only identifiers will be returned.
$cur_header = $outputObj->create_header($identifier, $datestamp,$setspec);
}
if ($status_deleted) {
$cur_header->setAttribute("status","deleted");
}
}
// ResumptionToken
if (isset($restoken)) {
if(isset($expirationdatetime)) {
$outputObj->create_resumpToken($restoken,$expirationdatetime,$num_rows,$cursor);
} else {
$outputObj->create_resumpToken('',null,$num_rows,$deliveredrecords);
}
}
// end ListRecords
if (SHOW_QUERY_ERROR) {echo "Debug listrecord.php reached to the end.\n\n";}
?>