Created
March 11, 2014 15:13
-
-
Save jveldboom/9487876 to your computer and use it in GitHub Desktop.
Split ONIX files into smaller chunks
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 | |
class onixSplitter | |
{ | |
function split($file,$header,$footer,$split_at=2000) | |
{ | |
$this->file = $file; | |
$this->header = $header; | |
$this->footer = $footer; | |
$this->split_at = $split_at; | |
$this->write_num = 1; // starting number for files written | |
$this->read($this->file); | |
} | |
function explodeHeader() | |
{ | |
$return = explode("\n",$this->header); | |
foreach($return as $k => $v){ | |
$return[$k] = trim($v); | |
} | |
return $return; | |
} | |
function read() | |
{ | |
$handle = fopen($this->file, "r"); | |
if ($handle) | |
{ | |
$header_array = $this->explodeHeader(); | |
$write = array(); | |
$count = 0; | |
while (($line = fgets($handle)) !== false) | |
{ | |
if(!in_array(trim($line),$header_array) AND $line != $this->footer) | |
{ | |
$check = trim(strtolower($line)); | |
if($check == '<product>'){ | |
$write[$count] = ''; | |
} | |
$write[$count] .= $line; | |
if($check == '</product>'){ | |
++$count; | |
} | |
if($count == $this->split_at) | |
{ | |
$this->writeSplit($write); | |
unset($write); | |
$count = 0; | |
} | |
} | |
} | |
// write remaining lines | |
if(isset($write)) | |
{ | |
$this->writeSplit($write); | |
} | |
} | |
} | |
function writeSplit($data) | |
{ | |
$p = pathinfo($this->file); | |
$file = $p['dirname'].'/'.$p['filename'].'-'.$this->write_num.'.'.$p['extension']; | |
$fp = fopen($file, 'w'); | |
fwrite($fp, $this->header.PHP_EOL); | |
foreach($data as $k => $v){ | |
fwrite($fp, $data[$k].PHP_EOL); | |
} | |
fwrite($fp, $this->footer); | |
fclose($fp); | |
++$this->write_num; | |
} | |
} | |
$header = '<?xml version="1.0" encoding="ISO-8859-1"?> | |
<!DOCTYPE ONIXmessage SYSTEM "http://www.editeur.org/onix/2.1/short/onix-international.dtd"> | |
<ONIXmessage> | |
<header> | |
<m173>2013975</m173> | |
<m174>Random House, LLC</m174> | |
<m175>Content Management Group</m175> | |
<m283>[email protected]</m283> | |
<m180>653</m180> | |
<m182>201402020831</m182> | |
<m183>FULLCAT; Full product extraction</m183> | |
</header>'; | |
$footer = '</ONIXMessage>'; | |
$split = new onixSplitter; | |
$split->split('/path/to/onix.xml',$header,$footer,200); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment