From 472b396afc76d49b12fe15dad2c391cdb2adad01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= Date: Fri, 3 Mar 2017 08:41:22 +0200 Subject: [PATCH] Web|Builder: Download counters and latest build JSON queries --- webapi/1/admin/bdb.php | 20 ++---- webapi/1/builds.php | 107 ++++++++++++++++++++++++++++-- webapi/1/include/builds.inc.php | 9 ++- webapi/1/include/database.inc.php | 21 ++++++ 4 files changed, 136 insertions(+), 21 deletions(-) diff --git a/webapi/1/admin/bdb.php b/webapi/1/admin/bdb.php index 193617a0cd..9f40142ba9 100644 --- a/webapi/1/admin/bdb.php +++ b/webapi/1/admin/bdb.php @@ -26,8 +26,7 @@ function add_build($json_args) $db = db_open(); $build = (int) $args->build; - $type = ($args->type == 'stable'? BT_STABLE : - ($args->type == 'candidate'? BT_CANDIDATE : BT_UNSTABLE)); + $type = build_type_from_text($args->type); $version = $db->real_escape_string($args->version); $major = (int) $args->major; $minor = (int) $args->minor; @@ -44,21 +43,10 @@ function add_build($json_args) $values .= ", FROM_UNIXTIME($ts)"; } - db_query($db, "INSERT INTO ".DB_TABLE_BUILDS - . " ($header) VALUES ($values)"); + db_query($db, "INSERT INTO ".DB_TABLE_BUILDS." ($header) VALUES ($values)"); $db->close(); } -function get_platform_id($db, $platform) -{ - $result = db_query($db, "SELECT id FROM ".DB_TABLE_PLATFORMS - ." WHERE platform='$platform'"); - while ($row = $result->fetch_assoc()) { - return $row['id']; - } - return 0; -} - function add_file($json_args) { $args = json_decode($json_args); @@ -67,7 +55,7 @@ function add_file($json_args) $db = db_open(); $build = (int) $args->build; - $plat_id = get_platform_id($db, $args->platform); + $plat_id = db_find_platform_id($db, $args->platform); $type = ($args->type == 'binary'? FT_BINARY : ($args->type == 'log'? FT_LOG : FT_NONE)); $name = $db->real_escape_string($args->name); @@ -197,6 +185,7 @@ function purge_old_builds() . "size INT UNSIGNED NOT NULL, " . "md5 CHAR(32), " . "signature TEXT, " + . "dl_total INT UNSIGNED NOT NULL DEFAULT 0, " . "timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP" . ") CHARACTER SET utf8"; db_query($db, $sql); @@ -213,6 +202,7 @@ function purge_old_builds() . "label VARCHAR(30) NOT NULL, " . "blurb TEXT, " . "changes TEXT, " + . "dl_total INT UNSIGNED NOT NULL DEFAULT 0, " . "timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP" . ") CHARACTER SET utf8"; db_query($db, $sql); diff --git a/webapi/1/builds.php b/webapi/1/builds.php index e80ef4acbf..ea01b76c8b 100644 --- a/webapi/1/builds.php +++ b/webapi/1/builds.php @@ -16,8 +16,8 @@ * http://www.gnu.org/licenses/gpl.html */ -ini_set('display_errors', 1); -error_reporting(E_ALL ^ E_NOTICE); +//ini_set('display_errors', 1); +//error_reporting(E_ALL ^ E_NOTICE); require_once('include/builds.inc.php'); @@ -36,6 +36,27 @@ function show_signature($filename) } } +function download_file($filename) +{ + $db = db_open(); + $name = $db->real_escape_string($filename); + $result = db_query($db, "SELECT id, build FROM ".DB_TABLE_FILES." WHERE name='$name'"); + if ($row = $result->fetch_assoc()) { + // Increment the download counters. + db_query($db, "UPDATE ".DB_TABLE_FILES." SET dl_total=dl_total+1 " + ."WHERE id=$row[id]"); + db_query($db, "UPDATE ".DB_TABLE_BUILDS." SET dl_total=dl_total+1 " + ."WHERE build=$row[build]"); + // Redirect to the archive. + header('Status: 307 Temporary Redirect'); + header("Location: ".DENG_ARCHIVE_URL."/".$filename); + } + else { + header('Status: 404 Not Found'); + } + $db->close(); +} + function generate_header($page_title) { header('Content-Type: text/html;charset=UTF-8'); @@ -123,8 +144,8 @@ function generate_build_page($number) .""); $last_plat = $plat; } - $main_url = DENG_ARCHIVE_URL."/".$bin['name']; - $mirror_url = sfnet_link($build_info, $bin['name']); + $main_url = download_link($bin['name']); + $mirror_url = sfnet_link($build_info['type'], $bin['name']); echo(""); echo("
$bin[name]
" ."
" @@ -325,6 +346,71 @@ function generate_build_feed() $db->close(); } +function generate_platform_latest_json($platform, $build_type) +{ + header('Content-Type: application/json'); + + $db = db_open(); + $plat = db_get_platform($db, $platform); + if (empty($plat)) { + echo("{}\n"); + $db->close(); + return; + } + $type = build_type_from_text($build_type); + if ($type == BT_CANDIDATE) { + $type_cond = "b.type!=".BT_UNSTABLE; // can also be stable + } + else { + $type_cond = "b.type=".$type; + } + $result = db_query($db, "SELECT f.name, f.size, f.md5, f.build, b.version, b.major, b.minor, b.patch, UNIX_TIMESTAMP(b.timestamp) FROM ".DB_TABLE_FILES + ." f LEFT JOIN ".DB_TABLE_BUILDS." b ON f.build=b.build " + ."WHERE $type_cond AND f.plat_id=$plat[id] ORDER BY b.timestamp DESC, f.name " + ."LIMIT 1"); + $resp = []; + if ($row = $result->fetch_assoc()) { + $filename = $row['name']; + $build = $row['build']; + $version = human_version($row['version'], $build, build_type_text($type)); + $plat_name = $plat['name']; + $bits = $plat['cpu_bits']; + $date = gmstrftime(RFC_TIME, $row['UNIX_TIMESTAMP(b.timestamp)']); + if ($bits > 0) { + $full_title = "Doomsday $version for $plat_name or later (${bits}-bit)"; + } + else { + $full_title = "Doomsday $version $plat_name"; + } + $resp += [ + 'build_uniqueid' => (int) $build, + 'build_type' => build_type_text($type), + 'build_startdate' => $date, + 'platform_name' => $platform, + 'title' => "Doomsday ".omit_zeroes($row['version']), + 'fulltitle' => $full_title, + 'version' => omit_zeroes($row['version']), + 'version_major' => (int) $row['major'], + 'version_minor' => (int) $row['minor'], + 'version_patch' => (int) $row['patch'], + 'direct_download_uri' => download_link($filename), + 'direct_download_fallback_uri' => sfnet_link($type, $filename), + 'file_size' => (int) $row['size'], + 'file_md5' => $row['md5'], + 'release_changeloguri' => DENG_API_URL."/builds?number=$build&format=html", + 'release_notesuri' => DENG_WIKI_URL."/Doomsday_version_" + .omit_zeroes($row['version']), + 'release_date' => $date, + 'is_unstable' => ($type == BT_UNSTABLE) + ]; + echo(json_encode($resp)); + } + else { + echo("{}\n"); + } + $db->close(); +} + //--------------------------------------------------------------------------------------- setlocale(LC_ALL, 'en_US.UTF-8'); @@ -334,6 +420,16 @@ function generate_build_feed() show_signature($filename); return; } + if ($filename = $_GET['dl']) { + download_file($filename); + return; + } + if ($latest_for = $_GET['latest_for']) { + $type = $_GET['type']; + if (empty($type)) $type = 'stable'; + generate_platform_latest_json($latest_for, $type); + return; + } $number = $_GET['number']; $format = $_GET['format']; if ($format == 'html') { @@ -347,4 +443,7 @@ function generate_build_feed() else if ($format == 'feed') { generate_build_feed(); } + else if (empty($number) && empty($format)) { + generate_build_index_page(); + } } diff --git a/webapi/1/include/builds.inc.php b/webapi/1/include/builds.inc.php index 345918a669..8c11261c9f 100644 --- a/webapi/1/include/builds.inc.php +++ b/webapi/1/include/builds.inc.php @@ -79,10 +79,15 @@ function cmp_name($a, $b) return strcmp($a['name'], $b['name']); } -function sfnet_link($build_data, $name) +function download_link($name) +{ + return DENG_API_URL."/builds?dl=".$name; +} + +function sfnet_link($build_type, $name) { $sfnet_url = 'http://sourceforge.net/projects/deng/files/Doomsday%20Engine/'; - if ($build_data['type'] == BT_STABLE) { + if ($build_type == BT_STABLE) { $sfnet_url .= "$build_data[version]/"; } else { diff --git a/webapi/1/include/database.inc.php b/webapi/1/include/database.inc.php index fe5ceaacd3..69dddd4fb5 100644 --- a/webapi/1/include/database.inc.php +++ b/webapi/1/include/database.inc.php @@ -62,3 +62,24 @@ function build_type_text($build_type) } return ''; } + +function build_type_from_text($text) +{ + return ($text == 'stable'? BT_STABLE : ($text == 'candidate'? BT_CANDIDATE : BT_UNSTABLE)); +} + +function db_get_platform($db, $platform) +{ + $result = db_query($db, "SELECT * FROM ".DB_TABLE_PLATFORMS + ." WHERE platform='$platform'"); + return $result->fetch_assoc(); +} + +function db_find_platform_id($db, $platform) +{ + $plat = db_get_platform($db, $platform); + if (array_key_exists('id', $plat)) { + return $plat['id']; + } + return 0; +}