]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/Units.php
Use UTF-8 for calendar-de.js
[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 __construct()
27     {
28         if (defined('DISABLE_UNITS') and DISABLE_UNITS)
29             $this->errcode = 1;
30         elseif (defined("UNITS_EXE")) // ignore dynamic check
31             $this->errcode = 0;
32         else
33             exec("units m2", $o, $this->errcode);
34     }
35
36     /**
37      * $this->_attribute_base = $units->Definition($this->_attribute);
38      *
39      * @param string $query
40      * @return string
41      */
42     function Definition($query)
43     {
44         static $Definitions = array();
45         if (isset($Definitions[$query])) return $Definitions[$query];
46         if ($this->errcode)
47             return $query;
48         $query = preg_replace("/,/", "", $query);
49         if ($query == '' or $query == '*')
50             return ($Definitions[$query] = '');
51         // detect date values, currently only ISO: YYYY-MM-DD or YY-MM-DD
52         if (preg_match("/^(\d{2,4})-(\d{1,2})-(\d{1,2})$/", $query, $m)) {
53             $date = mktime(0, 0, 0, $m[2], $m[3], $m[1]);
54             return ($Definitions[$query] = "$date date");
55         }
56         if (preg_match("/^(\d{2,4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{2}):?(\d{2})?$/", $query, $m)) {
57             $date = mktime($m[4], $m[5], @$m[6], $m[2], $m[3], $m[1]);
58             return ($Definitions[$query] = "$date date");
59         }
60         $def = $this->_cmd("\"$query\"");
61         if (preg_match("/Definition: (.+)$/", $def, $m))
62             return ($Definitions[$query] = $m[1]);
63         else {
64             trigger_error("units: " . $def, E_USER_WARNING);
65             return '';
66         }
67     }
68
69     /**
70      * We must ensure that the same baseunits are matched against.
71      * We cannot compare m^2 to m or ''
72      * $val_base = $this->_units->basevalue($value); // SemanticAttributeSearchQuery
73      *
74      * @param string $query
75      * @param bool $def
76      * @return bool|string
77      */
78     function basevalue($query, $def = false)
79     {
80         if (!$def)
81             $def = $this->Definition($query);
82         if ($def) {
83             if (is_numeric($def)) // e.g. "1 million"
84                 return $def;
85             if (preg_match("/^([-0-9].*) \w.*$/", $def, $m))
86                 return $m[1];
87         }
88         return '';
89     }
90
91     /**
92      * $this->_unit = $units->baseunit($this->_attribute);  // SemanticAttributeSearchQuery
93      * and Cached_SemanticLink::_expandurl()
94      *
95      * @param string $query
96      * @param bool $def
97      * @return string
98      */
99     function baseunit($query, $def = false)
100     {
101         if (!$def)
102             $def = $this->Definition($query);
103         if ($def) {
104             if (preg_match("/ (.+)$/", $def, $m))
105                 return $m[1];
106         }
107         return '';
108     }
109
110     function _cmd($args)
111     {
112         if ($this->errcode)
113             return $args;
114         if (defined("UNITS_EXE")) {
115             $s = UNITS_EXE . " $args";
116             $result = `$s`;
117         } else
118             $result = `units $args`;
119         return trim($result);
120     }
121 }