Subversion Repositories cheapmusic

Rev

Rev 18 | Rev 58 | 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 ArtistsXmlStreamer extends XmlStreamer
9
{
10
	public function processNode($xmlString, $elementName, $nodeIndex)
11
	{
12
		global $cnt;
13
		global $fh;
14
		global $fhVariations;
15
		global $fhAlias;
16
		global $fhGroups;
17
		global $fhMembers;
18
		global $fhUrls;
19
		global $fhProfiles;
20
 
21
		$xml = simplexml_load_string($xmlString, 'simple_xml_extended');
22
 
23
		if ($elementName == 'artist') {
24
			++$cnt;
25
 
26
			$id = $xml->id;
27
			$name = cleanName($xml->name);
28
			$realname = $xml->realname;
29
 
30
			if (isset($xml->profile) && !empty($xml->profile)) {
31
				$profile = str_replace("\r", '', $xml->profile);
32
				$profile = rtrim($profile);
44 - 33
				if (!empty($profile)) {
34
					fputcsv2($fhProfiles, array($id, $profile), ',', '"');
35
				}
18 - 36
			}
37
 
38
			if (isset($xml->namevariations)) {
39
				foreach ($xml->namevariations->name as $n) {
44 - 40
					$cn = cleanName($n);
41
					if (!empty($cn)) {
42
						fputcsv2($fhVariations, array($id, $cn), ',', '"');
43
					}
18 - 44
				}
45
			}
46
 
47
			if (isset($xml->aliases)) {
48
				foreach ($xml->aliases->name as $n) {
44 - 49
					$cn = cleanName($n);
50
					if (!empty($cn)) {
51
						fputcsv2($fhAlias, array($id, $cn), ',', '"');
52
					}
18 - 53
				}
54
			}
55
 
56
			if (isset($xml->members)) {
57
				foreach ($xml->members as $n) {
44 - 58
					if (!empty($n->id)) {
59
						fputcsv2($fhMembers, array($id, $n->id), ',', '"');
60
					}
18 - 61
				}
62
			}
63
 
64
			if (isset($xml->groups)) {
65
				foreach ($xml->groups->name as $n) {
44 - 66
					$cn = cleanName($n);
67
					if (!empty($cn)) {
68
						fputcsv2($fhGroups, array($id, $cn), ',', '"');
69
					}
18 - 70
				}
71
			}
72
 
73
			if (isset($xml->urls)) {
74
				foreach ($xml->urls->url as $n) {
44 - 75
					if (!empty($n)) {
76
						fputcsv2($fhUrls, array($id, $n), ',', '"');
77
					}
18 - 78
				}
79
			}
80
 
81
			$arr = array(
82
				$id,
83
				$name,
84
				$realname
85
			);
86
 
44 - 87
			fputcsv2($fh, $arr, ',', '"');
18 - 88
		}
89
 
90
		return true;
91
	}
92
}
93
 
44 - 94
$xmlfile = "./in/discogs_20190701_artists.xml.gz";
18 - 95
 
96
$fh = fopen("out/artists.load", "w+");
97
$fhVariations = fopen("out/artistVariations.load", "w+");
98
$fhAlias = fopen("out/artistAliases.load", "w+");
99
$fhGroups = fopen("out/artistGroups.load", "w+");
100
$fhMembers = fopen("out/artistMembers.load", "w+");
101
$fhUrls = fopen("out/artistUrls.load", "w+");
102
$fhProfiles = fopen("out/artistProfiles.load", "w+");
103
 
104
$xmlstream = "compress.zlib://$xmlfile";
105
$xmlfileSize = gzfilesize($xmlfile);
106
 
107
$streamer = new ArtistsXmlStreamer($xmlstream, $xmlfileSize);
108
if ($streamer->parse()) {
109
	echo "Finished $cnt artists." . PHP_EOL;
110
} else {
111
	echo "Couldn't find root node" . PHP_EOL;
112
}
113
 
114
fclose($fh);
115
fclose($fhVariations);
116
fclose($fhAlias);
117
fclose($fhGroups);
118
fclose($fhMembers);
119
fclose($fhUrls);
120
fclose($fhProfiles);