Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#9837 closed bug (fixed)

Nightly images webpage buggy

Reported by: kneekoo Owned by: haiku-web
Priority: normal Milestone:
Component: Website/CMS Version:
Keywords: Cc:
Blocked By: Blocking:
Has a Patch: no Platform: All

Description

http://www.haiku-files.org/haiku/development/

I noticed some builds have "xz zip" while others have "zip xz". I nagged the people in #haiku-dev and I got asked if I'm willing to fix it, which I agreed to do. So I got the source code and found out several bugs, outdated PHP code and instead of a quick fix I decided to rewrite the whole thing.

Here's my proposal as a replacement to the current page:

<?php
function format_bytes($size) {
	$units = array(' B', ' KiB', ' MiB', ' GiB', ' TiB');
	for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
	return round($size, 2).$units[$i];
}

function scanDirectory($dirName) {
	$revisions = array();

	// add or remove release types in the $key array and the HTML will automatically be adjusted to them
	// format: "filestamp" => "label"
	$key = array("anyboot" => "Anyboot", "vmware" => "VMDK", "raw" => "Raw", "cd" => "ISO");

	$types = implode("|", array_keys($key));
	if ($listing = scandir($dirName)) {
		foreach ($listing as $file) {
			if ($data = preg_match("/haiku-nightly-(r|hrev)([0-9]+)-x86gcc2hybrid-($types)\.(tar\.xz|zip)/", $file, $matches)) {
				$date = date("Y-m-j", filemtime($dirName.'/'.$file));
				$extension = $matches[4] == "tar.xz" ? "xz" : $matches[4];
				$size = format_bytes(filesize($dirName.'/'.$file));
				if (!array_key_exists($matches[2], $revisions)) {
					$revisions[$matches[2]] = array("link" => "<a href='http://dev.haiku-os.org/changeset/".$matches[2]."'>".$matches[1].$matches[2]."</a>");
					foreach($key as $type) $revisions[$matches[2]][$type] = array();
				}
				$package = "<a href=\"./$file\" title=\"$size $date\">$extension</a>";
				$revisions[$matches[2]][$key[$matches[3]]][] = $package;
			}
		}
?>
		<table>
			<tr>
				<th>Revision</th>
<?php
				foreach($key as $name) echo "				<th>$name</th>\n";
?>
			</tr>
<?php
		foreach($revisions as $build => $data) {
?>
			<tr>
				<td><?php echo $data['link']; ?></td>
<?php
			foreach($key as $name) {
?>
				<td><?php echo implode(" ", $data[$name]); ?></td>
<?php
			}
?>
			</tr>
<?php
		}
?>
		</table>
<?php
	}
}
?>

The old code uses ereg, it produces some PHP notices and the HTML formatting is not perfect, while my rewrite takes generates perfect HTML formatting and has more compact PHP code. I tested the code against 6 different (full) builds available on the nightlies page and everything's peachy.

<mmadia> when you're done, make a new ticket on dev.haiku-os.org. thanks. <kneekoo> ok

Change History (5)

comment:1 by kneekoo, 6 years ago

By the way, the following:

