forked from EHRI/TeiEditions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TeiEditionsPlugin.php
executable file
·441 lines (402 loc) · 13.9 KB
/
TeiEditionsPlugin.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<?php
/**
* Omeka TEI Editions
*
* @copyright Copyright 2018 King's College London, Department of Digital Humanities
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GPLv3
*/
require_once dirname(__FILE__) . '/helpers/TeiEditionsFunctions.php';
require_once dirname(__FILE__) . '/helpers/TeiEditionsViewHelpers.php';
/**
* Simple Pages plugin.
*/
class TeiEditionsPlugin extends Omeka_Plugin_AbstractPlugin
{
/**
* @var array Hooks for the plugin.
*/
protected $_hooks = array('install', 'uninstall', 'upgrade', 'initialize',
'define_acl', 'define_routes', 'config_form', 'config',
'public_head');
/**
* @var array Filters for the plugin.
*/
protected $_filters = array(
'admin_navigation_main',
'public_navigation_main',
'item_previous',
'item_next'
);
/**
* @var array Options and their default values.
*/
protected $_options = array();
public function setUp()
{
add_shortcode('editions_item', 'tei_editions_item_shortcode');
add_shortcode('editions_items', 'tei_editions_items_shortcode');
add_shortcode('editions_recent_items', 'tei_editions_recent_items_shortcode');
add_shortcode('editions_index', 'tei_editions_index_shortcode');
parent::setUp();
}
/**
* Create a new XPath-to-element mapping.
*
* @param string $element_id the element id
* @param string $xpath the XPath
* @return TeiEditionsFieldMapping
* @throws Omeka_Record_Exception
* @throws Omeka_Validate_Exception
*/
private function createMapping($element_id, $xpath)
{
$mapping = new TeiEditionsFieldMapping;
$mapping->path = $xpath;
$mapping->element_id = $element_id;
$mapping->save(true);
return $mapping;
}
/**
* Create a new element
*
* @param string $name the element name
* @param string $description the element description
* @return Element
* @throws Omeka_Record_Exception
* @throws Omeka_Validate_Exception
*/
private function createElement($name, $description)
{
$elem = new Element;
$elem->setName($name);
$elem->setDescription($description);
$elem->setElementSet("Item Type Metadata");
$elem->save(true);
return $elem;
}
/**
* Retrieve or create a new item type.
*
* @param string $name the item type name
* @param string $description a description
* @return ItemType
* @throws Omeka_Record_Exception
* @throws Omeka_Validate_Exception
*/
private function getOrCreateItemType($name, $description)
{
$types = get_db()->getTable('ItemType')->findBy(["name" => $name], 1);
if ($types) {
return $types[0];
}
// we need to create a new item type
$type = new ItemType;
$type->name = $name;
$type->description = $description;
$type->save(true);
return $type;
}
/**
* Create mappings for DC metadata
*
* @param array $dc_mappings
* @return array
* @throws Omeka_Record_Exception
* @throws Omeka_Validate_Exception
*/
private function createDublinCoreMappings($dc_mappings)
{
$dc_set = get_db()->getTable('ElementSet')
->findBy(["name" => "Dublin Core"], 1)[0];
$dc_elements_to_ids = [];
foreach ($dc_set->getElements() as $element) {
$dc_elements_to_ids[$element->name] = $element->id;
}
foreach ($dc_mappings as $name => $xpaths) {
foreach ($xpaths as $xpath) {
$this->createMapping($dc_elements_to_ids[$name], $xpath);
}
}
return array($element, $name, $xpath);
}
/**
* Create mappings for Item Type metadata
*
* @param array $item_type_mappings
* @throws Omeka_Record_Exception
* @throws Omeka_Validate_Exception
*/
private function createItemTypeMappings($item_type_mappings)
{
foreach ($item_type_mappings as $type_name => $data) {
$type = $this->getOrCreateItemType($type_name, $data["description"]);
$elements = get_db()->getTable('Element')->findByItemType($type->id);
$elements_to_ids = [];
foreach ($elements as $element) {
$elements_to_ids[$element->name] = $element->id;
}
$elements_to_add = [];
foreach ($data["mappings"] as $name => $details) {
if (!isset($elements_to_ids[$name])) {
$elem = $this->createElement($name, $details["description"]);
$elements_to_ids[$name] = $elem->id;
$elements_to_add[] = $elem;
}
}
if ($elements_to_add) {
$type->addElements($elements_to_add);
$type->save();
}
foreach ($data["mappings"] as $name => $config) {
foreach ($config["xpaths"] as $xpath) {
$this->createMapping($elements_to_ids[$name], $xpath);
}
}
}
}
/**
* Install the plugin.
* @throws Exception
*/
public function hookInstall()
{
$this->_db->query(<<<SQL
CREATE TABLE IF NOT EXISTS {$this->_db->prefix}tei_editions_field_mappings (
id int(10) unsigned NOT NULL auto_increment,
element_id int(10) unsigned NOT NULL,
path tinytext collate utf8_unicode_ci NOT NULL,
FOREIGN KEY (element_id)
REFERENCES {$this->_db->prefix}elements(id)
ON DELETE CASCADE,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
SQL
);
$dc_mappings = [
"Identifier" => ["/tei:TEI/tei:teiHeader/tei:profileDesc/tei:creation/tei:idno"],
"Title" => ["/tei:TEI/tei:teiHeader/tei:fileDesc/tei:titleStmt/tei:title"],
"Subject" => ["/tei:TEI/tei:teiHeader/tei:fileDesc/tei:sourceDesc/tei:list/tei:item/tei:name"],
"Description" => ["/tei:TEI/tei:teiHeader/tei:profileDesc/tei:abstract"],
"Creator" => [
"/tei:TEI/tei:teiHeader/tei:profileDesc/tei:creation/tei:persName",
"/tei:TEI/tei:teiHeader/tei:profileDesc/tei:creation/tei:orgName"
],
"Source" => [
"/tei:TEI/tei:teiHeader/tei:fileDesc/tei:sourceDesc/tei:bibl",
"/tei:TEI/tei:teiHeader/tei:fileDesc/tei:sourceDesc/tei:msDesc/tei:msIdentifier/tei:collection/@ref"
],
"Publisher" => ["/tei:TEI/tei:teiHeader/tei:fileDesc/tei:publicationStmt/tei:publisher/tei:ref"],
"Date" => ["/tei:TEI/tei:teiHeader/tei:profileDesc/tei:creation/tei:date/@when"],
"Rights" => ["/tei:TEI/tei:teiHeader/tei:fileDesc/tei:publicationStmt/tei:availability/tei:licence"],
"Format" => ["/tei:TEI/tei:teiHeader/tei:fileDesc/tei:sourceDesc/tei:msDesc/tei:physDesc"],
"Language" => [
"/tei:TEI/tei:teiHeader/tei:profileDesc/tei:langUsage/tei:language",
"/tei:TEI/tei:teiHeader/tei:fileDesc/tei:sourceDesc/tei:bibl/tei:textLang"
],
"Coverage" => ["/tei:TEI/tei:teiHeader/tei:profileDesc/tei:creation/tei:placeName"]
];
$item_type_mappings = [
"Text" => [
"description" => "Text items",
"mappings" => [
"Text" => [
"description" => "The TEI text",
"xpaths" => [
"/tei:TEI/tei:text/tei:body"
]
]
]
],
"TEI" => [
"description" => "TEI items",
"mappings" => [
"Person" => [
"description" => "Persons mentioned in the text.",
"xpaths" => [
"/tei:TEI/tei:teiHeader/tei:fileDesc/tei:sourceDesc/tei:listPerson/tei:person/tei:persName"
]
],
"Organisation" => [
"description" => "Organisations mentioned in the text.",
"xpaths" => [
"/tei:TEI/tei:teiHeader/tei:fileDesc/tei:sourceDesc/tei:listOrg/tei:org/tei:orgName"
]
],
"Place" => [
"description" => "Places mentioned in the text.",
"xpaths" => [
"/tei:TEI/tei:teiHeader/tei:fileDesc/tei:sourceDesc/tei:listPlace/tei:place/tei:placeName"
]
]
]
]
];
$this->_db->getAdapter()->beginTransaction();
try {
$this->createDublinCoreMappings($dc_mappings);
$this->createItemTypeMappings($item_type_mappings);
$this->_db->getAdapter()->commit();
} catch (Exception $e) {
$this->_db->getAdapter()->rollBack();
throw $e;
}
$this->_installOptions();
}
/**
* Uninstall the plugin.
*/
public function hookUninstall()
{
$this->_db->query(
"DROP TABLE IF EXISTS {$this->_db->prefix}tei_editions_field_mappings");
$this->_db->getAdapter()->beginTransaction();
$item_types = get_db()->getTable("ItemType")->findBy(['name' => 'TEI']);
if (!empty($item_types)) {
$type = $item_types[0];
$elements = get_db()->getTable('Element')->findByItemType($type->id);
foreach ($elements as $element) {
$element->delete();
}
$type->delete();
}
$this->_db->getAdapter()->commit();
$this->_uninstallOptions();
}
/**
* Upgrade the plugin.
*
* @param array $args contains: 'old_version' and 'new_version'
*/
public function hookUpgrade($args)
{
}
/**
* Add the translations.
*/
public function hookInitialize()
{
}
/**
* @param array $args
* @throws Zend_Config_Exception
*/
public function hookDefineRoutes($args)
{
$args['router']->addConfig(new Zend_Config_Ini(
dirname(__FILE__) . "/routes.ini")
);
}
/**
* Display the plugin config form.
*/
public function hookConfigForm()
{
require dirname(__FILE__) . '/config_form.php';
}
/**
* Define the ACL.
*
* @param array $args
*/
public function hookDefineAcl($args)
{
$acl = $args['acl'];
$mappingResource = new Zend_Acl_Resource('TeiEditions_FieldMapping');
$acl->add($mappingResource);
}
/**
* Set the options from the config form input.
*/
public function hookConfig()
{
set_option('tei_editions_default_item_type', (int)$_POST['tei_editions_default_item_type']);
set_option('tei_editions_template_neatline', (int)$_POST['tei_editions_template_neatline']);
}
public function hookPublicHead($args)
{
// prevent text widgets showing on Neatline embedded maps by adding
// a param: neatline-embed=true
if (array_key_exists("neatline-embed", $_GET)) {
queue_css_file('neatline-overrides', $media = "all", $conditional = false, $dir = 'css');
}
}
/**
* Add a link to the administrative navigation bar.
*
* @param array $nav The array of label/URI pairs.
* @return array
*/
public function filterAdminNavigationMain($nav)
{
$nav[] = array(
'label' => __('Editions'),
'uri' => url('tei-editions')
);
return $nav;
}
/**
* Add the pages to the public main navigation options.
*
* @param array Navigation array.
* @return array Filtered navigation array.
*/
public function filterPublicNavigationMain($nav)
{
array_unshift($nav, array(
'label' => __('Editions'),
'uri' => url('editions')
));
return $nav;
}
/**
* Override item previous to exclude non-public items.
*
* @param $unused
* @param array $args
* @return Omeka_Record_AbstractRecord
*/
public function filterItemPrevious($unused, array $args) {
$item = $args['item'];
$table = $this->_db->getTable('Item');
$select = $table->getSelect()
->limit(1)
->where('items.id < ? AND items.public', $item->id);
$this->_filterSolrExcludes($select);
$select->order('items.id DESC');
return $table->fetchObject($select);
}
/**
* Override item next to exclude non-public items.
*
* @param $unused
* @param array $args
* @return Omeka_Record_AbstractRecord
*/
public function filterItemNext($unused, array $args) {
$item = $args['item'];
$table = $this->_db->getTable('Item');
$select = $table->getSelect()
->limit(1)
->where('items.id > ? AND items.public', $item->id);
$this->_filterSolrExcludes($select);
$select->order('items.id ASC');
return $table->fetchObject($select);
}
/**
* If the Solr plugin is enabled, exclude items from
* previous/next that are excluded from indexing.
*
* @param Zend_Db_Select $select
* @return Zend_Db_Select
*/
private function _filterSolrExcludes(Zend_Db_Select $select) {
return !plugin_is_active('SolrSearch')
? $select
: $select->where("(
items.collection_id IS NULL
OR items.collection_id NOT IN (
SELECT collection_id FROM {$this->_db->prefix}solr_search_excludes
)
)");
}
}