CVS: phpweather base_object.php,1.11,1.12 configurator.php,1.14,1.15 ...
Brought to you by:
iridium
From: Martin G. <gim...@us...> - 2002-03-20 19:26:54
|
Update of /cvsroot/phpweather/phpweather In directory usw-pr-cvs1:/tmp/cvs-serv31666 Modified Files: base_object.php configurator.php data_retrieval.php db_layer.php images-test.php phpweather.php Log Message: All files now use require_once() instead of just require() - this means, that there's no need for those 'if defined('...') return ...' statements at the top of each file any more. Index: base_object.php =================================================================== RCS file: /cvsroot/phpweather/phpweather/base_object.php,v retrieving revision 1.11 retrieving revision 1.12 diff -u -3 -r1.11 -r1.12 --- base_object.php 17 Jul 2001 12:12:03 -0000 1.11 +++ base_object.php 20 Mar 2002 19:26:50 -0000 1.12 @@ -1,10 +1,4 @@ <?php -/* This code makes sure that the file is only included once. */ -if (defined('BASE_OBJECT')) { - return; -} else { - define('BASE_OBJECT', true); -} /** * Provides some basic capabilities like error-handling and handling Index: configurator.php =================================================================== RCS file: /cvsroot/phpweather/phpweather/configurator.php,v retrieving revision 1.14 retrieving revision 1.15 diff -u -3 -r1.14 -r1.15 --- configurator.php 19 Aug 2001 13:58:50 -0000 1.14 +++ configurator.php 20 Mar 2002 19:26:50 -0000 1.15 @@ -443,7 +443,7 @@ } elseif ($action == 'do_sql') { define('PHPWEATHER_BASE_DIR', dirname(__FILE__)); -require(PHPWEATHER_BASE_DIR . '/db_layer.php'); +require_once(PHPWEATHER_BASE_DIR . '/db_layer.php'); $db = new db_layer(array()); if ($db->db->create_tables()) { Index: data_retrieval.php =================================================================== RCS file: /cvsroot/phpweather/phpweather/data_retrieval.php,v retrieving revision 1.19 retrieving revision 1.20 diff -u -3 -r1.19 -r1.20 --- data_retrieval.php 15 Mar 2002 12:01:48 -0000 1.19 +++ data_retrieval.php 20 Mar 2002 19:26:50 -0000 1.20 @@ -1,12 +1,6 @@ <?php -/* This code makes sure that the file is only included once. */ -if (defined('DATA_RETRIEVAL')) { - return; -} else { - define('DATA_RETRIEVAL', true); -} -require(PHPWEATHER_BASE_DIR . '/db_layer.php'); +require_once(PHPWEATHER_BASE_DIR . '/db_layer.php'); /** * This class contains all the logic needed to get and store METARs. @@ -22,11 +16,12 @@ * The METAR is stored here. * * The property is used whenever someone wants access to the raw - * METAR. Although you shouldn't do it, it is possible to set this - * directly. But that is only something the developers should do for - * testing... :-) + * METAR. This should be used for reading only, if you want to + * change the METAR (for testing purposes etc.), then use + * set_metar() instead. * * @var string + * @see set_metar() */ var $metar; @@ -46,10 +41,10 @@ * @param array The initial properties of the object. */ function data_retrieval($input) { - /* We start by calling the parent constructor. */ + /* We start by calling the parent constructor. */ $this->db_layer($input); - - /* Then we set the station. */ + + /* Then we set the station. */ $this->set_icao($this->properties['icao']); } @@ -75,11 +70,10 @@ * @param string The ICAO of the new station. */ function set_icao($new_icao) { - - /* We start by adding slashes, since $new_icao might come directly - * from the user. - */ - + + /* We start by adding slashes, since $new_icao might come directly + * from the user. + */ $new_icao = addslashes($new_icao); if ($new_icao != $this->get_icao()) { $this->properties['icao'] = strtoupper($new_icao); @@ -99,7 +93,6 @@ * @access public * @return string The raw METAR. */ - function get_metar() { if (empty($this->metar)) { /* The METAR is not set - we try to load it */ @@ -112,13 +105,13 @@ } /** - * Sets the metar directly, for testing etc + * Sets the METAR directly, for testing etc * - * It loads and decodes the metar if it is - * different from the old one. If the new metar is the same as - * the old one, nothing is changed. + * It loads and decodes the METAR if it is different from the old + * one. If the new METAR is the same as the old one, nothing is + * changed. * - * Also sets the ICAO to be correct for this metar + * Also sets the ICAO to be correct for this METAR. * * @access public * @param string The METAR we want decoded. @@ -150,7 +143,6 @@ * @access public * @return string The raw METAR. */ - function get_metar_from_db() { if (!$this->db->connect()) { return false; @@ -160,28 +152,32 @@ $this->debug('get_metar_from_db(): Found the METAR in the database'); list($metar, $timestamp) = $data; - /* We set the METAR right away, and then count on - * get_metar_from_web() to set it to something else, if - * necessary. - */ + /* We set the METAR right away, and then count on + * get_metar_from_web() to set it to something else, if + * necessary. + */ $this->metar = $metar; if ($this->properties['always_use_db'] || $timestamp > time() - 3600) { - /* We have asked explicit for a cached METAR, or the METAR is - * still fresh. Either way - we return the METAR we found in - * the database. - */ - - $this->debug('get_metar_from_db(): Using previously cached METAR for <code>' . $this->get_location() . '</code>. The METAR expires in ' . ($timestamp + 3600 - time()) . ' seconds.'); + /* We have asked explicit for a cached METAR, or the METAR is + * still fresh. Either way - we return the METAR we found in + * the database. + */ + $this->debug('get_metar_from_db(): Using previously cached METAR for <code>' . + $this->get_location() . '</code>. The METAR expires in ' . + ($timestamp + 3600 - time()) . ' seconds.'); return $metar; } else { - /* The METAR is too old, so we fetch new */ - $this->debug('get_metar_from_db(): The METAR for <code>' . $this->get_location() . '</code> was ' . (time() - 3600 - $timestamp) . ' seconds too old.'); + /* The METAR is too old, so we fetch new */ + $this->debug('get_metar_from_db(): The METAR for <code>' . + $this->get_location() . '</code> was ' . + (time() - 3600 - $timestamp) . ' seconds too old.'); return $this->get_metar_from_web(false); } } else { - /* We need to get a new METAR from the web. */ - $this->debug('get_metar_from_db(): New station <code>' . $this->get_location() . '</code> - fetching a new METAR.'); + /* We need to get a new METAR from the web. */ + $this->debug('get_metar_from_db(): New station <code>' . + $this->get_location() . '</code> - fetching a new METAR.'); return $this->get_metar_from_web(true); } } @@ -190,21 +186,22 @@ * Fetches a METAR from the Internet. * * The METAR is fetched via HTTP from the National Weather Services - * public server at http://weather.noaa.gov . + * public server. The files can be found under the + * http://weather.noaa.gov/pub/data/observations/metar/stations/ + * directory as ICAO.TXT where ICAO is replaced by the actual ICAO. * * @param boolean Should the station be inserted into the database, * or should we update an already existing entry? * @access public * @return string The raw METAR. */ - function get_metar_from_web($new_station) { $metar = ''; $icao = $this->get_icao(); if ($this->properties['use_proxy']) { - /* We use a proxy */ - /* Inspirated by code from Paul Kairis <Pau...@sa...> */ + /* We use a proxy */ + /* Inspirated by code from Paul Kairis <Pau...@sa...> */ $fp = fsockopen($this->properties['proxy_host'], $this->properties['proxy_port']); @@ -215,15 +212,15 @@ "Pragma: no-cache\r\n". "Cache-Contol: no-cache\r\n\r\n"); - /* We check the status line */ + /* We check the status line */ if (strpos(fgets($fp, 1024), '200 ')) { - /* Then we seek until we find the empty line between the - * headers and the contents. - */ + /* Then we seek until we find the empty line between the + * headers and the contents. + */ do { $line = fgets($fp, 1024); } while ($line != "\r\n"); - /* We know now, that the following lines are the contents. */ + /* We know now, that the following lines are the contents. */ unset($file); while ($line = fgets($fp, 1024)) { $file[] = $line; @@ -232,61 +229,56 @@ } } } else { - /* No proxy - we just fetch the file the normal way. */ - /* We use the @file notation here because it might fail. */ + /* No proxy - we just fetch the file the normal way. */ + /* We use the @file notation here because it might fail. */ $file = @file('http://weather.noaa.gov/pub/data/' . "observations/metar/stations/$icao.TXT"); } - /* Here we test to see if we actually got a METAR. */ + + /* Here we test to see if we actually got a METAR. */ if (is_array($file)) { - /* The first line in the file is the date */ - list(, $date) = each($file); - $date = trim($date); - /* The next lines are the METAR itself. We make sure that we - * don't put a space in front of the first line. - */ - list(, $line) = each($file); - $metar = trim($line); - /* Then the rest of the lines... */ - while (list(, $line) = each($file)) { - $metar .= ' ' . trim($line); - } + + /* The first line in the file is the date */ + $date = trim(array_shift($lines)); - /* We don't want any double-spaces in our METAR */ - $metar = ereg_replace('[ ]+', ' ', $metar); + /* The remaining lines are the METAR itself. This will merge the + * remaining lines into one line by removing new-lines: + */ + $metar = ereg_replace("[\n\r ]+", ' ', trim(implode(' ', $lines))); $date = explode(':', strtr($date, '/ ', '::')); if ($date[2] > gmdate('j')) { - /* The day is greater that the current day of month - * => the report is from last month. - */ - $date[1]--; + /* The day is greater that the current day of month. This + * implies, that the report is from last month. + */ + $date[1]--; } - $timestamp = gmmktime($date[3], $date[4], 0, $date[1], $date[2], $date[0]); + $timestamp = gmmktime($date[3], $date[4], 0, $date[1], + $date[2], $date[0]); if (!ereg('[0-9]{6}Z', $metar)) { - /* Some reports don't even have a time-part, so we insert the - * current time. This might not be the time of the report, but - * it was broken anyway :-) - */ - $metar = gmdate('dHi', $timestamp) . 'Z ' . $metar; + /* Some reports don't even have a time-part, so we insert the + * current time. This might not be the time of the report, but + * it was broken anyway :-) + */ + $metar = gmdate('dHi', $timestamp) . 'Z ' . $metar; } - + if ($timestamp < (time() - 3300)) { - /* The timestamp in the METAR is more than 55 minutes old. We - * adjust the timestamp, so that we won't try to fetch a new - * METAR within the next 5 minutes. After 5 minutes, the - * timestamp will again be more than 1 hour old. - */ + /* The timestamp in the METAR is more than 55 minutes old. We + * adjust the timestamp, so that we won't try to fetch a new + * METAR within the next 5 minutes. After 5 minutes, the + * timestamp will again be more than 1 hour old. + */ $timestamp = time() - 3300; } } else { - /* If we end up here, it means that there was no file. If the - * station was a new station, we set the metar to an empty - * string, else we just use the old METAR. We adjust the time - * stored in the database in both cases, so that the server - * isn't stressed too much. - */ + /* If we end up here, it means that there was no file. If the + * station was a new station, we set the metar to an empty + * string, else we just use the old METAR. We adjust the time + * stored in the database in both cases, so that the server + * isn't stressed too much. + */ if ($new_station) { $metar = ''; } else { @@ -295,15 +287,17 @@ $timestamp = time() - 3000; } - /* We then cache the METAR in our database */ + /* We then cache the METAR in our database */ if ($new_station) { - $this->debug('get_metar_from_web(): Inserting new METAR for <code>' . $this->get_location() . '</code>'); + $this->debug('get_metar_from_web(): Inserting new METAR for <code>' . + $this->get_location() . '</code>'); $this->db->insert_metar($icao, $metar, $timestamp); } else { - $this->debug('get_metar_from_web(): Updating METAR for <code>' . $this->get_location() . '</code>'); + $this->debug('get_metar_from_web(): Updating METAR for <code>' . + $this->get_location() . '</code>'); $this->db->update_metar($icao, $metar, $timestamp); } - /* We update and return the METAR */ + /* We update and return the METAR */ $this->metar = $metar; return $metar; } Index: db_layer.php =================================================================== RCS file: /cvsroot/phpweather/phpweather/db_layer.php,v retrieving revision 1.11 retrieving revision 1.12 diff -u -3 -r1.11 -r1.12 --- db_layer.php 7 Mar 2002 09:42:41 -0000 1.11 +++ db_layer.php 20 Mar 2002 19:26:50 -0000 1.12 @@ -1,16 +1,10 @@ <?php -/* This code makes sure that the file is only included once. */ -if (defined('DB_LAYER')) { - return; -} else { - define('DB_LAYER', true); -} -require(PHPWEATHER_BASE_DIR . '/base_object.php'); -require(PHPWEATHER_BASE_DIR . '/db/pw_db_null.php'); -require(PHPWEATHER_BASE_DIR . '/db/pw_db_dba.php'); -require(PHPWEATHER_BASE_DIR . '/db/pw_db_mysql.php'); -require(PHPWEATHER_BASE_DIR . '/db/pw_db_pgsql.php'); +require_once(PHPWEATHER_BASE_DIR . '/base_object.php'); +require_once(PHPWEATHER_BASE_DIR . '/db/pw_db_null.php'); +require_once(PHPWEATHER_BASE_DIR . '/db/pw_db_dba.php'); +require_once(PHPWEATHER_BASE_DIR . '/db/pw_db_mysql.php'); +require_once(PHPWEATHER_BASE_DIR . '/db/pw_db_pgsql.php'); /** * This class is used to maintain the database-object. Index: images-test.php =================================================================== RCS file: /cvsroot/phpweather/phpweather/images-test.php,v retrieving revision 1.1 retrieving revision 1.2 diff -u -3 -r1.1 -r1.2 --- images-test.php 17 Mar 2002 14:02:15 -0000 1.1 +++ images-test.php 20 Mar 2002 19:26:50 -0000 1.2 @@ -8,12 +8,12 @@ error_reporting(E_ALL); -require('phpweather.php'); +require_once('phpweather.php'); -require('output/pw_text_en.php'); -require('output/pw_text_da.php'); +require_once('output/pw_text_en.php'); +require_once('output/pw_text_da.php'); -require('output/pw_images.php'); +require_once('output/pw_images.php'); $weather = new phpweather(array('icao' => 'EHEH')); Index: phpweather.php =================================================================== RCS file: /cvsroot/phpweather/phpweather/phpweather.php,v retrieving revision 1.16 retrieving revision 1.17 diff -u -3 -r1.16 -r1.17 --- phpweather.php 17 Mar 2002 13:32:25 -0000 1.16 +++ phpweather.php 20 Mar 2002 19:26:50 -0000 1.17 @@ -3,7 +3,7 @@ /* The location of this file defines the base directory */ define('PHPWEATHER_BASE_DIR', dirname(__FILE__)); -require(PHPWEATHER_BASE_DIR . '/data_retrieval.php'); +require_once(PHPWEATHER_BASE_DIR . '/data_retrieval.php'); /** * PHP Weather is a class that understands how to parse a raw METAR. |