PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/extensions.ext/wrapper/discogs/DiscogsClient.php

https://code.google.com/p/ontowiki/
PHP | 482 lines | 373 code | 72 blank | 37 comment | 29 complexity | 1e3a2c5b98770c007077c4605d7cd946 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. require_once 'Zend/Http/Client.php';
  3. require_once 'Erfurt/Wrapper/Exception.php';
  4. require_once 'extensions/wrapper/iClient.php';
  5. /**
  6. * @category OntoWiki
  7. * @package OntoWiki_extensions_wrapper
  8. */
  9. class DiscogsClient extends Zend_Http_Client implements iClient
  10. {
  11. private $_apiKey;
  12. public function __construct($uri, $api_key)
  13. {
  14. if (!$api_key) {
  15. throw new Erfurt_Wrapper_Exception('No API Key specified!');
  16. }
  17. $this->_apiKey = $api_key;
  18. parent::__construct($uri);
  19. }
  20. public function setUri($uri)
  21. {
  22. parent::setUri(str_replace(' ', '+', $uri));
  23. }
  24. public function get($uri, $raw_data = null)
  25. {
  26. if (strpos($uri, 'artist')) {
  27. if ($raw_data) {
  28. return $this->parseArtist($uri, $raw_data);
  29. } else {
  30. return $this->getArtist($uri);
  31. }
  32. } elseif (strpos($uri, 'release')) {
  33. if ($raw_data) {
  34. return $this->parseRelease($uri, $raw_data);
  35. } else {
  36. return $this->getRelease($uri);
  37. }
  38. } elseif (strpos($uri, 'label')) {
  39. if ($raw_data) {
  40. return $this->parseLabel($uri, $raw_data);
  41. } else {
  42. return $this->getLabel($uri);
  43. }
  44. } else {
  45. $msg = 'Requested object type not available (' . $uri . ')!';
  46. throw new Erfurt_Wrapper_Exception($msg);
  47. }
  48. }
  49. public function request($method = null)
  50. {
  51. $this->resetParameters();
  52. $this->setParameterGet(
  53. array(
  54. 'f' => 'xml',
  55. 'api_key' => $this->_apiKey
  56. )
  57. );
  58. return parent::request($method);
  59. }
  60. public function getArtist($uri)
  61. {
  62. $this->setUri($uri);
  63. return $this->request();
  64. }
  65. public function getRelease($uri)
  66. {
  67. $this->setUri($uri);
  68. return $this->request();
  69. }
  70. public function getLabel($uri)
  71. {
  72. $this->setUri($uri);
  73. return $this->request();
  74. }
  75. public function getTrack($uri)
  76. {
  77. }
  78. // subfunction for parsing single artist (subclass of foaf:Agent)
  79. private function _parseMusicArtist($uri, $raw_data, $result)
  80. {
  81. // rdf:type
  82. $result[$uri]['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'] = array();
  83. $result[$uri]['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'][] = array(
  84. 'type' => 'uri',
  85. 'value' => 'http://purl.org/ontology/mo/MusicArtist'
  86. );
  87. // foaf:name
  88. $result[$uri]['http://xmlns.com/foaf/0.1/name'] = array();
  89. $result[$uri]['http://xmlns.com/foaf/0.1/name'][]= array(
  90. 'type' => 'literal',
  91. 'value' => str_replace(
  92. array('%2B', '%2b', 'http://discogs.com/artist/'),
  93. array(' ', ' ', ''),
  94. $uri
  95. )
  96. );
  97. // foaf:givenname
  98. // foaf:surname
  99. // mo:member_of
  100. $pattern = '|<groups>(.*)</groups>|s';
  101. preg_match($pattern, $raw_data, $match);
  102. if ($match) {
  103. $pattern = '|<name>([^>]*)</name>|s';
  104. preg_match_all($pattern, $match[1], $match);
  105. if ($match) {
  106. $result[$uri]['http://purl.org/ontology/mo/member_of'] = array();
  107. foreach ($match[1] as $name) {
  108. $result[$uri]['http://purl.org/ontology/mo/member_of'][]= array(
  109. 'type' => 'uri',
  110. 'value' => 'http://discogs.com/artist/' . $name
  111. );
  112. }
  113. }
  114. }
  115. return $result;
  116. }
  117. // subfunction for parsing music group (subclass of MusicArtist)
  118. private function _parseMusicGroup($uri, $raw_data, $result)
  119. {
  120. // rdf:type
  121. $result[$uri]['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'] = array();
  122. $result[$uri]['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'][] = array(
  123. 'type' => 'uri',
  124. 'value' => 'http://purl.org/ontology/mo/MusicGroup'
  125. );
  126. // foaf:name
  127. $result[$uri]['http://xmlns.com/foaf/0.1/name'] = array();
  128. $result[$uri]['http://xmlns.com/foaf/0.1/name'][]= array(
  129. 'type' => 'literal',
  130. 'value' => str_replace(
  131. array('%2B', '%2b', 'http://discogs.com/artist/'),
  132. array(' ', ' ', ''),
  133. $uri
  134. )
  135. );
  136. // mo:member
  137. $pattern = '|<members>(.*)</members>|s';
  138. preg_match($pattern, $raw_data, $match);
  139. if ($match) {
  140. $pattern = '|<name>([^>]*)</name>|s';
  141. preg_match_all($pattern, $match[1], $match);
  142. if ($match) {
  143. $result[$uri]['http://purl.org/ontology/mo/member'] = array();
  144. foreach ($match[1] as $name) {
  145. $result[$uri]['http://purl.org/ontology/mo/member'][]= array(
  146. 'type' => 'uri',
  147. 'value' => 'http://discogs.com/artist/' . $name
  148. );
  149. }
  150. }
  151. }
  152. return $result;
  153. }
  154. public function parseArtist($uri, $raw_data)
  155. {
  156. $result[$uri] = array();
  157. // mo:discogs
  158. $result[$uri]['http://purl.org/ontology/mo/discogs'] = array();
  159. $result[$uri]['http://purl.org/ontology/mo/discogs'][]= array(
  160. 'type' => 'literal',
  161. 'value' => str_replace('http://discogs', 'http://www.discogs', $uri)
  162. );
  163. // mo:myspace
  164. // foaf:weblog
  165. // foaf:homepage
  166. // foaf:holdsAccount
  167. $pattern = '|<url>([^<]+)</url>|s';
  168. preg_match_all($pattern, $raw_data, $match);
  169. if ($match) {
  170. $result[$uri]['http://xmlns.com/foaf/0.1/weblog'] = array();
  171. $result[$uri]['http://purl.org/ontology/mo/myspace'] = array();
  172. $result[$uri]['http://xmlns.com/foaf/0.1/holdsAccount'] = array();
  173. $result[$uri]['http://xmlns.com/foaf/0.1/homepage'] = array();
  174. foreach ($match[1] as $url) {
  175. if (strpos($url, 'blogspot')) {
  176. $result[$uri]['http://xmlns.com/foaf/0.1/weblog'][] = array(
  177. 'type' => 'literal',
  178. 'value' => $url
  179. );
  180. } elseif (strpos($url, 'myspace')) {
  181. $result[$uri]['http://purl.org/ontology/mo/myspace'][] = array(
  182. 'type' => 'literal',
  183. 'value' => $url
  184. );
  185. } elseif (strpos($url, 'twitter')) {
  186. $result[$uri]['http://xmlns.com/foaf/0.1/holdsAccount'][] = array(
  187. 'type' => 'uri',
  188. 'value' => $url
  189. );
  190. } else {
  191. $result[$uri]['http://xmlns.com/foaf/0.1/homepage'][] = array(
  192. 'type' => 'literal',
  193. 'value' => $url
  194. );
  195. }
  196. }
  197. }
  198. // MusicGroup or MusicArtist?
  199. $pattern = '|<members>(.*)</members>|s';
  200. preg_match($pattern, $raw_data, $match);
  201. if ($match) {
  202. $result = $this->_parseMusicGroup($uri, $raw_data, $result);
  203. } else {
  204. $result = $this->_parseMusicArtist($uri, $raw_data, $result);
  205. }
  206. // mo:made
  207. $pattern = '|<release id="([^"]+)"|s';
  208. preg_match_all($pattern, $raw_data, $match);
  209. if ($match) {
  210. $result[$uri]['http://xmlns.com/foaf/0.1/made'] = array();
  211. foreach ($match[1] as $rel_id) {
  212. $result[$uri]['http://xmlns.com/foaf/0.1/made'][]= array(
  213. 'type' => 'uri',
  214. 'value' => 'http://discogs.com/release/' . $rel_id
  215. );
  216. }
  217. }
  218. // mo:image
  219. $pattern = '|<image.+uri="([^"]+)"|';
  220. preg_match($pattern, $raw_data, $match);
  221. if ($match) {
  222. $result[$uri]['http://purl.org/ontology/mo/image'] = array();
  223. $result[$uri]['http://purl.org/ontology/mo/image'][] = array(
  224. 'type' => 'uri',
  225. 'value' => $match[1]
  226. );
  227. }
  228. return $result;
  229. }
  230. public function parseRelease($uri, $raw_data)
  231. {
  232. $result[$uri] = array();
  233. // rdf:type
  234. $result[$uri]['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'] = array();
  235. $result[$uri]['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'][] = array(
  236. 'type' => 'uri',
  237. 'value' => 'http://purl.org/ontology/mo/MusicalManifestation'
  238. );
  239. $va = false;
  240. $doc = new DOMDocument();
  241. $doc->loadXml($raw_data);
  242. $xpath = new DOMXPath($doc);
  243. // dc:title
  244. $nl = $xpath->query('/resp/release/title');
  245. $result[$uri]['http://purl.org/dc/elements/1.1/title'] = array();
  246. $result[$uri]['http://purl.org/dc/elements/1.1/title'][]= array(
  247. 'type' => 'literal',
  248. 'value' => $nl->item(0)->nodeValue
  249. );
  250. // foaf:maker
  251. $nl = $xpath->query('/resp/release/artists/artist/name');
  252. foreach ($nl as $node) {
  253. if ($node->nodeValue != 'Various') {
  254. $result[$uri]['http://xmlns.com/foaf/0.1/maker'] = array();
  255. $result[$uri]['http://xmlns.com/foaf/0.1/maker'][]= array(
  256. 'type' => 'uri',
  257. 'value' => 'http://discogs.com/artist/' . $node->nodeValue
  258. );
  259. } else {
  260. $va = true;
  261. }
  262. }
  263. // mo:catalogue_number
  264. $result[$uri]['http://purl.org/ontology/mo/catalogue_number'] = array();
  265. $nl = $xpath->query('/resp/release/labels/label/@catno');
  266. foreach ($nl as $node) {
  267. $result[$uri]['http://purl.org/ontology/mo/catalogue_number'][]= array(
  268. 'type' => 'literal',
  269. 'value' => $node->nodeValue
  270. );
  271. }
  272. // mo:label
  273. $result[$uri]['http://purl.org/ontology/mo/label'] = array();
  274. $nl = $xpath->query('/resp/release/labels/label/@name');
  275. foreach ($nl as $node) {
  276. $result[$uri]['http://purl.org/ontology/mo/label'][]= array(
  277. 'type' => 'uri',
  278. 'value' => 'http://discogs.com/label/' . $node->nodeValue
  279. );
  280. }
  281. // mo:release_type
  282. $result[$uri]['http://purl.org/ontology/mo/release_type'] = array();
  283. $nl = $xpath->query('/resp/release/formats/format/descriptions/description');
  284. foreach ($nl as $node) {
  285. $result[$uri]['http://purl.org/ontology/mo/release_type'][] = array(
  286. 'type' => 'literal',
  287. 'value' => $node->nodeValue
  288. );
  289. }
  290. // mo:image
  291. $result[$uri]['http://purl.org/ontology/mo/image'] = array();
  292. $nl = $xpath->query('/resp/release/images/image/@uri');
  293. foreach ($nl as $node) {
  294. $result[$uri]['http://purl.org/ontology/mo/image'][] = array(
  295. 'type' => 'uri',
  296. 'value' => $node->nodeValue
  297. );
  298. }
  299. // mo:track
  300. $result[$uri]['http://purl.org/ontology/mo/track'] = array();
  301. $nl = $xpath->query('/resp/release/tracklist/track');
  302. foreach ($nl as $node) {
  303. $track_pos = $xpath->query('position', $node)->item(0)->nodeValue;
  304. $track_title = $xpath->query('title', $node)->item(0)->nodeValue;
  305. $track_maker = $xpath->query('artists/artist/name', $node);
  306. $track_uri = $uri . '/' . $track_title;
  307. $result[$uri]['http://purl.org/ontology/mo/track'][] = array(
  308. 'type' => 'uri',
  309. 'value' => $track_uri
  310. );
  311. $result[$track_uri]['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'] = array();
  312. $result[$track_uri]['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'][] = array(
  313. 'type' => 'uri',
  314. 'value' => 'http://purl.org/ontology/mo/Track'
  315. );
  316. $result[$track_uri]['http://purl.org/dc/elements/1.1/title'] = array();
  317. $result[$track_uri]['http://purl.org/dc/elements/1.1/title'][] = array(
  318. 'type' => 'literal',
  319. 'value' => $track_title
  320. );
  321. $result[$track_uri]['http://purl.org/ontology/mo/track_number'] = array();
  322. $result[$track_uri]['http://purl.org/ontology/mo/track_number'][] = array(
  323. 'type' => 'literal',
  324. 'value' => $track_pos
  325. );
  326. if ($va) {
  327. $result[$track_uri]['http://xmlns.com/foaf/0.1/maker'] = array();
  328. foreach ($track_maker as $artist) {
  329. $result[$track_uri]['http://xmlns.com/foaf/0.1/maker'][]= array(
  330. 'type' => 'uri',
  331. 'value' => 'http://discogs.com/artist/' . $artist->nodeValue
  332. );
  333. }
  334. }
  335. }
  336. return $result;
  337. }
  338. public function parseLabel($uri, $raw_data)
  339. {
  340. $result[$uri] = array();
  341. // rdf:type
  342. $result[$uri]['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'] = array();
  343. $result[$uri]['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'][] = array(
  344. 'type' => 'uri',
  345. 'value' => 'http://xmlns.com/foaf/0.1/Organization'
  346. );
  347. // foaf:name
  348. $pattern = '|<name>([^<]+)</name>|s';
  349. preg_match($pattern, $raw_data, $match);
  350. if ($match) {
  351. $result[$uri]['http://xmlns.com/foaf/0.1/name'] = array();
  352. $result[$uri]['http://xmlns.com/foaf/0.1/name'][] = array(
  353. 'type' => 'literal',
  354. 'value' => $match[1]
  355. );
  356. }
  357. // mo:myspace
  358. // foaf:weblog
  359. // foaf:homepage
  360. // foaf:holdsAccount
  361. $pattern = '|<url>([^<]+)</url>|s';
  362. preg_match_all($pattern, $raw_data, $match);
  363. if ($match) {
  364. $result[$uri]['http://xmlns.com/foaf/0.1/weblog'] = array();
  365. $result[$uri]['http://purl.org/ontology/mo/myspace'] = array();
  366. $result[$uri]['http://xmlns.com/foaf/0.1/holdsAccount'] = array();
  367. $result[$uri]['http://xmlns.com/foaf/0.1/homepage'] = array();
  368. foreach ($match[1] as $url) {
  369. if (strpos($url, 'blogspot')) {
  370. $result[$uri]['http://xmlns.com/foaf/0.1/weblog'][] = array(
  371. 'type' => 'literal',
  372. 'value' => $url
  373. );
  374. } elseif (strpos($url, 'myspace')) {
  375. $result[$uri]['http://purl.org/ontology/mo/myspace'][] = array(
  376. 'type' => 'literal',
  377. 'value' => $url
  378. );
  379. } elseif (strpos($url, 'twitter')) {
  380. $result[$uri]['http://xmlns.com/foaf/0.1/holdsAccount'][] = array(
  381. 'type' => 'uri',
  382. 'value' => $url
  383. );
  384. } else {
  385. $result[$uri]['http://xmlns.com/foaf/0.1/homepage'][] = array(
  386. 'type' => 'literal',
  387. 'value' => $url
  388. );
  389. }
  390. }
  391. }
  392. // mo:is_label_of
  393. $pattern = '|<release id="([^"]+)"|s';
  394. preg_match_all($pattern, $raw_data, $match);
  395. if ($match) {
  396. $result[$uri]['http://purl.org/ontology/mo/is_label_of'] = array();
  397. foreach ($match[1] as $rel_id) {
  398. $result[$uri]['http://purl.org/ontology/mo/is_label_of'][]= array(
  399. 'type' => 'uri',
  400. 'value' => 'http://discogs.com/release/' . $rel_id
  401. );
  402. }
  403. }
  404. return $result;
  405. }
  406. public function parseTrack($uri, $raw_data)
  407. {
  408. }
  409. }
  410. ?>