<?php
		foreach($revisions as $build => $data) {
?>

can be replaced with:

<?php
		foreach($revisions as $data) {
?>

I only used $build for testing.

comment:2 by kneekoo, 6 years ago

<?php
function format_bytes($size) {
	$units = array(' B', ' KiB', ' MiB', ' GiB', ' TiB');
	for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
	return round($size, 2).$units[$i];
}

function scanDirectory($dirName) {
	$revisions = array();

	// add or remove release types in the $key and $compilers arrays and the HTML will automatically be adjusted to them
	// format: "filestamp1" => "label1", "filestamp2" => "label2"
	$key = array("anyboot" => "Anyboot", "vmware" => "VMDK", "raw" => "Raw", "cd" => "ISO");
	// format: "filestamp1", "filestamp2"
	$compilers = array("x86gcc2hybrid", "x86gcc2", "x86gcc4");

	$types = implode("|", array_keys($key));
	$compiler_list = implode("|", $compilers);
	if ($listing = scandir($dirName)) {
		foreach ($listing as $file) {
			if ($data = preg_match("/-nightly-(r|hrev)([0-9]+)-($compiler_list)-($types)\.(tar\.xz|zip)/", $file, $matches)) {
				$date = date("Y-m-j", filemtime($dirName.'/'.$file));
				$extension = $matches[5] == "tar.xz" ? "xz" : $matches[5];
				$size = format_bytes(filesize($dirName.'/'.$file));
				if (!array_key_exists($matches[2], $revisions)) {
					$revisions[$matches[2]] = array("link" => "<a href='http://dev.haiku-os.org/changeset/".$matches[2]."'>".$matches[1].$matches[2]."</a>");
					foreach($key as $type) $revisions[$matches[2]][$type] = array();
				}
				$package = "<a href=\"./$file\" title=\"$size $date\">$extension</a>";
				$revisions[$matches[2]][$key[$matches[4]]][] = $package;
			}
		}
?>
		<table>
			<tr>
				<th>Revision</th>
<?php
				foreach($key as $name) echo "				<th>$name</th>\n";
?>
			</tr>
<?php
		foreach($revisions as $data) {
?>
			<tr>
				<td><?php echo $data['link']; ?></td>
<?php
			foreach($key as $name) {
?>
				<td><?php echo implode(" ", $data[$name]); ?></td>
<?php
			}
?>
			</tr>
<?php
		}
?>
		</table>
<?php
	}
}
?>

comment:3 by kneekoo, 6 years ago

OK, this code covers even more build cases, easily configurable.

<?php
function format_bytes($size) {
	$units = array(' B', ' KiB', ' MiB', ' GiB', ' TiB');
	for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
	return round($size, 2).$units[$i];
}

function scanDirectory($dirName) {
	$revisions = array();

	// add or remove release types in the $key, $compilers and $releases arrays and the HTML will automatically be adjusted to them
	// format: "filestamp1" => "label1", "filestamp2" => "label2"
	$key = array("anyboot" => "Anyboot", "vmware" => "VMDK", "raw" => "Raw", "cd" => "ISO");
	// format: "filestamp1", "filestamp2"
	$compilers = array("x86gcc2hybrid", "x86gcc2", "x86gcc4");
	// format: "filestamp1", "filestamp2"
	$releases = array("r", "hrev", "hrevr1alpha4");

	$types = implode("|", array_keys($key));
	$compiler_list = implode("|", $compilers);
	$release_list = implode("|", $releases);
	if ($listing = scandir($dirName)) {
		foreach ($listing as $file) {
			if ($data = preg_match("/-($release_list)([0-9]+)-($compiler_list)-($types)\.(tar\.xz|zip)/", $file, $matches)) {
				$date = date("Y-m-j", filemtime($dirName.'/'.$file));
				$extension = $matches[5] == "tar.xz" ? "xz" : $matches[5];
				$size = format_bytes(filesize($dirName.'/'.$file));
				if (!array_key_exists($matches[2], $revisions)) {
					$revisions[$matches[2]] = array("link" => "<a href='http://dev.haiku-os.org/changeset/".$matches[2]."'>".$matches[1].$matches[2]."</a>");
					foreach($key as $type) $revisions[$matches[2]][$type] = array();
				}
				$package = "<a href=\"./$file\" title=\"$size $date\">$extension</a>";
				$revisions[$matches[2]][$key[$matches[4]]][] = $package;
			}
		}
?>
		<table>
			<tr>
				<th>Revision</th>
<?php
				foreach($key as $name) echo "				<th>$name</th>\n";
?>
			</tr>
<?php
		foreach($revisions as $data) {
?>
			<tr>
				<td><?php echo $data['link']; ?></td>
<?php
			foreach($key as $name) {
?>
				<td><?php echo implode(" ", $data[$name]); ?></td>
<?php
			}
?>
			</tr>
<?php
		}
?>
		</table>
<?php
	}
}
?>

comment:4 by stpere, 6 years ago

Resolution: fixed
Status: newclosed

Patch applied! Thanks!

comment:5 by kneekoo, 6 years ago

Finally, it looks normal. :D

Note: See TracTickets for help on using tickets.