SpdxLicensesUpdater.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. use Composer\Json\JsonFormatter;
  13. /**
  14. * The SPDX Licenses Updater scrapes licenses from the spdx website
  15. * and updates the "res/spdx-licenses.json" file accordingly.
  16. *
  17. * The class is used by the update script "bin/update-spdx-licenses".
  18. */
  19. class SpdxLicensesUpdater
  20. {
  21. private $licensesUrl = 'http://www.spdx.org/licenses/';
  22. public function update()
  23. {
  24. $json = json_encode($this->getLicenses(), true);
  25. $prettyJson = JsonFormatter::format($json, true, true);
  26. file_put_contents(__DIR__ . '/../../../res/spdx-licenses.json', $prettyJson);
  27. }
  28. private function getLicenses()
  29. {
  30. $licenses = array();
  31. $dom = new \DOMDocument;
  32. $dom->loadHTMLFile($this->licensesUrl);
  33. $xPath = new \DOMXPath($dom);
  34. $trs = $xPath->query('//table//tbody//tr');
  35. // iterate over each row in the table
  36. foreach ($trs as $tr) {
  37. $tds = $tr->getElementsByTagName('td'); // get the columns in this row
  38. if ($tds->length < 4) {
  39. throw new \Exception('Obtaining the license table failed. Wrong table format. Found less than 4 cells in a row.');
  40. }
  41. if (trim($tds->item(3)->nodeValue) == 'License Text') {
  42. $fullname = trim($tds->item(0)->nodeValue);
  43. $identifier = trim($tds->item(1)->nodeValue);
  44. $osiApproved = ((isset($tds->item(2)->nodeValue) && $tds->item(2)->nodeValue === 'Y')) ? true : false;
  45. // The license URL is not scraped intentionally to keep json file size low.
  46. // It's build when requested, see SpdxLicense->getLicenseByIdentifier().
  47. //$licenseURL = = $tds->item(3)->getAttribute('href');
  48. $licenses += array($identifier => array($fullname, $osiApproved));
  49. }
  50. }
  51. return $licenses;
  52. }
  53. }