jsonify/jsonify.php

62 lines
1.4 KiB
PHP

#!/usr/bin/php
<?php
/**
*
* JSONify v0.1.0 - Convert word lists into JSON documents
*
*/
echo "Jsonify 0.1.0 - miguel456@spacejewel-hosting.com " . PHP_EOL;
// debug
function logMessage($message, $level = "INFO", $stderr = true)
{
if ($level == "ERROR" && $stderr)
{
fwrite(STDERR, $level . " >> " . $message . PHP_EOL);
exit(1);
}
echo $level . " >> " . $message . PHP_EOL;
}
$addPlaceholders = $argv[2] ?? false;
if (!isset($argv[1]))
{
logMessage("No file name provided. Please provide a filename to continue. It must be a full path to the file.", "ERROR");
}
$keys = [];
if ($file = fopen($argv[1], "r"))
{
// keep reading until end of file
while (!feof($file))
{
$str = fgets($file, 8192);
// read unti end of line
if ($str !== false)
{
array_push($keys, trim($str));
}
}
// An use of this is a translation file, where all words for translation need to have a translated key
logMessage("Filling with placeholder strings!");
$keys = array_fill_keys($keys, "translatemenow");
fclose($file);
} else {
logMessage("Unable to open file $file for reading.", "ERROR");
}
file_put_contents($_SERVER['HOME'] . "/jsonify.out.json", json_encode($keys, JSON_PRETTY_PRINT));
logMessage("Successfully written JSONified output to disk. You can find it at " . $_SERVER['HOME'] . "/jsonify.out.json");
exit(0);