Subversion Repositories cheapmusic

Rev

Rev 44 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
require_once('php/XmlStreamer.php');
require_once('php/tools.php');
ini_set("memory_limit", "256M");

$cnt = 0;

class MastersXmlStreamer extends XmlStreamer
{
        public function processNode($xmlString, $elementName, $nodeIndex)
        {
                global $cnt;
                global $fh;
                global $fhArtists;

                $xml = simplexml_load_string($xmlString, 'simple_xml_extended');

                if ($elementName == 'master') {
                        ++$cnt;

                        $id = $xml->Attribute('id');
                        $mainRelease = $xml->main_release;
                        $year = $xml->year;
                        $title = $xml->title;

                        $genres = [];
                        if (isset($xml->genres)) {
                                foreach ($xml->genres->genre as $n) {
                                        $genres[] = $n;
                                }
                        }

                        $styles = [];
                        if (isset($xml->styles)) {
                                foreach ($xml->styles->style as $n) {
                                        $styles[] = $n;
                                }
                        }

                        if (isset($xml->artists)) {
                                foreach ($xml->artists->artist as $n) {
                                        if (!empty($n->id)) {
                                                fputcsv2($fhArtists, array($id, $n->id), ',', '"');
                                        }
                                }
                        }

                        $arr = array(
                                $id,
                                $mainRelease,
                                $year,
                                $title,
                                join(", ", $genres),
                                join(", ", $styles)
                        );

                        fputcsv2($fh, $arr, ',', '"');
                }

                return true;
        }
}

$xmlfile = "./in/discogs_20190801_masters.xml.gz";

$fh = fopen("out/masters.load", "w+");
$fhArtists = fopen("out/masterArtists.load", "w+");

$xmlstream = "compress.zlib://$xmlfile";
$xmlfileSize = gzfilesize($xmlfile);

$streamer = new MastersXmlStreamer($xmlstream, $xmlfileSize);
if ($streamer->parse()) {
        echo "Finished $cnt masters." . PHP_EOL;
} else {
        echo "Couldn't find root node" . PHP_EOL;
}

fclose($fh);
fclose($fhArtists);