list & updatecheck actions are now working to load all Olson zones from files.

This commit is contained in:
Andrew McMillan 2011-09-19 08:40:54 +12:00
parent 5f4b40a643
commit 45ed6fb3f4
12 changed files with 2272 additions and 1 deletions

76
inc/tz/list.php Normal file
View File

@ -0,0 +1,76 @@
<?php
/**
* DAViCal Timezone Service handler - capabilitis
*
* @package davical
* @subpackage tzservice
* @author Andrew McMillan <andrew@morphoss.com>
* @copyright Morphoss Ltd
* @license http://gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('vComponent.php');
$response = new XMLDocument( array("" => "urn:ietf:params:xml:ns:timezone-service") );
$tzlist = $response->NewXMLElement('timezone-list');
$tzlist->NewElement('dtstamp', gmdate('Ymd\THis\Z'));
$sql = 'SELECT our_tzno, tzid, active, to_char(last_modified AT TIME ZONE \'UTC\',\'YYYYMMDD\"T\"HH24MISS"Z"\') AS last_modified, olson_name, vtimezone FROM timezones';
$params = array();
$where = '';
if ( $returnall !== true ) {
$where = 'active';
}
if ( !empty($changedsince) ) {
if ( !empty($where) ) $where .= ' AND ';
$where .= 'last_modified >= :changedsince';
$params[':changedsince'] = $changedsince;
}
if ( !empty($where)) $sql .= ' WHERE '.$where;
/*
<dtstamp>2009-10-11T09:32:11Z</dtstamp>
<summary>
<tzid>America/New_York</tzid>
<last-modified>2009-09-17T01:39:34Z</last-modified>
<alias>US/Eastern</alias>
<local-name lang="en_US">America/New_York</local-name>
<summary>
*/
$q2 = new AwlQuery();
$qry = new AwlQuery($sql,$params);
if ( $qry->Exec('tz/list',__LINE__,__FILE__) && $qry->rows() > 0 ) {
while( $tz = $qry->Fetch() ) {
$elements = array(
new XMLElement('tzid', $tz->tzid),
new XMLElement('last-modified', $tz->last_modified)
);
if ( $tz->active != 't' ) {
$elements[] = new XMLElement('inactive' );
}
if ( $tz->tzid != $tz->olson_name ) {
$elements[] = new XMLElement('alias', $tz->olson_name );
}
if ( $q2->QDo('SELECT * FROM tz_aliases WHERE our_tzno = ?', array($tz->our_tzno)) ) {
while( $alias = $q2->Fetch() ) {
$elements[] = new XMLElement('alias', $alias->tzalias );
}
}
if ( !empty($lang) && $q2->QDo('SELECT * FROM tz_localnames WHERE our_tzno = ? AND locale = ?', array($tz->our_tzno, $lang)) && $q2->rows() > 0 ) {
while( $local = $q2->Fetch() ) {
$attr = array( 'lang' => $local->locale );
if ( $local->preferred == 't' ) $attr['preferred'] = 'true';
$elements[] = new XMLElement('local-name', $local->localised_name, $attr );
}
}
else {
$elements[] = new XMLElement('local-name', $tz->tzid, array( 'lang' => $lang ) );
}
$tzlist->NewElement('summary', $elements);
}
}
header('Content-Type: application/xml; charset="utf-8"');
echo $response->Render($tzlist);
exit(0);

100
inc/tz/updatecheck.php Normal file
View File

