1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
<?php
$ROOT = realpath(__DIR__ . '/..');
function process_file($path) {
$year = date('Y', filemtime($path));
$contents = file_get_contents($path);
$copy_statements = 0;
$output = '';
foreach (preg_split("/[\r\n]/", $contents) as $line) {
if (preg_match("/^(.+)Copyright\s+\(c\)\s+(\d+)-?(\d*) (Nicholas.+)$/", $line, $m)) {
$copy_statements++;
if ($m[2] != $year and $m[3] != $year) {
// Change the line
$line = "$m[1]Copyright (c) $m[2]-$year $m[4]";
}
}
// Remove trailing whitespace
$line = rtrim($line);
$output .= "$line\n";
}
// Remove surplus line endings
while (substr($output, -2) == "\n\n") {
$output = substr($output, 0, -1);
}
if ($copy_statements == 0) {
print "Warning: $path does not contain any copyright statements\n";
} else {
file_put_contents($path, $output);
}
}
function process_directory($path) {
$dir = opendir($path);
while ($file = readdir($dir)) {
if (substr($file, 0, 1) == '.') {
continue;
}
$filepath = $path . '/' . $file;
if (is_dir($filepath)) {
process_directory($filepath);
} elseif (is_file($filepath)) {
if (substr($file, -4) == '.php') {
process_file($filepath);
}
} else {
print "Unknown type: $filepath\n";
}
}
closedir($dir);
}
process_directory($ROOT . '/examples');
process_directory($ROOT . '/lib');
process_directory($ROOT . '/test');
|