PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/easyrdf/easyrdf/lib/EasyRdf/Utils.php

https://gitlab.com/reasonat/test8
PHP | 291 lines | 155 code | 22 blank | 114 comment | 37 complexity | 1c051e49d2f71b8addce604099d760e6 MD5 | raw file
  1. <?php
  2. /**
  3. * EasyRdf
  4. *
  5. * LICENSE
  6. *
  7. * Copyright (c) 2009-2013 Nicholas J Humfrey. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. * 3. The name of the author 'Nicholas J Humfrey" may be used to endorse or
  17. * promote products derived from this software without specific prior
  18. * written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * @package EasyRdf
  33. * @copyright Copyright (c) 2009-2013 Nicholas J Humfrey
  34. * @license http://www.opensource.org/licenses/bsd-license.php
  35. */
  36. /**
  37. * Class containing static utility functions
  38. *
  39. * @package EasyRdf
  40. * @copyright Copyright (c) 2009-2013 Nicholas J Humfrey
  41. * @license http://www.opensource.org/licenses/bsd-license.php
  42. */
  43. class EasyRdf_Utils
  44. {
  45. /**
  46. * Convert a string into CamelCase
  47. *
  48. * A capital letter is inserted for any non-letter (including userscore).
  49. * For example:
  50. * 'hello world' becomes HelloWorld
  51. * 'rss-tag-soup' becomes RssTagSoup
  52. * 'FOO//BAR' becomes FooBar
  53. *
  54. * @param string The input string
  55. * @return string The input string converted to CamelCase
  56. */
  57. public static function camelise($str)
  58. {
  59. $cc = '';
  60. foreach (preg_split('/[\W_]+/', $str) as $part) {
  61. $cc .= ucfirst(strtolower($part));
  62. }
  63. return $cc;
  64. }
  65. /**
  66. * Check if something is an associative array
  67. *
  68. * Note: this method only checks the key of the first value in the array.
  69. *
  70. * @param mixed $param The variable to check
  71. * @return bool true if the variable is an associative array
  72. */
  73. public static function isAssociativeArray($param)
  74. {
  75. if (is_array($param)) {
  76. $keys = array_keys($param);
  77. if ($keys[0] === 0) {
  78. return false;
  79. } else {
  80. return true;
  81. }
  82. } else {
  83. return false;
  84. }
  85. }
  86. /**
  87. * Remove the fragment from a URI (if it has one)
  88. *
  89. * @param mixed $uri A URI
  90. * @return string The same URI with the fragment removed
  91. */
  92. public static function removeFragmentFromUri($uri)
  93. {
  94. $pos = strpos($uri, '#');
  95. if ($pos === false) {
  96. return $uri;
  97. } else {
  98. return substr($uri, 0, $pos);
  99. }
  100. }
  101. /** Return pretty-print view of a resource URI
  102. *
  103. * This method is mainly intended for internal use and is used by
  104. * EasyRdf_Graph and EasyRdf_Sparql_Result to format a resource
  105. * for display.
  106. *
  107. * @param mixed $resource An EasyRdf_Resource object or an associative array
  108. * @param string $format Either 'html' or 'text'
  109. * @param string $color The colour of the text
  110. * @return string
  111. */
  112. public static function dumpResourceValue($resource, $format = 'html', $color = 'blue')
  113. {
  114. if (!preg_match('/^#?[-\w]+$/', $color)) {
  115. throw new InvalidArgumentException(
  116. "\$color must be a legal color code or name"
  117. );
  118. }
  119. if (is_object($resource)) {
  120. $resource = strval($resource);
  121. } elseif (is_array($resource)) {
  122. $resource = $resource['value'];
  123. }
  124. $short = EasyRdf_Namespace::shorten($resource);
  125. if ($format == 'html') {
  126. $escaped = htmlentities($resource, ENT_QUOTES);
  127. if (substr($resource, 0, 2) == '_:') {
  128. $href = '#' . $escaped;
  129. } else {
  130. $href = $escaped;
  131. }
  132. if ($short) {
  133. return "<a href='$href' style='text-decoration:none;color:$color'>$short</a>";
  134. } else {
  135. return "<a href='$href' style='text-decoration:none;color:$color'>$escaped</a>";
  136. }
  137. } else {
  138. if ($short) {
  139. return $short;
  140. } else {
  141. return $resource;
  142. }
  143. }
  144. }
  145. /** Return pretty-print view of a literal
  146. *
  147. * This method is mainly intended for internal use and is used by
  148. * EasyRdf_Graph and EasyRdf_Sparql_Result to format a literal
  149. * for display.
  150. *
  151. * @param mixed $literal An EasyRdf_Literal object or an associative array
  152. * @param string $format Either 'html' or 'text'
  153. * @param string $color The colour of the text
  154. * @return string
  155. */
  156. public static function dumpLiteralValue($literal, $format = 'html', $color = 'black')
  157. {
  158. if (!preg_match('/^#?[-\w]+$/', $color)) {
  159. throw new InvalidArgumentException(
  160. "\$color must be a legal color code or name"
  161. );
  162. }
  163. if (is_object($literal)) {
  164. $literal = $literal->toRdfPhp();
  165. } elseif (!is_array($literal)) {
  166. $literal = array('value' => $literal);
  167. }
  168. $text = '"'.$literal['value'].'"';
  169. if (isset($literal['lang'])) {
  170. $text .= '@' . $literal['lang'];
  171. }
  172. if (isset($literal['datatype'])) {
  173. $short = EasyRdf_Namespace::shorten($literal['datatype']);
  174. if ($short) {
  175. $text .= "^^$short";
  176. } else {
  177. $text .= "^^<".$literal['datatype'].">";
  178. }
  179. }
  180. if ($format == 'html') {
  181. return "<span style='color:$color'>".
  182. htmlentities($text, ENT_COMPAT, "UTF-8").
  183. "</span>";
  184. } else {
  185. return $text;
  186. }
  187. }
  188. /** Clean up and split a mime-type up into its parts
  189. *
  190. * @param string $mimeType A MIME Type, optionally with parameters
  191. * @return array $type, $parameters
  192. */
  193. public static function parseMimeType($mimeType)
  194. {
  195. $parts = explode(';', strtolower($mimeType));
  196. $type = trim(array_shift($parts));
  197. $params = array();
  198. foreach ($parts as $part) {
  199. if (preg_match('/^\s*(\w+)\s*=\s*(.+?)\s*$/', $part, $matches)) {
  200. $params[$matches[1]] = $matches[2];
  201. }
  202. }
  203. return array($type, $params);
  204. }
  205. /** Execute a command as a pipe
  206. *
  207. * The proc_open() function is used to open a pipe to a
  208. * a command line process, writing $input to STDIN, returning STDOUT
  209. * and throwing an exception if anything is written to STDERR or the
  210. * process returns non-zero.
  211. *
  212. * @param string $command The command to execute
  213. * @param array $args Optional list of arguments to pass to the command
  214. * @param string $input Optional buffer to send to the command
  215. * @param string $dir Path to directory to run command in (defaults to /tmp)
  216. * @return string The result of the command, printed to STDOUT
  217. */
  218. public static function execCommandPipe($command, $args = null, $input = null, $dir = null)
  219. {
  220. $descriptorspec = array(
  221. 0 => array('pipe', 'r'),
  222. 1 => array('pipe', 'w'),
  223. 2 => array('pipe', 'w')
  224. );
  225. // Use the system tmp directory by default
  226. if (!$dir) {
  227. $dir = sys_get_temp_dir();
  228. }
  229. if (is_array($args)) {
  230. $fullCommand = implode(
  231. ' ',
  232. array_map('escapeshellcmd', array_merge(array($command), $args))
  233. );
  234. } else {
  235. $fullCommand = escapeshellcmd($command);
  236. if ($args) {
  237. $fullCommand .= ' '.escapeshellcmd($args);
  238. }
  239. }
  240. $process = proc_open($fullCommand, $descriptorspec, $pipes, $dir);
  241. if (is_resource($process)) {
  242. // $pipes now looks like this:
  243. // 0 => writeable handle connected to child stdin
  244. // 1 => readable handle connected to child stdout
  245. // 2 => readable handle connected to child stderr
  246. if ($input) {
  247. fwrite($pipes[0], $input);
  248. }
  249. fclose($pipes[0]);
  250. $output = stream_get_contents($pipes[1]);
  251. fclose($pipes[1]);
  252. $error = stream_get_contents($pipes[2]);
  253. fclose($pipes[2]);
  254. // It is important that you close any pipes before calling
  255. // proc_close in order to avoid a deadlock
  256. $returnValue = proc_close($process);
  257. if ($returnValue) {
  258. throw new EasyRdf_Exception(
  259. "Error while executing command $command: ".$error
  260. );
  261. }
  262. } else {
  263. throw new EasyRdf_Exception(
  264. "Failed to execute command $command"
  265. );
  266. }
  267. return $output;
  268. }
  269. }