Skip to content

Commit

Permalink
Update IANA TLD list updater script to drop IanaVersion comment
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksei Khudiakov <[email protected]>
  • Loading branch information
Xerkus committed Jun 12, 2023
1 parent 7bee1db commit ce7afa7
Showing 1 changed file with 20 additions and 45 deletions.
65 changes: 20 additions & 45 deletions bin/update_hostname_validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,15 @@
}

/** @psalm-var list<string> $newFileContent */
$newFileContent = []; // new file content
$insertDone = false; // becomes 'true' when we find start of $validTlds declaration
$insertFinish = false; // becomes 'true' when we find end of $validTlds declaration
$checkOnly = isset($argv[1]) ? $argv[1] === '--check-only' : false;
$response = getOfficialTLDs();
$ianaVersion = getVersionFromString('Version', strtok($response, "\n"));
$validatorVersion = getVersionFromString('IanaVersion', file_get_contents(LAMINAS_HOSTNAME_VALIDATOR_FILE));

if ($checkOnly && $ianaVersion > $validatorVersion) {
printf(
'TLDs must be updated, please run `php bin/update_hostname_validator.php` and push your changes%s',
PHP_EOL
);
exit(1);
}

if ($checkOnly) {
printf('TLDs are up-to-date%s', PHP_EOL);
exit(0);
}
$newFileContent = []; // new file content
$insertDone = false; // becomes 'true' when we find start of $validTlds declaration
$insertFinish = false; // becomes 'true' when we find end of $validTlds declaration
$checkOnly = isset($argv[1]) ? $argv[1] === '--check-only' : false;
$response = getOfficialTLDs();

foreach (file(LAMINAS_HOSTNAME_VALIDATOR_FILE) as $line) {
// Replace old version number with new one
if (preg_match('/\*\s+IanaVersion\s+\d+/', $line, $matches)) {
$newFileContent[] = sprintf(" * IanaVersion %s\n", $ianaVersion);
continue;
}
$currentFileContent = file(LAMINAS_HOSTNAME_VALIDATOR_FILE);

foreach ($currentFileContent as $line) {
if ($insertDone === $insertFinish) {
// Outside of $validTlds definition; keep line as-is
$newFileContent[] = $line;
Expand Down Expand Up @@ -84,6 +65,19 @@
exit(1);
}

if ($currentFileContent === $newFileContent) {
printf('Nothing to do. Validator has no TLD changes.%s', PHP_EOL);
exit(0);
}

if ($checkOnly) {
printf(
'TLDs must be updated, please run `php bin/update_hostname_validator.php` and push your changes%s',
PHP_EOL
);
exit(1);
}

if (false === @file_put_contents(LAMINAS_HOSTNAME_VALIDATOR_FILE, $newFileContent)) {
printf('Error: cannot write info file "%s"%s', LAMINAS_HOSTNAME_VALIDATOR_FILE, PHP_EOL);
exit(1);
Expand All @@ -109,25 +103,6 @@ function getOfficialTLDs(): string
}
}

/**
* Extract the first match of a string like
* "Version 2015072300" from the given string
*
* @param string $prefix
* @param string $string
* @return string
* @throws Exception
*/
function getVersionFromString($prefix, $string)
{
$matches = [];
if (! preg_match(sprintf('/%s\s+((\d+)?)/', $prefix), $string, $matches)) {
throw new Exception('Error: cannot get last update date');
}

return $matches[1];
}

/**
* Extract new Valid TLDs from a string containing one per line.
*
Expand Down

0 comments on commit ce7afa7

Please sign in to comment.