8 * Copyright (c) 2009-2013 Nicholas J Humfrey. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * 3. The name of the author 'Nicholas J Humfrey" may be used to endorse or
18 * promote products derived from this software without specific prior
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 * @copyright Copyright (c) 2009-2013 Nicholas J Humfrey
35 * @license http://www.opensource.org/licenses/bsd-license.php
40 * Class containing static utility functions
43 * @copyright Copyright (c) 2009-2013 Nicholas J Humfrey
44 * @license http://www.opensource.org/licenses/bsd-license.php
50 * Convert a string into CamelCase
52 * A capital letter is inserted for any non-letter (including userscore).
54 * 'hello world' becomes HelloWorld
55 * 'rss-tag-soup' becomes RssTagSoup
56 * 'FOO//BAR' becomes FooBar
58 * @param string The input string
59 * @return string The input string converted to CamelCase
61 public static function camelise($str)
64 foreach (preg_split('/[\W_]+/', $str) as $part) {
65 $cc .= ucfirst(strtolower($part));
71 * Check if something is an associative array
73 * Note: this method only checks the key of the first value in the array.
75 * @param mixed $param The variable to check
76 * @return bool true if the variable is an associative array
78 public static function isAssociativeArray($param)
80 if (is_array($param)) {
81 $keys = array_keys($param);
93 * Remove the fragment from a URI (if it has one)
95 * @param mixed $uri A URI
96 * @return string The same URI with the fragment removed
98 public static function removeFragmentFromUri($uri)
100 $pos = strpos($uri, '#');
101 if ($pos === false) {
104 return substr($uri, 0, $pos);
108 /** Return pretty-print view of a resource URI
110 * This method is mainly intended for internal use and is used by
111 * EasyRdf_Graph and EasyRdf_Sparql_Result to format a resource
114 * @param mixed $resource An EasyRdf_Resource object or an associative array
115 * @param string $format Either 'html' or 'text'
116 * @param string $color The colour of the text
119 public static function dumpResourceValue($resource, $format = 'html', $color = 'blue')
121 if (!preg_match('/^#?[-\w]+$/', $color)) {
122 throw new InvalidArgumentException(
123 "\$color must be a legal color code or name"
127 if (is_object($resource)) {
128 $resource = strval($resource);
129 } elseif (is_array($resource)) {
130 $resource = $resource['value'];
133 $short = EasyRdf_Namespace::shorten($resource);
134 if ($format == 'html') {
135 $escaped = htmlentities($resource, ENT_QUOTES);
136 if (substr($resource, 0, 2) == '_:') {
137 $href = '#' . $escaped;
142 return "<a href='$href' style='text-decoration:none;color:$color'>$short</a>";
144 return "<a href='$href' style='text-decoration:none;color:$color'>$escaped</a>";
155 /** Return pretty-print view of a literal
157 * This method is mainly intended for internal use and is used by
158 * EasyRdf_Graph and EasyRdf_Sparql_Result to format a literal
161 * @param mixed $literal An EasyRdf_Literal object or an associative array
162 * @param string $format Either 'html' or 'text'
163 * @param string $color The colour of the text
166 public static function dumpLiteralValue($literal, $format = 'html', $color = 'black')
168 if (!preg_match('/^#?[-\w]+$/', $color)) {
169 throw new InvalidArgumentException(
170 "\$color must be a legal color code or name"
174 if (is_object($literal)) {
175 $literal = $literal->toRdfPhp();
176 } elseif (!is_array($literal)) {
177 $literal = array('value' => $literal);
180 $text = '"'.$literal['value'].'"';
181 if (isset($literal['lang'])) {
182 $text .= '@' . $literal['lang'];
184 if (isset($literal['datatype'])) {
185 $short = EasyRdf_Namespace::shorten($literal['datatype']);
189 $text .= "^^<".$literal['datatype'].">";
193 if ($format == 'html') {
194 return "<span style='color:$color'>".
195 htmlentities($text, ENT_COMPAT, "UTF-8").
202 /** Clean up and split a mime-type up into its parts
204 * @param string $mimeType A MIME Type, optionally with parameters
205 * @return array $type, $parameters
207 public static function parseMimeType($mimeType)
209 $parts = explode(';', strtolower($mimeType));
210 $type = trim(array_shift($parts));
212 foreach ($parts as $part) {
213 if (preg_match('/^\s*(\w+)\s*=\s*(.+?)\s*$/', $part, $matches)) {
214 $params[$matches[1]] = $matches[2];
217 return array($type, $params);
220 /** Execute a command as a pipe
222 * The proc_open() function is used to open a pipe to a
223 * a command line process, writing $input to STDIN, returning STDOUT
224 * and throwing an exception if anything is written to STDERR or the
225 * process returns non-zero.
227 * @param string $command The command to execute
228 * @param array $args Optional list of arguments to pass to the command
229 * @param string $input Optional buffer to send to the command
230 * @param string $dir Path to directory to run command in (defaults to /tmp)
231 * @return string The result of the command, printed to STDOUT
233 public static function execCommandPipe($command, $args = null, $input = null, $dir = null)
235 $descriptorspec = array(
236 0 => array('pipe', 'r'),
237 1 => array('pipe', 'w'),
238 2 => array('pipe', 'w')
241 // Use the system tmp directory by default
243 $dir = sys_get_temp_dir();
246 if (is_array($args)) {
247 $fullCommand = implode(
249 array_map('escapeshellcmd', array_merge(array($command), $args))
252 $fullCommand = escapeshellcmd($command);
254 $fullCommand .= ' '.escapeshellcmd($args);
258 $process = proc_open($fullCommand, $descriptorspec, $pipes, $dir);
259 if (is_resource($process)) {
260 // $pipes now looks like this:
261 // 0 => writeable handle connected to child stdin
262 // 1 => readable handle connected to child stdout
263 // 2 => readable handle connected to child stderr
266 fwrite($pipes[0], $input);
270 $output = stream_get_contents($pipes[1]);
272 $error = stream_get_contents($pipes[2]);
275 // It is important that you close any pipes before calling
276 // proc_close in order to avoid a deadlock
277 $returnValue = proc_close($process);
279 throw new EasyRdf_Exception(
280 "Error while executing command $command: ".$error
284 throw new EasyRdf_Exception(
285 "Failed to execute command $command"