-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDspaceRestapiHarvesterPlugin.php
More file actions
329 lines (283 loc) · 11.2 KB
/
DspaceRestapiHarvesterPlugin.php
File metadata and controls
329 lines (283 loc) · 11.2 KB
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
<?php
/**
* DspaceRestapiHarvesterPlugin class - represents the DSpace Connector plugin
*
* @package DspaceRestapiHarvester
* @copyright Copyright 2008-2013 Fondren Library at Rice University
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GPLv3
* @author Ying Jin ying.jin@rice.edu
* @modified from OaipmhHarvester
*/
/** Path to plugin directory */
defined('DSPACE_RESTAPI_HARVESTER_PLUGIN_DIR')
or define('DSPACE_RESTAPI_HARVESTER_PLUGIN_DIR', dirname(__FILE__));
require_once dirname(__FILE__) . '/functions.php';
/**
* Dspace Connector plugin.
*/
class DspaceRestapiHarvesterPlugin extends Omeka_Plugin_AbstractPlugin
{
/**
* @var array Hooks for the plugin.
*/
protected $_hooks = array('install',
'uninstall',
'admin_items_show',
'public_items_show',
'public_items_browse_each'
);
/**
* @var array Filters for the plugin.
*/
protected $_filters = array('admin_navigation_main');
/**
* @var array Options and their default values.
*/
protected $_options = array();
private $_elementSets;
private $_elements;
public function __construct(){
parent::__construct();
$ini_array = parse_ini_file("elements.ini");
foreach($ini_array as $setKey => $setValue) {
if(strpos($setKey,'.') == false){
// this is for the elementSet label
$elementSets[$setKey] = $setValue;
}else{
$elements[$setKey] = $setValue;
}
}
// Set the elements.
$this->_elementSets = $elementSets;
$this->_elements = $elements;
}
/**
* read in element ini file
*/
/**
* Install the plugin.
*/
public function hookInstall()
{
$db = $this->_db;
$sql = "
CREATE TABLE IF NOT EXISTS `{$db->prefix}dspace_restapi_harvester_harvests` (
`id` int unsigned NOT NULL auto_increment,
`collection_id` int unsigned default NULL,
`source_collection_id` VARCHAR(36) NOT NULL,
`base_url` text NOT NULL,
`collection_spec` text,
`collection_name` text,
`collection_handle` text,
`collection_size` int unsigned default NULL,
`status` enum('queued','in progress','completed','error','deleted','killed') NOT NULL default 'queued',
`status_messages` text,
`resumption_token` text,
`initiated` datetime default NULL,
`completed` datetime default NULL,
`start_from` datetime default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
$db->query($sql);
$sql = "
CREATE TABLE IF NOT EXISTS `{$db->prefix}dspace_restapi_harvester_records` (
`id` int unsigned NOT NULL auto_increment,
`harvest_id` int unsigned NOT NULL,
`item_id` int unsigned default NULL,
`identifier` text NOT NULL,
`handle` text NOT NULL,
`datestamp` tinytext NOT NULL,
PRIMARY KEY (`id`),
KEY `identifier_idx` (identifier(255)),
UNIQUE KEY `item_id_idx` (item_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
$db->query($sql);
$sql = "
CREATE TABLE IF NOT EXISTS `{$db->prefix}dspace_restapi_harvester_elementsets` (
`id` int unsigned NOT NULL auto_increment,
`elementset_id` int unsigned NOT NULL,
`elementset_name` text NOT NULL,
`elementset_label` text NOT NULL,
PRIMARY KEY (`id`),
KEY `elementset_name_idx` (elementset_name(255)),
UNIQUE KEY `elementset_id_idx` (elementset_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
$db->query($sql);
$sql = "
CREATE TABLE IF NOT EXISTS `{$db->prefix}dspace_restapi_harvester_elements` (
`id` int unsigned NOT NULL auto_increment,
`element_id` int unsigned NOT NULL,
`elementset_id` int unsigned NOT NULL,
`element_name` text NOT NULL,
`element_label` text NOT NULL,
PRIMARY KEY (`id`),
KEY `element_name_idx` (element_name(255)),
UNIQUE KEY `element_id_idx` (element_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
$db->query($sql);
/** prepare the elements if they are not there */
// Add the new element set.
if ($this->_elementSets){
$setIds = array();
while ((list($setKey, $setValue) = each($this->_elementSets))){
$findSet = $db->getTable('ElementSet')->findByName($setValue);
if(!$findSet){
// insert the set to omeka elementset first if can't find it in the table
$sql = "
INSERT INTO `{$db->ElementSet}` (`name`)
VALUES (?)";
$db->query($sql, $setValue);
$findSet = $db->getTable('ElementSet')->findByName($setValue);
}
// insert into dspace_restapi_harvester elementsets too
$sql = "
INSERT INTO `{$db->DspaceRestapiHarvester_Elementset}` (`elementset_id`, `elementset_name`, `elementset_label`)
VALUES (?, ?, ?)";
$db->query($sql, array($findSet->id, $setKey, $setValue));
$setIds[$setKey] = $findSet->id;
}
// go through the elements
if($this->_elements){
while (list($elementKey, $elementValue) = each($this->_elements)){
$setKey = substr($elementKey, 0, strpos($elementKey, '.'));
$setId = $setIds[$setKey];
$findElement = $db->getTable('Element')->findByElementSetNameAndElementName($this->_elementSets[$setKey], $elementValue);
if (!$findElement) {
// insert into omeka elements table
$sql = "
INSERT INTO `{$db->Element}` (`element_set_id`, `name`)
VALUES (?, ?)";
$db->query($sql, array($setId, $elementValue));
$findElement = $db->getTable('Element')->findByElementSetNameAndElementName($this->_elementSets[$setKey], $elementValue);
}
// insert into dspace_restapi_harvester elements table too
$sql = "
INSERT INTO `{$db->DspaceRestapiHarvester_Element}` (`elementset_id`, `element_id`, `element_name`, `element_label`)
VALUES (?, ?, ?, ?)";
$db->query($sql, array($setId, $findElement->id, $elementKey, $elementValue));
}
}
}
}
/**
* Uninstall the plugin.
*/
public function hookUninstall()
{
$db = $this->_db;
// drop the tables
$sql = "DROP TABLE IF EXISTS `{$db->prefix}dspace_restapi_harvester_harvests`;";
$db->query($sql);
$sql = "DROP TABLE IF EXISTS `{$db->prefix}dspace_restapi_harvester_records`;";
$db->query($sql);
$sql = "DROP TABLE IF EXISTS `{$db->prefix}dspace_restapi_harvester_elements`;";
$db->query($sql);
$sql = "DROP TABLE IF EXISTS `{$db->prefix}dspace_restapi_harvester_elementsets`;";
$db->query($sql);
$this->_uninstallOptions();
}
/**
* Render the datastream on public show page.
*
* @return void.
*/
public function hookPublicItemsShow()
{
$this->showItemBitstream();
/*// Get the record.
$record = get_current_record('item');
$id = $record->id; // this is the item_id from omeka
$existRecord = $this->_db->getTable('DspaceRestapiHarvester_Record')->findByItemId($id);
$source_item_id = $existRecord->identifier;
if ($existRecord) {
// get the harvest info and then complie the bitstream list from it
$harvest_id = $existRecord->harvest_id;
$handle = $existRecord->handle;
$harvester = $this->_db->getTable('DspaceRestapiHarvester_Harvest')->findByHarvestId($harvest_id);
$bitstreams = $harvester->listBitstreams($source_item_id, $handle);
$thumbList = $bitstreams["thumbnail"];
$origList = $bitstreams["original"];
$html = "";
if($origList){
foreach($origList as $key => $value){
if(array_key_exists($key, $thumbList)){
// Construct HTML.
$html .= "<a href='{$value}'><img alt='{$key}' src='{$thumbList[$key]}' /></a><br/>";
}else{
$html .= "<a href='$value'>{$key}</a><br/>";
}
}
echo $html;
}else{
echo "No original Files !!!";
}
}else{echo "No existing Records!!!";}*/
}
/**
* Render the datastream on public show page.
*
* @return void.
*/
public function hookAdminItemsShow()
{
$this->showItemBitstream();
}
public function hookPublicItemsBrowseEach()
{
$this->showItemBitstream();
}
/**
* Add the DSpace Connector link to the admin main navigation.
*
* @param array Navigation array.
* @return array Filtered navigation array.
*/
public function filterAdminNavigationMain($nav)
{
$nav[] = array(
'label' => __('Dspace Restapi Harvester'),
'uri' => url('dspace-restapi-harvester'),
'privilege' => 'index'
);
return $nav;
}
public function showItemBitstream(){
// Get the record.
$record = get_current_record('item');
$id = $record->id; // this is the item_id from omeka
$existRecord = $this->_db->getTable('DspaceRestapiHarvester_Record')->findByItemId($id);
if ($existRecord) {
// get the harvest info and then compile the bitstream list from it
$source_item_id = $existRecord->identifier;
//echo $html .= "Source Item ID: $source_item_id<br/>";
$harvest_id = $existRecord->harvest_id;
$handle = $existRecord->handle;
$harvester = $this->_db->getTable('DspaceRestapiHarvester_Harvest')->findByHarvestId($harvest_id);
$bitstreams = $harvester->listBitstreams($source_item_id, $handle);
$thumbList = $bitstreams["thumbnail"];
$origList = $bitstreams["original"];
//$debugInfo = $bitstreams["debuginfo"];
if($origList){
foreach($origList as $key => $value){
if(array_key_exists($key, $thumbList)){
// Construct HTML.
$html .= "<a href='{$value}'><img alt='{$key}' src='{$thumbList[$key]}' /></a><br/>";
}else{
$html .= "<a href='$value'>{$key}</a><br/>";
}
}
echo $html;
}else{
/* if($debugInfo){
foreach ($debugInfo as $key => $value){
$html .= "$key == $value <br />";
}
}
echo $html;*/
echo "No original Files !!!";
}
}else{
echo "No existing Records!!!";
}
}
}