]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/Units.php
Allow bold, italics or underlined for numbers
[SourceForge/phpwiki.git] / lib / Units.php
1 <?php
2 /**
3  *
4  * Interface to man units(1), /usr/share/units.dat
5  *
6  * $ units "372.0 mi2"
7  *         Definition: 9.6347558e+08 m^2
8  * $ units "372.0 mi2" m^2
9  *         Definition: 9.6347558e+08 m^2
10  *
11  * Called by:
12  *    CachedMarkup.php: Cached_SemanticLink::_expandurl()
13  *    SemanticWeb.php: class SemanticAttributeSearchQuery
14  *
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.
19  *
20  * TODO: understand dates and maybe times
21  *   YYYY-MM-DD, "CW"ww/yy (CalendarWeek)
22  */
23
24 class Units
25 {
26     function Units()
27     {
28         if (DISABLE_UNITS)
29             $this->errcode = 1;
30         elseif (defined("UNITS_EXE")) // ignore dynamic check
31             $this->errcode = 0; else
32             exec("units m2", $o, $this->errcode);
33     }
34
35     /**
36      * $this->_attribute_base = $units->Definition($this->_attribute);
37      */
38     function Definition($query)
39     {
40         static $Definitions = array();
41         if (isset($Definitions[$query])) return $Definitions[$query];
42         if ($this->errcode)
43             return $query;
44         $query = preg_replace("/,/", "", $query);
45         if ($query == '' or $query == '*')
46             return ($Definitions[$query] = '');
47         // detect date values, currently only ISO: YYYY-MM-DD or YY-MM-DD
48         if (preg_match("/^(\d{2,4})-(\d{1,2})-(\d{1,2})$/", $query, $m)) {
49             $date = mktime(0, 0, 0, $m[2], $m[3], $m[1]);
50             return ($Definitions[$query] = "$date date");
51         }
52         if (preg_match("/^(\d{2,4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{2}):?(\d{2})?$/", $query, $m)) {
53             $date = mktime($m[4], $m[5], @$m[6], $m[2], $m[3], $m[1]);
54             return ($Definitions[$query] = "$date date");
55         }
56         $def = $this->_cmd("\"$query\"");
57         if (preg_match("/Definition: (.+)$/", $def, $m))
58             return ($Definitions[$query] = $m[1]);
59         else {
60             trigger_error("units: " . $def, E_USER_WARNING);
61             return '';
62         }
63     }
64
65     /**
66      * We must ensure that the same baseunits are matched against.
67      * We cannot compare m^2 to m or ''
68      * $val_base = $this->_units->basevalue($value); // SemanticAttributeSearchQuery
69      */
70     function basevalue($query, $def = false)
71     {
72         if (!$def) $def = $this->Definition($query);
73         if ($def) {
74             if (is_numeric($def)) // e.g. "1 million"
75                 return $def;
76             if (preg_match("/^([-0-9].*) \w.*$/", $def, $m))
77                 return $m[1];
78         }
79         return '';
80     }
81
82     /**
83      * $this->_unit = $units->baseunit($this->_attribute);  // SemanticAttributeSearchQuery
84      * and Cached_SemanticLink::_expandurl()
85      */
86     function baseunit($query, $def = false)
87     {
88         if (!$def) $def = $this->Definition($query);
89         if ($def) {
90             if (preg_match("/ (.+)$/", $def, $m))
91                 return $m[1];
92         }
93         return '';
94     }
95
96     function _cmd($args)
97     {
98         if ($this->errcode) return $args;
99         if (defined("UNITS_EXE")) {
100             $s = UNITS_EXE . " $args";
101             $result = `$s`;
102         } else
103             $result = `units $args`;
104         return trim($result);
105     }
106 }