forked from jpatokal/openflights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtop10.php
151 lines (139 loc) · 4.55 KB
/
top10.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
<?php
include 'locale.php';
include 'db_pdo.php';
include 'helper.php';
include 'filter.php';
$uid = $_SESSION["uid"];
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"];
$trid = $_POST["trid"];
$units = $_SESSION["units"];
$mode = $_POST["mode"];
switch($mode) {
case "D":
$mode = "SUM(distance)";
if($units == "K") $mode = "ROUND($mode * $KMPERMILE)";
break;
case "F":
default:
$mode = "COUNT(fid)";
break;
}
$limit = $_POST["limit"];
if(! $limit) {
$limit = "10";
}
if($limit == "-1") {
$limit = "9999";
}
// Verify that this trip and user are public
if($uid == 1 && $trid && $trid != "0") {
// Verify that we're allowed to access this trip
$sth = $dbh->prepare("SELECT * FROM trips WHERE trid=?");
$sth->execute([$trid]);
if($row = $sth->fetch()) {
if($row["uid"] != $uid and $row["public"] == "N") {
die('Error;' . _("This trip is not public."));
} else {
$uid = $row["uid"];
}
}
}
if($user && $user != "0") {
// Verify that we're allowed to view this user's flights
$sth = $dbh->prepare("SELECT uid,public FROM users WHERE name=?");
$sth->execute([$user]);
if($row = $sth->fetch()) {
if($row["public"] == "N") {
die('Error;' . _("This user's flights are not public."));
} else {
$uid = $row["uid"];
}
}
}
$filter = getFilterString($dbh, $_POST);
// List top 10 routes
$sql = "SELECT DISTINCT s.iata AS siata,s.icao AS sicao,s.apid AS sapid,d.iata AS diata,d.icao AS dicao,d.apid AS dapid,$mode AS times FROM flights AS f, airports AS s, airports AS d WHERE f.src_apid=s.apid AND f.dst_apid=d.apid AND f.uid=:uid $filter GROUP BY s.apid,d.apid ORDER BY times DESC LIMIT :limit";
$sth = $dbh->prepare($sql);
$sth->bindValue(':uid', $uid, PDO::PARAM_INT);
$sth->bindValue(':limit', intval($limit), PDO::PARAM_INT);
$sth->execute();
$first = true;
while ($row = $sth->fetch()) {
if($first) {
$first = false;
} else {
printf(":");
}
$src = format_apcode2($row["siata"], $row["sicao"]);
$dst = format_apcode2($row["diata"], $row["dicao"]);
printf ("%s,%s,%s,%s,%s", $src, $row["sapid"], $dst, $row["dapid"], $row["times"]);
}
printf ("\n");
// List top 10 airports
//$sql = "select a.name, a.iata, a.icao, $mode as count, a.apid from airports as a, " .
// "(select src_apid as apid, distance, count(*) as fid from flights where uid = $uid group by src_apid" .
// " UNION ALL " .
// "select dst_apid as apid, distance, count(*) as fid from flights where uid = $uid group by dst_apid ) as f " .
// "where f.apid=a.apid $filter " .
// "group by a.apid order by count desc limit $limit";
//print $sql;
//
// ^^^ this is even faster, but $mode has to be SUM(fid), not COUNT(fid), to count flights correctly...
$sql = "select a.name, a.iata, a.icao, $mode as count, a.apid from airports as a, " .
"(select src_apid as apid, distance, fid from flights as f where uid = :uid $filter " .
" UNION ALL " .
"select dst_apid as apid, distance, fid from flights as f where uid = :uid $filter ) as f " .
"where f.apid=a.apid " .
"group by a.apid order by count desc limit :limit";
$sth = $dbh->prepare($sql);
$sth->bindValue(':uid', $uid, PDO::PARAM_INT);
$sth->bindValue(':limit', intval($limit), PDO::PARAM_INT);
$sth->execute();
$first = true;
while ($row = $sth->fetch()) {
if($first) {
$first = false;
} else {
printf(":");
}
$code = format_apcode($row);
printf ("%s,%s,%s,%s", $row["name"], $code, $row["count"], $row["apid"]);
}
printf ("\n");
// List top 10 airlines
$sql = "select a.name, $mode as count, a.alid from airlines as a, flights as f where f.uid=:uid and f.alid=a.alid $filter group by f.alid order by count desc limit :limit";
$sth = $dbh->prepare($sql);
$sth->bindValue(':uid', $uid, PDO::PARAM_INT);
$sth->bindValue(':limit', intval($limit), PDO::PARAM_INT);
$sth->execute();
$first = true;
while ($row = $sth->fetch()) {
if($first) {
$first = false;
} else {
printf(":");
}
printf ("%s,%s,%s", $row["name"], $row["count"], $row["alid"]);
}
printf ("\n");
// List top 10 plane types
$sql = "select p.name, $mode as count from planes as p, flights as f where f.uid=:uid and p.plid=f.plid $filter group by f.plid order by count desc limit :limit";
$sth = $dbh->prepare($sql);
$sth->bindValue(':uid', $uid, PDO::PARAM_INT);
$sth->bindValue(':limit', intval($limit), PDO::PARAM_INT);
$sth->execute();
$first = true;
while ($row = $sth->fetch()) {
if($first) {
$first = false;
} else {
printf(":");
}
printf ("%s,%s", $row["name"], $row["count"]);
}
?>