Skip to content

Commit

Permalink
Make this working again.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Oct 17, 2016
1 parent 9576454 commit 6354352
Showing 1 changed file with 91 additions and 90 deletions.
181 changes: 91 additions & 90 deletions maintainer-tools/horde-update-ISO3166-countries
Expand Up @@ -2,189 +2,190 @@
<?php
/**
* This is a script to fetch the current ISO-3166 country definitions and
* update Horde's country list file in horde/lib/NLS/countries.php in an
* update Horde's country list file in Horde/Nls/Countries.php in an
* interactive way.
*
* ** This script needs to be run from the base Horde directory. **
*
* The source for the country list is the International Organization for
* Standardization (http://iso.org).
*
* Copyright 2004-2009 The Horde Project (http://www.horde.org/)
* Copyright 2004-2016 The Horde Project (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
*
* @category Horde
* @package maintainer_tools
* @author Marko Djukic <marko@oblo.com>
* @author Jan Schneider <jan@horde.org>
*
* @todo Would be good to expand this to fetch also ISO-3166-2 lists from
* somewhere for lists of countries' regions/states.
*/

require_once getcwd() . '/lib/Application.php';

if (!Horde_CLI::runningFromCLI()) {
exit("Must be run from the command line\n");
$baseFile = getcwd() . '/lib/Application.php';
if (file_exists($baseFile)) {
require_once $baseFile;
} else {
require_once 'PEAR/Config.php';
require_once PEAR_Config::singleton()
->get('horde_dir', null, 'pear.horde.org') . '/lib/Application.php';
}
Horde_CLI::init();
Horde_Registry::appInit('horde', array(
'authentication' => 'none',
'cli' => true
));

/* Location on ISO website where to fetch the updates from. */
$url = 'http://www.iso.org/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1-semic.txt';
$url = 'https://raw.githubusercontent.com/datasets/country-list/master/data.csv';

/* Update Horde's file. */
$countries = new Countries($url);
$updated = $countries->update();
if ($updated == false) {
/* Nothing found to update. */
$countries->cli->writeln('Nothing to update. Exiting.');
$cli->writeln('Nothing to update. Exiting.');
exit;
}

/* Save the new file. */
$countries->save();
exit;

class Countries {

var $old_data = array();
var $new_data = array();
var $cli;
var $_url;
class Countries
{
protected $_old_data = array();
protected $_new_data = array();
protected $_url;

/**
* Constructor
*/
function Countries($url)
public function __construct($url)
{
$this->_url = $url;

$this->cli = Horde_CLI::singleton();

$this->old_data = $this->_loadOld();
$this->new_data = $this->_loadNew();
$this->_loadOld();
$this->_loadNew();
}

function update()
public function update()
{
global $cli;

$accept_all = false;
$updated = false;
foreach ($this->new_data as $code => $name) {
foreach ($this->_new_data as $code => $name) {
if ($accept_all == true) {
/* Auto accept activated just select all auto names. */
$this->new_data[$code] = $this->_newName($name);
} elseif (isset($this->old_data[$code]) &&
strtolower($this->old_data[$code]) == strtolower($name)) {
$this->_new_data[$code] = $name;
} elseif (isset($this->_old_data[$code]) &&
$this->_old_data[$code] == $name) {
/* Old name matches new name so assume no changes. */
$this->new_data[$code] = $this->old_data[$code];
$this->_new_data[$code] = $this->_old_data[$code];
} else {
$new_name = $this->_newName($name);
if (isset($this->old_data[$code])) {
$new_name = $name;
if (isset($this->_old_data[$code])) {
/* There is an existing old name, show the two versions. */
$old = $this->cli->red($this->old_data[$code]);
$this->cli->writeln(sprintf('Old name: %s', $old));
$new = $this->cli->green($new_name);
$this->cli->writeln(sprintf('New name: %s', $new));
$old = $cli->red($this->_old_data[$code]);
$cli->writeln(sprintf('Old name: %s', $old));
$new = $cli->green($new_name);
$cli->writeln(sprintf('New name: %s', $new));
}
/* Prompt for a new country name. */
$accept = $this->cli->prompt(sprintf('New country name \"%s\", convert to: \"%s\"?', $name, $new_name), array('y' => 'Yes', 'n' => 'No', 'a' => 'Accept All'));
$accept = $cli->prompt(
sprintf(
'New country name for "%s", convert to: "%s"?',
$this->_old_data[$code],
$new_name
),
array('y' => 'Yes', 'n' => 'No', 'a' => 'Accept All')
);
if ($accept == 'n') {
/* User does not accept auto name, ask for input. */
$new_name = $this->cli->prompt('Enter the new name: ', true);
$new_name = $cli->prompt('Enter the new name: ', true);
} elseif ($accept == 'a') {
/* User asked to accept all, flag it. */
$accept_all = true;
}
$updated = true;
$this->new_data[$code] = $new_name;
$this->_new_data[$code] = $new_name;
}
}

return $updated;
}

function save()
public function save()
{
$filename = HORDE_BASE . '/lib/NLS/countries.php';
$old_file = file($filename);
global $cli;

$filename = 'Horde/Nls/Countries.php';
if (!$fp = fopen($filename, 'r+', true)) {
$cli->fatal(sprintf('Cannot open file %s.', $filename));
}
$new_file = '';
foreach ($old_file as $key => $line) {
while (!feof($fp) && $line = fgets($fp)) {
$new_file .= $line;
if ($line == "\$countries = array(\n") {
break;
}
}
$new_data = array();
foreach ($this->new_data as $code => $name) {
$new_data[] = sprintf(' \'%s\' => \'%s\'', $code, $name);
$_new_data = array();
foreach ($this->_new_data as $code => $name) {
$_new_data[] = sprintf(' \'%s\' => Horde_Nls_Translation::t("%s")', $code, $name);
}
$new_file .= implode(",\n", $_new_data) . "\n";
while (!feof($fp) && $line = fgets($fp)) {
if (substr($line, 0, 4) == ' ') {
continue;
}
$new_file .= $line;
}
$new_file .= implode(", \n", $new_data) . ');';

/* Save the new file. */
if (!$fp = @fopen($filename, 'w')) {
$this->cli->fatal(sprintf('Cannot open file %s.', $filename));
fseek($fp, 0);
if (!ftruncate($fp, 0)) {
$cli->fatal(sprintf('Cannot truncate file %s.', $filename));
}

if (!fwrite($fp, $new_file)) {
$this->cli->fatal(sprintf('Cannot write file %s.', $filename));
$cli->fatal(sprintf('Cannot write file %s.', $filename));
}
fclose($fp);
$this->cli->writeln('Success, saved new country data.');

$cli->writeln('Success, saved new country data.');
}

function &_loadOld()
protected function _loadOld()
{
require_once HORDE_BASE . '/lib/NLS/countries.php';
return $countries;
include 'Horde/Nls/Countries.php';
$this->_old_data = $countries;
}

function &_loadNew()
protected function _loadNew()
{
$new_file = Countries::_readURL($this->_url);
if (is_a($new_file, 'PEAR_Error')) {
$this->cli->fatal(sprintf('Cannot read source for country data. %s', $new_file->getMessage()));
}
global $cli;

$lines = explode("\r\n", $new_file);
$new_data = array();
foreach ($lines as $line) {
if (!preg_match('/^(.*?);(\w\w)$/', $line, $matches)) {
continue;
}
$new_data[$matches[2]] = $matches[1];
$new_file = $this->_readURL($this->_url);
fgetcsv($new_file);
$this->_new_data = array();
while (!feof($new_file) && $line = fgetcsv($new_file)) {
$this->_new_data[$line[1]] = $line[0];
}
return $new_data;
}

function _readURL()
protected function _readURL()
{
$options['method'] = 'GET';
$options['timeout'] = 5;
$options['allowRedirects'] = true;

$http = new HTTP_Request($this->_url, $options);
@$http->sendRequest();
if ($http->getResponseCode() != 200) {
return PEAR::raiseError(sprintf('Could not open %s.', $this->_url));
}
global $injector;

return $http->getResponseBody();
}

function _newName($name)
{
$no_caps = array('AND', 'OF', 'THE');
$parts = explode(' ', $name);
foreach ($parts as $key => $part) {
if (in_array($part, $no_caps)) {
$part = strtolower($part);
} else {
$part = preg_replace('/(\w)(\w*(\'S)?)/e', "$1 . strtolower('$2')", $part);
}
$parts[$key] = $part;
$response = $injector
->getInstance('Horde_Http_Client')
->get($this->_url);
if ($response->code != 200) {
throw new Exception(sprintf('Could not open %s.', $this->_url));
}

return implode(' ', $parts);
return $response->getStream();
}

}

0 comments on commit 6354352

Please sign in to comment.