Subversion Repositories cheapmusic

Rev

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

Rev Author Line No. Line
18 - 1
<?php
2
require_once('php/XmlStreamer.php');
3
require_once('php/tools.php');
4
ini_set("memory_limit", "256M");
5
 
6
$cnt = 0;
7
 
8
class MastersXmlStreamer extends XmlStreamer
9
{
10
	public function processNode($xmlString, $elementName, $nodeIndex)
11
	{
12
		global $cnt;
13
		global $fh;
14
		global $fhArtists;
15
 
16
		$xml = simplexml_load_string($xmlString, 'simple_xml_extended');
17
 
18
		if ($elementName == 'master') {
19
			++$cnt;
20
 
21
			$id = $xml->Attribute('id');
22
			$mainRelease = $xml->main_release;
23
			$year = $xml->year;
24
			$title = $xml->title;
25
 
26
			$genres = [];
44 - 27
			if (isset($xml->genres)) {
18 - 28
				foreach ($xml->genres->genre as $n) {
29
					$genres[] = $n;
30
				}
31
			}
32
 
33
			$styles = [];
34
			if (isset($xml->styles)) {
35
				foreach ($xml->styles->style as $n) {
36
					$styles[] = $n;
37
				}
38
			}
39
 
40
			if (isset($xml->artists)) {
41
				foreach ($xml->artists->artist as $n) {
44 - 42
					if (!empty($n->id)) {
43
						fputcsv2($fhArtists, array($id, $n->id), ',', '"');
44
					}
18 - 45
				}
46
			}
47
 
48
			$arr = array(
49
				$id,
50
				$mainRelease,
51
				$year,
52
				$title,
53
				join(", ", $genres),
54
				join(", ", $styles)
55
			);
56
 
44 - 57
			fputcsv2($fh, $arr, ',', '"');
18 - 58
		}
59
 
60
		return true;
61
	}
62
}
63
 
58 - 64
$xmlfile = "./in/discogs_20190801_masters.xml.gz";
18 - 65
 
66
$fh = fopen("out/masters.load", "w+");
67
$fhArtists = fopen("out/masterArtists.load", "w+");
68
 
69
$xmlstream = "compress.zlib://$xmlfile";
70
$xmlfileSize = gzfilesize($xmlfile);
71
 
72
$streamer = new MastersXmlStreamer($xmlstream, $xmlfileSize);
73
if ($streamer->parse()) {
74
	echo "Finished $cnt masters." . PHP_EOL;
75
} else {
76
	echo "Couldn't find root node" . PHP_EOL;
77
}
78
 
79
fclose($fh);
80
fclose($fhArtists);