CVS: phpweather-1.x phpweather.inc,1.11,1.12 config-dist.inc,1.1,1.2
Brought to you by:
iridium
From: Martin G. <gim...@us...> - 2002-08-26 19:44:52
|
Update of /cvsroot/phpweather/phpweather-1.x In directory usw-pr-cvs1:/tmp/cvs-serv23980 Modified Files: phpweather.inc config-dist.inc Log Message: This patch from Reini Urban <ru...@x-...> adds support for the PHP database (dbm-style) abstraction layer. Also, the pretty_print_metar() function is now a wrapper around get_pretty_print_metar() which ends by returning the pretty print instead of printing it. Index: phpweather.inc =================================================================== RCS file: /cvsroot/phpweather/phpweather-1.x/phpweather.inc,v retrieving revision 1.11 retrieving revision 1.12 diff -u -3 -r1.11 -r1.12 --- phpweather.inc 12 Aug 2002 23:59:16 -0000 1.11 +++ phpweather.inc 26 Aug 2002 19:44:40 -0000 1.12 @@ -38,6 +38,15 @@ $useDBM = 0; /* turn off so rest of program won't use */ } +} elseif ($useDBA) { + /* Open the DBM databases: */ + $dbmMetar = dba_open ("metar", "c", $DBAmethod); + $dbmTimestamp = dba_open ("metarTimestamp", "c", $DBAmethod); + if (!$dbmMetar || !$dbmTimestamp) { + echo "<p>Unable to open DBA files!</p>"; + $useDBA = 0; /* turn off so rest of program won't use */ + } + } elseif ($usePSQL) { /* Make a connection to the PostgreSQL database: */ $conn = pg_Connect("host=$db_hostname dbname=$db_name port=5432 " . @@ -109,7 +118,7 @@ * Meteorological Handbook No. 1 for code groups P, 6 and 7) used in * several places, so standardized in one function. */ - if ($precip_mm>0) { + if ($precip_mm > 0) { $amount = sprintf($strings['mm_inches'], $precip_mm, $precip_in); } else { $amount = $strings['a_trace']; @@ -154,11 +163,18 @@ function pretty_print_metar($metar, $location) { + /* + * This is just a wrapper for the get_pretty_print_metar() function. + */ + print(get_pretty_print_metar($metar, $location)); +} + +function get_pretty_print_metar($metar, $location) { global $strings; /* * The main pretty-print function. * You should pass a metar and a location, eg. 'Aalborg, - * Denmark'. That produces something like this: + * Denmark'. It then returns something like this: * * 14 minutes ago, at 12:20 UTC, the wind was blowing at a speed * of 4.6 meters per second (10.4 miles per hour) from West in @@ -175,8 +191,7 @@ if (!$metar) { /* We don't want to display all sorts of silly things if the metar is empty. */ - printf($strings['no_data'], $location); - return; + return sprintf($strings['no_data'], $location); } @@ -333,24 +348,34 @@ $weather_str = ''; } - printf($strings['pretty_print_metar'], - $minutes_old, - $gmtime, - $wind_str, - $location, - $data['temp_c'], - $data['temp_f'], - $windchill_str, - $data['altimeter_hpa'], - $data['altimeter_inhg'], - $data['rel_humidity'], - $sky_str, - $visibility, - $runway_str, - $weather_str, - $prec_str, - $temp_str); + return sprintf($strings['pretty_print_metar'], + $minutes_old, + $gmtime, + $wind_str, + $location, + $data['temp_c'], + $data['temp_f'], + $windchill_str, + $data['altimeter_hpa'], + $data['altimeter_inhg'], + $data['rel_humidity'], + $sky_str, + $visibility, + $runway_str, + $weather_str, + $prec_str, + $temp_str); +} + + +// catch output into buffer and return it as string +function pretty_print_metar_string($metar, $location) { + ob_start(); + pretty_print_metar($metar, $location); + $m = ob_get_contents(); + ob_end_clean(); + return $m; } function pretty_print_metar_wap($metar, $location) { @@ -386,7 +411,7 @@ * Aalborg, Denmark. */ - global $useOCI, $useMySQL, $useDBM, $usePSQL, $conn, $dbmMetar, + global $useOCI, $useMySQL, $useDBM, $useDBA, $usePSQL, $conn, $dbmMetar, $dbmTimestamp, $useXML, $XMLMetar, $XMLParser, $XMLFile; if ($useMySQL) { @@ -404,10 +429,16 @@ } } elseif ($useDBM) { if (dbmexists($dbmMetar, $station) && - dbmexists ($dbmTimestamp, $station)) { /* found station */ + dbmexists ($dbmTimestamp, $station)) { /* found station */ $metar = dbmfetch ($dbmMetar, $station); $timestamp = dbmfetch($dbmTimestamp, $station); } + } elseif ($useDBA) { + if (dba_exists($station, $dbmMetar) && + dba_exists ($station, $dbmTimestamp)) { /* found station */ + $metar = dba_fetch ($station, $dbmMetar); + $timestamp = dba_fetch($station, $dbmTimestamp); + } } elseif ($useOCI) { $query = "SELECT metar,(timestamp-to_date('01-JAN-70','DD-MON-YY')) time_stamp FROM metars WHERE station='$station'"; $stmt = OCIParse($conn,$query); @@ -448,7 +479,7 @@ * metar. The new METAR is returned. */ global $useMySQL, $usePSQL, $useOCI, $conn, - $useDBM, $dbmMetar, $dbmTimestamp, + $useDBM, $useDBA, $dbmMetar, $dbmTimestamp, $useProxy, $proxy_host, $proxy_port, $useXML, $XMLMetar, $XMLParser, $XMLFile; @@ -492,7 +523,7 @@ $date = trim($file[0]); $metar = trim($file[1]); for ($i = 2; $i < count($file); $i++) { - $metar .= ' ' . trim($file[$i]); + $metar .= ' ' . trim($file[i]); } /* The date is in the form 2000/10/09 14:50 UTC. This seperates @@ -552,6 +583,18 @@ "timestamp='$date' WHERE station='$station'"; } pg_exec($conn,$query); + } elseif ($useDBA) { + if ($new) { + /* Insert the new record */ + dba_insert ($station, $metar, $dbmMetar); + dba_insert ($station, $date_unixtime, $dbmTimestamp); + dba_sync ($dbmMetar); dba_sync ($dbmTimestamp); + } else { + /* Update the old record */ + dba_replace ($station, $metar, $dbmMetar); + dba_replace ($station, $date_unixtime, $dbmTimestamp); + dba_sync ($dbmMetar); dba_sync ($dbmTimestamp); + } } elseif ($useDBM) { if ($new) { /* Insert the new record */ @@ -963,7 +1006,7 @@ number_format($decoded_metar['temp_c'] + 5/9 * ($e - 10),1); $decoded_metar['humidex_f'] = number_format($decoded_metar['humidex_c'] * 9/5 + 32, 1); - + /* * Windchill. * @@ -1006,7 +1049,7 @@ * refreshed the page, the new weather will be shown. */ - global $useMySQL, $useDBM, $usePSQL, $conn, $dbmMetar, $dbmTimestamp; + global $useMySQL, $useDBM, $useDBA, $usePSQL, $conn, $dbmMetar, $dbmTimestamp; global $useXML, $XMLMetar, $XMLParser, $XMLFile; if ($useMySQL) { @@ -1029,7 +1072,13 @@ $station = dbmfirstkey($dbmMetar); while ($station) { fetch_metar($station, 0); - $station = dmbnextkey($dbmMetar, $station); + $station = dbmnextkey($dbmMetar, $station); + } + } elseif ($useDBA) { + $station = dba_firstkey($dbmMetar); + while ($station) { + fetch_metar($station, 0); + $station = dba_nextkey($dbmMetar); } } elseif ($useOCI) { $query = "SELECT station FROM metars"; Index: config-dist.inc =================================================================== RCS file: /cvsroot/phpweather/phpweather-1.x/config-dist.inc,v retrieving revision 1.1 retrieving revision 1.2 diff -u -3 -r1.1 -r1.2 --- config-dist.inc 17 May 2002 22:10:43 -0000 1.1 +++ config-dist.inc 26 Aug 2002 19:44:40 -0000 1.2 @@ -35,6 +35,11 @@ /* set to 1 to use XML */ $useXML = 0; +/* set to 1 to use a DBA database */ +$useDBA = 0; +/* the DBA handler to use, if $useDBA is set to 1: */ +$DBAmethod = 'db2'; // or ndbm, gdbm, db3 if db2 doesn't work. + /* If you're using a database that requires you to log on, then set * the following variables. Make sure to protect the 'config.php' file * so that other people cannot read your password! |