Skip to content

Instantly share code, notes, and snippets.

@PatSO3
Last active November 12, 2018 20:06
Show Gist options
  • Save PatSO3/47d08ac120e27161227147a420021269 to your computer and use it in GitHub Desktop.
Save PatSO3/47d08ac120e27161227147a420021269 to your computer and use it in GitHub Desktop.
Script used to grab LO IDs and Titles then dump into csv format
<?php
if(!isset($argv[1])){
print_r("ERR: directory name required as \$argv[1]... (php getTopLevelTitleNodes.php LO_object_directory)". PHP_EOL);
return;
}
$lo_object_dir = $argv[1];
$xml_files = glob("./". $lo_object_dir ."/*.xml");
// print "starting" . "\n";
$title_list = array();
foreach ($xml_files as $file) {
$doc = new DOMDocument();
$doc->loadXML(file_get_contents($file));
$xpath = new DOMXpath($doc);
preg_match('~Content_(.*?).xml~', $file, $file_id);
$title_nodes = $xpath->query('//title');
foreach ($title_nodes as $title_node) {
// check if the parent node is 'LearningObject' since we don't want any titles within the LOBody, we just want the top level titles
if($title_node->parentNode->nodeName == 'LearningObject'){
$title_list[] = array('LO ID' => $file_id[1], 'Title' => $title_node->nodeValue);
}
}
}
// print_r($title_list);
// print_r(count($title_list) . " top level titles were captured out of " . count($xml_files) . " files." . PHP_EOL . "[LOID] => title" . PHP_EOL);
function str_putcsv($data) {
# Generate CSV data from array
$fh = fopen('php://temp', 'rw'); # don't create a file, attempt
# to use memory instead
# write out the headers
fputcsv($fh, array_keys(current($data)));
# write out the data
foreach ( $data as $row ) {
fputcsv($fh, $row);
}
rewind($fh);
$csv = stream_get_contents($fh);
fclose($fh);
return $csv;
}
print_r(str_putcsv($title_list));
@PatSO3
Copy link
Author

PatSO3 commented Nov 12, 2018

Just place this script in the same directory as your "LO XML files' directory" and add that LO directory as an argument in the command line.

I left the print statements in if you just want the array output and don't need it in csv format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment