SpdxLicensesUpdater.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Util;
  12. /**
  13. * The SPDX Licenses Updater scrapes licenses from the spdx website
  14. * and updates the "res/spdx-licenses.json" file accordingly.
  15. *
  16. * The class is used by the update script "bin/update-spdx-licenses".
  17. */
  18. class SpdxLicensesUpdater
  19. {
  20. /**
  21. * @param string $file
  22. * @param string $url
  23. */
  24. public function dumpLicenses($file, $url = 'http://www.spdx.org/licenses/')
  25. {
  26. $options = 0;
  27. if (defined('JSON_PRETTY_PRINT')) {
  28. $options |= JSON_PRETTY_PRINT;
  29. }
  30. if (defined('JSON_UNESCAPED_SLASHES')) {
  31. $options |= JSON_UNESCAPED_SLASHES;
  32. }
  33. $licenses = json_encode($this->getLicenses($url), $options);
  34. file_put_contents($file, $licenses);
  35. }
  36. /**
  37. * @param string $file
  38. * @param string $url
  39. */
  40. public function dumpExceptions($file, $url = 'http://www.spdx.org/licenses/exceptions-index.html')
  41. {
  42. $options = 0;
  43. if (defined('JSON_PRETTY_PRINT')) {
  44. $options |= JSON_PRETTY_PRINT;
  45. }
  46. if (defined('JSON_UNESCAPED_SLASHES')) {
  47. $options |= JSON_UNESCAPED_SLASHES;
  48. }
  49. $exceptions = json_encode($this->getExceptions($url), $options);
  50. file_put_contents($file, $exceptions);
  51. }
  52. /**
  53. * @param string $url
  54. *
  55. * @return array
  56. */
  57. private function getLicenses($url)
  58. {
  59. $licenses = array();
  60. $dom = new \DOMDocument;
  61. @$dom->loadHTMLFile($url);
  62. $xPath = new \DOMXPath($dom);
  63. $trs = $xPath->query('//table//tbody//tr');
  64. // iterate over each row in the table
  65. foreach ($trs as $tr) {
  66. $tds = $tr->getElementsByTagName('td'); // get the columns in this row
  67. if ($tds->length !== 4) {
  68. continue;
  69. }
  70. if (trim($tds->item(3)->nodeValue) == 'License Text') {
  71. $fullname = trim($tds->item(0)->nodeValue);
  72. $identifier = trim($tds->item(1)->nodeValue);
  73. $osiApproved = ((isset($tds->item(2)->nodeValue) && $tds->item(2)->nodeValue === 'Y')) ? true : false;
  74. // The license URL is not scraped intentionally to keep json file size low.
  75. // It's build when requested, see SpdxLicense->getLicenseByIdentifier().
  76. //$licenseURL = $tds->item(3)->getAttribute('href');
  77. $licenses += array($identifier => array($fullname, $osiApproved));
  78. }
  79. }
  80. return $licenses;
  81. }
  82. /**
  83. * @param string $url
  84. *
  85. * @return array
  86. */
  87. private function getExceptions($url)
  88. {
  89. $exceptions = array();
  90. $dom = new \DOMDocument;
  91. @$dom->loadHTMLFile($url);
  92. $xPath = new \DOMXPath($dom);
  93. $trs = $xPath->query('//table//tbody//tr');
  94. // iterate over each row in the table
  95. foreach ($trs as $tr) {
  96. $tds = $tr->getElementsByTagName('td'); // get the columns in this row
  97. if ($tds->length !== 3) {
  98. continue;
  99. }
  100. if (trim($tds->item(2)->nodeValue) == 'License Exception Text') {
  101. $fullname = trim($tds->item(0)->nodeValue);
  102. $identifier = trim($tds->item(1)->nodeValue);
  103. // The license URL is not scraped intentionally to keep json file size low.
  104. // It's build when requested, see SpdxLicense->getLicenseExceptionByIdentifier().
  105. //$licenseURL = $tds->item(2)->getAttribute('href');
  106. $exceptions += array($identifier => array($fullname));
  107. }
  108. }
  109. return $exceptions;
  110. }
  111. }