@ -0,0 +1,100 @@
<?php
/**
* DAViCal Timezone Service handler - capabilitis
*
* @package davical
* @subpackage tzservice
* @author Andrew McMillan <andrew@morphoss.com>
* @copyright Morphoss Ltd
* @license http://gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('vCalendar.php');
$new_zones = 0;
$modified_zones = 0;
if ( empty($c->tzsource) ) $c->tzsource = '../zonedb/vtimezones';
if ( preg_match('{^http}', $c->tzsource ) ) {
}
else if ( file_exists($c->tzsource) ) {
/**
* Find all files recursively within the diectory given.
* @param string $dirname The directory to find files in
* @return array of filenames with full path
*/
function recursive_files( $dirname ) {
$d = opendir($dirname);
$result = array();
while( $fn = readdir($d) ) {
if ( substr($fn,0,1) == '.' ) continue;
$fn = $dirname.'/'.$fn;
if ( is_dir($fn) ) {
$result = array_merge($result,recursive_files($fn));
}
else {
$result[] = $fn;
}
}
return $result;
}
$qry = new AwlQuery();
foreach( recursive_files($c->tzsource) AS $filename ) {
$tzid = str_replace('.ics', '', str_replace($c->tzsource.'/', '', $filename));
$tzrow = null;
if ( $qry->QDo('SELECT * FROM timezones WHERE tzid=:tzid', array(':tzid' => $tzid)) && $qry->rows() > 0 ) {
$tzrow = $qry->Fetch();
}
$vtimezone = file_get_contents( $filename, false );
if ( $vtimezone == $tzrow->vtimezone ) {
dbg_error_log('tz/updatecheck', 'Skipping zone "%s" - no change', $tzid );
continue;
}
$vtz = new vCalendar($vtimezone);
$last_modified = $vtz->GetProperty('LAST-MODIFIED');
if ( empty($last_modified) ) {
$last_modified = gmdate('Ymd\THis\Z');
// Then it was probably that way when we last updated the data, too :-(
if ( !empty($tzrow) ) {
$old_vtz = new vCalendar($tzrow->vtimezone);
$old_vtz->ClearProperties('LAST-MODIFIED');
// We need to add & remove this property so the Render is equivalent.
$vtz->AddProperty('LAST-MODIFIED',$last_modified);
$vtz->ClearProperties('LAST-MODIFIED');
if ( $vtz->Render() == $old_vtz->Render() ) {
dbg_error_log('tz/updatecheck', 'Skipping zone "%s" - no change', $tzid );
continue;
}
}
$vtz->AddProperty('LAST-MODIFIED',$last_modified);
}
dbg_error_log('tz/updatecheck', 'Writing %s zone for "%s"', (empty($tzrow)?"new":"updated"), $tzid );
$params = array(
':tzid' => $tzid,
':olson_name' => $tzid,
':vtimezone' => $vtz->Render(),
':last_modified' => $last_modified,
':etag' => md5($vtz->Render())
);
if ( empty($tzrow) ) {
$new_zones++;
$sql = 'INSERT INTO timezones(tzid,active,olson_name,last_modified,etag,vtimezone) ';
$sql .= 'VALUES(:tzid,TRUE,:olson_name,:last_modified,:etag,:vtimezone)';
}
else {
$modified_zones++;
$sql = 'UPDATE timezones SET active=TRUE, olson_name=:olson_name, last_modified=:last_modified, ';
$sql .= 'etag=:etag, vtimezone=:vtimezone WHERE tzid=:tzid';
}
$qry->QDo($sql,$params);
}
header('Content-type: text/plain');
printf('Added %d new zones and updated data for %d zones', $new_zones, $modified_zones);
}
else {
dbg_error_log('ERROR', '$c->tzsource is not configured to a good source of timezone data');
}
exit(0);

View File

