michael revisó este gist 3 hours ago. Ir a la revisión
2 files changed, 211 insertions
import.php(archivo creado)
| @@ -0,0 +1,100 @@ | |||
| 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(archivo creado)
| @@ -0,0 +1,111 @@ | |||
| 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 | + | } | |