import.php
· 4.3 KiB · PHP
原始檔案
// ensure compatibility with files containing unicode characters
shell_exec('git config core.quotepath off');
// Now process all of the other changed content
$diff = shell_exec('git diff --name-only '.$argv[3].' ' . $argv[4]);
if($diff == '') {
echo "\n*** No changes detected. Are you running the latest version of the action? See https://github.com/neatnik/weblog.lol for details.";
}
else {
$diff = explode("\n", $diff);
// we'll need to exclude any deleted from the unstaged list, as they may just have been deleted by our deployment script
$edited = shell_exec('git status --porcelain | grep -v " D " | sed s/^...//');
$edited = explode("\n", $edited);
echo "\n*** These items have changed with this push:\n";
print_r($diff);
echo "\n*** These items have additionally changed during processing:\n";
print_r($edited);
$diff = array_unique(array_merge($diff, $edited));
foreach($diff as $file) {
if($file == '') {
continue;
}
// remove quotes from filename
if(substr($file, 0, 1) == '"') $file = substr($file, 1);
if(substr($file, -1) == '"') $file = substr($file, 0, -1);
if(strtolower(substr($file, 0, 7)) !== 'weblog/' && strtolower(substr($file, 0, 14)) !== 'configuration/') {
echo "\n*** Skipping file: $file";
continue;
}
echo "\n*** Examining file: $file...";
if(strtolower($file) == 'configuration/configuration.txt' || strtolower($file) == 'weblog/configuration/configuration.txt') {
$configuration_update = $file;
}
if(strtolower($file) == 'configuration/template.html' || strtolower($file) == 'weblog/configuration/template.html') {
echo "\n*** Updating template...";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.omg.lol/address/'.$argv[1].'/weblog/template');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($file));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
curl_setopt($ch, CURLOPT_XOAUTH2_BEARER, $argv[2]);
$response = curl_exec($ch);
curl_close($ch);
}
if(substr(strtolower($file), -3) !== '.md' && substr(strtolower($file), -9) !== '.markdown') {
echo "\n*** File doesn’t end in .md or .markdown; skipping.";
continue;
}
$filename = $file;
$filename = str_replace('/', '_', $filename);
$filename = substr($filename, 7); // removes 'weblog_'
$filename = substr($filename, 0, -3);
if(file_exists($file)) {
echo "\n*** Updating file: $file...";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.omg.lol/address/'.$argv[1].'/weblog/entry/'.urlencode($filename));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($file));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
curl_setopt($ch, CURLOPT_XOAUTH2_BEARER, $argv[2]);
$response = curl_exec($ch);
curl_close($ch);
}
else {
echo "\n*** Deleting file: $file...";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.omg.lol/address/'.$argv[1].'/weblog/delete/'.$filename);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
curl_setopt($ch, CURLOPT_XOAUTH2_BEARER, $argv[2]);
$response = curl_exec($ch);
curl_close($ch);
}
}
// save the configuration for last, since it will trigger a rebuild
if(isset($configuration_update)) {
echo "\n*** Updating configuration...";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.omg.lol/address/'.$argv[1].'/weblog/configuration');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($configuration_update));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
curl_setopt($ch, CURLOPT_XOAUTH2_BEARER, $argv[2]);
$response = curl_exec($ch);
curl_close($ch);
}
}
echo "\n*** Entry processing complete. Have a nice day.";
| 1 | // ensure compatibility with files containing unicode characters |
| 2 | shell_exec('git config core.quotepath off'); |
| 3 | |
| 4 | // Now process all of the other changed content |
| 5 | $diff = shell_exec('git diff --name-only '.$argv[3].' ' . $argv[4]); |
| 6 | if($diff == '') { |
| 7 | echo "\n*** No changes detected. Are you running the latest version of the action? See https://github.com/neatnik/weblog.lol for details."; |
| 8 | } |
| 9 | else { |
| 10 | $diff = explode("\n", $diff); |
| 11 | // we'll need to exclude any deleted from the unstaged list, as they may just have been deleted by our deployment script |
| 12 | $edited = shell_exec('git status --porcelain | grep -v " D " | sed s/^...//'); |
| 13 | $edited = explode("\n", $edited); |
| 14 | |
| 15 | echo "\n*** These items have changed with this push:\n"; |
| 16 | print_r($diff); |
| 17 | echo "\n*** These items have additionally changed during processing:\n"; |
| 18 | print_r($edited); |
| 19 | |
| 20 | $diff = array_unique(array_merge($diff, $edited)); |
| 21 | |
| 22 | foreach($diff as $file) { |
| 23 | |
| 24 | if($file == '') { |
| 25 | continue; |
| 26 | } |
| 27 | |
| 28 | // remove quotes from filename |
| 29 | if(substr($file, 0, 1) == '"') $file = substr($file, 1); |
| 30 | if(substr($file, -1) == '"') $file = substr($file, 0, -1); |
| 31 | |
| 32 | if(strtolower(substr($file, 0, 7)) !== 'weblog/' && strtolower(substr($file, 0, 14)) !== 'configuration/') { |
| 33 | echo "\n*** Skipping file: $file"; |
| 34 | continue; |
| 35 | } |
| 36 | |
| 37 | echo "\n*** Examining file: $file..."; |
| 38 | |
| 39 | if(strtolower($file) == 'configuration/configuration.txt' || strtolower($file) == 'weblog/configuration/configuration.txt') { |
| 40 | $configuration_update = $file; |
| 41 | } |
| 42 | if(strtolower($file) == 'configuration/template.html' || strtolower($file) == 'weblog/configuration/template.html') { |
| 43 | echo "\n*** Updating template..."; |
| 44 | $ch = curl_init(); |
| 45 | curl_setopt($ch, CURLOPT_URL, 'https://api.omg.lol/address/'.$argv[1].'/weblog/template'); |
| 46 | curl_setopt($ch, CURLOPT_POST, 1); |
| 47 | curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($file)); |
| 48 | curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BEARER); |
| 49 | curl_setopt($ch, CURLOPT_XOAUTH2_BEARER, $argv[2]); |
| 50 | $response = curl_exec($ch); |
| 51 | curl_close($ch); |
| 52 | } |
| 53 | |
| 54 | if(substr(strtolower($file), -3) !== '.md' && substr(strtolower($file), -9) !== '.markdown') { |
| 55 | echo "\n*** File doesn’t end in .md or .markdown; skipping."; |
| 56 | continue; |
| 57 | } |
| 58 | |
| 59 | $filename = $file; |
| 60 | $filename = str_replace('/', '_', $filename); |
| 61 | $filename = substr($filename, 7); // removes 'weblog_' |
| 62 | $filename = substr($filename, 0, -3); |
| 63 | |
| 64 | if(file_exists($file)) { |
| 65 | echo "\n*** Updating file: $file..."; |
| 66 | $ch = curl_init(); |
| 67 | curl_setopt($ch, CURLOPT_URL, 'https://api.omg.lol/address/'.$argv[1].'/weblog/entry/'.urlencode($filename)); |
| 68 | curl_setopt($ch, CURLOPT_POST, 1); |
| 69 | curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($file)); |
| 70 | curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BEARER); |
| 71 | curl_setopt($ch, CURLOPT_XOAUTH2_BEARER, $argv[2]); |
| 72 | $response = curl_exec($ch); |
| 73 | curl_close($ch); |
| 74 | } |
| 75 | else { |
| 76 | echo "\n*** Deleting file: $file..."; |
| 77 | $ch = curl_init(); |
| 78 | curl_setopt($ch, CURLOPT_URL, 'https://api.omg.lol/address/'.$argv[1].'/weblog/delete/'.$filename); |
| 79 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
| 80 | curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BEARER); |
| 81 | curl_setopt($ch, CURLOPT_XOAUTH2_BEARER, $argv[2]); |
| 82 | $response = curl_exec($ch); |
| 83 | curl_close($ch); |
| 84 | } |
| 85 | } |
| 86 | // save the configuration for last, since it will trigger a rebuild |
| 87 | if(isset($configuration_update)) { |
| 88 | echo "\n*** Updating configuration..."; |
| 89 | $ch = curl_init(); |
| 90 | curl_setopt($ch, CURLOPT_URL, 'https://api.omg.lol/address/'.$argv[1].'/weblog/configuration'); |
| 91 | curl_setopt($ch, CURLOPT_POST, 1); |
| 92 | curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($configuration_update)); |
| 93 | curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BEARER); |
| 94 | curl_setopt($ch, CURLOPT_XOAUTH2_BEARER, $argv[2]); |
| 95 | $response = curl_exec($ch); |
| 96 | curl_close($ch); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | echo "\n*** Entry processing complete. Have a nice day."; |
pre-process.php
· 4.4 KiB · PHP
原始檔案
shell_exec('git config core.quotepath off');
echo 'git diff --name-only '.$argv[1].' ' . $argv[2];
$diff = shell_exec('git diff --name-only '.$argv[1].' ' . $argv[2]);
if($diff == '') {
echo "\n*** No changes detected. Are you running the latest version of the action? See https://github.com/neatnik/weblog.lol for details.";
exit;
}
$diff = explode("\n", $diff);
// get MD5 hashes of all static files
$cssFiles = getHashes('css', ['css']);
$jsFiles = getHashes('js', ['js']);
$imageFiles = getHashes('images', ['jpg','jpeg','gif','png','svg']);
$toReplace = array_merge(
$cssFiles,
$jsFiles,
$imageFiles,
);
// make sure we scan all templates for references to static files, regardless of whether these have changed or not
foreach($diff as $file) {
if(array_key_exists($file,$cssFiles) || array_key_exists($file, $jsFiles)) {
$diff[] = 'configuration/template.html';
$templates = scandir('weblog/templates');
foreach($templates as $template) {
if(in_array(pathinfo('weblog/templates/' . $template, PATHINFO_EXTENSION), ['md','markdown','html'])) {
$diff[] = 'weblog/templates/' . $template;
}
}
}
}
$diff = array_unique($diff);
// Replace references to these static files in any .md and .html files
foreach($diff as $file) {
if($file == '') {
continue;
}
$fileEnding = pathinfo($file, PATHINFO_EXTENSION);
if(!in_array($fileEnding, ['md','markdown','html'])) {
echo PHP_EOL . 'Skipping file: ' . $file;
continue;
}
echo "\n*** Examining file: $file...";
// Firstly: get the relative directory from the file we are looking at, to the root directory
$structure = explode('/', $file);
foreach($structure as $i => $folder) {
if($i !== count($structure) - 1) {
$structure[$i] = '..';
}
}
unset($structure[count($structure) - 1]);
$relativeDir = join('/', $structure) . '/';
// now replace any relative references to the static files, found in HTML files, as well as in markdown links
$fileContents = file_get_contents($file);
foreach($toReplace as $cssFile => $hash) {
$pattern = '#(src=|href=)([\'"])' . preg_quote($relativeDir) . preg_quote($cssFile) . '([\'"])#';
$replacement = '$1$2https://media.thms.uk/' . $cssFile . '?v=' . $hash . '$3';
echo "\n*** replacing " . $pattern . ' with ' . $replacement;
$fileContents = preg_replace($pattern, $replacement, $fileContents);
}
// finally, replace relative links to embedded links in markdown files
if(pathinfo($file, PATHINFO_EXTENSION) === 'md') {
foreach($imageFiles as $imageFile => $hash) {
$pattern = '|(!\[[^]]+])\(' . preg_quote($relativeDir) . preg_quote($imageFile) . '\)|';
$replacement = '$1(https://media.thms.uk/' . $imageFile . '?v=' . $hash . ')';
echo "\n*** replacing " . $pattern . ' with ' . $replacement;
$fileContents = preg_replace($pattern, $replacement, $fileContents);
}
}
file_put_contents($file, $fileContents);
}
// delete unchanged static files, so that we don't upload them to S3 every time
foreach(['css','js','images'] as $directory) {
$o_dir = new RecursiveDirectoryIterator($directory);
$o_iter = new RecursiveIteratorIterator($o_dir);
foreach($o_iter as $file) {
echo PHP_EOL . $file;
if(in_array($file->getExtension(), ['jpg','jpeg','gif','png','svg', 'css','js']) && !in_array($file->getPathName(), $diff)) {
echo PHP_EOL . '*** Deleting unchanged ' . $file->getPathName();
unlink($file->getPathName());
}
}
}
function getHashes(string $directory, array $types): array
{
$return = [];
$o_dir = new RecursiveDirectoryIterator($directory);
$o_iter = new RecursiveIteratorIterator($o_dir);
foreach($o_iter as $file) {
if(in_array($file->getExtension(), $types)) {
$return[$file->getPathName()] = hash_file('md5', $file->getPathName());
}
}
return $return;
}
| 1 | shell_exec('git config core.quotepath off'); |
| 2 | echo 'git diff --name-only '.$argv[1].' ' . $argv[2]; |
| 3 | $diff = shell_exec('git diff --name-only '.$argv[1].' ' . $argv[2]); |
| 4 | if($diff == '') { |
| 5 | echo "\n*** No changes detected. Are you running the latest version of the action? See https://github.com/neatnik/weblog.lol for details."; |
| 6 | exit; |
| 7 | } |
| 8 | |
| 9 | $diff = explode("\n", $diff); |
| 10 | |
| 11 | // get MD5 hashes of all static files |
| 12 | $cssFiles = getHashes('css', ['css']); |
| 13 | $jsFiles = getHashes('js', ['js']); |
| 14 | $imageFiles = getHashes('images', ['jpg','jpeg','gif','png','svg']); |
| 15 | |
| 16 | $toReplace = array_merge( |
| 17 | $cssFiles, |
| 18 | $jsFiles, |
| 19 | $imageFiles, |
| 20 | ); |
| 21 | |
| 22 | // make sure we scan all templates for references to static files, regardless of whether these have changed or not |
| 23 | foreach($diff as $file) { |
| 24 | if(array_key_exists($file,$cssFiles) || array_key_exists($file, $jsFiles)) { |
| 25 | $diff[] = 'configuration/template.html'; |
| 26 | $templates = scandir('weblog/templates'); |
| 27 | foreach($templates as $template) { |
| 28 | if(in_array(pathinfo('weblog/templates/' . $template, PATHINFO_EXTENSION), ['md','markdown','html'])) { |
| 29 | $diff[] = 'weblog/templates/' . $template; |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | $diff = array_unique($diff); |
| 35 | |
| 36 | // Replace references to these static files in any .md and .html files |
| 37 | foreach($diff as $file) { |
| 38 | |
| 39 | if($file == '') { |
| 40 | continue; |
| 41 | } |
| 42 | |
| 43 | $fileEnding = pathinfo($file, PATHINFO_EXTENSION); |
| 44 | if(!in_array($fileEnding, ['md','markdown','html'])) { |
| 45 | echo PHP_EOL . 'Skipping file: ' . $file; |
| 46 | continue; |
| 47 | } |
| 48 | |
| 49 | echo "\n*** Examining file: $file..."; |
| 50 | |
| 51 | // Firstly: get the relative directory from the file we are looking at, to the root directory |
| 52 | $structure = explode('/', $file); |
| 53 | foreach($structure as $i => $folder) { |
| 54 | if($i !== count($structure) - 1) { |
| 55 | $structure[$i] = '..'; |
| 56 | } |
| 57 | } |
| 58 | unset($structure[count($structure) - 1]); |
| 59 | $relativeDir = join('/', $structure) . '/'; |
| 60 | |
| 61 | // now replace any relative references to the static files, found in HTML files, as well as in markdown links |
| 62 | $fileContents = file_get_contents($file); |
| 63 | foreach($toReplace as $cssFile => $hash) { |
| 64 | $pattern = '#(src=|href=)([\'"])' . preg_quote($relativeDir) . preg_quote($cssFile) . '([\'"])#'; |
| 65 | $replacement = '$1$2https://media.thms.uk/' . $cssFile . '?v=' . $hash . '$3'; |
| 66 | |
| 67 | echo "\n*** replacing " . $pattern . ' with ' . $replacement; |
| 68 | |
| 69 | $fileContents = preg_replace($pattern, $replacement, $fileContents); |
| 70 | } |
| 71 | |
| 72 | // finally, replace relative links to embedded links in markdown files |
| 73 | if(pathinfo($file, PATHINFO_EXTENSION) === 'md') { |
| 74 | foreach($imageFiles as $imageFile => $hash) { |
| 75 | $pattern = '|(!\[[^]]+])\(' . preg_quote($relativeDir) . preg_quote($imageFile) . '\)|'; |
| 76 | $replacement = '$1(https://media.thms.uk/' . $imageFile . '?v=' . $hash . ')'; |
| 77 | |
| 78 | echo "\n*** replacing " . $pattern . ' with ' . $replacement; |
| 79 | |
| 80 | $fileContents = preg_replace($pattern, $replacement, $fileContents); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | file_put_contents($file, $fileContents); |
| 85 | } |
| 86 | |
| 87 | // delete unchanged static files, so that we don't upload them to S3 every time |
| 88 | foreach(['css','js','images'] as $directory) { |
| 89 | $o_dir = new RecursiveDirectoryIterator($directory); |
| 90 | $o_iter = new RecursiveIteratorIterator($o_dir); |
| 91 | foreach($o_iter as $file) { |
| 92 | echo PHP_EOL . $file; |
| 93 | if(in_array($file->getExtension(), ['jpg','jpeg','gif','png','svg', 'css','js']) && !in_array($file->getPathName(), $diff)) { |
| 94 | echo PHP_EOL . '*** Deleting unchanged ' . $file->getPathName(); |
| 95 | unlink($file->getPathName()); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | function getHashes(string $directory, array $types): array |
| 101 | { |
| 102 | $return = []; |
| 103 | $o_dir = new RecursiveDirectoryIterator($directory); |
| 104 | $o_iter = new RecursiveIteratorIterator($o_dir); |
| 105 | foreach($o_iter as $file) { |
| 106 | if(in_array($file->getExtension(), $types)) { |
| 107 | $return[$file->getPathName()] = hash_file('md5', $file->getPathName()); |
| 108 | } |
| 109 | } |
| 110 | return $return; |
| 111 | } |