24 'licenses' => array(
'licenseId',
'licenseText',
'name'),
25 'exceptions' => array(
'licenseExceptionId',
'licenseExceptionText',
'name')
29 function startProcessingLicenseData()
35 $deleteDeprecated =
false;
36 $newLicenseRefData = array();
39 'licenses' =>
'https://spdx.org/licenses/licenses.json',
40 'exceptions' =>
'https://spdx.org/licenses/exceptions.json'
42 $usage =
"Usage: " . basename($argv[0]) .
" [options]
44 Create new licenseref.json file. Options are:
45 -E Update all existing licenses and also add new licenses.
46 (NOTE: there may be failure of test cases)
48 -e Only update existing licenses.
49 (NOTE: there may be failure of test cases)
51 -n Only add new licenses.
53 -d Delete deprecated licenses.
55 --type Usually licenses/exceptions (optional)
56 (ex: --type 'licenses')
58 --url From where you want to download (optional)
59 (ex: --url 'https://spdx.org/licenses/licenses.json')
62 (if --type and --url is empty then the script will automatically download the from below)
63 For type 'licenses' URL is : $scanList[licenses]
65 For type 'exceptions' URL is : $scanList[exceptions]";
67 $options = getopt(
"hcEend", array(
"type:",
"url:"));
69 $type = array_key_exists(
"type", $options) ? $options[
"type"] :
'';
70 $URL = array_key_exists(
"url", $options) ? $options[
"url"] :
'';
71 foreach ($options as $option => $optVal) {
76 $updateWithNew = $option;
79 $updateExisting = $option;
82 $addNewLicense = $option;
85 $deleteDeprecated =
true;
98 if (!empty($updateWithNew) || !empty($updateExisting) || !empty($addNewLicense)) {
99 if (!empty($type) && !empty($URL)) {
100 $newLicenseRefData = $this->
getListSPDX($type, $URL, $updateWithNew, $updateExisting, $addNewLicense,
101 $newLicenseRefData, $deleteDeprecated);
102 }
else if (!empty($type) && empty($URL)) {
103 echo
"Notice: --url cannot be empty if --type is provided \n";
104 }
else if (empty($type) && !empty($URL)) {
105 echo
"Notice: --type cannot be empty if --url is provided \n";
107 foreach ($scanList as $type => $URL) {
108 $newLicenseRefData = $this->
getListSPDX($type, $URL, $updateWithNew, $updateExisting, $addNewLicense,
109 $newLicenseRefData, $deleteDeprecated);
112 $newFileName =
"licenseRefNew.json";
113 if (file_exists($newFileName)) {
114 unlink($newFileName);
117 $file = fopen($newFileName,
'w+');
118 file_put_contents($newFileName, json_encode($newLicenseRefData, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES));
120 echo
"\n\n INFO: new $newFileName file created \n\n";
122 echo
"\nINVALID OPTION PROVIDED\n\n";
138 function getListSPDX($type, $URL, $updateWithNew, $updateExisting, $addNewLicense, $existingLicenseRefData,
143 if (!is_dir($LIBEXECDIR)) {
144 print
"FATAL: Directory '$LIBEXECDIR' does not exist.\n";
148 $dir = opendir($LIBEXECDIR);
150 print
"FATAL: Unable to access '$LIBEXECDIR'.\n";
154 $fileName =
"$LIBEXECDIR/licenseRef.json";
155 if (!file_exists($fileName)) {
156 print
"FATAL: File '$fileName' does not exist.\n";
160 if (empty($existingLicenseRefData)) {
161 echo
"INFO: get existing licenseRef.json from $LIBEXECDIR\n";
162 $getExistingLicenseRefData = file_get_contents(
"$fileName");
164 $existingLicenseRefData = (array) json_decode($getExistingLicenseRefData,
true);
167 $getList = json_decode(file_get_contents($URL));
168 foreach ($getList->$type as $listValue) {
170 if (strstr($URL,
"spdx.org") !==
false) {
172 if (substr_compare($listValue->detailsUrl,
".html", -5) === 0) {
173 $baseUrl = str_replace(
"exceptions.json",
"", $URL);
174 $listValue->detailsUrl = $baseUrl . str_replace(
"./",
"", $listValue->reference);
177 $getCurrentData = file_get_contents($listValue->detailsUrl);
178 $getCurrentData = (array) json_decode($getCurrentData,
true);
179 echo
"INFO: search for license " . $getCurrentData[$this->mapArrayData[$type][0]] .
"\n";
181 $licenseIdCheck = array_search($getCurrentData[$this->mapArrayData[$type][0]],
182 array_column($existingLicenseRefData,
'rf_shortname'));
183 $currentText = $this->
replaceUnicode($getCurrentData[$this->mapArrayData[$type][1]]);
184 $textCheck = array_search($currentText, array_column($existingLicenseRefData,
'rf_text'));
185 if ($deleteDeprecated && $listValue->isDeprecatedLicenseId && (
186 is_numeric($licenseIdCheck) &&
187 (!empty($updateWithNew) || !empty($updateExisting)))) {
189 echo
"INFO: removing deprecated license " .
190 $getCurrentData[$this->mapArrayData[$type][0]] .
"\n";
191 unset($existingLicenseRefData[$licenseIdCheck]);
192 $existingLicenseRefData = array_values($existingLicenseRefData);
194 } elseif ($listValue->isDeprecatedLicenseId) {
197 if (is_numeric($licenseIdCheck) &&
198 (!empty($updateWithNew) || !empty($updateExisting))) {
200 if (array_key_exists(
'rf_spdx_compatible',
201 $existingLicenseRefData[$licenseIdCheck])) {
202 unset($existingLicenseRefData[$licenseIdCheck][
'rf_spdx_compatible']);
206 is_numeric($licenseIdCheck) &&
207 !is_numeric($textCheck) &&
208 (!empty($updateWithNew) ||
209 !empty($updateExisting)
212 $existingLicenseRefData[$licenseIdCheck][
'rf_fullname'] = $getCurrentData[$this->mapArrayData[$type][2]];
213 $existingLicenseRefData[$licenseIdCheck][
'rf_text'] = $getCurrentData[$this->mapArrayData[$type][1]];
214 $existingLicenseRefData[$licenseIdCheck][
'rf_url'] = $getCurrentData[
'seeAlso'][0];
215 $existingLicenseRefData[$licenseIdCheck][
'rf_notes'] = (array_key_exists(
"licenseComments", $getCurrentData) ? $getCurrentData[
'licenseComments'] : $existingLicenseRefData[$licenseIdCheck][
'rf_notes']);
216 echo
"INFO: license " . $getCurrentData[$this->mapArrayData[$type][0]] .
" updated\n\n";
219 !is_numeric($licenseIdCheck) &&
220 !is_numeric($textCheck) &&
221 (!empty($updateWithNew) ||
222 !empty($addNewLicense)
225 $existingLicenseRefData[] = array(
226 'rf_shortname' => $getCurrentData[$this->mapArrayData[$type][0]],
227 'rf_text' => $getCurrentData[$this->mapArrayData[$type][1]],
228 'rf_url' => $getCurrentData[
'seeAlso'][0],
229 'rf_add_date' =>
null,
230 'rf_copyleft' =>
null,
231 'rf_OSIapproved' =>
null,
232 'rf_fullname' => $getCurrentData[$this->mapArrayData[$type][2]],
233 'rf_FSFfree' =>
null,
234 'rf_GPLv2compatible' =>
null,
235 'rf_GPLv3compatible' =>
null,
236 'rf_notes' => (array_key_exists(
"licenseComments", $getCurrentData) ? $getCurrentData[
'licenseComments'] :
null),
240 'rf_text_updatable' =>
"f",
241 'rf_detector_type' => 1,
244 'rf_spdx_compatible' => $listValue->isDeprecatedLicenseId ==
false,
247 echo
"INFO: new license " . $getCurrentData[$this->mapArrayData[$type][0]] .
" added\n\n";
250 return array_values($existingLicenseRefData);
261 if ($text ===
null) {
284 return StringOperation::replaceUnicodeControlChar(str_replace($search,
295 for ($i = 0; $i < count($newLicenseRefData); $i++) {
296 $newLicenseRefData[$i][
"rf_fullname"] = $this->
replaceUnicode($newLicenseRefData[$i][
"rf_fullname"]);
297 $newLicenseRefData[$i][
"rf_text"] = $this->
replaceUnicode($newLicenseRefData[$i][
"rf_text"]);
298 $newLicenseRefData[$i][
"rf_notes"] = $this->
replaceUnicode($newLicenseRefData[$i][
"rf_notes"]);
303 $obj->startProcessingLicenseData();
sanitizeRefData(&$newLicenseRefData)
getListSPDX($type, $URL, $updateWithNew, $updateExisting, $addNewLicense, $existingLicenseRefData, $deleteDeprecated)
get SPDX license or exception list and update licenseref.json