Skip to content

Commit

Permalink
Web|Builder: Download counters and latest build JSON queries
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Mar 3, 2017
1 parent aa27383 commit 472b396
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 21 deletions.
20 changes: 5 additions & 15 deletions webapi/1/admin/bdb.php
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
107 changes: 103 additions & 4 deletions webapi/1/builds.php
Expand Up @@ -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');

Expand All @@ -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');
Expand Down Expand Up @@ -123,8 +144,8 @@ function generate_build_page($number)
."</td>");
$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("<td class='binary'>");
echo("<div class='filename'><a href='$main_url'>$bin[name]</a></div>"
."<div class='fileinfo'>"
Expand Down Expand Up @@ -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');
Expand All @@ -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') {
Expand All @@ -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();
}
}
9 changes: 7 additions & 2 deletions webapi/1/include/builds.inc.php
Expand Up @@ -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 {
Expand Down
21 changes: 21 additions & 0 deletions webapi/1/include/database.inc.php
Expand Up @@ -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;
}

0 comments on commit 472b396

Please sign in to comment.