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

177 lines
6.6 KiB
PHP
Raw Normal View History

version 2 - refactor js files to be proper module exportrs with an index.js - fix inconsistent naming of TLD format files containing simple array of each TLD; now all named using the "list" suffix instead of some using "enum" - formats/js/tld-enum/list.js - formats/json/tld-list.json - formats/php/TldEnum/TldList.php - new "desc" (description) format file; hashmap of each TLD with corresponding description - formats/js/tld-enum/desc.js - formats/json/tld-desc.json - formats/php/TldEnum/TldDesc.php - new "type" format file; hashmap of each TLD with corresponding TLD type - formats/js/tld-enum/type.js - formats/json/tld-type.json - formats/php/TldEnum/TldType.php - new "info" format file; array of each TLD with hashmap containing the "domain", "description", and "type" properties - formats/js/tld-enum/info.js - formats/json/tld-info.json - formats/php/TldEnum/TldInfo.php - examples for new format files - examples/js-desc-demo.js - examples/js-info-demo.js - examples/js-type-demo.js - examples/php-TldDesc-demo.php - examples/php-TldInfo-demo.php - examples/php-TldType-demo.php - fixes to bin/update-formats.sh - can now skip downloading data from IANA - creates new format files with newly created helpers - created new helpers for new format files - bin/helpers/generate-js-tld-desc.js - bin/helpers/generate-js-tld-info.js - bin/helpers/generate-js-tld-type.js - bin/helpers/generate-json-tld-desc.js - bin/helpers/generate-json-tld-info.js - bin/helpers/generate-json-tld-type.js - bin/helpers/generate-php-tld-desc.php - bin/helpers/generate-php-tld-info.php - bin/helpers/generate-php-tld-type.php
2018-06-13 05:51:12 +00:00
#!/usr/bin/env php
<?php
new class() {
2018-06-13 08:10:41 +00:00
const ME_NAME = 'generate-php-tld-desc.php';
const TLDS_CSV_PATH = __DIR__ . '/../../../tlds.csv';
const TLD_DESC_PATH = __DIR__ . '/../../../formats/php/TldEnum/TldDesc.php';
version 2 - refactor js files to be proper module exportrs with an index.js - fix inconsistent naming of TLD format files containing simple array of each TLD; now all named using the "list" suffix instead of some using "enum" - formats/js/tld-enum/list.js - formats/json/tld-list.json - formats/php/TldEnum/TldList.php - new "desc" (description) format file; hashmap of each TLD with corresponding description - formats/js/tld-enum/desc.js - formats/json/tld-desc.json - formats/php/TldEnum/TldDesc.php - new "type" format file; hashmap of each TLD with corresponding TLD type - formats/js/tld-enum/type.js - formats/json/tld-type.json - formats/php/TldEnum/TldType.php - new "info" format file; array of each TLD with hashmap containing the "domain", "description", and "type" properties - formats/js/tld-enum/info.js - formats/json/tld-info.json - formats/php/TldEnum/TldInfo.php - examples for new format files - examples/js-desc-demo.js - examples/js-info-demo.js - examples/js-type-demo.js - examples/php-TldDesc-demo.php - examples/php-TldInfo-demo.php - examples/php-TldType-demo.php - fixes to bin/update-formats.sh - can now skip downloading data from IANA - creates new format files with newly created helpers - created new helpers for new format files - bin/helpers/generate-js-tld-desc.js - bin/helpers/generate-js-tld-info.js - bin/helpers/generate-js-tld-type.js - bin/helpers/generate-json-tld-desc.js - bin/helpers/generate-json-tld-info.js - bin/helpers/generate-json-tld-type.js - bin/helpers/generate-php-tld-desc.php - bin/helpers/generate-php-tld-info.php - bin/helpers/generate-php-tld-type.php
2018-06-13 05:51:12 +00:00
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";
2018-06-13 08:10:41 +00:00
echo " Generates new PHP format file 'TldDesc.php' from the 'tlds.csv' file\n";
version 2 - refactor js files to be proper module exportrs with an index.js - fix inconsistent naming of TLD format files containing simple array of each TLD; now all named using the "list" suffix instead of some using "enum" - formats/js/tld-enum/list.js - formats/json/tld-list.json - formats/php/TldEnum/TldList.php - new "desc" (description) format file; hashmap of each TLD with corresponding description - formats/js/tld-enum/desc.js - formats/json/tld-desc.json - formats/php/TldEnum/TldDesc.php - new "type" format file; hashmap of each TLD with corresponding TLD type - formats/js/tld-enum/type.js - formats/json/tld-type.json - formats/php/TldEnum/TldType.php - new "info" format file; array of each TLD with hashmap containing the "domain", "description", and "type" properties - formats/js/tld-enum/info.js - formats/json/tld-info.json - formats/php/TldEnum/TldInfo.php - examples for new format files - examples/js-desc-demo.js - examples/js-info-demo.js - examples/js-type-demo.js - examples/php-TldDesc-demo.php - examples/php-TldInfo-demo.php - examples/php-TldType-demo.php - fixes to bin/update-formats.sh - can now skip downloading data from IANA - creates new format files with newly created helpers - created new helpers for new format files - bin/helpers/generate-js-tld-desc.js - bin/helpers/generate-js-tld-info.js - bin/helpers/generate-js-tld-type.js - bin/helpers/generate-json-tld-desc.js - bin/helpers/generate-json-tld-info.js - bin/helpers/generate-json-tld-type.js - bin/helpers/generate-php-tld-desc.php - bin/helpers/generate-php-tld-info.php - bin/helpers/generate-php-tld-type.php
2018-06-13 05:51:12 +00:00
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);
}
$tldDescFile = static::TLD_DESC_PATH;
$tldDescDir = pathinfo($tldDescFile,\PATHINFO_DIRNAME);
if (file_exists($tldDescFile)) {
if (!is_file($tldDescFile)) {
static::_echo_error("(FATAL) existing path for 'TldDesc.php' was not a file as expected: $tldDescFile",1);
}
if (!is_writable($tldDescFile)) {
static::_echo_error("(FATAL) existing path for 'TldDesc.php' is not writable: $tldDescFile",1);
}
} else {
if (!file_exists($tldDescDir) || !is_dir($tldDescDir)) {
static::_echo_error("(FATAL) path for 'TldDesc.php' directory does not exist as expected: $tldDescDir",1);
}
if (!is_writable($tldDescDir)) {
static::_echo_error("(FATAL) path for 'TldDesc.php' directory is not writeable: $tldDescDir",1);
}
}
$existingMd5 = null;
if (file_exists($tldDescFile)) {
$existingMd5 = md5_file($tldDescFile);
$tldDescBasename = pathinfo($tldDescFile,\PATHINFO_FILENAME);
$backupTldDescFile = $tldDescDir . \DIRECTORY_SEPARATOR . "$tldDescBasename-$existingMd5-backup.php";
if (!file_exists($backupTldDescFile)) {
if (!copy($tldDescFile,$backupTldDescFile)) {
static::_echo_error("(FATAL) failed to create backup for 'TldDesc.php' (source: $tldDescFile, dest: $backupTldDescFile)",1);
}
}
}
if (false === ($newTldDescFile = tempnam ( sys_get_temp_dir() , 'tld-enum-' ))) {
static::_echo_error("(FATAL) unable to initialize new 'TldDesc.php' file",1);
}
register_shutdown_function(function() use($newTldDescFile) {
if (is_file($newTldDescFile) && is_writable($newTldDescFile)) {
unlink($newTldDescFile);
}
});
$tldDesc = [];
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);
}
if (!isset($row[1])) {
static::_echo_error("(FATAL) invalid 'tlds.csv', missing column 2 on row #$i: $fileTldsCsv",1);
}
$domain = $row[0];
$desc = $row[1];
$tldDesc[$domain] = $desc;
$i++;
}
fclose($handle);
echo "done\n";
}
echo "generating new 'TldDesc.php' file...";
if (false === file_put_contents($newTldDescFile,static::TLD_DESC_SOURCE_START_CLASS)) {
static::_echo_error("(FATAL) failed to write to new 'TldDesc.php' file",1);
}
if (false === file_put_contents($newTldDescFile,static::TLD_DESC_SOURCE_START_TLD_DESC_CONST,\FILE_APPEND)) {
static::_echo_error("(FATAL) failed to write to new 'TldDesc.php' file",1);
}
$tldDescExport = var_export($tldDesc, true);
2018-06-14 21:58:20 +00:00
$tldDescExport = substr($tldDescExport,strlen("array ("));
$tldDescExport = substr($tldDescExport,0,-1);
$tldDescExport = ' ['.$tldDescExport.']';
version 2 - refactor js files to be proper module exportrs with an index.js - fix inconsistent naming of TLD format files containing simple array of each TLD; now all named using the "list" suffix instead of some using "enum" - formats/js/tld-enum/list.js - formats/json/tld-list.json - formats/php/TldEnum/TldList.php - new "desc" (description) format file; hashmap of each TLD with corresponding description - formats/js/tld-enum/desc.js - formats/json/tld-desc.json - formats/php/TldEnum/TldDesc.php - new "type" format file; hashmap of each TLD with corresponding TLD type - formats/js/tld-enum/type.js - formats/json/tld-type.json - formats/php/TldEnum/TldType.php - new "info" format file; array of each TLD with hashmap containing the "domain", "description", and "type" properties - formats/js/tld-enum/info.js - formats/json/tld-info.json - formats/php/TldEnum/TldInfo.php - examples for new format files - examples/js-desc-demo.js - examples/js-info-demo.js - examples/js-type-demo.js - examples/php-TldDesc-demo.php - examples/php-TldInfo-demo.php - examples/php-TldType-demo.php - fixes to bin/update-formats.sh - can now skip downloading data from IANA - creates new format files with newly created helpers - created new helpers for new format files - bin/helpers/generate-js-tld-desc.js - bin/helpers/generate-js-tld-info.js - bin/helpers/generate-js-tld-type.js - bin/helpers/generate-json-tld-desc.js - bin/helpers/generate-json-tld-info.js - bin/helpers/generate-json-tld-type.js - bin/helpers/generate-php-tld-desc.php - bin/helpers/generate-php-tld-info.php - bin/helpers/generate-php-tld-type.php
2018-06-13 05:51:12 +00:00
if (false === file_put_contents($newTldDescFile, $tldDescExport,\FILE_APPEND)) {
static::_echo_error("(FATAL) failed to write to new 'TldDesc.php' file",1);
}
if (false === file_put_contents($newTldDescFile,static::TLD_DESC_SOURCE_END_TLD_DESC_CONST,\FILE_APPEND)) {
static::_echo_error("(FATAL) failed to write to new 'TldDesc.php' file",1);
}
if (false === file_put_contents($newTldDescFile,static::TLD_DESC_SOURCE_END_CLASS,\FILE_APPEND)) {
static::_echo_error("(FATAL) failed to write to new 'TldDesc.php' file",1);
}
echo "done\n";
if ($existingMd5!==null) {
$newTldDescMd5 = md5_file($newTldDescFile);
if ($existingMd5 == $newTldDescMd5) {
static::_echo_error("(NOTICE) ignoring newly generated 'TldDesc.php' file that is identical to the existing file (md5: $existingMd5, path: $tldDescFile)");
return;
}
if (!unlink($tldDescFile)) {
static::_echo_error("(FATAL) failed to remove stale 'TldDesc.php': $tldDescFile",1);
}
}
if (!copy($newTldDescFile,$tldDescFile)) {
static::_echo_error("(FATAL) failed to save new 'TldDesc.php': $tldDescFile",1);
}
echo "saved new 'TldDesc.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_DESC_SOURCE_START_CLASS = <<<SOURCE
<?php
namespace TldEnum;
class TldDesc {
SOURCE;
const TLD_DESC_SOURCE_START_TLD_DESC_CONST = <<<SOURCE
const TLD_DESC =
SOURCE;
const TLD_DESC_SOURCE_END_TLD_DESC_CONST = <<<SOURCE
;
SOURCE;
const TLD_DESC_SOURCE_END_CLASS = <<<SOURCE
}
SOURCE;
};