Skip to content

Commit

Permalink
Reworked scripts to account for random order in oai input files for e…
Browse files Browse the repository at this point in the history
…ducation levels and categories.
  • Loading branch information
TimoBoer committed Jan 26, 2023
1 parent 32b3bb4 commit f312b68
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 70 deletions.
81 changes: 44 additions & 37 deletions oai-pmh/management/categories.php
Expand Up @@ -8,42 +8,36 @@
if ($argc > 1) {
clearCategoryTable();
createCategoryTable();

$lookup = [];
for ($args = 1; $args < $argc; $args++) {

$xmlfile = $argv[$args];
//if($xmlfile == "./vocabularies/opleidingsdomeinen.xml"){
// $source_url = "https://vdex.kennisnet.nl/kennisnetset/2015.01/mbo_opleidingsdomeinen_studierichtingen-knset.xml";
//}
//else {
// $source_url = "https://vdex.kennisnet.nl/kennisnetset/2015.01/bve_domeinoverstijgende_vakken-knset.xml";
//}
$source_url = $xmlfile;
echo $source_url . "\n";
$xml = simplexml_load_file($xmlfile);

$nodes = $xml->xpath('//vdex:term');
$relations = $xml->xpath("//vdex:relationship");

for ($i = 0; $i < count($nodes); $i++) {
$node = $nodes[$i];
foreach ($nodes as $node) {
$row = [];
$ns = $node->getNamespaces();
$c = $node->children($ns['vdex']);
$tempParent = null;

$row[label] = (string)$c->caption->langstring;
$row[ID] = (string)$c->termIdentifier;
$row[url] = $source_url;
foreach ($relations as $relation){
$relationChild = $relation->children($ns['vdex']);
if ((string)$relationChild->sourceTerm == (string)$c->termIdentifier and (string)$relationChild->relationshipType == "BT"){
$tempParent = (string)$relationChild->targetTerm;
if ((string)$relationChild->sourceTerm == $row[ID] and (string)$relationChild->relationshipType == "BT") {
$row[parent] = (string)$relationChild->targetTerm;
$lookup[$row[ID]] = $row;
break;
}
}

$tempTaxon = (string)$c->termIdentifier;
$tempLabel = (string)$c->caption->langstring;
insertCategory($source_url,$tempTaxon,$tempLabel, $tempParent);
$lookup[$row[ID]] = $row;
}
}
insertCategory($lookup);
}
else
{
Expand Down Expand Up @@ -90,28 +84,41 @@ function clearCategoryTable() {
db_query($q);
}

function insertCategory($source_url, $taxon, $label,$parent){
function insertCategory($lookup)
{
global $xerte_toolkits_site;
$prefix = $xerte_toolkits_site->database_table_prefix;

$q = "INSERT INTO {$xerte_toolkits_site->database_table_prefix}syndicationcategories(category_name) VALUES (?)";
db_query($q,array($label));

$q2 = "SELECT category_id,category_name FROM {$xerte_toolkits_site->database_table_prefix}syndicationcategories WHERE category_name like ?";
$result = db_query($q2,array($label));
$return_id = $result[0]['category_id'];

$q3 = "INSERT INTO {$xerte_toolkits_site->database_table_prefix}oai_categories(category_id,taxon,label,source_url,parent_id) VALUES (?,?,?,?,?)";
if (is_null($parent)) {
$params = array($return_id, $taxon, $label, $source_url, null);
db_query($q3,$params);
} else {
$q ="SELECT category_id FROM {$xerte_toolkits_site->database_table_prefix}oai_categories WHERE taxon = ?";
$parent_id = db_query_one($q, array($parent))["category_id"];
$params = array($return_id, $taxon, $label, $source_url, $parent_id);
db_query($q3,$params);
$q4 = "UPDATE {$xerte_toolkits_site->database_table_prefix}syndicationcategories SET parent_id = ? WHERE category_id = ?";
db_query($q4,array($parent_id, $return_id));
foreach ($lookup as $i => $row) {
//generate serversideID populate both tables
$q = "INSERT INTO {$prefix}syndicationcategories(category_name) VALUES (?)";
$serverside_id = db_query_one($q, array($row[label]));
$lookup[$i][serverID] = $serverside_id;
$q2 = "INSERT INTO {$prefix}oai_categories(category_id,taxon,label,source_url) VALUES (?,?,?,?)";
$params = array($serverside_id, $row[ID], $row[label], $row[url]);
$res = db_query($q2, $params);
}
foreach ($lookup as $row){
if (!is_null($row[parent])){
$q3 = "update {$prefix}syndicationcategories SET parent_id = ? WHERE category_id = ?";
$parent_id = $lookup[$row[parent]][serverID];
$params = array($parent_id, $row[serverID]);
$res = db_query($q3, $params);
$q4 = "update {$prefix}oai_categories SET parent_id = ? WHERE category_id = ?";
$res = db_query($q4, $params);
}
}
}

}
// $q3 = "INSERT INTO {$xerte_toolkits_site->database_table_prefix}oai_categories(category_id,taxon,label,source_url,parent_id) VALUES (?,?,?,?,?)";
// if (is_null($parent)) {
// $params = array($return_id, $taxon, $label, $source_url, null);
// db_query($q3,$params);
// } else {
// $q ="SELECT category_id FROM {$xerte_toolkits_site->database_table_prefix}oai_categories WHERE taxon = ?";
// $parent_id = db_query_one($q, array($parent))["category_id"];
// $params = array($return_id, $taxon, $label, $source_url, $parent_id);
// db_query($q3,$params);
// $q4 = "UPDATE {$xerte_toolkits_site->database_table_prefix}syndicationcategories SET parent_id = ? WHERE category_id = ?";
// db_query($q4,array($parent_id, $return_id));
// }
85 changes: 52 additions & 33 deletions oai-pmh/management/educational.php
Expand Up @@ -16,25 +16,25 @@

$nodes = $xml->xpath('//vdex:term');
$relations = $xml->xpath("//vdex:relationship");
$lookup = [];

for ($i = 0; $i < count($nodes); $i++) {
$tempParent = null;
$node = $nodes[$i];
foreach ($nodes as $node) {
$row = [];
$c = $node->children($ns['vdex']);

$row[label] = (string)$c->caption->langstring;
$row[ID] = (string)$c->termIdentifier;
foreach ($relations as $relation){
$relationChild = $relation->children($ns['vdex']);
if ((string)$relationChild->sourceTerm == (string)$c->termIdentifier and (string)$relationChild->relationshipType == "BT"){
$tempParent = (string)$relationChild->targetTerm;
break;
}
if ((string)$relationChild->sourceTerm == $row[ID] and (string)$relationChild->relationshipType == "BT") {
$row[parent] = (string)$relationChild->targetTerm;
$lookup[$row[ID]] = $row;
break;
}

$tempLabel = (string)$c->caption->langstring;
$tempID = (string)$c->termIdentifier;

insertEducational($tempID,$tempLabel, $tempParent);
}
$lookup[$row[ID]] = $row;
}
insertEducational($lookup);

}
function createEducationalTable() {
global $xerte_toolkits_site;
Expand All @@ -60,30 +60,49 @@ function clearEducationalTable() {
db_query($q);
}

function insertEducational($termID, $label, $parent = null){
function insertEducational($lookup, $termID = null, $label = null, $parent = null){

global $xerte_toolkits_site;
$prefix = $xerte_toolkits_site->database_table_prefix;

$q = "INSERT INTO {$prefix}educationlevel(educationlevel_name) VALUES (?)";
$res = db_query($q,array($label));

$q2 = "SELECT educationlevel_id,educationlevel_name FROM {$prefix}educationlevel WHERE educationlevel_name like ?";
$result = db_query($q2,array($label));
$return_id = $result[0]['educationlevel_id'];

$q3 = "INSERT INTO {$prefix}oai_education(education_id,term_id,label,parent_id) VALUES (?,?,?,?)";
if (is_null($parent)){
$params = array($return_id, $termID, $label, $parent);
$res = db_query($q3,$params);
} else {
$q = "SELECT education_id FROM {$prefix}oai_education WHERE term_id = ?";
$parent_id = db_query_one($q, array($parent))["education_id"];
$params = array($return_id, $termID, $label, $parent_id);
$res = db_query($q3,$params);
$q4 = "UPDATE {$prefix}educationlevel SET parent_id = ? WHERE educationlevel_id = ?";
$res = db_query($q4,array($parent_id, $return_id));
foreach ($lookup as $i => $row){
//generate serversideID populate both tables
$q = "INSERT INTO {$prefix}educationlevel(educationlevel_name) VALUES (?)";
$serverside_id = db_query_one($q,array($row[label]));
$lookup[$i][serverID] = $serverside_id;
$q2 = "INSERT INTO {$prefix}oai_education(education_id,term_id,label) VALUES (?,?,?)";
$params = array($serverside_id, $row[ID], $row[label]);
$res = db_query($q2, $params);
}
foreach ($lookup as $row){
if (!is_null($row[parent])){
$q3 = "update {$prefix}educationlevel SET parent_id = ? WHERE educationlevel_id = ?";
$parent_id = $lookup[$row[parent]][serverID];
$params = array($parent_id, $row[serverID]);
$res = db_query($q3, $params);
$q4 = "update {$prefix}oai_education SET parent_id = ? WHERE education_id = ?";
$res = db_query($q4, $params);
}
}

// $q = "INSERT INTO {$prefix}educationlevel(educationlevel_name) VALUES (?)";
// $res = db_query($q,array($label));
//
// $q2 = "SELECT educationlevel_id,educationlevel_name FROM {$prefix}educationlevel WHERE educationlevel_name like ?";
// $result = db_query($q2,array($label));
// $return_id = $result[0]['educationlevel_id'];
//
// $q3 = "INSERT INTO {$prefix}oai_education(education_id,term_id,label,parent_id) VALUES (?,?,?,?)";
// if (is_null($parent)){
// $params = array($return_id, $termID, $label, $parent);
// $res = db_query($q3,$params);
// } else {
// $q = "SELECT education_id FROM {$prefix}oai_education WHERE term_id = ?";
// $parent_id = db_query_one($q, array($parent))["education_id"];
// $params = array($return_id, $termID, $label, $parent_id);
// $res = db_query($q3,$params);
// $q4 = "UPDATE {$prefix}educationlevel SET parent_id = ? WHERE educationlevel_id = ?";
// $res = db_query($q4,array($parent_id, $return_id));
// }

}

0 comments on commit f312b68

Please sign in to comment.