2012-03-01 04:01:19 +04:00
|
|
|
<?php
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
// This script contains utility functions used by multiple other scripts.
|
|
|
|
|
|
|
|
|
|
|
|
// Function to safely escape variables handed to awk
|
|
|
|
function awk_quote($string) {
|
|
|
|
return strtr(preg_replace("/([\]\[^$.*?+{}\/\\\\()|])/", '\\\\$1', $string),
|
|
|
|
array('`'=>'\140',"'"=>'\047'));
|
|
|
|
}
|
|
|
|
|
2012-04-12 20:43:23 +04:00
|
|
|
// Function to sanitize names to be used in IDs, etc.
|
|
|
|
function sanitize_name($string, $maxlength = 0) {
|
2012-04-12 20:41:10 +04:00
|
|
|
$conv_name = strtolower(iconv('UTF-8', 'ASCII//TRANSLIT', $string));
|
|
|
|
$newname = ''; $i = 0;
|
2012-04-12 20:43:23 +04:00
|
|
|
while (($i < strlen($conv_name)) && (!$maxlength || (strlen($newname) < $maxlength))) {
|
2012-04-12 20:41:10 +04:00
|
|
|
if (((ord($conv_name{$i}) >= 48) && (ord($conv_name{$i}) <= 57)) ||
|
|
|
|
((ord($conv_name{$i}) >= 97) && (ord($conv_name{$i}) <= 122))) {
|
|
|
|
$newname .= $conv_name{$i};
|
|
|
|
}
|
2012-04-16 15:25:52 +04:00
|
|
|
elseif (strlen($newname) && ($newname{strlen($newname)-1} != '_')) {
|
2012-04-12 20:41:10 +04:00
|
|
|
$newname .= '_';
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
$newname = trim($newname, '_');
|
|
|
|
return $newname;
|
|
|
|
}
|
|
|
|
|
2012-03-01 04:01:19 +04:00
|
|
|
// Function to calculate square of value - mean
|
|
|
|
function arr_mean($array) { return array_sum($array) / count($array); }
|
|
|
|
|
|
|
|
// Function to calculate square of value - mean
|
|
|
|
function dist_square($x, $mean) { return pow($x - $mean, 2); }
|
|
|
|
|
|
|
|
// Function to calculate standard deviation (uses sist_square)
|
|
|
|
function arr_stddev($array, $mean = null) {
|
|
|
|
// square root of sum of squares devided by N-1
|
|
|
|
if (is_null($mean)) { $mean = arr_mean($array); }
|
2012-06-09 00:11:15 +04:00
|
|
|
return sqrt(array_sum(array_map('dist_square', $array,
|
2012-03-01 04:01:19 +04:00
|
|
|
array_fill(0, count($array), $mean))) /
|
|
|
|
(count($array) - 1));
|
|
|
|
}
|
|
|
|
|
2012-06-09 00:11:15 +04:00
|
|
|
// Function to format a numerical value with units
|
|
|
|
function formatValue($aValue, $aPrecision, $aUnit) {
|
|
|
|
$formatted = '';
|
|
|
|
if ($aUnit == 'kMG') {
|
2012-06-09 00:58:37 +04:00
|
|
|
$val = $aValue;
|
|
|
|
$prec = $aPrecision;
|
2012-06-09 00:11:15 +04:00
|
|
|
$unit = '';
|
|
|
|
if ($aValue > 1e10) {
|
|
|
|
$prec = ($prec === null) ? 0 : $prec;
|
|
|
|
$val = round($val / 1e9, $prec);
|
|
|
|
$unit = 'G';
|
|
|
|
}
|
2012-06-09 00:57:01 +04:00
|
|
|
elseif ($aValue > 1e9) {
|
2012-06-09 00:11:15 +04:00
|
|
|
$prec = ($prec === null) ? 1 : $prec;
|
|
|
|
$val = round($val / 1e9, $prec);
|
|
|
|
$unit = 'G';
|
|
|
|
}
|
2012-06-09 00:57:01 +04:00
|
|
|
elseif ($aValue > 1e7) {
|
2012-06-09 00:11:15 +04:00
|
|
|
$prec = ($prec === null) ? 0 : $prec;
|
|
|
|
$val = round($val / 1e6, $prec);
|
|
|
|
$unit = 'M';
|
|
|
|
}
|
2012-06-09 00:57:01 +04:00
|
|
|
elseif ($aValue > 1e6) {
|
2012-06-09 00:11:15 +04:00
|
|
|
$prec = ($prec === null) ? 1 : $prec;
|
|
|
|
$val = round($val / 1e6, $prec);
|
|
|
|
$unit = 'M';
|
|
|
|
}
|
2012-06-09 00:57:01 +04:00
|
|
|
elseif ($aValue > 1e4) {
|
2012-06-09 00:11:15 +04:00
|
|
|
$prec = ($prec === null) ? 0 : $prec;
|
|
|
|
$val = round($val / 1e3, $prec);
|
|
|
|
$unit = 'k';
|
|
|
|
}
|
2012-06-09 00:57:01 +04:00
|
|
|
elseif ($aValue > 1e3) {
|
2012-06-09 00:11:15 +04:00
|
|
|
$prec = ($prec === null) ? 1 : $prec;
|
2012-06-09 00:57:52 +04:00
|
|
|
$val = round($val / 1e3, $prec);
|
2012-06-09 00:11:15 +04:00
|
|
|
$unit = 'k';
|
|
|
|
}
|
2012-06-09 00:57:01 +04:00
|
|
|
elseif ($prec !== null) {
|
2012-06-09 00:11:15 +04:00
|
|
|
$val = round($val, $prec);
|
|
|
|
}
|
|
|
|
$formatted = $val.$unit;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$formatted = round($aValue, $aPrecision);
|
|
|
|
if ($aUnit)
|
|
|
|
$formatted .= $aUnit;
|
|
|
|
}
|
|
|
|
return $formatted;
|
|
|
|
}
|
|
|
|
|
2012-03-01 04:01:19 +04:00
|
|
|
?>
|