blob: aa0f55c0abf8736667a7434dcb9d99ed4d7b06bc [file] [log] [blame]
<?php
/*******************************************************************************
* Copyright (c) 2017 Eclipse Foundation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************/
require_once (dirname(__FILE__) . "/database.inc");
class License {
var $id;
var $text;
var $url;
private function __construct($id, $text, $url) {
$this->id = $id;
$this->text = $text;
$this->url = $url;
}
public static function getLicense($id) {
// FIXME This should be in the (a) database.
switch ($id) {
case 'EPL2.0':
case 'EPL-2.0':
return new License('EPL-2.0', 'Eclipse Public License v. 2.0', 'http://www.eclipse.org/legal/epl-2.0');
case 'EPL1.0':
case 'EPL-1.0':
return new License('EPL-1.0', 'Eclipse Public License v. 1.0', 'http://www.eclipse.org/legal/epl-v10.html');
case 'EDL1.0':
case 'EDL-1.0':
case 'EDL':
return new License('LicenseRef-EDL-1.0', 'Eclipse Distribution License v. 1.0', 'http://www.eclipse.org/org/documents/edl-v10.php');
case 'ASL2.0':
case 'Apache-2.0':
return new License('Apache-2.0', 'Apache License, Version 2.0', 'https://www.apache.org/licenses/LICENSE-2.0');
case 'CCBY3':
case 'CC-BY-3.0':
return new License('CC-BY-3.0', 'Creative Commons Attribution 3.0 Unported', 'https://creativecommons.org/licenses/by/4.0/');
case 'GPL-2.0':
return new License('GPL-2.0', 'GNU General Public License, version 2', 'https://www.gnu.org/copyleft/gpl.html');
case 'GPL-2.0_CP':
return new License('GPL-2.0 WITH Classpath-exception-2.0', 'GNU General Public License, version 2 with the GNU Classpath Exception', 'https://www.gnu.org/software/classpath/license.html');
case 'GPL-2.0_AE':
return new License('LicenseRef-GPL-2.0-with-Assembly-exception', 'GNU General Public License, version 2 with OpenJDK Assembly Exception', 'http://openjdk.java.net/legal/assembly-exception.html');
default:
return null;
}
}
public static function getLicenses($codes) {
$licenses = array();
foreach($codes as $code) {
$licenses[] = self::getLicense($code);
}
return $licenses;
}
/**
* Return an array of licenses for a project.
*
* @param string $id Project Id
* @return License[]
*/
public static function getLicensesForProject($id) {
$sql = "
select
l.LicenseId as id,
l.description as text
from ProjectLicenses as pl
join SYS_Licenses as l on pl.LicenseId=l.LicenseId
where pl.ProjectId='$id'";
$licenses = array ();
query ( 'foundation', $sql, array (), function ($row) use (&$licenses) {
$licenses [] = self::getLicense($row ['id']);
} );
// Make sure that the EPL is always at the top of the list.
usort($licenses, function($a, $b) {
if ($a->getId() == 'EPL-2.0') return -1;
if ($b->getId() == 'EPL-2.0') return 1;
if ($a->getId() == 'EPL-1.0') return -1;
if ($b->getId() == 'EPL-1.0') return 1;
return strcasecmp($a->getId(), $b->getId());
});
return $licenses;
}
public static function getDefaultFileHeader($licenses) {
$lines = array();
$lines[] = "Copyright (c) {date} {owner}[ and others]";
$lines[] = self::getLicensesStatement($licenses);
$lines[] = "SPDX-License-Identifier: " . self::getSPDXExpression($licenses);
return implode("\n\n", $lines);
}
public static function getAlternativeFileHeader($licenses) {
$lines = array();
$lines[] = "Copyright (c) {date} Contributors to the Eclipse Foundation";
$lines[] = "See the NOTICE file(s) distributed with this work for additional information regarding copyright ownership.";
$lines[] = self::getLicensesStatement($licenses);
$lines[] = "SPDX-License-Identifier: " . self::getSPDXExpression($licenses);
return implode("\n\n", $lines);
}
/**
* Answer a statement (suitable for use in a file header)
* that describes the licenses of the project.
*
* @param $licenses License[]
* @param $nbsp string value to use as the "non-breaking" space in the license names.
* @return string
*/
public static function getLicensesStatement($licenses) {
// Initial statement of license(s)
$parts = array();
$secondary = array();
foreach($licenses as $license) {
$text = $license->getText();
if ($license->isSecondaryLicense()) {
$secondary[] = "{$text} which is available at {$license->getUrl()}";
} else {
$parts[] = "{$text} which is available at {$license->getUrl()}";
}
}
$statement = "This program and the accompanying materials are made available under the terms of the ";
$statement .= implodeWithConjunction($parts, 'or the') . ".";
if ($secondary) {
$statement .= "\n\n";
// FIXME License text is inlined; should come from data.
$epl20 = License::getLicense('EPL-2.0');
// FIXME We assume that secondary licenses only apply to EPL-2.0 (true for now)
$statement .= "This Source Code may also be made available under the following Secondary Licenses when the conditions for such availability set forth in the {$epl20->getText()} are satisfied: ";
$statement .= implodeWithConjunction($secondary, 'and') . ".";
}
return $statement;
}
public static function getSPDXExpression($licenses) {
$codes = array();
foreach($licenses as $license) {
$codes[] = $license->getSPDXCode();
}
return implode(' OR ', $codes);
}
public static function parseLicenses($text) {
$licenses = array();
foreach(explode(',', $text) as $code) {
if ($license = self::getLicense($code)) {
$licenses[] = $license;
}
}
if (!$licenses) $licenses[] = License::getLicense('EPL-2.0');
return $licenses;
}
public function getId() {
return $this->id;
}
public function getText() {
return $this->text;
}
public function getSPDXCode() {
return $this->id;
}
/**
* Answers true when the receiver is a secondary
* license according to the terms of the EPL-2.0,
* or false otherwise.
*
* Note that the ability for a license to decide
* whether or not it is secondary is temporary
* functionality based on a stop-gap implementation.
*
* TODO Reconsider this implementation.
*/
public function isSecondaryLicense() {
return preg_match("/^GPL/", $this->getId());
}
public function getUrl() {
return $this->url;
}
}
?>