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
This commit is contained in:
Doug Bird 2018-06-12 22:51:12 -07:00
parent e289011074
commit 300cc523e0
37 changed files with 36315 additions and 1654 deletions

4
.gitignore vendored
View File

@ -6,6 +6,4 @@ composer.lock
*.log
!**.gitkeep
/*-backup.csv
/formats/php/TldEnum/*-backup.php
/formats/json/*-backup.json
/formats/js/*-backup.js
/formats/**/*-backup.*

View File

@ -0,0 +1,111 @@
#!/usr/bin/env node
const meName = 'generate-js-desc.js';
process.on('unhandledRejection', error => {
console.error(meName + ": (FATAL)", error);
process.exit(1);
});
const countries = require('country-data').countries;
const country = require('countryjs');
const parse = require('csv-parse');
const fs = require('fs-extra');
const path = require('path');
const md5File = require('md5-file/promise');
const pathinfo = require('pathinfo');
const program = require('commander');
const tmp = require('tmp');
//tmp.setGracefulCleanup();
const fileTldDescJs = path.dirname(require.main.filename) + '/../../formats/js/tld-enum/desc.js';
const fileTldsCsv = path.dirname(require.main.filename) + '/../../tlds.csv';
program
.option('-q, --quiet', 'Quiet Mode')
.parse(process.argv);
if (!program.quiet) {
console.log(meName);
console.log(" (c) 2017 Doug Bird, All Rights Reserved.");
console.log(" see README.md for licensing and other information");
console.log(" https://github.com/katmore/tld-enum#readme");
console.log("");
console.log(" Generates new javascript format file 'desc.js' from the 'tlds.csv' file");
console.log("");
}
(async() => {
const tldDescStartTldDesc = 'module.exports = ';
const tldDescEndTldDesc = ';';
const tmpDir = tmp.dirSync({ unsafeCleanup: true });
const fileNewTldDescJs = tmpDir.name + '/desc.js';
let existingMd5 = null;
if (fs.existsSync(fileTldDescJs)) {
existingMd5 = await md5File(fileTldDescJs);
const pathinfoTlds = pathinfo(fileTldDescJs);
const fileBackupTlds = pathinfoTlds.dirname + pathinfoTlds.sep + pathinfoTlds.basename + '-' + existingMd5 + '-backup.js';
if (!fs.existsSync(fileBackupTlds)) {
fs.copySync(fileTldDescJs, fileBackupTlds);
}
}
process.stdout.write("reading 'tlds.csv'...");
let parser = parse({ delimiter: ',' });
let tldDesc = {};
parser.on('readable', function() {
let i = 0;
let row, domain, desc;
while (row = parser.read()) {
if (!row.length) {
console.error(meName + ": (FATAL) invalid 'tlds.csv' row #" + i + " in '" + fileTldsCsv+"'");
process.exit(1);
}
if (typeof row[1] === 'undefined') {
console.error(meName + ": (FATAL) invalid 'tlds.csv', missing column 2 on row #" + i + " in '" + fileTldsCsv+"'");
process.exit(1);
}
domain=row[0];
desc=row[1];
tldDesc[domain]=desc;
i++;
}
});
parser.write(fs.readFileSync(fileTldsCsv));
parser.end();
console.log("done");
process.stdout.write("generating new 'desc.js' file...");
fs.writeFileSync(fileNewTldDescJs, tldDescStartTldDesc);
fs.appendFileSync(fileNewTldDescJs, JSON.stringify(tldDesc, null, 2));
fs.appendFileSync(fileNewTldDescJs, tldDescEndTldDesc);
console.log("done");
if (existingMd5) {
const newMd5 = await md5File(fileNewTldDescJs);
if (newMd5 == existingMd5) {
console.error(meName + ": (NOTICE) ignoring newly generated 'desc.js' file that is identical to the existing file (md5: " + existingMd5 + ", path: " + fileTldDescJs + ")");
return;
}
}
fs.copySync(fileNewTldDescJs, fileTldDescJs);
console.log("saved new 'desc.js' file");
})();

View File

@ -0,0 +1,120 @@
#!/usr/bin/env node
const meName = 'generate-js-info.js';
process.on('unhandledRejection', error => {
console.error(meName + ": (FATAL)", error);
process.exit(1);
});
const countries = require('country-data').countries;
const country = require('countryjs');
const parse = require('csv-parse');
const fs = require('fs-extra');
const path = require('path');
const md5File = require('md5-file/promise');
const pathinfo = require('pathinfo');
const program = require('commander');
const tmp = require('tmp');
//tmp.setGracefulCleanup();
const fileTldInfoJs = path.dirname(require.main.filename) + '/../../formats/js/tld-enum/info.js';
const fileTldsCsv = path.dirname(require.main.filename) + '/../../tlds.csv';
program
.option('-q, --quiet', 'Quiet Mode')
.parse(process.argv);
if (!program.quiet) {
console.log(meName);
console.log(" (c) 2017 Doug Bird, All Rights Reserved.");
console.log(" see README.md for licensing and other information");
console.log(" https://github.com/katmore/tld-enum#readme");
console.log("");
console.log(" Generates new javascript format file 'info.js' from the 'tlds.csv' file");
console.log("");
}
(async() => {
const tldInfoStartTldInfo = 'module.exports = ';
const tldInfoEndTldInfo = ';';
const tmpDir = tmp.dirSync({ unsafeCleanup: true });
const fileNewTldInfoJs = tmpDir.name + '/info.js';
let existingMd5 = null;
if (fs.existsSync(fileTldInfoJs)) {
existingMd5 = await md5File(fileTldInfoJs);
const pathinfoTlds = pathinfo(fileTldInfoJs);
const fileBackupTlds = pathinfoTlds.dirname + pathinfoTlds.sep + pathinfoTlds.basename + '-' + existingMd5 + '-backup.js';
if (!fs.existsSync(fileBackupTlds)) {
fs.copySync(fileTldInfoJs, fileBackupTlds);
}
}
process.stdout.write("reading 'tlds.csv'...");
let parser = parse({ delimiter: ',' });
let tldInfo = [];
parser.on('readable', function() {
let i = 0;
let row, domain, desc, type;
while (row = parser.read()) {
if (!row.length) {
console.error(meName + ": (FATAL) invalid 'tlds.csv' row #" + i + " in '" + fileTldsCsv+"'");
process.exit(1);
}
if (typeof row[1] === 'undefined') {
console.error(meName + ": (FATAL) invalid 'tlds.csv', missing column 2 on row #" + i + " in '" + fileTldsCsv+"'");
process.exit(1);
}
if (typeof row[2] === 'undefined') {
console.error(meName + ": (FATAL) invalid 'tlds.csv', missing column 3 on row #" + i + " in '" + fileTldsCsv+"'");
process.exit(1);
}
domain=row[0];
desc=row[1];
type=row[2];
tldInfo.push({
'domain' : domain,
'description' : desc,
'type' : type,
});
i++;
}
});
parser.write(fs.readFileSync(fileTldsCsv));
parser.end();
console.log("done");
process.stdout.write("generating new 'info.js' file...");
fs.writeFileSync(fileNewTldInfoJs, tldInfoStartTldInfo);
fs.appendFileSync(fileNewTldInfoJs, JSON.stringify(tldInfo, null, 2));
fs.appendFileSync(fileNewTldInfoJs, tldInfoEndTldInfo);
console.log("done");
if (existingMd5) {
const newMd5 = await md5File(fileNewTldInfoJs);
if (newMd5 == existingMd5) {
console.error(meName + ": (NOTICE) ignoring newly generated 'info.js' file that is identical to the existing file (md5: " + existingMd5 + ", path: " + fileTldInfoJs + ")");
return;
}
}
fs.copySync(fileNewTldInfoJs, fileTldInfoJs);
console.log("saved new 'info.js' file");
})();

