Subversion Repositories cheapmusic

Rev

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