list-of-top-level-domains/bin/helpers/generate-php-tld-enum.php

179 lines
6.7 KiB
PHP
Executable File

#!/usr/bin/env php
<?php
new class() {
const ME_NAME = 'generate-php-tld-enum.php';
const TLDS_CSV_PATH = __DIR__ . '/../../tlds.csv';
const TLD_ENUM_PATH = __DIR__ . '/../../formats/php/TldEnum/TldEnum.php';
private static $_quietMode = false;
public function __construct() {
$opt = getopt ( "q",['quiet']);
if (isset($opt['q']) || isset($opt['quiet'])) {
static::$_quietMode = true;
}
if (!static::$_quietMode) {
echo static::ME_NAME."\n";
echo " (c) 2017 Doug Bird, All Rights Reserved.\n";
echo " see README.md for licensing and other information\n";
echo " https://github.com/katmore/tld-enum#readme\n";
echo "\n";
echo " Generates new PHP format files from the 'tlds.csv' file\n";
echo "\n";
}
$fileTldsCsv = static::TLDS_CSV_PATH;
if (!is_file($fileTldsCsv) || !is_readable($fileTldsCsv)) {
static::_echo_error("(FATAL) not a readable path to 'tlds.csv': $fileTldsCsv",1);
}
$tldEnumFile = static::TLD_ENUM_PATH;
$tldEnumDir = pathinfo($tldEnumFile,\PATHINFO_DIRNAME);
if (file_exists($tldEnumFile)) {
if (!is_file($tldEnumFile)) {
static::_echo_error("(FATAL) existing path for 'TldEnum.php' was not a file as expected: $tldEnumFile",1);
}
if (!is_writable($tldEnumFile)) {
static::_echo_error("(FATAL) existing path for 'TldEnum.php' is not writable: $tldEnumFile",1);
}
} else {
if (!file_exists($tldEnumDir) || !is_dir($tldEnumDir)) {
static::_echo_error("(FATAL) path for 'TldEnum.php' directory does not exist as expected: $tldEnumDir",1);
}
if (!is_writable($tldEnumDir)) {
static::_echo_error("(FATAL) path for 'TldEnum.php' directory is not writeable: $tldEnumDir",1);
}
}
$existingMd5 = null;
if (file_exists($tldEnumFile)) {
$existingMd5 = md5_file($tldEnumFile);
$tldEnumBasename = pathinfo($tldEnumFile,\PATHINFO_FILENAME);
$backupTldEnumFile = $tldEnumDir . \DIRECTORY_SEPARATOR . "$tldEnumBasename-$existingMd5-backup.php";
if (!file_exists($backupTldEnumFile)) {
if (!copy($tldEnumFile,$backupTldEnumFile)) {
static::_echo_error("(FATAL) failed to create backup for 'TldEnum.php' (source: $tldEnumFile, dest: $backupTldEnumFile)",1);
}
}
}
if (false === ($newTldEnumFile = tempnam ( sys_get_temp_dir() , 'tld-enum-' ))) {
static::_echo_error("(FATAL) unable to initialize new 'TldEnum.php' file",1);
}
register_shutdown_function(function() use($newTldEnumFile) {
if (is_file($newTldEnumFile) && is_writable($newTldEnumFile)) {
unlink($newTldEnumFile);
}
});
$tldEnum = [];
if (($handle = fopen($fileTldsCsv, "r")) === false) {
static::_echo_error("(FATAL) unable to open 'tlds.csv' in read mode: $fileTldsCsv",1);
} else {
echo "reading 'tlds.csv'...";
$i=0;
while (($row = fgetcsv($handle, 1000, ",")) !== false) {
if (!count($row)) {
static::_echo_error("(FATAL) invalid 'tlds.csv' row #$i: $fileTldsCsv",1);
}
//echo $row[0]."\n";
$tldEnum []= $row[0];
$i++;
}
fclose($handle);
echo "done\n";
}
//echo "new TldEnum.php: $newTldEnumFile\n";
echo "generating new 'TldEnum.php' file...";
if (false === file_put_contents($newTldEnumFile,static::TLD_ENUM_SOURCE_START_CLASS)) {
static::_echo_error("(FATAL) failed to write to new 'TldEnum.php' file",1);
}
if (false === file_put_contents($newTldEnumFile,static::TLD_ENUM_SOURCE_START_TLD_ENUM_CONST,\FILE_APPEND)) {
static::_echo_error("(FATAL) failed to write to new 'TldEnum.php' file",1);
}
$tldEnumExport = var_export($tldEnum, true);
$tldEnumExport = str_replace('array','',$tldEnumExport);
$tldEnumExport = str_replace('(','[',$tldEnumExport);
$tldEnumExport = str_replace(')',']',$tldEnumExport);
$tldEnumExport = str_replace('=>','',$tldEnumExport);
$tldEnumExport = preg_replace('/[0-9]+/', '', $tldEnumExport);
//$tldEnumExport = json_encode($tldEnum,\JSON_PRETTY_PRINT);
if (false === file_put_contents($newTldEnumFile, $tldEnumExport,\FILE_APPEND)) {
static::_echo_error("(FATAL) failed to write to new 'TldEnum.php' file",1);
}
if (false === file_put_contents($newTldEnumFile,static::TLD_ENUM_SOURCE_END_TLD_ENUM_CONST,\FILE_APPEND)) {
static::_echo_error("(FATAL) failed to write to new 'TldEnum.php' file",1);
}
if (false === file_put_contents($newTldEnumFile,static::TLD_ENUM_SOURCE_END_CLASS,\FILE_APPEND)) {
static::_echo_error("(FATAL) failed to write to new 'TldEnum.php' file",1);
}
echo "done\n";
if ($existingMd5!==null) {
$newTldEnumMd5 = md5_file($newTldEnumFile);
if ($existingMd5 == $newTldEnumMd5) {
static::_echo_error("(NOTICE) ignoring newly generated 'TldEnum.php' file that is identical to the existing file (md5: $existingMd5, path: $tldEnumFile)");
return;
}
if (!unlink($tldEnumFile)) {
static::_echo_error("(FATAL) failed to remove stale 'TldEnum.php': $tldEnumFile",1);
}
}
if (!copy($newTldEnumFile,$tldEnumFile)) {
static::_echo_error("(FATAL) failed to save new 'TldEnum.php': $tldEnumFile",1);
}
echo "saved new 'TldEnum.php' file\n";
}
private static function _echo_error(string $str, int $fatal_exit_status=null) : void {
if (substr($str,0,1)!=="\n") {
$str .= "\n";
}
$str = static::ME_NAME . ": ".$str;
if (\PHP_SAPI=='cli') {
fwrite(\STDERR,$str);
} else {
echo $str ;
}
if (is_int($fatal_exit_status)) {
exit($fatal_exit_status);
}
}
const TLD_ENUM_SOURCE_START_CLASS = <<<SOURCE
<?php
namespace TldEnum;
class TldEnum {
SOURCE;
const TLD_ENUM_SOURCE_START_TLD_ENUM_CONST = <<<SOURCE
const TLD_ENUM =
SOURCE;
const TLD_ENUM_SOURCE_END_TLD_ENUM_CONST = <<<SOURCE
;
SOURCE;
const TLD_ENUM_SOURCE_END_CLASS = <<<SOURCE
}
SOURCE;
};