View File

@ -1,6 +1,6 @@
#!/usr/bin/env node
const meName = 'generate-js-tld-enum.js';
const meName = 'generate-js-list.js';
process.on('unhandledRejection', error => {
console.error(meName + ": (FATAL)", error);
@ -17,9 +17,7 @@ const pathinfo = require('pathinfo');
const program = require('commander');
const tmp = require('tmp');
//tmp.setGracefulCleanup();
const fileTldListJs = path.dirname(require.main.filename) + '/../../formats/js/tld-enum.js';
const fileTldListJs = path.dirname(require.main.filename) + '/../../formats/js/tld-enum/list.js';
const fileTldsCsv = path.dirname(require.main.filename) + '/../../tlds.csv';
program
@ -30,21 +28,20 @@ if (!program.quiet) {
console.log(meName);
console.log(" (c) 2017 Doug Bird, All Rights Reserved.");
console.log(" see README.md for licensing and other information");
console.log(" https://github.com/katmore/tld-enum#readme");
console.log(" https://github.com/katmore/tld-list#readme");
console.log("");
console.log(" Generates new javascript format files from the 'tlds.csv' file");
console.log(" Generates new javascript format file 'list.js' from the 'tlds.csv' file");
console.log("");
}
(async() => {
const tldEnumStartTldList = 'exports.tldList = ';
const tldEnumStartTldList = 'module.exports = ';
const tldEnumEndTldList = ';';
//const tmpDir = tmp.dirSync({ unsafeCleanup: true });
const tmpDir = tmp.dirSync();
const tmpDir = tmp.dirSync({ unsafeCleanup: true });
const fileNewTldListJs = tmpDir.name + '/tld-enum.js';
const fileNewTldListJs = tmpDir.name + '/list.js';
let existingMd5 = null;
@ -82,7 +79,7 @@ if (!program.quiet) {
console.log("done");
process.stdout.write("generating new 'tld-enum.js' file...");
process.stdout.write("generating new 'list.js' file...");
fs.writeFileSync(fileNewTldListJs, tldEnumStartTldList);
@ -95,12 +92,12 @@ if (!program.quiet) {
if (existingMd5) {
const newMd5 = await md5File(fileNewTldListJs);
if (newMd5 == existingMd5) {
console.error(meName + ": (NOTICE) ignoring newly generated 'tld-enum.js' file that is identical to the existing file (md5: " + existingMd5 + ", path: " + fileTldListJs + ")");
console.error(meName + ": (NOTICE) ignoring newly generated 'list.js' file that is identical to the existing file (md5: " + existingMd5 + ", path: " + fileTldListJs + ")");
return;
}
}
fs.copySync(fileNewTldListJs, fileTldListJs);
console.log("saved new 'tld-enum.js' file");
console.log("saved new 'list.js' file");
})();

View File

@ -0,0 +1,111 @@
#!/usr/bin/env node
const meName = 'generate-js-type.js';
process.on('unhandledRejection', error => {
console.error(meName + ": (FATAL)", error);
process.exit(1);
});
const countries = require('country-data').countries;
const country = require('countryjs');
const parse = require('csv-parse');
const fs = require('fs-extra');
const path = require('path');
const md5File = require('md5-file/promise');
const pathinfo = require('pathinfo');
const program = require('commander');
const tmp = require('tmp');
//tmp.setGracefulCleanup();
const fileTldTypeJs = path.dirname(require.main.filename) + '/../../formats/js/tld-enum/type.js';
const fileTldsCsv = path.dirname(require.main.filename) + '/../../tlds.csv';
program
.option('-q, --quiet', 'Quiet Mode')
.parse(process.argv);
if (!program.quiet) {
console.log(meName);
console.log(" (c) 2017 Doug Bird, All Rights Reserved.");
console.log(" see README.md for licensing and other information");
console.log(" https://github.com/katmore/tld-enum#readme");
console.log("");
console.log(" Generates new javascript format file 'type.js' from the 'tlds.csv' file");
console.log("");
}
(async() => {
const tldTypeStartTldType = 'module.exports = ';
const tldTypeEndTldType = ';';
const tmpDir = tmp.dirSync({ unsafeCleanup: true });
const fileNewTldTypeJs = tmpDir.name + '/type.js';
let existingMd5 = null;
if (fs.existsSync(fileTldTypeJs)) {
existingMd5 = await md5File(fileTldTypeJs);
const pathinfoTlds = pathinfo(fileTldTypeJs);
const fileBackupTlds = pathinfoTlds.dirname + pathinfoTlds.sep + pathinfoTlds.basename + '-' + existingMd5 + '-backup.js';
if (!fs.existsSync(fileBackupTlds)) {
fs.copySync(fileTldTypeJs, fileBackupTlds);
}
}
process.stdout.write("reading 'tlds.csv'...");
let parser = parse({ delimiter: ',' });
let tldType = {};
parser.on('readable', function() {
let i = 0;
let row, domain, type;
while (row = parser.read()) {
if (!row.length) {
console.error(meName + ": (FATAL) invalid 'tlds.csv' row #" + i + " in '" + fileTldsCsv+"'");
process.exit(1);
}
if (typeof row[2] === 'undefined') {
console.error(meName + ": (FATAL) invalid 'tlds.csv', missing column 3 on row #" + i + " in '" + fileTldsCsv+"'");
process.exit(1);
}
domain=row[0];
type=row[2];
tldType[domain]=type;
i++;
}
});
parser.write(fs.readFileSync(fileTldsCsv));
parser.end();
console.log("done");
process.stdout.write("generating new 'type.js' file...");
fs.writeFileSync(fileNewTldTypeJs, tldTypeStartTldType);
fs.appendFileSync(fileNewTldTypeJs, JSON.stringify(tldType, null, 2));
fs.appendFileSync(fileNewTldTypeJs, tldTypeEndTldType);
console.log("done");
if (existingMd5) {
const newMd5 = await md5File(fileNewTldTypeJs);
if (newMd5 == existingMd5) {
console.error(meName + ": (NOTICE) ignoring newly generated 'type.js' file that is identical to the existing file (md5: " + existingMd5 + ", path: " + fileTldTypeJs + ")");
return;
}
}
fs.copySync(fileNewTldTypeJs, fileTldTypeJs);
console.log("saved new 'type.js' file");
})();

View File

@ -0,0 +1,104 @@
#!/usr/bin/env node
const meName = 'generate-js-tld-desc.json';
process.on('unhandledRejection', error => {
console.error(meName + ": (FATAL)", error);
process.exit(1);
});
const countries = require('country-data').countries;
const country = require('countryjs');
const parse = require('csv-parse');
const fs = require('fs-extra');
const path = require('path');
const md5File = require('md5-file/promise');
const pathinfo = require('pathinfo');
const program = require('commander');
const tmp = require('tmp');
//tmp.setGracefulCleanup();
const fileTldDescJs = path.dirname(require.main.filename) + '/../../formats/json/tld-desc.json';
const fileTldsCsv = path.dirname(require.main.filename) + '/../../tlds.csv';
program
.option('-q, --quiet', 'Quiet Mode')
.parse(process.argv);
if (!program.quiet) {
console.log(meName);
console.log(" (c) 2017 Doug Bird, All Rights Reserved.");
console.log(" see README.md for licensing and other information");
console.log(" https://github.com/katmore/tld-enum#readme");
console.log("");
console.log(" Generates new javascript desc format file from the 'tlds.csv' file");
console.log("");
}
(async() => {
const tmpDir = tmp.dirSync({ unsafeCleanup: true });
const fileNewTldDescJson = tmpDir.name + '/tld-desc.json';
let existingMd5 = null;
if (fs.existsSync(fileTldDescJs)) {
existingMd5 = await md5File(fileTldDescJs);
const pathinfoTlds = pathinfo(fileTldDescJs);
const fileBackupTlds = pathinfoTlds.dirname + pathinfoTlds.sep + pathinfoTlds.basename + '-' + existingMd5 + '-backup.js';
if (!fs.existsSync(fileBackupTlds)) {
fs.copySync(fileTldDescJs, fileBackupTlds);
}
}
process.stdout.write("reading 'tlds.csv'...");
let parser = parse({ delimiter: ',' });
let tldDesc = {};
parser.on('readable', function() {
let i = 0;
let row, domain, desc;
while (row = parser.read()) {
if (!row.length) {
console.error(meName + ": (FATAL) invalid 'tlds.csv' row #" + i + " in '" + fileTldsCsv+"'");
process.exit(1);
}
if (typeof row[1] === 'undefined') {
console.error(meName + ": (FATAL) invalid 'tlds.csv', missing column 2 on row #" + i + " in '" + fileTldsCsv+"'");
process.exit(1);
}
domain=row[0];
desc=row[1];
tldDesc[domain]=desc;
i++;
}
});
parser.write(fs.readFileSync(fileTldsCsv));
parser.end();
console.log("done");
process.stdout.write("generating new 'tld-desc.json' file...");
fs.appendFileSync(fileNewTldDescJson, JSON.stringify(tldDesc, null, 2));
console.log("done");
if (existingMd5) {
const newMd5 = await md5File(fileNewTldDescJson);
if (newMd5 == existingMd5) {
console.error(meName + ": (NOTICE) ignoring newly generated 'tld-desc.json' file that is identical to the existing file (md5: " + existingMd5 + ", path: " + fileTldDescJs + ")");
return;
}
}
fs.copySync(fileNewTldDescJson, fileTldDescJs);
console.log("saved new 'tld-desc.json' file");
})();

View File

@ -0,0 +1,113 @@
#!/usr/bin/env node
const meName = 'generate-js-tld-info.json';
process.on('unhandledRejection', error => {
console.error(meName + ": (FATAL)", error);
process.exit(1);
});
const countries = require('country-data').countries;
const country = require('countryjs');
const parse = require('csv-parse');
const fs = require('fs-extra');
const path = require('path');
const md5File = require('md5-file/promise');
const pathinfo = require('pathinfo');
const program = require('commander');
const tmp = require('tmp');
//tmp.setGracefulCleanup();
const fileTldInfoJs = path.dirname(require.main.filename) + '/../../formats/json/tld-info.json';
const fileTldsCsv = path.dirname(require.main.filename) + '/../../tlds.csv';
program
.option('-q, --quiet', 'Quiet Mode')
.parse(process.argv);
if (!program.quiet) {
console.log(meName);
console.log(" (c) 2017 Doug Bird, All Rights Reserved.");
console.log(" see README.md for licensing and other information");
console.log(" https://github.com/katmore/tld-enum#readme");
console.log("");
console.log(" Generates new javascript desc format file from the 'tlds.csv' file");
console.log("");
}
(async() => {
const tmpDir = tmp.dirSync({ unsafeCleanup: true });
const fileNewTldInfoJs = tmpDir.name + '/tld-info.json';
let existingMd5 = null;
if (fs.existsSync(fileTldInfoJs)) {
existingMd5 = await md5File(fileTldInfoJs);
const pathinfoTlds = pathinfo(fileTldInfoJs);
const fileBackupTlds = pathinfoTlds.dirname + pathinfoTlds.sep + pathinfoTlds.basename + '-' + existingMd5 + '-backup.js';
if (!fs.existsSync(fileBackupTlds)) {
fs.copySync(fileTldInfoJs, fileBackupTlds);
}
}
process.stdout.write("reading 'tlds.csv'...");
let parser = parse({ delimiter: ',' });
let tldInfo = [];
parser.on('readable', function() {
let i = 0;
let row, domain, desc, type;
while (row = parser.read()) {
if (!row.length) {
console.error(meName + ": (FATAL) invalid 'tlds.csv' row #" + i + " in '" + fileTldsCsv+"'");
process.exit(1);
}
if (typeof row[1] === 'undefined') {
console.error(meName + ": (FATAL) invalid 'tlds.csv', missing column 2 on row #" + i + " in '" + fileTldsCsv+"'");
process.exit(1);
}
if (typeof row[2] === 'undefined') {
console.error(meName + ": (FATAL) invalid 'tlds.csv', missing column 3 on row #" + i + " in '" + fileTldsCsv+"'");
process.exit(1);
}
domain=row[0];
desc=row[1];
type=row[2];
tldInfo.push({
'domain' : domain,
'description' : desc,
'type' : type,
});
i++;
}
});
parser.write(fs.readFileSync(fileTldsCsv));
parser.end();
console.log("done");
process.stdout.write("generating new 'tld-info.json' file...");
fs.appendFileSync(fileNewTldInfoJs, JSON.stringify(tldInfo, null, 2));
console.log("done");
if (existingMd5) {
const newMd5 = await md5File(fileNewTldInfoJs);
if (newMd5 == existingMd5) {
console.error(meName + ": (NOTICE) ignoring newly generated 'tld-info.json' file that is identical to the existing file (md5: " + existingMd5 + ", path: " + fileTldInfoJs + ")");
return;
}
}
fs.copySync(fileNewTldInfoJs, fileTldInfoJs);
console.log("saved new 'tld-info.json' file");
})();

View File

@ -0,0 +1,104 @@
#!/usr/bin/env node
const meName = 'generate-js-tld-type.json';
process.on('unhandledRejection', error => {
console.error(meName + ": (FATAL)", error);
process.exit(1);
});
const countries = require('country-data').countries;
const country = require('countryjs');
const parse = require('csv-parse');
const fs = require('fs-extra');
const path = require('path');
const md5File = require('md5-file/promise');
const pathinfo = require('pathinfo');
const program = require('commander');
const tmp = require('tmp');
//tmp.setGracefulCleanup();
const fileTldTypeJs = path.dirname(require.main.filename) + '/../../formats/json/tld-type.json';
const fileTldsCsv = path.dirname(require.main.filename) + '/../../tlds.csv';
program
.option('-q, --quiet', 'Quiet Mode')
.parse(process.argv);
if (!program.quiet) {
console.log(meName);
console.log(" (c) 2017 Doug Bird, All Rights Reserved.");
console.log(" see README.md for licensing and other information");
console.log(" https://github.com/katmore/tld-enum#readme");
console.log("");
console.log(" Generates new javascript desc format file from the 'tlds.csv' file");
console.log("");
}
(async() => {
const tmpDir = tmp.dirSync({ unsafeCleanup: true });
const fileNewTldTypeJson = tmpDir.name + '/tld-type.json';
let existingMd5 = null;
if (fs.existsSync(fileTldTypeJs)) {
existingMd5 = await md5File(fileTldTypeJs);
const pathinfoTlds = pathinfo(fileTldTypeJs);
const fileBackupTlds = pathinfoTlds.dirname + pathinfoTlds.sep + pathinfoTlds.basename + '-' + existingMd5 + '-backup.js';
if (!fs.existsSync(fileBackupTlds)) {
fs.copySync(fileTldTypeJs, fileBackupTlds);
}
}
process.stdout.write("reading 'tlds.csv'...");
let parser = parse({ delimiter: ',' });
let tldType = {};
parser.on('readable', function() {
let i = 0;
let row, domain, type;
while (row = parser.read()) {
if (!row.length) {
console.error(meName + ": (FATAL) invalid 'tlds.csv' row #" + i + " in '" + fileTldsCsv+"'");
process.exit(1);
}
if (typeof row[2] === 'undefined') {
console.error(meName + ": (FATAL) invalid 'tlds.csv', missing column 3 on row #" + i + " in '" + fileTldsCsv+"'");
process.exit(1);
}
domain=row[0];
type=row[2];
tldType[domain]=type;
i++;
}
});
parser.write(fs.readFileSync(fileTldsCsv));
parser.end();
console.log("done");
process.stdout.write("generating new 'tld-type.json' file...");
fs.appendFileSync(fileNewTldTypeJson, JSON.stringify(tldType, null, 2));
console.log("done");
if (existingMd5) {
const newMd5 = await md5File(fileNewTldTypeJson);
if (newMd5 == existingMd5) {
console.error(meName + ": (NOTICE) ignoring newly generated 'tld-type.json' file that is identical to the existing file (md5: " + existingMd5 + ", path: " + fileTldTypeJs + ")");
return;
}
}
fs.copySync(fileNewTldTypeJson, fileTldTypeJs);
console.log("saved new 'tld-type.json' file");
})();

View File

@ -0,0 +1,179 @@
#!/usr/bin/env php
<?php
new class() {
const ME_NAME = 'generate-php-tld-enum.php';
const TLDS_CSV_PATH = __DIR__ . '/../../tlds.csv';
const TLD_DESC_PATH = __DIR__ . '/../../formats/php/TldEnum/TldDesc.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);
}
$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 "new TldDesc.php: $newTldDescFile\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);
$tldDescExport = str_replace('array','',$tldDescExport);
$tldDescExport = str_replace('(','[',$tldDescExport);
$tldDescExport = str_replace(')',']',$tldDescExport);
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;
};

View File

@ -0,0 +1,211 @@
#!/usr/bin/env php
<?php
new class() {
const ME_NAME = 'generate-php-tld-enum.php';
const TLDS_CSV_PATH = __DIR__ . '/../../tlds.csv';
const TLD_INFO_PATH = __DIR__ . '/../../formats/php/TldEnum/TldInfo.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);
}
$tldInfoFile = static::TLD_INFO_PATH;
$tldInfoDir = pathinfo($tldInfoFile,\PATHINFO_DIRNAME);
if (file_exists($tldInfoFile)) {
if (!is_file($tldInfoFile)) {
static::_echo_error("(FATAL) existing path for 'TldInfo.php' was not a file as expected: $tldInfoFile",1);
}
if (!is_writable($tldInfoFile)) {
static::_echo_error("(FATAL) existing path for 'TldInfo.php' is not writable: $tldInfoFile",1);
}
} else {
if (!file_exists($tldInfoDir) || !is_dir($tldInfoDir)) {
static::_echo_error("(FATAL) path for 'TldInfo.php' directory does not exist as expected: $tldInfoDir",1);
}
if (!is_writable($tldInfoDir)) {
static::_echo_error("(FATAL) path for 'TldInfo.php' directory is not writeable: $tldInfoDir",1);
}
}
$existingMd5 = null;
if (file_exists($tldInfoFile)) {
$existingMd5 = md5_file($tldInfoFile);
$tldInfoBasename = pathinfo($tldInfoFile,\PATHINFO_FILENAME);
$backupTldInfoFile = $tldInfoDir . \DIRECTORY_SEPARATOR . "$tldInfoBasename-$existingMd5-backup.php";
if (!file_exists($backupTldInfoFile)) {
if (!copy($tldInfoFile,$backupTldInfoFile)) {
static::_echo_error("(FATAL) failed to create backup for 'TldInfo.php' (source: $tldInfoFile, dest: $backupTldInfoFile)",1);
}
}
}
if (false === ($newTldInfoFile = tempnam ( sys_get_temp_dir() , 'tld-enum-' ))) {
static::_echo_error("(FATAL) unable to initialize new 'TldInfo.php' file",1);
}
register_shutdown_function(function() use($newTldInfoFile)
{
if (is_file($newTldInfoFile) && is_writable($newTldInfoFile)) {
unlink($newTldInfoFile);
}
});
echo "generating new 'TldInfo.php' file...";
if (false === file_put_contents($newTldInfoFile,static::TLD_INFO_SOURCE_START_CLASS)) {
static::_echo_error("(FATAL) failed to write to new 'TldInfo.php' file",1);
}
if (false === file_put_contents($newTldInfoFile,static::TLD_INFO_SOURCE_START_TLD_INFO_CONST,\FILE_APPEND)) {
static::_echo_error("(FATAL) failed to write to new 'TldInfo.php' file",1);
}
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);
}
if (!isset($row[2])) {
static::_echo_error("(FATAL) invalid 'tlds.csv', missing column 3 on row #$i: $fileTldsCsv",1);
}
$domain = $row[0];
$desc = $row[1];
$type = $row[2];
$tldInfoElem = [
'domain'=>$domain,
'description'=>$desc,
'type'=>$type,
];
$tldInfoElem = var_export($tldInfoElem, true);
$tldInfoElem = str_replace('array','',$tldInfoElem);
$tldInfoElem = str_replace('(','[',$tldInfoElem);
$tldInfoElem = str_replace(')',']',$tldInfoElem);
if ($i!=0) {
$tldInfoElem = ",\n$tldInfoElem";
}
if (false === file_put_contents($newTldInfoFile, $tldInfoElem,\FILE_APPEND)) {
static::_echo_error("(FATAL) failed to write to new 'TldInfo.php' file",1);
}
$i++;
}
fclose($handle);
echo "done\n";
}
//echo "new TldInfo.php: $newTldInfoFile\n";
if (false === file_put_contents($newTldInfoFile,static::TLD_INFO_SOURCE_END_TLD_INFO_CONST,\FILE_APPEND)) {
static::_echo_error("(FATAL) failed to write to new 'TldInfo.php' file",1);
}
if (false === file_put_contents($newTldInfoFile,static::TLD_INFO_SOURCE_END_CLASS,\FILE_APPEND)) {
static::_echo_error("(FATAL) failed to write to new 'TldInfo.php' file",1);
}
echo "done\n";
if ($existingMd5!==null) {
$newTldInfoMd5 = md5_file($newTldInfoFile);
if ($existingMd5 == $newTldInfoMd5) {
static::_echo_error("(NOTICE) ignoring newly generated 'TldInfo.php' file that is identical to the existing file (md5: $existingMd5, path: $tldInfoFile)");
return;
}
if (!unlink($tldInfoFile)) {
static::_echo_error("(FATAL) failed to remove stale 'TldInfo.php': $tldInfoFile",1);
}
}
if (!copy($newTldInfoFile,$tldInfoFile)) {
static::_echo_error("(FATAL) failed to save new 'TldInfo.php': $tldInfoFile",1);
}
echo "saved new 'TldInfo.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_INFO_SOURCE_START_CLASS = <<<SOURCE
<?php
namespace TldEnum;
class TldInfo {
SOURCE;
const TLD_INFO_SOURCE_START_TLD_INFO_CONST = <<<SOURCE
const TLD_INFO = [
SOURCE;
const TLD_INFO_SOURCE_END_TLD_INFO_CONST = <<<SOURCE
];
SOURCE;
const TLD_INFO_SOURCE_END_CLASS = <<<SOURCE
/**
* converts a TLD_INFO element into a TldInfoItem object
*
* @param array \$info element from \\TldEnum\\TldInfo::TLD_INFO
*
* @return \\TldEnum\\TldInfoItem
*/
public static function toInfoItem(array \$info) : TldInfoItem {
\$infoItem = new TldInfoItem;
foreach(\$infoItem as \$prop=>&\$val) {
if (isset(\$info[\$prop])) {
\$val = \$info[\$prop];
}
}
unset(\$prop);
unset(\$val);
return \$infoItem;
}
}
SOURCE;
};

