Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fix CKEditor 4 line endings
#!/usr/bin/env php
<?php
const FIX_EOL = true;
const FIX_BOM = true;
set_error_handler(
static function ($errno, $errmsg, $errfile, $errline)
{
throw new RuntimeException("Error {$errno}: {$errmsg}\nFile: {$errfile}\nLine: {$errline}");
},
-1
);
const UTF8_BOM = "\xEF\xBB\xBF";
function isCKEditorDir($dir)
{
return is_file("{$dir}/ckeditor.js") && is_dir("{$dir}/adapters") && is_dir("{$dir}/plugins") && is_dir("{$dir}/skins");
}
function getCKEditorDir($arg)
{
$dir = realpath($arg);
if ($dir === false || !is_dir($dir)) {
throw new RuntimeException("Directory not found: {$arg}");
}
$dir = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', $dir), '/');
$paths = ['concrete', 'js', 'ckeditor4', 'vendor'];
for ($numPaths = 0; $numPaths <= count($paths); $numPaths++) {
$tmp = $dir;
if ($numPaths > 0) {
$tmp .= '/' . implode('/', array_slice($paths, -$numPaths));
}
if (isCKEditorDir($tmp)) {
return $tmp;
}
}
throw new RuntimeException('Failed to find the CKEditor directory under ' . str_replace('/', DIRECTORY_SEPARATOR, $dir));
}
function listFiles($ckDir)
{
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$ckDir,
FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS
)
);
foreach ($iterator as $file) {
if ($file->isFile()) {
switch (strtolower($file->getExtension())) {
case 'css':
case 'html':
case 'js':
case 'json':
case 'md':
case 'php':
case 'txt':
yield $file->getPathname();
break;
case 'gif':
case 'jpg':
case 'fla':
case 'eot':
case 'png':
case 'ttf':
case 'svg':
case 'swf':
case 'woff':
break;
case '':
switch ($file->getFilename()) {
case 'LICENSE':
yield $file->getPathname();
break;
default:
throw new RuntimeException('Unrecognized file: ' . str_replace('/', DIRECTORY_SEPARATOR, $file->getPathname()));
}
break;
default:
throw new RuntimeException("Unrecognized extension '{$file->getExtension()}' for file " . str_replace('/', DIRECTORY_SEPARATOR, $file->getPathname()));
}
}
}
}
function fixLineEndings($ckFile)
{
$originalContents = file_get_contents($ckFile);
if ($originalContents === false) {
throw new RuntimeException('Failed to read file ' . str_replace('/', DIRECTORY_SEPARATOR, $ckFile));
}
$fixedContents = $originalContents;
if (FIX_EOL) {
$fixedContents = str_replace("\r", "\n", str_replace("\r\n", "\n", $originalContents));
}
if (FIX_BOM && substr($fixedContents, 0, strlen(UTF8_BOM)) === UTF8_BOM) {
$fixedContents = substr($fixedContents, strlen(UTF8_BOM));
}
if ($fixedContents === $originalContents) {
return false;
}
if (file_put_contents($ckFile, $fixedContents) === false) {
throw new RuntimeException('Failed to write file ' . str_replace('/', DIRECTORY_SEPARATOR, $ckFile));
}
return true;
}
try {
if (empty($argv) || count($argv) !== 2) {
throw new RuntimeException('Please specify the path to the concrete5 root folder.');
}
$ckDir = getCKEditorDir($argv[1]);
$totalFiles = 0;
$fixedFiles = 0;
foreach (listFiles($ckDir) as $ckFile) {
$totalFiles++;
if (fixLineEndings($ckFile) === true) {
$fixedFiles++;
}
}
echo "{$fixedFiles} files fixed out of {$totalFiles} in ", str_replace('/', DIRECTORY_SEPARATOR, $ckDir), "\n";
} catch (RuntimeException $x) {
fwrite(STDERR, $x->getMessage() . "\n");
exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment