Update of /cvsroot/phpweather/phpweather
In directory usw-pr-cvs1:/tmp/cvs-serv32396
Modified Files:
configurator.php
Log Message:
The configurator can now also be used to generate the necessary
databases/tables! After that's done, it will load the stations-table
with data.
Index: configurator.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/configurator.php,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -3 -r1.3 -r1.4
--- configurator.php 2001/05/19 10:16:56 1.3
+++ configurator.php 2001/05/19 16:22:09 1.4
@@ -201,15 +201,15 @@
$properties['db_metars'] =
new text('db_metars',
- 'metars.db',
- 'This is the name of the database that is used to cache ' .
- 'METARs.');
+ 'metars',
+ 'This is the name of the database/table that is used ' .
+ 'to cache the METARs.');
$properties['db_stations'] =
new text('db_stations',
- 'stations.db',
- 'This is the name of the database that is used to store ' .
- 'the names of the stations.');
+ 'stations',
+ 'This is the name of the database/table that is used ' .
+ 'to store the names of the stations.');
$properties['db_type'] =
new select('db_type',
@@ -284,8 +284,6 @@
echo "</pre>\n";
$config = "<?php
-echo '<p>defaults.php was included!</p>';
-
/* This is a local configurations file for PHP Weather.
It was generated on the $timestamp. */\n\n";
@@ -306,7 +304,7 @@
} else {
$fp = @fopen('defaults.php', 'w');
}
-
+
echo "<p>Here's the configuration file based on your answers:</p>
<pre>
@@ -320,175 +318,43 @@
} else {
echo "<p>A configuration file could <b>not</b> be written. You should copy the
above configuration to a file, and manually save it as <code>default.php</code>.\n";
}
-
-
-
-} elseif ($action == 'dosql') {
- if ($db_engine == 'MySQL') {
- /* We try to make a connection to the MySQL database. */
- if (mysql_connect($db_hostname, $db_username, $db_password)) {
- echo "<p>Connection established to MySQL database at $hostname.</p>\n";
- $db_connection = 1;
- } else {
- echo "<p>Connection failed!</p>\n";
- $db_connection = 0;
- }
- } elseif ($db_engine == 'PostgreSQL') {
- /* We try to make a connection to the PostgreSQL database. */
- if ($conn = pg_connect($hostname, '', $db_name)) {
- echo "<p>Connection established to PostgreSQL database at $hostname.</p>\n";
- $db_connection = 1;
- } else {
- echo '<p>Connection failed! PostgreSQL said: ' . pg_errormessage() . "</p>\n";
- $db_connection = 0;
- }
- } elseif ($db_engine == 'dbm') {
- if (true) {
- echo "<p>Dbm-files created succesfully.</p>\n";
- $db_connection = 1;
- } else {
- echo "<p>Unable to create dbm-files.</p>\n";
- $db_connection = 0;
- }
- } else {
- /* No database engine - this is an error! */
- $db_connection = 0;
- }
-
- if(!$db_connection) {
- echo "<p>The script couldn't connect to the database, so <b>no</b> rows were
inserted.</p>\n";
+ if ($db_type != 'null') {
+ echo "<p>Click <a href=\"configurator.php?action=do_sql\">here</a> to create the
+necessary databases.</p>\n";
} else {
- $fp = fopen('airports.csv', 'r');
-
- if ($db_engine == 'MySQL') {
- /* We use MySQL. */
- mysql_select_db($db_name);
- /* First we make a table for the METARs */
- mysql_query("DROP TABLE IF EXISTS $db_metars_table");
- mysql_query("
-CREATE TABLE $db_metars_table (
- metar varchar(255) NOT NULL,
- timestamp timestamp(14),
- station varchar(4) NOT NULL,
- PRIMARY KEY (station),
- UNIQUE station (station)
-)");
- echo "<p>Table $db_metars_table has been created.</p>\n";
-
- /* Then we make a table for the stations. */
- mysql_query("DROP TABLE IF EXISTS $db_stations_table");
- mysql_query("
-CREATE TABLE $db_stations_table (
- icao varchar(4) NOT NULL,
- block_nr char(2) NOT NULL,
- station_nr char(3) NOT NULL,
- name varchar(255) NOT NULL,
- state char(2) NOT NULL,
- country varchar(64) NOT NULL,
- wmo_region tinyint(4) DEFAULT '0' NOT NULL,
- latitude varchar(9) NOT NULL,
- longitude varchar(10) NOT NULL,
- u_latitude varchar(9) NOT NULL,
- u_longitude varchar(10) NOT NULL,
- elevation smallint(6) DEFAULT '0' NOT NULL,
- u_elevation smallint(6) DEFAULT '0' NOT NULL,
- rbsn char(1) NOT NULL,
- PRIMARY KEY (icao),
- UNIQUE icao (icao),
- KEY country (country)
-)");
- echo "<p>Table $db_stations_table has been created.</p>\n";
- echo "<p>The rows will now be inserted.</p>\n";
- while ($row = fgetcsv($fp, 1024, ';')) {
- for ($i = 0; $i < count($row); $i++) {
- $row[$i] = addslashes($row[$i]);
- }
- mysql_query("INSERT INTO $db_stations_table VALUES('$row[0]', '$row[1]',
'$row[2]', '$row[3]', '$row[4]', '$row[5]', '$row[6]', '$row[7]', '$row[8]',
'$row[9]', '$row[10]', '$row[11]', '$row[12]', '$row[13]')");
- $inserted_rows++;
- }
- } elseif ($db_engine == 'PostgreSQL') {
- /* We use PostgreSQL. */
- /* First we make a table for the METARs */
- pg_exec("
-CREATE TABLE $db_metars_table (
- metar VARCHAR(255) NOT NULL,
- timestamp TIMESTAMP,
- station VARCHAR(4) PRIMARY KEY NOT NULL
-)", $conn);
- echo "<p>Table $db_metars_table has been created.</p>\n";
-
- /* Then we make a table for the stations. */
- /* [...] */
- echo "<p>Table $db_stations_table has been created.</p>\n";
- echo "<p>The rows will now be inserted.</p>\n";
- while ($row = fgetcsv($fp, 1024, ';')) {
- for ($i = 0; $i < count($row); $i++) {
- $row[$i] = addslashes($row[$i]);
- }
- pg_exec("INSERT INTO $db_stations_table (icao, block_nr, station_nr, name,
state, country, wmo_region, latitude, longitude, u_latitude, u_longitude, elevation,
u_elevation, rbsn) VALUES('$row[0]', '$row[1]', '$row[2]', '$row[3]', '$row[4]',
'$row[5]', '$row[6]', '$row[7]', '$row[8]', '$row[9]', '$row[10]', '$row[11]',
'$row[12]', '$row[13]')", $conn);
- $inserted_rows++;
- }
- } elseif ($db_engine == 'dbm') {
- echo "<p>The rows will now be inserted.</p>\n";
- while ($row = fgetcsv($fp, 1024, ';')) {
- for ($i = 0; $i < count($row); $i++) {
- $row[$i] = addslashes($row[$i]);
- }
- dbminsert($icao-name, $row[0], $row[3]);
- dbminsert($icao-country, $row[0], $row[5]);
- $inserted_rows++;
- }
- }
- echo "<p>$inserted_rows rows were inserted.</p>\n";
+ echo "<p>You're all set!</p>\n";
}
-
- /* We now print a configurationsfile: */
-
- $timestamp = date ('M/j-Y H:i:s');
- echo "<p>Here's a configurationfile based on your answers:</p>\n<hr>\n";
- echo "<pre>
-/* Configurations file for PHP Weather. Generated on $timestamp.
-
- You should save this piece somewhere safe (since it contains your
- password. Then make another file called config.inc in the same
- directory as phpweather.inc. This file should then include the
- secret config-file like this:
-
- <?php
- include(\"$DOCUMENT_ROOT/path/to/secret/place/config.inc\");
- ?>
-
- This way you don't have to change anything when you upgrade to a
- new version. */
-/* The type of database. */
-\$db_engine = '$db_engine';
-
-/* Name of database. */
-\$db_name = '$db_name';
-
-/* Username for database. */
-\$db_username = '$db_username';
-
-/* Password for database. */
-\$db_password = '$db_password';
-
-/* Name of table used for caching the METARs. */
-\$db_metars_table = '$db_metars_table';
+
+} elseif ($action == 'do_sql') {
-/* Name of table used for airport information. */
-\$db_stations_table = '$db_stations_table';
+define('PHPWEATHER_BASE_DIR', getcwd());
+require('db_layer.php');
-/* Hostname of the machine running the database. */
-\$db_hostname = '$db_hostname';
+ $db = new db_layer(array());
+ if ($db->db->create_tables()) {
+ $rows = 0;
+ $countries = 0;
+ echo "<p>The database has been configured. It will now be filled with data.</p>\n";
+ $fp = fopen('stations.csv', 'r');
+ while ($row = fgets($fp, 1024)) {
+ $row = trim($row);
+ if (substr($row, 0, 2) == '##' && substr($row, -2) == '##') {
+ /* We've found a country */
+ $country = substr($row, 3, -3);
+ $countries++;
+ echo "<p>Now processing stations in $country.</p>\n";
+ } elseif ($row[0] != '#' && $row != '') {
+ list($icao, $name) = explode(';', $row, 2);
+ $rows++;
+ $db->db->insert_station($icao, $name, $country);
+ }
+ }
+ echo "<p>Data about $rows stations from $countries countries were inserted.</p>\n";
+ } else {
+ echo "<p>The database was <b>not</b> configured!</p>\n";
+ }
-/* How much is the time off? If you're having problems with the time
- being off, then adjust it here. If all the times are 1 hour behind,
- then set it to 3600.*/
-//\$time_shift = $time_shift
-</pre>\n<hr>\n";
-
} else {
/* No action has been specified - we make a form. */
_______________________________________________
PHPWeather-checkins mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/phpweather-checkins