@ -0,0 +1,115 @@
HTTP/1.1 200 OK
Date: Dow, 01 Jan 2000 00:00:00 GMT
Content-Length: 2986
Content-Type: application/xml; charset="utf-8"
<?xml version="1.0" encoding="utf-8" ?>
<capabilities xmlns="urn:ietf:params:xml:ns:timezone-service">
<info>
<primary-source>Olson:2011m</primary-source>
<contact>mailto:tzs@example.org</contact>
</info>
<operation>
<action>list</action>
<description>List timezone identifiers and localized forms
</description>
<accept-parameter>
<name>lang</name>
<required>false</required>
<multi>true</multi>
<description>Specify desired localized form(s)</description>
</accept-parameter>
<accept-parameter>
<name>changedsince</name>
<required>false</required>
<multi>false</multi>
<description>Limit result to timezones changed since the
given date
</description>
</accept-parameter>
<accept-parameter>
<name>returnall</name>
<required>false</required>
<multi>false</multi>
<description>If present inactive timezones will be returned.
</description>
</accept-parameter>
</operation>
<operation>
<action>get</action>
<description>
Returns one or more timezones as specified by the
tzid parameter.
</description>
<accept-parameter>
<name>format</name>
<required>false</required>
<multi>false</multi>
<value>text/calendar</value>
<value>application/calendar+xml</value>
<description>Specify required format for timezone.
</description>
</accept-parameter>
<accept-parameter>
<name>lang</name>
<required>false</required>
<multi>true</multi>
<description>Specify desired localized form(s)</description>
</accept-parameter>
<accept-parameter>
<name>tzid</name>
<required>true</required>
<multi>true</multi>
<description>Specify desired timezone identifiers
</description>
</accept-parameter>
</operation>
<operation>
<action>expand</action>
<description>
Expands the specified timezone(s) into local onset and UTC
offsets
</description>
<accept-parameter>
<name>tzid</name>
<required>true</required>
<multi>true</multi>
<description>Specify desired timezone identifiers</description>
</accept-parameter>
<accept-parameter>
<name>start</name>
<required>false</required>
<multi>false</multi>
<description>
Specify start of the period of interest. If omitted the
current year is assumed.
</description>
</accept-parameter>
<accept-parameter>
<name>end</name>
<required>false</required>
<multi>false</multi>
<description>
Specify end of the period of interest.
If omitted the current year + 10 is assumed.
</description>
</accept-parameter>
</operation>
<operation>
<action>capabilities</action>
<description>Gets the capabilities of the server</description>
</operation>
</capabilities>

View File

@ -0,0 +1,81 @@
HTTP/1.1 200 OK
Date: Dow, 01 Jan 2000 00:00:00 GMT
Content-Length: 2677
Content-Type: application/xml; charset="utf-8"
<?xml version="1.0" encoding="utf-8" ?>
<urn:ietf:params:xml:ns:timezone-service:timezone-list xmlns:urn:ietf:params:xml:ns:timezone-service="">
<dtstamp>all good</dtstamp>
<summary>
<tzid>Pacific/Auckland</tzid>
<last-modified>20110919T032240Z</last-modified>
<inactive/>
<local-name lang="en_US">Pacific/Auckland</local-name>
</summary>
<summary>
<tzid>America/Los_Angeles</tzid>
<last-modified>20110918T082240Z</last-modified>
<inactive/>
<local-name lang="en_US">America/Los_Angeles</local-name>
</summary>
<summary>
<tzid>Europe/Helsinki</tzid>
<last-modified>20110918T182240Z</last-modified>
<inactive/>
<local-name lang="en_US">Europe/Helsinki</local-name>
</summary>
<summary>
<tzid>New Zealand Standard Time</tzid>
<last-modified>20110919T032248Z</last-modified>
<inactive/>
<alias>Pacific/Auckland</alias>
<local-name lang="en_US">New Zealand Standard Time</local-name>
</summary>
<summary>
<tzid>/softwarestudio.org/Olson_20011030_5/Pacific/Auckland</tzid>
<last-modified>20110919T032253Z</last-modified>
<inactive/>
<alias>Pacific/Auckland</alias>
<local-name lang="en_US">/softwarestudio.org/Olson_20011030_5/Pacific/Auckland</local-name>
</summary>
<summary>
<tzid>/mozilla.org/20050126_1/Pacific/Auckland</tzid>
<last-modified>20110919T032256Z</last-modified>
<inactive/>
<alias>Pacific/Auckland</alias>
<local-name lang="en_US">/mozilla.org/20050126_1/Pacific/Auckland</local-name>
</summary>
<summary>
<tzid>/mozilla.org/20050126_1/Antarctica/McMurdo</tzid>
<last-modified>20110919T032256Z</last-modified>
<inactive/>
<alias>Antarctica/McMurdo</alias>
<local-name lang="en_US">/mozilla.org/20050126_1/Antarctica/McMurdo</local-name>
</summary>
<summary>
<tzid>/mozilla.org/20070129_1/Antarctica/McMurdo</tzid>
<last-modified>20110919T032258Z</last-modified>
<inactive/>
<alias>Antarctica/McMurdo</alias>
<local-name lang="en_US">/mozilla.org/20070129_1/Antarctica/McMurdo</local-name>
</summary>
<summary>
<tzid>Europe/Prague</tzid>
<last-modified>20110919T032319Z</last-modified>
<inactive/>
<local-name lang="en_US">Europe/Prague</local-name>
</summary>
<summary>
<tzid>America/New_York</tzid>
<last-modified>20110918T112328Z</last-modified>
<inactive/>
<local-name lang="en_US">America/New_York</local-name>
</summary>
<summary>
<tzid>(UTC-05:00) Eastern Time (US &amp; Canada)</tzid>
<last-modified>20110918T112401Z</last-modified>
<inactive/>
<alias>America/New_York</alias>
<local-name lang="en_US">(UTC-05:00) Eastern Time (US &amp; Canada)</local-name>
</summary>
</urn:ietf:params:xml:ns:timezone-service:timezone-list>

