Created
August 27, 2020 00:02
-
-
Save CharlieEtienne/c40e4f5ffb9d8be7aef9243607c3ffb5 to your computer and use it in GitHub Desktop.
Split multi SCORM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('BASEDIR', "splitted/"); | |
$unite = 0; | |
foreach( glob("MODULE*/*/imsmanifest.xml") as $file ) { | |
$chapitre = 0; | |
$parent_dir = dirname($file); | |
$dest_parent_dir = BASEDIR . $parent_dir; | |
$xmlstr = file_get_contents($file); | |
$manifest = new SimpleXMLElement($xmlstr); | |
foreach( $manifest->organizations->organization->item as $item ) { | |
$new_content = new SimpleXMLElement($xmlstr); | |
unset($new_content->organizations->organization[ 0 ]->item); | |
unset($new_content->resources->resource); | |
Splitter::xml_adopt($new_content->organizations->organization[ 0 ], $item); | |
$new_item = $new_content->organizations->organization->item[ 0 ]; | |
foreach( $manifest->resources->resource as $resource ) { | |
if( (string)$new_item[ "identifierref" ] === (string)$resource[ "identifier" ] ) { | |
Splitter::xml_adopt($new_content->resources, $resource); | |
} | |
} | |
$new_resource = $new_content->resources->resource[ 0 ]; | |
$title = (string)$new_item->title; | |
$resource_href = (string)$new_resource[ 'href' ]; | |
$resource_dir = dirname($resource_href); | |
$resources_full_path = $parent_dir . '/' . $resource_dir; | |
$dest_resource_dir = $dest_parent_dir . '/' . $unite . '-' . $chapitre . '/' . $resource_dir; | |
$new_manifest_path = $dest_parent_dir . '/' . $unite . '-' . $chapitre . '/' . 'imsmanifest.xml'; | |
$zip_file_path = $dest_parent_dir . '/' . $unite . '-' . $chapitre; | |
echo "### TRAITEMENT chapitre " . $unite . '-' . $chapitre, PHP_EOL, $parent_dir, PHP_EOL, $title, PHP_EOL; | |
Splitter::recursive_copy((string)$resources_full_path, (string)$dest_resource_dir); | |
$dom = new DOMDocument('1.0'); | |
$dom->preserveWhiteSpace = false; | |
$dom->formatOutput = true; | |
$dom->loadXML($new_content->asXML()); | |
$dom->save($new_manifest_path); | |
ExtendedZip::zipTree($zip_file_path, $zip_file_path .'.zip', ZipArchive::CREATE, $unite . '-' . $chapitre); | |
$chapitre++; | |
} | |
$unite++; | |
} | |
class Splitter | |
{ | |
/** | |
* Add child nodes to XML root recursively | |
* | |
* @param \SimpleXMLElement $root | |
* @param \SimpleXMLElement $new | |
* @param null $namespace | |
*/ | |
public static function xml_adopt( SimpleXMLElement $root, SimpleXMLElement $new, $namespace = null ) { | |
$node = $root->addChild($new->getName(), (string)$new, $namespace); | |
foreach( $new->attributes() as $attr => $value ) { | |
$node->addAttribute($attr, $value); | |
} | |
$namespaces = array_merge(array( null ), $new->getNameSpaces(true)); | |
foreach( $namespaces as $space ) { | |
foreach( $new->children($space) as $child ) { | |
self::xml_adopt($node, $child, $space); | |
} | |
} | |
} | |
public static function recursive_copy( $src, $dst ) { | |
if( !is_dir($src) ) { | |
echo "$src is not a valid directory\n"; | |
return false; | |
} | |
$dir = opendir($src); | |
@mkdir($dst, 0777, true); | |
while( false !== ( $file = readdir($dir) ) ) { | |
if( ( $file != '.' ) && ( $file != '..' ) ) { | |
if( is_dir($src . '/' . $file) ) { | |
self::recursive_copy($src . '/' . $file, $dst . '/' . $file); | |
} | |
else { | |
copy($src . '/' . $file, $dst . '/' . $file); | |
} | |
} | |
} | |
closedir($dir); | |
} | |
} | |
class ExtendedZip extends ZipArchive { | |
// Member function to add a whole file system subtree to the archive | |
public function addTree($dirname, $localname = '') { | |
if ($localname) { | |
$this->addEmptyDir($localname); | |
} | |
$this->_addTree($dirname, $localname); | |
} | |
// Internal function, to recurse | |
protected function _addTree($dirname, $localname) { | |
$dir = opendir($dirname); | |
while ($filename = readdir($dir)) { | |
// Discard . and .. | |
if ($filename == '.' || $filename == '..') | |
continue; | |
// Proceed according to type | |
$path = $dirname . '/' . $filename; | |
$localpath = $localname ? ($localname . '/' . $filename) : $filename; | |
if (is_dir($path)) { | |
// Directory: add & recurse | |
$this->addEmptyDir($localpath); | |
$this->_addTree($path, $localpath); | |
} | |
else if (is_file($path)) { | |
// File: just add | |
$this->addFile($path, $localpath); | |
} | |
} | |
closedir($dir); | |
} | |
// Helper function | |
public static function zipTree($dirname, $zipFilename, $flags = 0, $localname = '') { | |
$zip = new self(); | |
$zip->open($zipFilename, $flags); | |
$zip->addTree($dirname, $localname); | |
$zip->close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment