-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemic_cwrc.inc
319 lines (287 loc) · 9.89 KB
/
emic_cwrc.inc
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
<?php
/**
* Form to select page for CWRC editing
* @param array $form
* @param string $pid
* @return string
*/
function cwrc_page_select_form($form, $pid) {
module_load_include('inc', 'islandora_book', 'book_pack_utils');
$page_pids = get_page_pids($pid);
$form = array();
$form['pages'] = array(
'#title' => t('Choose page to annotate'),
'#type' => 'select',
'#options' => $page_pids,
);
return $form;
}
/**
* Persists CWRC datastream to page object
*/
function save_cwrc_data() {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$data = html_entity_decode(stripslashes($_POST['text']), ENT_QUOTES, 'UTF-8');
$cwrc = str_replace('<br>', '<br />', $data);
$cwrc = str_replace('&', '&', $cwrc);
$pid = ($_POST['file_pid']);
save_cwrc_to_fedora($pid, $cwrc);
echo "Success";
}
/**
* Retrieves CWRC datastream from object
* @param string $pid
*/
function get_cwrc_data($pid) {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$item = new Fedora_Item($pid);
global $base_url;
guarantee_cwrc_datastream($pid);
$fedora_url = variable_get('fedora_base_url', 'http://localhost:8080/fedora');
$file_url = "$base_url/fedora/repository/$pid/CWRC";
$file = $item->get_datastream_dissemination("CWRC");
echo $file;
}
/**
* Setup params for CWRC - if CWRC datastream does not exit, make one.
* @global url $base_url
* @global user object $user
* @param string $pid
*/
function setupCWRC($pid) {
global $base_url;
global $user;
module_load_include('inc', 'fedora_repository', 'ContentModel');
if (!user_access('use CWRC')) {
return;
}
$cant_edit = user_access('edit CWRC') ? false : true;
module_load_include('inc', 'islandora_book', 'book_pack_utils');
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$fedora_url = variable_get('fedora_base_url', 'http://localhost:8080/fedora');
guarantee_cwrc_datastream($pid);
$books = get_collection_from_pid($pid);
$book = $books[0];
$item = new Fedora_Item($book);
$title = $item->objectProfile->objLabel;
$pages = get_sorted_pages($book);
$cModels = array();
foreach ($pages as $page) {
$cm_object = ContentModel::loadFromObject($page);
$cModels[] = $cm_object->pid;
}
$position = array_search($pid, $pages);
$results = array();
$results['BASE_PATH'] = $base_url;
$results['uid'] = $user->uid;
$results['fedora_url'] = $fedora_url;
$results['position'] = $position;
$results['pages'] = $pages;
$results['cModels'] = $cModels;
$results['title'] = $title;
$results['authority_url'] = $base_url . '/cwrc/authorities/';
$results['no_edit'] = $cant_edit;
$results['page_count'] = count($pages);
$results['authority_mappings'] = emic_cwrc_get_authority_mappings();
$json = json_encode($results);
echo $json;
}
/**
* Constructs cwrc datastream if none exists
* @param string $pid
*/
function make_cwrc_datastream($pid) {
global $base_url;
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$item = new Fedora_Item($pid);
$ocr_in = $item->get_datastream_dissemination('OCR');
$lines = explode("\n", $ocr_in);
$preface = '<?xml version="1.0"?>
<html>
<head>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:w="http://cwrctc.artsrn.ualberta.ca/#"></rdf:RDF>
</head>
<body>
<tei id="struct_01" _tag="TEI" _display="TEI" _struct="true">
<span _tag="text" _struct="true" id="struct_02" _display="text">';
$postscript = ' </span>
</tei>
</body>
</html>';
$ocr = "";
$count = 100;
if (is_array($lines)) {
foreach ($lines as $line) {
$line = trim(preg_replace('/&£/', '£', $line));
$line = htmlspecialchars($line);
$count++;
// the l tag is not supported in version 2 of cwrcwriter
// $ocr .= "<l xml:id='struct_$count'>$line</l>";
// $ocr .= $line . '<span _editable="true" _struct="true" _tag="lb" id="struct_' . $count++ . '"></span>';
$count++;
$ocr .= $line . trim("<span _editable='true' _struct='true' _tag='lb' id='struct_$count' />");
}
}
if (empty($ocr)) {
$ocr = "Blank Page";
}
$cwrc_stream = $preface . $ocr . $postscript;
save_cwrc_to_fedora($pid, $cwrc_stream);
}
/**
* Persists stream to fedora object
* @param string $pid
* @param xml $cwrc_stream
*/
function save_cwrc_to_fedora($pid, $cwrc_stream) {
global $base_url;
$xml = simplexml_load_string($cwrc_stream);
$xml->registerXPathNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
$entities = $xml->xpath("//rdf:Description");
foreach ($entities as $entity) {
$urn = emic_cwrc_generate_uuid();
$entity->addAttribute('rdf:about', "urn:uuid:$urn", 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
$entity->addChild('w:urn', "urn:uuid:$urn", 'http://cwrctc.artsrn.ualberta.ca/#');
$output = $entity->asXML();
}
$flat_pid = str_replace(':', '_', $pid);
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$cwrc_stream = preg_replace("/&/", '&', $cwrc_stream);
$cwrc_stream = preg_replace("/<>/", '', $cwrc_stream);
$ocr = get_ocr_from_cwrc($cwrc_stream);
$item = new Fedora_Item($pid);
if ($item->datastreams['CWRC']) {
$item->modify_datastream_by_value($xml->asXML(), 'CWRC', 'CWRC', 'text/plain', 'M');
}
else {
$item->add_datastream_from_string($cwrc_stream, 'CWRC', 'CWRC', 'text/plain', 'M');
}
}
/**
* Causes cwrc datatream to be built if not already present in object.
*
* @param string $pid
*/
function guarantee_cwrc_datastream($pid) {
global $base_url;
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$item = new Fedora_Item($pid);
if (!array_key_exists("CWRC", $item->datastreams)) {
make_cwrc_datastream($pid);
}
}
// maps types to collections
function emic_cwrc_get_authorities($dataType, $query = "") {
$mappings = array(
'Tag Place' => array('collection' => 'islandora:9247', 'type' => t('Place')),
'Tag Person' => array('collection' => 'islandora:9239', 'type' => t('Person')),
'Tag Event' => array('collection' => 'islandora:9242', 'type' => t('Event')),
'Tag Organization' => array('collection' => 'islandora:9236', 'type' => t('Organization')),
);
$authorities = cwrc_get_authorities_list($mappings[$dataType], $query);
if ($authorities) {
$json = json_encode($authorities);
echo $json;
}
}
// builds resultset for typeahead
function cwrc_get_authorities_list($setup_data, $query) {
$collection = $setup_data['collection'];
$query_string = 'collection:"' . $collection . '"';
// $records = emic_process_query($query_string);
$terms = explode(' ', $query);
$terms = array_filter($terms);
$solr = variable_get('islandora_solr_search_block_url', 'http://localhost:8080/solr');
$url = "http://$solr/select?indent=on&version=2.2&q=$query_string&fq=&start=0&rows=1000&qt=standard&wt=json&explainOther=&hl.fl=";
$results = json_decode(file_get_contents($url), TRUE);
$docs = $results['response']['docs'];
foreach ($docs as $doc) {
$pid = $doc['PID'];
$contents = array();
foreach ($doc['emic.searchname'] as $sample) {
$identifier = null;
if (!empty($sample) & !$identifier) {
$identifier = $sample;
}
}
//$identifier = implode('', $doc['emic.searchname']);
$contents['identifier'] = $identifier;
$contents['Object'] = "<a href='/fedora/repository/$pid' target='_blank'>$pid</a>";
$contents['Note'] = $doc['dc.description'][0];
$records[$doc['PID']] = $contents;
}
while (count($terms) > 0) {
$term = array_shift($terms) . "*";
$query_string = 'collection:"' . $collection . '" AND emic.searchname:' . strtolower($term);
$new_records = emic_process_query($query_string);
if (is_array($new_records) && is_array($records)) {
$records = array_intersect_key($records, $new_records);
}
}
if (is_array($records)) {
return array_values($records);
}
}
function get_ocr_from_cwrc($cwrc) {
$doc = new DOMDocument();
$doc->strictErrorChecking = FALSE;
@$doc->loadHTML($cwrc);
$xml = @simplexml_import_dom($doc);
$body = $xml->body->asXML();
$body = preg_replace('/<br.>/', "\n", $body);
$ocr = strip_tags($body);
return $ocr;
}
function emic_cwrc_get_authority_mappings() {
$mappings = array(
'Place' => 'islandora:placesAuthority',
'Person' => 'islandora:personsAuthority',
'Event' => 'islandora:eventsAuthority',
'Organization' => 'islandora:orgsAuthority',
);
return $mappings;
}
/**
* Generates uuid
* @return uuid
*/
function emic_cwrc_generate_uuid() {
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
function emic_process_query($query_string) {
module_load_include('inc', 'islandora_solr_search', 'IslandoraSolrQueryProcessor');
$query_processor = new IslandoraSolrQueryProcessor();
$query_processor->buildAndExecuteQuery($query_string);
$raw_results = $query_processor->solrResult;
$rawResponse = $raw_results->getRawResponse();
$responseArray = json_decode($rawResponse, true);
$records = array();
$options = array(
'attributes' => array('target' => '_blank'),
);
foreach ($responseArray['response']['docs'] as $result) {
$title = $result['dc.subjectFacet'][0];
if (isset($result['dc.date'][0])) {
$title .= ' - ' . $result['dc.date'][0];
}
$auth_pid = str_replace('info:fedora/', '', $result['PID']);
if (!$title) {
$title = str_replace('info:fedora/', '', $result['title'][0]);
}
$note = $result['dc.description'][0];
$link = l($auth_pid, "fedora/repository/$auth_pid", $options);
$record = array('identifier' => $title, 'Object' => $link);
if ($note) {
$record['Note'] = $note;
}
$records[$result['PID']] = $record;
}
return $records;
}