View File

@ -2,6 +2,7 @@
# List timezone server timezones
#
TYPE=GET
URL=http://regression.host/tz.php?action=list
URL=http://regression.host/tz.php?action=list&returnall&lang=en_US
HEAD
REPLACE=/dtstamp>[0-9TZ]{16}/dtstamp>all good/

View File

@ -0,0 +1,9 @@
HTTP/1.1 200 OK
Date: Dow, 01 Jan 2000 00:00:00 GMT
Content-Length: 239
Content-Type: application/xml; charset="utf-8"
<?xml version="1.0" encoding="utf-8" ?>
<urn:ietf:params:xml:ns:timezone-service:timezone-list xmlns:urn:ietf:params:xml:ns:timezone-service="">
<dtstamp>all good</dtstamp>
</urn:ietf:params:xml:ns:timezone-service:timezone-list>

View File

@ -0,0 +1,8 @@
#
# List timezone server timezones
#
TYPE=GET
URL=http://regression.host/tz.php?action=list
HEAD
REPLACE=/dtstamp>[0-9TZ]{16}/dtstamp>all good/

View File

@ -0,0 +1,6 @@
HTTP/1.1 200 OK
Date: Dow, 01 Jan 2000 00:00:00 GMT
Content-Length: 48
Content-Type: text/plain
Added 362 new zones and updated data for 5 zones

View File

@ -0,0 +1,7 @@
#
# Request the timezone server update itself
#
TYPE=GET
URL=http://regression.host/tz.php?action=updatecheck
HEAD

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,9 @@
#
# List timezone server timezones
#
TYPE=GET
URL=http://regression.host/tz.php?action=list
HEAD
REPLACE=/dtstamp>[0-9TZ]{16}/dtstamp>all good/
REPLACE=/last-modified>[0-9TZ]{16}/last-modified>all good/

16
zonedb/update-tzdata.sh Executable file
View File

@ -0,0 +1,16 @@
#!/bin/sh
wget --continue 'ftp://elsie.nci.nih.gov/pub/tz*.tar.gz'
TZCODEFILE="`ls -t tzcode*.tar.gz|tail -n 1`"
TZDATAFILE="`ls -t tzdata*.tar.gz|tail -n 1`"
(
mkdir tzcode && cd tzcode && tar -xfz ../$TZCODEFILE
)
(
mkdir tzdata && cd tzdata && tar -xfz ../$TZDATAFILE
)
vzic --olson-dir tzdata --output-dir vtimezones