forked from jpatokal/openflights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.php
192 lines (175 loc) · 7 KB
/
stats.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
include 'locale.php';
include 'db.php';
include 'helper.php';
include 'filter.php';
$uid = $_SESSION["uid"];
$public = "O"; // by default...
if(!$uid or empty($uid)) {
// If not logged in, default to demo mode
$uid = 1;
}
// This applies only when viewing another's flights
$user = $_POST["user"];
if(! $user) {
$user = $_GET["user"];
}
$trid = $_POST["trid"];
// Verify that this trip and user are public
$filter = "";
if($uid == 1 && $trid && $trid != "0") {
// Verify that we're allowed to access this trip
$sql = "SELECT * FROM trips WHERE trid=" . mysql_real_escape_string($trid);
$result = mysql_query($sql, $db);
if($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$public = $row["public"];
if($row["uid"] != $uid and $public == "N") {
die('Error;' . _("This trip is not public."));
} else {
$uid = $row["uid"];
}
} else {
die('Error;' . _("Trip not found."));
}
}
if($user && $user != "0") {
// Verify that we're allowed to view this user's flights
$sql = "SELECT uid,public FROM users WHERE name='" . mysql_real_escape_string($user) . "'";
$result = mysql_query($sql, $db);
if($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$public = $row["public"];
if($public == "N") {
die('Error;' . _("This user's flights are not public."));
} else {
$uid = $row["uid"];
}
} else {
die('Error;' . _("User not found."));
}
}
$filter = "f.uid=" . $uid . getFilterString($_POST);
$array = array();
// Convert mi to km if units=K
$units = $_SESSION["units"];
if($units == "K") {
$unit = _("km");
$multiplier = "* " . $KMPERMILE;
} else {
$unit = _("mi");
$multiplier = "";
}
// unique airports, and countries
$sql = "SELECT COUNT(DISTINCT a.apid) AS num_airports, COUNT(DISTINCT a.country) AS num_countries FROM flights AS f,airports AS a WHERE (f.src_apid=a.apid OR f.dst_apid=a.apid) AND " . $filter;
$result = mysql_query($sql, $db);
if($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$array += $row;
}
// unique airlines, unique planes, total distance (mi), average distance (localized), average duration
$sql = "SELECT COUNT(DISTINCT alid) AS num_airlines, COUNT(DISTINCT plid) AS num_planes, SUM(distance) AS distance, ROUND(AVG(distance) $multiplier) AS avg_distance, TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(duration))/COUNT(duration)), '%H:%i') AS avg_duration FROM flights AS f WHERE " . $filter;
$result = mysql_query($sql, $db);
if($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$array += $row;
}
$array["avg_distance"] .= " " . $unit;
$array["localedist"] = round($array["distance"] * ($units == "K" ? $KMPERMILE : 1)) . " " . $unit;
print json_encode($array) . "\n";
// longest and shortest
// 0 desc, 1 distance, 2 duration, 3 src_iata, 4 src_icao, 5 src_apid, 6 dst_iata, 7 dst_icao, 8 dst_apid
$sql = sprintf("(SELECT '%s',ROUND(f.distance %s) AS distance,DATE_FORMAT(duration, '%%H:%%i') AS duration,s.iata,s.icao,s.apid,d.iata,d.icao,d.apid FROM flights AS f,airports AS s,airports AS d WHERE f.src_apid=s.apid AND f.dst_apid=d.apid AND " . $filter . " ORDER BY distance DESC LIMIT 1) UNION " .
"(SELECT '%s',ROUND(f.distance %s) AS distance,DATE_FORMAT(duration, '%%H:%%i') AS duration,s.iata,s.icao,s.apid,d.iata,d.icao,d.apid FROM flights AS f,airports AS s,airports AS d WHERE f.src_apid=s.apid AND f.dst_apid=d.apid AND " . $filter . " ORDER BY distance ASC LIMIT 1)",
_("Longest"), $multiplier,
_("Shortest"), $multiplier);
$result = mysql_query($sql, $db);
$first = true;
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
if($first) {
$first = false;
} else {
printf(";");
}
$src_code = format_apcode2($row[3], $row[4]);
$dst_code = format_apcode2($row[6], $row[7]);
printf ("%s,%s %s,%s,%s,%s,%s,%s", $row[0], $row[1], $unit, $row[2], $src_code, $row[5], $dst_code, $row[8]);
}
printf ("\n");
// North, South, West, East
// 0 desc, 1 iata, 2 icao, 3 apid, 4 x, 5 y
$sql = sprintf("(SELECT '%s',iata,icao,apid,x,y FROM airports WHERE y=(SELECT MAX(y) FROM airports AS a, flights AS f WHERE (f.src_apid=a.apid OR f.dst_apid=a.apid) AND " . $filter . ") ORDER BY iata LIMIT 1) UNION " .
"(SELECT '%s',iata,icao,apid,x,y FROM airports WHERE y=(SELECT MIN(y) FROM airports AS a, flights AS f WHERE (f.src_apid=a.apid OR f.dst_apid=a.apid) AND " . $filter . ") ORDER BY iata LIMIT 1) UNION " .
"(SELECT '%s',iata,icao,apid,x,y FROM airports WHERE x=(SELECT MIN(x) FROM airports AS a, flights AS f WHERE (f.src_apid=a.apid OR f.dst_apid=a.apid) AND " . $filter . ") ORDER BY iata LIMIT 1) UNION " .
"(SELECT '%s',iata,icao,apid,x,y FROM airports WHERE x=(SELECT MAX(x) FROM airports AS a, flights AS f WHERE (f.src_apid=a.apid OR f.dst_apid=a.apid) AND " . $filter . ") ORDER BY iata LIMIT 1)",
addslashes(_("Northernmost")), addslashes(_("Southernmost")),
addslashes(_("Westernmost")), addslashes(_("Easternmost")));
$result = mysql_query($sql, $db);
$first = true;
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
if($first) {
$first = false;
} else {
printf(":");
}
$code = format_apcode2($row[1], $row[2]);
printf ("%s,%s,%s,%s,%s", $row[0], $code, $row[3], $row[4], $row[5]);
}
printf ("\n");
// Censor remaining info unless in full-public mode
if($public != "O") {
print "\n\n\n";
exit;
}
// Classes (by number of flights and distance)
$sql = "SELECT DISTINCT class,COUNT(*) num_flights,SUM(distance) distance FROM flights AS f WHERE " . $filter . " AND class != '' GROUP BY class ORDER BY class";
$result = mysql_query($sql, $db);
$class_by_flight_str = '';
$class_by_distance_str = '';
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
if(!empty($class_by_flight_str)) {
$class_by_flight_str .= ':';
$class_by_distance_str .= ':';
}
$class_by_flight_str .= "$row[0],$row[1]";
$class_by_distance_str .= "$row[0],$row[2]";
}
printf ("$class_by_flight_str\n");
// Reason
$sql = "SELECT DISTINCT reason,COUNT(*) FROM flights AS f WHERE " . $filter . " AND reason != '' GROUP BY reason ORDER BY reason";
$result = mysql_query($sql, $db);
$first = true;
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
if($first) {
$first = false;
} else {
printf(":");
}
printf ("%s,%s", $row[0], $row[1]);
}
printf ("\n");
// Seat Type
$sql = "SELECT DISTINCT seat_type,COUNT(*) FROM flights AS f WHERE " . $filter . " AND seat_type != '' GROUP BY seat_type ORDER BY seat_type";
$result = mysql_query($sql, $db);
$first = true;
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
if($first) {
$first = false;
} else {
printf(":");
}
printf ("%s,%s", $row[0], $row[1]);
}
printf ("\n");
// Mode
$sql = "SELECT DISTINCT mode,COUNT(*) FROM flights AS f WHERE " . $filter . " GROUP BY mode ORDER BY mode";
$result = mysql_query($sql, $db);
$first = true;
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
if($first) {
$first = false;
} else {
printf(":");
}
printf ("%s,%s", $row[0], $row[1]);
}
printf ("\n");
// Class (by distance); added at the end so as to not break other potential API users.
printf ("$class_by_distance_str\n");
?>