You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.5 KiB

  1. #!/usr/bin/php
  2. <?php
  3. $tag2link = array();
  4. $files = array('data/tag2link-wikidata.json', 'data/tag2link-sophox.json');
  5. foreach ($files as $file) {
  6. $data = json_decode(file_get_contents($file), true);
  7. foreach ($data['results']['bindings'] as $entry) {
  8. $key = substr($entry['OSM_key']['value'], 4);
  9. $link = $entry['formatter_URL']['value'];
  10. if (array_key_exists($key, $tag2link)) {
  11. // avoid duplicates
  12. $duplicates = array_filter($tag2link[$key]['formatter'], function ($e) use ($link) {
  13. return $e['link'] === $link;
  14. });
  15. if (sizeof($duplicates)) {
  16. if (array_key_exists('operatorLabel', $entry)) {
  17. foreach ($duplicates as $i => $d) {
  18. $tag2link[$key]['formatter'][$i]['operator'] = $entry['operatorLabel']['value'];
  19. }
  20. }
  21. continue;
  22. }
  23. }
  24. else {
  25. $tag2link[$key] = array(
  26. 'label' => $entry['itemLabel']['value'],
  27. 'formatter' => array(),
  28. );
  29. }
  30. $formatter = array(
  31. 'link' => $link,
  32. );
  33. if (array_key_exists('operatorLabel', $entry)) {
  34. $formatter['operator'] = $entry['operatorLabel']['value'];
  35. print "{$formatter['operator']}\n";
  36. }
  37. else if (preg_match("/^https?:\/\/([^\/]*)(\/.*|)$/", $link, $m)) {
  38. $formatter['operator'] = $m[1];
  39. }
  40. $tag2link[$key]['formatter'][] = $formatter;
  41. }
  42. }
  43. file_put_contents('dist/tag2link.json', json_encode($tag2link, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));