*
*/
class Licenses
{
protected $paths = [];
protected $mailMap = [];
public $authors = [];
public function __construct() {
$this->licenseText = <<
*
*/
EOD;
$this->licenseText = str_replace('@YEAR@', date("Y"), $this->licenseText);
}
/**
* @param string|string[] $folder
* @param string|bool $gitRoot
*/
function exec($folder, $gitRoot = false) {
if (is_array($folder)) {
foreach($folder as $f) {
$this->exec($f, $gitRoot);
}
return;
}
if ($gitRoot !== false && substr($gitRoot, -1) !== '/') {
$gitRoot .= '/';
}
if (is_file($folder)) {
$this->handleFile($folder, $gitRoot);
return;
}
$excludes = array_map(function($item) use ($folder) {
return $folder . '/' . $item;
}, ['vendor', '3rdparty', '.git', 'l10n', 'templates']);
$iterator = new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::SKIP_DOTS);
$iterator = new RecursiveCallbackFilterIterator($iterator, function($item) use ($folder, $excludes){
/** @var SplFileInfo $item */
foreach($excludes as $exclude) {
if (substr($item->getPath(), 0, strlen($exclude)) === $exclude) {
return false;
}
}
return true;
});
$iterator = new RecursiveIteratorIterator($iterator);
$iterator = new RegexIterator($iterator, '/^.+\.php$/i');
foreach ($iterator as $file) {
/** @var SplFileInfo $file */
$this->handleFile($file, $gitRoot);
}
}
function writeAuthorsFile() {
ksort($this->authors);
$template = "ownCloud is written by:
@AUTHORS@
With help from many libraries and frameworks including:
Open Collaboration Services
SabreDAV
jQuery
…
";
$authors = implode(PHP_EOL, array_map(function($author){
return " - ".$author;
}, $this->authors));
$template = str_replace('@AUTHORS@', $authors, $template);
file_put_contents(__DIR__.'/../AUTHORS', $template);
}
function handleFile($path, $gitRoot) {
$source = file_get_contents($path);
if ($this->isMITLicensed($source)) {
echo "MIT licensed file: $path" . PHP_EOL;
return;
}
$source = $this->eatOldLicense($source);
$authors = $this->getAuthors($path, $gitRoot);
$license = str_replace('@AUTHORS@', $authors, $this->licenseText);
$source = "',
'Jenkins for ownCloud ',
'Scrutinizer Auto-Fixer ',
]);
});
if ($gitRoot) {
$authors = array_map([$this, 'checkCoreMailMap'], $authors);
$authors = array_unique($authors);
}
$authors = array_map(function($author){
$this->authors[$author] = $author;
return " * @author $author";
}, $authors);
return implode(PHP_EOL, $authors);
}
private function checkCoreMailMap($author) {
if (empty($this->mailMap)) {
$content = file_get_contents(__DIR__ . '/../.mailmap');
$entries = explode("\n", $content);
foreach ($entries as $entry) {
if (strpos($entry, '> ') === false) {
$this->mailMap[$entry] = $entry;
} else {
list($use, $actual) = explode('> ', $entry);
$this->mailMap[$actual] = $use . '>';
}
}
}
if (isset($this->mailMap[$author])) {
return $this->mailMap[$author];
}
return $author;
}
}
$licenses = new Licenses;
if (isset($argv[1])) {
$licenses->exec($argv[1], isset($argv[2]) ? $argv[1] : false);
} else {
$licenses->exec([
'../apps/dav',
'../apps/encryption',
'../apps/federation',
'../apps/files',
'../apps/files_external',
'../apps/files_sharing',
'../apps/files_trashbin',
'../apps/files_versions',
'../apps/provisioning_api',
'../apps/user_ldap',
'../core',
'../lib',
'../ocs',
'../settings',
'../console.php',
'../cron.php',
'../index.php',
'../public.php',
'../remote.php',
'../status.php',
'../version.php',
]);
$licenses->writeAuthorsFile();
}