View File

@ -1,9 +1,9 @@
#!/usr/bin/env php
<?php
new class() {
const ME_NAME = 'generate-php-tld-enum.php';
const ME_NAME = 'generate-php-tld-list.php';
const TLDS_CSV_PATH = __DIR__ . '/../../tlds.csv';
const TLD_ENUM_PATH = __DIR__ . '/../../formats/php/TldEnum/TldEnum.php';
const TLD_ENUM_PATH = __DIR__ . '/../../formats/php/TldEnum/TldList.php';
private static $_quietMode = false;
public function __construct() {
$opt = getopt ( "q",['quiet']);
@ -17,7 +17,7 @@ new class() {
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 " Generates new PHP format file 'TldList.php' from the 'tlds.csv' file\n";
echo "\n";
}
@ -31,17 +31,17 @@ new class() {
$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);
static::_echo_error("(FATAL) existing path for 'TldList.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);
static::_echo_error("(FATAL) existing path for 'TldList.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);
static::_echo_error("(FATAL) path for 'TldList.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);
static::_echo_error("(FATAL) path for 'TldList.php' directory is not writeable: $tldEnumDir",1);
}
}
@ -52,13 +52,13 @@ new class() {
$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);
static::_echo_error("(FATAL) failed to create backup for 'TldList.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);
static::_echo_error("(FATAL) unable to initialize new 'TldList.php' file",1);
}
register_shutdown_function(function() use($newTldEnumFile) {
@ -85,16 +85,16 @@ new class() {
echo "done\n";
}
//echo "new TldEnum.php: $newTldEnumFile\n";
//echo "new TldList.php: $newTldEnumFile\n";
echo "generating new 'TldEnum.php' file...";
echo "generating new 'TldList.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);
static::_echo_error("(FATAL) failed to write to new 'TldList.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);
static::_echo_error("(FATAL) failed to write to new 'TldList.php' file",1);
}
$tldEnumExport = var_export($tldEnum, true);
@ -107,15 +107,15 @@ new class() {
//$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);
static::_echo_error("(FATAL) failed to write to new 'TldList.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);
static::_echo_error("(FATAL) failed to write to new 'TldList.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);
static::_echo_error("(FATAL) failed to write to new 'TldList.php' file",1);
}
echo "done\n";
@ -123,19 +123,19 @@ new class() {
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)");
static::_echo_error("(NOTICE) ignoring newly generated 'TldList.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);
static::_echo_error("(FATAL) failed to remove stale 'TldList.php': $tldEnumFile",1);
}
}
if (!copy($newTldEnumFile,$tldEnumFile)) {
static::_echo_error("(FATAL) failed to save new 'TldEnum.php': $tldEnumFile",1);
static::_echo_error("(FATAL) failed to save new 'TldList.php': $tldEnumFile",1);
}
echo "saved new 'TldEnum.php' file\n";
echo "saved new 'TldList.php' file\n";
}
@ -159,12 +159,12 @@ new class() {
<?php
namespace TldEnum;
class TldEnum {
class TldList {
SOURCE;
const TLD_ENUM_SOURCE_START_TLD_ENUM_CONST = <<<SOURCE
const TLD_ENUM =
const TLD_LIST =
SOURCE;
const TLD_ENUM_SOURCE_END_TLD_ENUM_CONST = <<<SOURCE

View File

@ -0,0 +1,179 @@
#!/usr/bin/env php
<?php
new class() {
const ME_NAME = 'generate-php-tld-enum.php';
const TLDS_CSV_PATH = __DIR__ . '/../../tlds.csv';
const TLD_TYPE_PATH = __DIR__ . '/../../formats/php/TldEnum/TldType.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);
}
$tldTypeFile = static::TLD_TYPE_PATH;
$tldTypeDir = pathinfo($tldTypeFile,\PATHINFO_DIRNAME);
if (file_exists($tldTypeFile)) {
if (!is_file($tldTypeFile)) {
static::_echo_error("(FATAL) existing path for 'TldType.php' was not a file as expected: $tldTypeFile",1);
}
if (!is_writable($tldTypeFile)) {
static::_echo_error("(FATAL) existing path for 'TldType.php' is not writable: $tldTypeFile",1);
}
} else {
if (!file_exists($tldTypeDir) || !is_dir($tldTypeDir)) {
static::_echo_error("(FATAL) path for 'TldType.php' directory does not exist as expected: $tldTypeDir",1);
}
if (!is_writable($tldTypeDir)) {
static::_echo_error("(FATAL) path for 'TldType.php' directory is not writeable: $tldTypeDir",1);
}
}
$existingMd5 = null;
if (file_exists($tldTypeFile)) {
$existingMd5 = md5_file($tldTypeFile);
$tldTypeBasename = pathinfo($tldTypeFile,\PATHINFO_FILENAME);
$backupTldTypeFile = $tldTypeDir . \DIRECTORY_SEPARATOR . "$tldTypeBasename-$existingMd5-backup.php";
if (!file_exists($backupTldTypeFile)) {
if (!copy($tldTypeFile,$backupTldTypeFile)) {
static::_echo_error("(FATAL) failed to create backup for 'TldType.php' (source: $tldTypeFile, dest: $backupTldTypeFile)",1);
}
}
}
if (false === ($newTldTypeFile = tempnam ( sys_get_temp_dir() , 'tld-enum-' ))) {
static::_echo_error("(FATAL) unable to initialize new 'TldType.php' file",1);
}
register_shutdown_function(function() use($newTldTypeFile) {
if (is_file($newTldTypeFile) && is_writable($newTldTypeFile)) {
unlink($newTldTypeFile);
}
});
$tldType = [];
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[2])) {
static::_echo_error("(FATAL) invalid 'tlds.csv', missing column 3 on row #$i: $fileTldsCsv",1);
}
$domain = $row[0];
$type = $row[2];
$tldType[$domain] = $type;
$i++;
}
fclose($handle);
echo "done\n";
}
//echo "new TldType.php: $newTldTypeFile\n";
echo "generating new 'TldType.php' file...";
if (false === file_put_contents($newTldTypeFile,static::TLD_TYPE_SOURCE_START_CLASS)) {
static::_echo_error("(FATAL) failed to write to new 'TldType.php' file",1);
}
if (false === file_put_contents($newTldTypeFile,static::TLD_TYPE_SOURCE_START_TLD_TYPE_CONST,\FILE_APPEND)) {
static::_echo_error("(FATAL) failed to write to new 'TldType.php' file",1);
}
$tldTypeExport = var_export($tldType, true);
$tldTypeExport = str_replace('array','',$tldTypeExport);
$tldTypeExport = str_replace('(','[',$tldTypeExport);
$tldTypeExport = str_replace(')',']',$tldTypeExport);
if (false === file_put_contents($newTldTypeFile, $tldTypeExport,\FILE_APPEND)) {
static::_echo_error("(FATAL) failed to write to new 'TldType.php' file",1);
}
if (false === file_put_contents($newTldTypeFile,static::TLD_TYPE_SOURCE_END_TLD_TYPE_CONST,\FILE_APPEND)) {
static::_echo_error("(FATAL) failed to write to new 'TldType.php' file",1);
}
if (false === file_put_contents($newTldTypeFile,static::TLD_TYPE_SOURCE_END_CLASS,\FILE_APPEND)) {
static::_echo_error("(FATAL) failed to write to new 'TldType.php' file",1);
}
echo "done\n";
if ($existingMd5!==null) {
$newTldTypeMd5 = md5_file($newTldTypeFile);
if ($existingMd5 == $newTldTypeMd5) {
static::_echo_error("(NOTICE) ignoring newly generated 'TldType.php' file that is identical to the existing file (md5: $existingMd5, path: $tldTypeFile)");
return;
}
if (!unlink($tldTypeFile)) {
static::_echo_error("(FATAL) failed to remove stale 'TldType.php': $tldTypeFile",1);
}
}
if (!copy($newTldTypeFile,$tldTypeFile)) {
static::_echo_error("(FATAL) failed to save new 'TldType.php': $tldTypeFile",1);
}
echo "saved new 'TldType.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_TYPE_SOURCE_START_CLASS = <<<SOURCE
<?php
namespace TldEnum;
class TldType {
SOURCE;
const TLD_TYPE_SOURCE_START_TLD_TYPE_CONST = <<<SOURCE
const TLD_TYPE =
SOURCE;
const TLD_TYPE_SOURCE_END_TLD_TYPE_CONST = <<<SOURCE
;
SOURCE;
const TLD_TYPE_SOURCE_END_CLASS = <<<SOURCE
}
SOURCE;
};

View File

@ -1,5 +1,5 @@
#!/bin/sh
# Updates the tlds.csv file from IANA and re-generates the native format files
# Updates the tlds.csv file using IANA data and re-generates the format files
#
# @author D. Bird <retran@gmail.com>
#
@ -19,6 +19,7 @@ QUIET_MODE=0
HELP_MODE=0
FORCE_PHP=0
SKIP_PHP=0
SKIP_CSV=0
while getopts :?qhua-: arg; do { case $arg in
q) QUIET_MODE=1;;
h|u|a) HELP_MODE=1;;
@ -26,6 +27,7 @@ while getopts :?qhua-: arg; do { case $arg in
quiet) QUIET_MODE=1;;
force-php) FORCE_PHP=1;;
skip-php) SKIP_PHP=1;;
skip-csv) SKIP_CSV=1;;
help|usage|about) HELP_MODE=1;;
*) >&2 echo "$ME_NAME: unrecognized long option --$OPTARG"; OPTION_STATUS=2;;
esac ;;
@ -48,27 +50,27 @@ fi
#
if ( [ "$QUIET_MODE" != "1" ] || [ "$HELP_MODE" = "1" ] ); then
echo "TLD update utility"
echo ""
echo "Updates the tlds.csv file from IANA and re-generates the native format files."
echo ""
echo "(c) 2017-2018 Doug Bird, All Rights Reserved."
echo "see README.md for licensing and other information"
echo "https://github.com/katmore/tld-enum#readme"
echo ""
fi
#
# apply help mode
#
if [ "$HELP_MODE" = "1" ]; then
echo "usage:"
echo " $ME_NAME [-h] | [-q][--skip-php]"
echo ""
echo "options:"
echo " -h,--help: Print a help message and exit."
echo " -q,--quiet: Print less messages."
echo " --force-php: Creating the PHP format file is mandatory."
echo " --skip-php: Always skip creating the PHP format file."
echo "Updates the tlds.csv file using IANA data and re-generates the format files."
echo ""
echo "usage:"
echo " $ME_NAME [-h]|[-q][format file options...]"
echo ""
echo "-h,--help: Print a help message and exit."
echo "-q,--quiet: Print only critical messages."
echo ""
echo "format file options:"
echo " --force-php: Creating the PHP format files is mandatory."
echo " --skip-php: Always skip creating the PHP format files."
echo " --skip-csv: Use existing tlds.csv and do not download new data from IANA."
exit 0
fi
@ -106,10 +108,10 @@ helper() { helper_file=$1; helper_label=$2
cmd_status=$?
[ "$cmd_status" != "0" ] && [ -n "$cmd_output" ] && >&2 printf "$cmd_output\n"
else
printf "generate new $helper_label: started\n\n"
printf "generate new $helper_label format file: started\n"
"$HELPER_DIR/$helper_file" -q
cmd_status=$?
[ "$cmd_status" = "0" ] && printf "\ngenerate new $helper_label: success\n"
[ "$cmd_status" = "0" ] && printf "generate new $helper_label format file: success\n"
fi
if [ "$cmd_status" != "0" ]; then
>&2 echo "$ME_NAME: (FATAL) helper for $helper_label failed ($helper_file exit status $cmd_status)"
@ -119,19 +121,47 @@ helper() { helper_file=$1; helper_label=$2
}
#
# execute the helpers
# execute CSV helper
#
helper generate-tlds-csv.js "tlds.csv CSV format file"
helper generate-js-tld-enum.js "JavaScript format files"
helper generate-json-tld-enum.js "JSON format files"
if [ "$SKIP_PHP" != "1" ]; then
helper generate-php-tld-enum.php "PHP format files"
if [ "$SKIP_CSV" != "1" ]; then
helper generate-tlds-csv.js "CSV 'tlds.csv'"
else
[ "$QUIET_MODE" = "0" ] && echo "$ME_NAME: (NOTICE) skipped PHP format files"
[ "$QUIET_MODE" = "0" ] && printf "$ME_NAME: (NOTICE) skipped downloading IANA data and skipped generating new CSV\n"
fi
#
# execute JavaScript helpers
#
helper generate-js-tld-desc.js "JavaScript 'desc.js'"
helper generate-js-tld-info.js "JavaScript 'info.js'"
helper generate-js-tld-list.js "JavaScript 'list.js'"
helper generate-js-tld-type.js "JavaScript 'type.js'"
#
# execute JSON helpers
#
helper generate-json-tld-desc.js "JSON 'tld-desc.json'"
helper generate-json-tld-info.js "JSON 'tld-info.json'"
helper generate-json-tld-list.js "JSON 'tld-list.json'"
helper generate-json-tld-type.js "JSON 'tld-type.json'"
#
# execute PHP helpers
#
if [ "$SKIP_PHP" != "1" ]; then
helper generate-php-tld-desc.php "PHP 'TldDesc.php'"
helper generate-php-tld-info.php "PHP 'TldInfo.php'"
helper generate-php-tld-list.php "PHP 'TldList.php'"
helper generate-php-tld-type.php "PHP 'TldType.php'"
else
[ "$QUIET_MODE" = "0" ] && printf "$ME_NAME: (NOTICE) skipped generating PHP format files\n"
fi
#
# success
#
if [ "$QUIET_MODE" = "0" ]; then
echo "format file updates were successful"
printf "format file updates were successful\n"
fi

View File

@ -1,13 +1,12 @@
//const tldEnum = require('tld-enum');
const tldEnum = require('../formats/js/tld-enum');
const tldList = require('../formats/js/tld-enum/list');
console.log("There are " + tldEnum.tldList.length + " IANA TLDs");
console.log("There are " + tldList.length + " IANA TLDs");
let tldCheck;
tldCheck = "com";
console.log("Is '" + tldCheck + "' a real TLD?");
if (tldEnum.tldList.indexOf(tldCheck.toLowerCase()) != -1) {
if (tldList.indexOf(tldCheck.toLowerCase()) != -1) {
console.log(" yes");
} else {
console.log(" no");
@ -15,7 +14,7 @@ if (tldEnum.tldList.indexOf(tldCheck.toLowerCase()) != -1) {
tldCheck = "somethingWeird";
console.log("Is '" + tldCheck + "' a real TLD?");
if (tldEnum.tldList.indexOf(tldCheck.toLowerCase()) != -1) {
if (tldList.indexOf(tldCheck.toLowerCase()) != -1) {
console.log(" yes");
} else {
console.log(" no");

27
examples/js-desc-demo.js Normal file
View File

@ -0,0 +1,27 @@
const tldDesc = require('../formats/js/tld-enum/desc');
console.log("There are " + Object.keys(tldDesc).length + " IANA TLDs");
console.log("");
let tldCheck;
tldCheck = "com";
console.log("Is '" + tldCheck + "' a real TLD?");
if (typeof(tldDesc[tldCheck.toLowerCase()])!=='undefined') {
console.log("yes");
console.log("description: "+tldDesc[tldCheck.toLowerCase()]);
} else {
console.log("no");
}
console.log("");
tldCheck = "somethingWeird";
console.log("Is '" + tldCheck + "' a real TLD?");
if (typeof(tldDesc[tldCheck.toLowerCase()])!=='undefined') {
console.log("yes");
console.log("description: "+tldDesc[tldCheck.toLowerCase()]);
} else {
console.log("no");
}

37
examples/js-info-demo.js Normal file
View File

@ -0,0 +1,37 @@
const tldInfo = require('../formats/js/tld-enum').info;
console.log("There are " + Object.keys(tldInfo).length + " IANA TLDs");
console.log("");
let tldCheck, foundTld;
tldCheck = "com";
console.log("Is '" + tldCheck + "' a real TLD?");
foundTld = tldInfo.find(function(e) {
return e.domain==tldCheck.toLowerCase();
});
if (typeof(foundTld)!=='undefined') {
console.log("yes");
console.log(JSON.stringify(foundTld, null, 2));
} else {
console.log("no");
}
console.log("");
tldCheck = "somethingWeird";
console.log("Is '" + tldCheck + "' a real TLD?");
foundTld = tldInfo.find(function(e) {
return e.domain==tldCheck.toLowerCase();
});
if (typeof(foundTld)!=='undefined') {
console.log("yes");
console.log(JSON.stringify(foundTld, null, 2));
} else {
console.log("no");
}

27
examples/js-type-demo.js Normal file
View File

@ -0,0 +1,27 @@
const tldType = require('../formats/js/tld-enum/type');
console.log("There are " + Object.keys(tldType).length + " IANA TLDs");
console.log("");
let tldCheck;
tldCheck = "com";
console.log("Is '" + tldCheck + "' a real TLD?");
if (typeof(tldType[tldCheck.toLowerCase()])!=='undefined') {
console.log("yes");
console.log("type: "+tldType[tldCheck.toLowerCase()]);
} else {
console.log("no");
}
console.log("");
tldCheck = "somethingWeird";
console.log("Is '" + tldCheck + "' a real TLD?");
if (typeof(tldType[tldCheck.toLowerCase()])!=='undefined') {
console.log("yes");
console.log("type: "+tldType[tldCheck.toLowerCase()]);
} else {
console.log("no");
}

View File

@ -0,0 +1,32 @@
<?php
use TldEnum\TldDesc;
require __DIR__ . '/../vendor/autoload.php';
echo "There are " . count(TldDesc::TLD_DESC) . " IANA TLDs\n";
echo "\n";
$tldCheck = "com";
echo "Is '$tldCheck' a real TLD?\n";
if (isset(TldDesc::TLD_DESC[strtolower($tldCheck)])) {
echo "yes\n";
echo "description: ".TldDesc::TLD_DESC[strtolower($tldCheck)]."\n";
} else {
echo "no\n";
}
echo "\n";
$tldCheck = "somethingWeird";
echo "Is '$tldCheck' a real TLD?\n";
if (isset(TldDesc::TLD_DESC[strtolower($tldCheck)])) {
echo "yes\n";
echo "description: ".TldDesc::TLD_DESC[strtolower($tldCheck)]."\n";
} else {
echo "no\n";
}
echo "\n";

View File

@ -0,0 +1,54 @@
<?php
use TldEnum\TldInfo;
require __DIR__ . '/../vendor/autoload.php';
echo "There are " . count(TldInfo::TLD_INFO) . " IANA TLDs\n";
echo "\n";
$tldCheck = "com";
echo "Is '$tldCheck' a real TLD?\n";
$foundTld = false;
foreach(TldInfo::TLD_INFO as $tldInfoElem) {
$tldInfoItem = TldInfo::toInfoItem($tldInfoElem);
if ($tldInfoItem->domain === strtolower($tldCheck)) {
echo "yes\n";
print_r($tldInfoItem);
$foundTld = true;
break 1;
}
}
unset($tldInfoElem);
if (!$foundTld) {
echo "no\n";
}
echo "\n";
$tldCheck = "somethingWeird";
echo "Is '$tldCheck' a real TLD?\n";
$foundTld = false;
foreach(TldInfo::TLD_INFO as $tldInfoElem) {
$tldInfoItem = TldInfo::toInfoItem($tldInfoElem);
if ($tldInfoItem->domain === strtolower($tldCheck)) {
echo "yes\n";
print_r($tldInfoItem);
$foundTld = true;
break 1;
}
}
unset($tldInfoElem);
if (!$foundTld) {
echo "no\n";
}

View File

@ -0,0 +1,32 @@
<?php
use TldEnum\TldType;
require __DIR__ . '/../vendor/autoload.php';
echo "There are " . count(TldType::TLD_TYPE) . " IANA TLDs\n";
echo "\n";
$tldCheck = "com";
echo "Is '$tldCheck' a real TLD?\n";
if (isset(TldType::TLD_TYPE[strtolower($tldCheck)])) {
echo "yes\n";
echo "type: ".TldType::TLD_TYPE[strtolower($tldCheck)]."\n";
} else {
echo "no\n";
}
echo "\n";
$tldCheck = "somethingWeird";
echo "Is '$tldCheck' a real TLD?\n";
if (isset(TldType::TLD_TYPE[strtolower($tldCheck)])) {
echo "yes\n";
echo "type: ".TldType::TLD_TYPE[strtolower($tldCheck)]."\n";
} else {
echo "no\n";
}
echo "\n";

View File

@ -1,14 +1,13 @@
<?php
use TldEnum\TldEnum;
use TldEnum\TldList;
//require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/../vendor/autoload.php';
echo "There are " . count(TldEnum::TLD_ENUM) . " IANA TLDs\n";
echo "There are " . count(TldList::TLD_LIST) . " IANA TLDs\n";
$tldCheck = "com";
echo "Is '$tldCheck' a real TLD?\n";
if (in_array(strtolower($tldCheck), TldEnum::TLD_ENUM)) {
if (in_array(strtolower($tldCheck), TldList::TLD_LIST)) {
echo " yes\n";
} else {
echo " no\n";
@ -16,7 +15,7 @@ if (in_array(strtolower($tldCheck), TldEnum::TLD_ENUM)) {
$tldCheck = "somethingWeird";
echo "Is '$tldCheck' a real TLD?\n";
if (in_array(strtolower($tldCheck), TldEnum::TLD_ENUM)) {
if (in_array(strtolower($tldCheck), TldList::TLD_LIST)) {
echo " yes\n";
} else {
echo " no\n";

1579
formats/js/tld-enum/desc.js Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,6 @@
module.exports = {
'desc' : require('./desc'),
'info' : require('./info'),
'list' : require('./list'),
'type' : require('./type'),
}

7887
formats/js/tld-enum/info.js Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
exports.tldList = [
module.exports = [
"aaa",
"aarp",
"abarth",

1579
formats/js/tld-enum/type.js Normal file

File diff suppressed because it is too large Load Diff

1579
formats/json/tld-desc.json Normal file

File diff suppressed because it is too large Load Diff

7887
formats/json/tld-info.json Normal file

File diff suppressed because it is too large Load Diff

1579
formats/json/tld-type.json Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,12 @@
<?php
namespace TldEnum;
class TldInfoItem {
public $domain;
public $description;
public $type;
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,8 @@
{
"name": "tld-enum",
"version": "1.0.8",
"version": "2.0.0",
"description": "Lists of every ICANN TLD in multiple formats",
"main": "formats/js/tld-enum.js",
"main": "formats/js/tld-enum",
"repository": {
"type": "git",
"url": "git+https://github.com/katmore/tld-enum.git"