4 * Interface to man units(1), /usr/share/units.dat
7 * Definition: 9.6347558e+08 m^2
8 * $ units "372.0 mi2" m^2
9 * Definition: 9.6347558e+08 m^2
12 * CachedMarkup.php: Cached_SemanticLink::_expandurl()
13 * SemanticWeb.php: class SemanticAttributeSearchQuery
15 * Windows requires the cygwin /usr/bin/units.
16 * All successfully parsed unit definitions are stored in the wikidb,
17 * so that subsequent expansions will not require /usr/bin/units be called again.
18 * So far even on Windows (cygwin) the process is fast enough.
20 * TODO: understand dates and maybe times
21 * YYYY-MM-DD, "CW"ww/yy (CalendarWeek)
26 function __construct()
30 elseif (defined("UNITS_EXE")) // ignore dynamic check
33 exec("units m2", $o, $this->errcode);
37 * $this->_attribute_base = $units->Definition($this->_attribute);
39 function Definition($query)
41 static $Definitions = array();
42 if (isset($Definitions[$query])) return $Definitions[$query];
45 $query = preg_replace("/,/", "", $query);
46 if ($query == '' or $query == '*')
47 return ($Definitions[$query] = '');
48 // detect date values, currently only ISO: YYYY-MM-DD or YY-MM-DD
49 if (preg_match("/^(\d{2,4})-(\d{1,2})-(\d{1,2})$/", $query, $m)) {
50 $date = mktime(0, 0, 0, $m[2], $m[3], $m[1]);
51 return ($Definitions[$query] = "$date date");
53 if (preg_match("/^(\d{2,4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{2}):?(\d{2})?$/", $query, $m)) {
54 $date = mktime($m[4], $m[5], @$m[6], $m[2], $m[3], $m[1]);
55 return ($Definitions[$query] = "$date date");
57 $def = $this->_cmd("\"$query\"");
58 if (preg_match("/Definition: (.+)$/", $def, $m))
59 return ($Definitions[$query] = $m[1]);
61 trigger_error("units: " . $def, E_USER_WARNING);
67 * We must ensure that the same baseunits are matched against.
68 * We cannot compare m^2 to m or ''
69 * $val_base = $this->_units->basevalue($value); // SemanticAttributeSearchQuery
71 function basevalue($query, $def = false)
73 if (!$def) $def = $this->Definition($query);
75 if (is_numeric($def)) // e.g. "1 million"
77 if (preg_match("/^([-0-9].*) \w.*$/", $def, $m))
84 * $this->_unit = $units->baseunit($this->_attribute); // SemanticAttributeSearchQuery
85 * and Cached_SemanticLink::_expandurl()
87 function baseunit($query, $def = false)
89 if (!$def) $def = $this->Definition($query);
91 if (preg_match("/ (.+)$/", $def, $m))
99 if ($this->errcode) return $args;
100 if (defined("UNITS_EXE")) {
101 $s = UNITS_EXE . " $args";
104 $result = `units $args`;
105 return trim($result);