From b0258437cdf18d9754117c4d3b2bb8a56a59878a Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Tue, 19 Nov 2013 15:34:42 +0200 Subject: [PATCH] - Added section on configuration to README.md - Made the application more configurable by the user - Made installation easier - API location is now dynamic based on window.location - Cleaned up some older code that was pointing to dev stuff - Now includes the SQL-table required to run the application - Application now exposes config to console for easier debugging - Search now supports user specified table name - Updated the OpenFlights airport data file source --- README.md | 13 ++++++ app/api/config.sample.php | 17 +++++-- app/api/import.php | 89 +++++++++++++++++++++++++++--------- app/api/lentolaskuri.sql | 12 +++++ app/api/search.php | 80 +++++++++++++++++++------------- app/scripts/config.js | 4 +- app/scripts/libs/airports.js | 2 +- app/scripts/main.js | 3 +- 8 files changed, 158 insertions(+), 62 deletions(-) create mode 100644 app/api/lentolaskuri.sql diff --git a/README.md b/README.md index 18d34ff..80e9bf0 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,19 @@ grunt server # Start development server grunt # Build minified and optimized version for release to /dist -folder ``` +### Configuration ### + +1. Configure database access in ``app/api/config.sample.php`` and save as ``app/api/config.php``. The ``config.php`` file gets ignored in ``.gitignore`` so no worries. +2. Build project using ``grunt``, this generates ``dist`` folder +3. Push files and folders in ``dist/*`` to your server, for example as **http://example.com/lentolaskuri** +4. Set up MySQL database table to your server and import airport data to your database: + - Visit **http://example.com/lentolaskuri/api/import.php** to create mysql table automagically (or import ``app/api/lentolaskuri.sql`` by hand, but change table name to one you used in ``config.php``) + - Change variable ``$config['create_table']`` to ``false`` in ``app/api/config.php`` to prevent further table creation attempts + - ProTip: You can make ``cron`` or similar visit this url weekly to update the latest airport data from openflights database +6. Test your lentolaskuri application +7. Donate money based on emissions to a good cause + + ## Credits ## - Build by [Juho Teperi](https://github.com/Deraen) in 2013 while working for [Ekokumppanit Oy](http://www.ekokumppanit.fi) diff --git a/app/api/config.sample.php b/app/api/config.sample.php index 06d5ebc..7b3492c 100644 --- a/app/api/config.sample.php +++ b/app/api/config.sample.php @@ -4,13 +4,20 @@ * * Configure these and open the project in your browser, * install script takes care of the rest. + * + * @category Lentolaskuri + * @package Lentolaskuri + * @author Ismo Vuorinen + * @license http://www.gnu.org/licenses/agpl.html GNU Affero General Public License + * @link https://github.com/Ekokumppanit/Lentolaskuri */ $config = array( - 'server' => 'localhost', - 'db' => 'database', - 'user' => 'username', - 'password' => 'password', - 'table' => 'airports' + 'server' => 'localhost', + 'db' => 'database', + 'user' => 'username', + 'password' => 'password', + 'table' => 'airports', + 'create_table' => true ); // ---- diff --git a/app/api/import.php b/app/api/import.php index d8cf52d..27151b5 100644 --- a/app/api/import.php +++ b/app/api/import.php @@ -1,28 +1,75 @@ + * @license http://www.gnu.org/licenses/agpl.html GNU Affero General Public License + * @link https://github.com/Ekokumppanit/Lentolaskuri + */ -require_once('config.php'); +require_once 'config.php'; -if (($handle = fopen("http://openflights.svn.sourceforge.net/viewvc/openflights/openflights/data/airports.dat", "r")) !== FALSE) { - while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { - $id = $mysqli->real_escape_string($data[0]); - $name = $mysqli->real_escape_string($data[1]); - $city = $mysqli->real_escape_string($data[2]); - $country = $mysqli->real_escape_string($data[3]); - $iata = $mysqli->real_escape_string($data[4]); - $icao = $mysqli->real_escape_string($data[5]); - if (empty($iata)) { - continue; - } - $lat = $mysqli->real_escape_string($data[6]); - $long = $mysqli->real_escape_string($data[7]); +// If we can see lentolaskuri.sql and config.php setting +// $config['create_table'] is true, we try to create table for airports. +if (is_readable('lentolaskuri.sql') && $config['create_table']) { + $create_table = file_get_contents('lentolaskuri.sql'); + if (! empty($create_table)) { - $query = "REPLACE INTO airports (id, name, city, country, iata, icao, lat, `long`) VALUES ('$id', '$name', '$city', '$country', '$iata', '$icao', '$lat', '$long')"; - if (!$mysqli->query($query)) { - printf("Error: %s\n", $mysqli->sqlstate); - exit(); - } - } - fclose($handle); + // Test if user has changed database table name and change accordingly + if ($config['table'] !== 'airports') { + $create_table = str_replace( + "CREATE TABLE IF NOT EXISTS `airports`", + "CREATE TABLE IF NOT EXISTS `".$config['db']."`.`".$config['table']."`", + $create_table + ); + } + + if (! $mysqli->query($create_table)) { + printf("Error: %s\n", $mysqli->sqlstate); + exit(); + } + + echo "Created table: {$config['db']}.{$config['table']} (Please turn off table creation now from config.php: 'create_table' => false )\n"; + + } else { + die("Couldn't find lentolaskuri.sql"); + } +} + + +// Try to load the airports.dat file, or bail out +if (($handle = fopen("http://sourceforge.net/p/openflights/code/HEAD/tree/openflights/data/airports.dat?format=raw", "r")) !== false) { + + // Similar to fgets() except that fgetcsv() parses the line it reads for + // fields in CSV format and returns an array containing the fields read. + while (($data = fgetcsv($handle, 1000, ",")) !== false) { + $id = $mysqli->real_escape_string($data[0]); + $name = $mysqli->real_escape_string($data[1]); + $city = $mysqli->real_escape_string($data[2]); + $country = $mysqli->real_escape_string($data[3]); + $iata = $mysqli->real_escape_string($data[4]); + $icao = $mysqli->real_escape_string($data[5]); + if (empty($iata)) { + continue; + } + $lat = $mysqli->real_escape_string($data[6]); + $long = $mysqli->real_escape_string($data[7]); + + // REPLACE works exactly like INSERT, except that if an old row in the + // table has the same value as a new row for a PRIMARY KEY or + // a UNIQUE index, the old row is deleted before the new row is inserted. + // http://dev.mysql.com/doc/refman/5.0/en/replace.html + $query = "REPLACE INTO ".$config['db'].".".$config['table']." (id, name, city, country, iata, icao, lat, `long`) " + . "VALUES ('$id', '$name', '$city', '$country', '$iata', '$icao', '$lat', '$long')"; + + if (! $mysqli->query($query)) { + printf("Error: %s\n", $mysqli->sqlstate); + exit(); + } + } + fclose($handle); } $mysqli->close(); diff --git a/app/api/lentolaskuri.sql b/app/api/lentolaskuri.sql new file mode 100644 index 0000000..31adf14 --- /dev/null +++ b/app/api/lentolaskuri.sql @@ -0,0 +1,12 @@ +CREATE TABLE IF NOT EXISTS `airports` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `name` varchar(200) NOT NULL, + `city` varchar(200) NOT NULL, + `country` varchar(200) NOT NULL, + `iata` varchar(4) NOT NULL, + `icao` varchar(4) NOT NULL, + `lat` float NOT NULL, + `long` float NOT NULL, + PRIMARY KEY (`id`), + FULLTEXT KEY `SEARCH` (`name`,`city`,`country`,`iata`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1; \ No newline at end of file diff --git a/app/api/search.php b/app/api/search.php index 3d50680..5538e57 100644 --- a/app/api/search.php +++ b/app/api/search.php @@ -1,56 +1,70 @@ + * @license http://www.gnu.org/licenses/agpl.html GNU Affero General Public License + * @link https://github.com/Ekokumppanit/Lentolaskuri + */ -require_once('config.php'); +require_once 'config.php'; header("Access-Control-Allow-Origin: *"); -$search = $mysqli->real_escape_string($_GET['s']); -$id = $mysqli->real_escape_string($_GET['i']); +$get_s = (empty($_GET['s'])) ? null : $_GET['s']; +$get_i = (empty($_GET['i'])) ? null : $_GET['i']; + +$search = $mysqli->real_escape_string($get_s); +$id = $mysqli->real_escape_string($get_i); if (empty($search) && empty($id)) { - exit(); + exit(); } $query = ""; if (!empty($search)) { - $query = " - SELECT *, - CASE - WHEN iata LIKE '$search%' THEN 100 - WHEN name LIKE '$search%' THEN 75 - WHEN name LIKE '%$search%' THEN 74 - WHEN city LIKE '$search%' THEN 70 - WHEN country LIKE '$search%' THEN 65 - WHEN city LIKE '%$search%' THEN 60 - WHEN country LIKE '%$search%' THEN 55 + $query = " + SELECT *, + CASE + WHEN iata LIKE '$search%' THEN 100 + WHEN name LIKE '$search%' THEN 75 + WHEN name LIKE '%$search%' THEN 74 + WHEN city LIKE '$search%' THEN 70 + WHEN country LIKE '$search%' THEN 65 + WHEN city LIKE '%$search%' THEN 60 + WHEN country LIKE '%$search%' THEN 55 ELSE 0 END - AS score - FROM airports - WHERE - name LIKE '$search%' OR - name LIKE '%$search%' OR - country LIKE '$search%' OR - country LIKE '%$search%' OR - city LIKE '$search%' OR - city LIKE '%$search%' OR - iata LIKE '$search%' - ORDER BY score DESC - LIMIT 10; - "; + AS + score + FROM + " . $config['table'] . " + WHERE + name LIKE '$search%' OR + name LIKE '%$search%' OR + country LIKE '$search%' OR + country LIKE '%$search%' OR + city LIKE '$search%' OR + city LIKE '%$search%' OR + iata LIKE '$search%' + ORDER BY + score DESC + LIMIT 10;"; } else if (!empty($id)) { - $query ="SELECT * FROM airports WHERE id LIKE '$id';"; + $query = "SELECT * FROM " . $config['table'] . " WHERE id LIKE '$id';"; } $results = array(); if ($result = $mysqli->query($query)) { - while ($row = $result->fetch_assoc()) { - $results[] = $row; - } + while ($row = $result->fetch_assoc()) { + $results[] = $row; + } - print(json_encode($results)); + print(json_encode($results)); } else { - printf("Error: %s\n", $mysqli->sqlstate); + printf("Error: %s\n", $mysqli->sqlstate); } diff --git a/app/scripts/config.js b/app/scripts/config.js index 3251c01..6cf335e 100644 --- a/app/scripts/config.js +++ b/app/scripts/config.js @@ -1,8 +1,10 @@ 'use strict'; +var searchlocation = window.location; + define(function () { return { - api: 'http://work.lentolaskuri.fi/api', + api: searchlocation + 'api', R: 6371, radiativeForceFactor: function (dist) { return (dist >= 500) ? 2.0 : 1.0; diff --git a/app/scripts/libs/airports.js b/app/scripts/libs/airports.js index c88e5c9..ab4c9e7 100644 --- a/app/scripts/libs/airports.js +++ b/app/scripts/libs/airports.js @@ -5,7 +5,7 @@ define([ 'config' ], function ($, _, config) { var airportById = function (element, callback) { - $.get('http://localhost:8000/search.php?i=' + element.val(), null, function (data) { + $.get( config.api + '/search.php?i=' + element.val(), null, function (data) { callback(data[0]); }); }; diff --git a/app/scripts/main.js b/app/scripts/main.js index fcfdd2f..0fbf461 100644 --- a/app/scripts/main.js +++ b/app/scripts/main.js @@ -48,9 +48,10 @@ require.config({ require([ 'jquery', 'app', + 'config', 'bootstrap', 'libs/handlebar_helpers' -], function ($, App) { +], function ($, App, config) { $('html:first').removeClass('no-js').addClass('js'); // JavaScript detection var app = new App(); });