2017-12-04 23:33:08 +00:00
#!/usr/bin/env php
< ? php
2017-12-14 04:49:59 +00:00
new class () {
2018-06-13 05:51:12 +00:00
const ME_NAME = 'generate-php-tld-list.php' ;
2017-12-14 04:49:59 +00:00
const TLDS_CSV_PATH = __DIR__ . '/../../tlds.csv' ;
2018-06-13 05:51:12 +00:00
const TLD_ENUM_PATH = __DIR__ . '/../../formats/php/TldEnum/TldList.php' ;
2017-12-14 04:49:59 +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 05:51:12 +00:00
echo " Generates new PHP format file 'TldList.php' from the 'tlds.csv' file \n " ;
2017-12-14 04:49:59 +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 );
}
$tldEnumFile = static :: TLD_ENUM_PATH ;
$tldEnumDir = pathinfo ( $tldEnumFile , \PATHINFO_DIRNAME );
if ( file_exists ( $tldEnumFile )) {
if ( ! is_file ( $tldEnumFile )) {
2018-06-13 05:51:12 +00:00
static :: _echo_error ( " (FATAL) existing path for 'TldList.php' was not a file as expected: $tldEnumFile " , 1 );
2017-12-14 04:49:59 +00:00
}
if ( ! is_writable ( $tldEnumFile )) {
2018-06-13 05:51:12 +00:00
static :: _echo_error ( " (FATAL) existing path for 'TldList.php' is not writable: $tldEnumFile " , 1 );
2017-12-14 04:49:59 +00:00
}
} else {
if ( ! file_exists ( $tldEnumDir ) || ! is_dir ( $tldEnumDir )) {
2018-06-13 05:51:12 +00:00
static :: _echo_error ( " (FATAL) path for 'TldList.php' directory does not exist as expected: $tldEnumDir " , 1 );
2017-12-14 04:49:59 +00:00
}
if ( ! is_writable ( $tldEnumDir )) {
2018-06-13 05:51:12 +00:00
static :: _echo_error ( " (FATAL) path for 'TldList.php' directory is not writeable: $tldEnumDir " , 1 );
2017-12-14 04:49:59 +00:00
}
}
$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 )) {
2018-06-13 05:51:12 +00:00
static :: _echo_error ( " (FATAL) failed to create backup for 'TldList.php' (source: $tldEnumFile , dest: $backupTldEnumFile ) " , 1 );
2017-12-14 04:49:59 +00:00
}
}
}
if ( false === ( $newTldEnumFile = tempnam ( sys_get_temp_dir () , 'tld-enum-' ))) {
2018-06-13 05:51:12 +00:00
static :: _echo_error ( " (FATAL) unable to initialize new 'TldList.php' file " , 1 );
2017-12-14 04:49:59 +00:00
}
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 " ;
}
2018-06-13 05:51:12 +00:00
//echo "new TldList.php: $newTldEnumFile\n";
2017-12-14 04:49:59 +00:00
2018-06-13 05:51:12 +00:00
echo " generating new 'TldList.php' file... " ;
2017-12-14 04:49:59 +00:00
if ( false === file_put_contents ( $newTldEnumFile , static :: TLD_ENUM_SOURCE_START_CLASS )) {
2018-06-13 05:51:12 +00:00
static :: _echo_error ( " (FATAL) failed to write to new 'TldList.php' file " , 1 );
2017-12-14 04:49:59 +00:00
}
if ( false === file_put_contents ( $newTldEnumFile , static :: TLD_ENUM_SOURCE_START_TLD_ENUM_CONST , \FILE_APPEND )) {
2018-06-13 05:51:12 +00:00
static :: _echo_error ( " (FATAL) failed to write to new 'TldList.php' file " , 1 );
2017-12-14 04:49:59 +00:00
}
2018-06-14 21:58:20 +00:00
$tldEnumExport = var_export ( $tldEnum , true );
$tldEnumExport = substr ( $tldEnumExport , strlen ( " array ( " ));
$tldEnumExport = substr ( $tldEnumExport , 0 , - 1 );
$tldEnumLine = [];
$i = 0 ;
foreach ( explode ( " \n " , $tldEnumExport ) as $line ) {
$filteredLine = preg_replace ( '/[0-9]+ => \'/' , " ' " , $line );
$tldEnumLine [] = $filteredLine ;
$i ++ ;
}
unset ( $line );
2017-12-14 04:49:59 +00:00
2018-06-14 21:58:20 +00:00
if ( false === file_put_contents ( $newTldEnumFile , " [ " . implode ( " \n " , $tldEnumLine ) . " ] " , \FILE_APPEND )) {
2018-06-13 05:51:12 +00:00
static :: _echo_error ( " (FATAL) failed to write to new 'TldList.php' file " , 1 );
2017-12-14 04:49:59 +00:00
}
if ( false === file_put_contents ( $newTldEnumFile , static :: TLD_ENUM_SOURCE_END_TLD_ENUM_CONST , \FILE_APPEND )) {
2018-06-13 05:51:12 +00:00
static :: _echo_error ( " (FATAL) failed to write to new 'TldList.php' file " , 1 );
2017-12-14 04:49:59 +00:00
}
if ( false === file_put_contents ( $newTldEnumFile , static :: TLD_ENUM_SOURCE_END_CLASS , \FILE_APPEND )) {
2018-06-13 05:51:12 +00:00
static :: _echo_error ( " (FATAL) failed to write to new 'TldList.php' file " , 1 );
2017-12-14 04:49:59 +00:00
}
echo " done \n " ;
if ( $existingMd5 !== null ) {
$newTldEnumMd5 = md5_file ( $newTldEnumFile );
if ( $existingMd5 == $newTldEnumMd5 ) {
2018-06-13 05:51:12 +00:00
static :: _echo_error ( " (NOTICE) ignoring newly generated 'TldList.php' file that is identical to the existing file (md5: $existingMd5 , path: $tldEnumFile ) " );
2017-12-14 04:49:59 +00:00
return ;
}
if ( ! unlink ( $tldEnumFile )) {
2018-06-13 05:51:12 +00:00
static :: _echo_error ( " (FATAL) failed to remove stale 'TldList.php': $tldEnumFile " , 1 );
2017-12-14 04:49:59 +00:00
}
}
if ( ! copy ( $newTldEnumFile , $tldEnumFile )) {
2018-06-13 05:51:12 +00:00
static :: _echo_error ( " (FATAL) failed to save new 'TldList.php': $tldEnumFile " , 1 );
2017-12-14 04:49:59 +00:00
}
2018-06-13 05:51:12 +00:00
echo " saved new 'TldList.php' file \n " ;
2017-12-14 04:49:59 +00:00
}
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 ;
2018-06-13 05:51:12 +00:00
class TldList {
2017-12-14 04:49:59 +00:00
SOURCE ;
const TLD_ENUM_SOURCE_START_TLD_ENUM_CONST = <<< SOURCE
2018-06-13 05:51:12 +00:00
const TLD_LIST =
2017-12-14 04:49:59 +00:00
SOURCE ;
const TLD_ENUM_SOURCE_END_TLD_ENUM_CONST = <<< SOURCE
;
SOURCE ;
const TLD_ENUM_SOURCE_END_CLASS = <<< SOURCE
}
SOURCE ;
};