paste.thms.uk/weblog-import.php 1 year ago

// 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.";