-
Notifications
You must be signed in to change notification settings - Fork 174
/
insert_hed_schema.php
110 lines (95 loc) · 3.25 KB
/
insert_hed_schema.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
<?php declare(strict_types=1);
// This script imports a HED Schema in XML format in the database
// Example usage: php insert_hed_schema.php https://raw.githubusercontent.com/hed-standard/hed-schemas/main/standard_schema/hedxml/HED8.2.0.xml
require_once __DIR__ . "/../../tools/generic_includes.php";
$fileLocation = $argv[1];
if (empty($fileLocation)) {
die("You must supply the HED schema XML location\n");
}
$db = \NDB_Factory::singleton()->database();
$xml = simplexml_load_file($fileLocation) or die("Error: Unable to load schema\n");
$hedAttributes = $xml->xpath('/HED')[0]->attributes();
$slashSplit = explode('/', $fileLocation);
$libraryName = explode(
'.xml',
end($slashSplit)
)[0]; // $hedAttributes->library->__toString();
$versionPattern = '~[0-9]\.[0-9]\.[0-9]~';
$libraryVersion = preg_match($versionPattern, $libraryName, $hedVersion)
? $hedVersion[0]
: 'n/a'; // $hedAttributes->version->__toString();
$libraryDescription = $xml->xpath('/HED/prologue')[0]->__toString();
$schema = $xml->xpath('/HED/schema/node');
if (!$schema) {
die("Failed to get nodes from schema");
}
$db->insert(
'hed_schema',
[
'Name' => $libraryName,
'Version' => $libraryVersion,
'Description' => $libraryDescription,
'URL' => $fileLocation
]
);
$newSchemaID = intval($db->getLastInsertId());
foreach ($schema as $parentNode) {
echo "Inserting '$parentNode->name' nodes\n";
insertNodes($newSchemaID, $parentNode, 0, '');
}
/**
* Insert HED tag nodes in DB
*
* @param int $schemaID PK of schema HED tag belongs to
* @param object $parentNode Parent node object
* @param int $parentID PK of parent node
* @param string $rootString Assembled HED string of parent nodes
*
* @return void
*/
function insertNodes($schemaID, $parentNode, $parentID, $rootString) : void
{
$nodeName = $parentNode->name->__toString();
// We are omitting and considering '#' nodes as leaf nodes
if (strcmp($nodeName, '#') == 0) {
return;
}
$longName = strlen($rootString) == 0 ? $nodeName : "$rootString/$nodeName";
$description = $parentNode->description ?: 'No description available';
$insertID = dbInsert(
$schemaID,
$parentID,
$nodeName,
$longName,
$description
);
foreach ($parentNode->node as $childNode) {
insertNodes($schemaID, $childNode, $insertID, $longName);
}
}
/**
* Insert HED tag node in DB
*
* @param int $schemaID PK of schema HED tag belongs to
* @param int $parentID PK of parent node
* @param string $nodeName HED tag short name
* @param string $longName Full HED tag name
* @param string $description Text description
*
* @return int Node Primary Key
*/
function dbInsert($schemaID, $parentID, $nodeName, $longName, $description) : int
{
global $db;
$db->insert(
'hed_schema_nodes',
[
'ParentID' => $parentID == 0 ? null : $parentID,
'SchemaID' => $schemaID,
'Name' => $nodeName,
'LongName' => $longName,
'Description' => $description
]
);
return intval($db->getLastInsertId());
}