PageRenderTime 28ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/s3db3.5.10/sparql2s3db.php

https://github.com/drobbins/s3db
PHP | 283 lines | 199 code | 50 blank | 34 comment | 25 complexity | f107917ead55d6c502e1c9c5235c65eb MD5 | raw file
  1. <?php
  2. #sparql serializer is an API that serializes a sparql query into an S3QL query
  3. ##HEADER: Read the file and include the functions for parsing the RDF
  4. ini_set('display_errors',0);
  5. if($_REQUEST['su3d'])
  6. ini_set('display_errors',1);
  7. if(file_exists('config.inc.php'))
  8. {
  9. include('config.inc.php');
  10. }
  11. else
  12. {
  13. Header('Location: index.php');
  14. exit;
  15. }
  16. include('dbstruct.php');
  17. @ini_set('allow_url_fopen', true);
  18. @set_time_limit(0);
  19. $x = ini_set('memory_limit', '500M');
  20. #echo ini_get('memory_limit');exit;
  21. @ini_set('upload_max_filesize', '128M');
  22. @ini_set('post_max_size', '256M');
  23. @ini_set('max_input_time', '-1');
  24. @ini_set('max_execution_time', '3000');
  25. @ini_set('expect.timeout', '-1');
  26. @ini_set('default_socket_timeout', '-1');
  27. ini_set('display_errors',0);
  28. if($_REQUEST['su3d'])
  29. ini_set('display_errors',1);
  30. define("RDFAPI_INCLUDE_DIR", S3DB_SERVER_ROOT."/pearlib/rdfapi-php/api/");
  31. include(RDFAPI_INCLUDE_DIR . "RdfAPI.php");
  32. include(RDFAPI_INCLUDE_DIR . "syntax/SyntaxN3.php");
  33. include(RDFAPI_INCLUDE_DIR . "syntax/SyntaxRDF.php");
  34. if(is_array($argv))
  35. foreach ($argv as $argin) {
  36. if(ereg('(format|url|noHTML)=(.*)', $argin, $zz))
  37. $in[$zz[1]] = $zz[2];
  38. }
  39. else {
  40. $in = $_REQUEST;
  41. }
  42. $rdf = ($in['url']=='')?"http://ibl.mdanderson.org/TCGA/extras/NUavLcFy5fn6WsN/KfN1YnDBCN4dD6j.project126/TCGADATA.rdf":urldecode($in['url']);
  43. $mysparql = '
  44. PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  45. PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
  46. PREFIX : <http://ibl.mdanderson.org/TCGA/>
  47. PREFIX s3db: <http://www.s3db.org/core#>
  48. SELECT *
  49. WHERE { ?x :R149 ?z FILTER regex(?z, "CGH") .
  50. }';
  51. //PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  52. //PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
  53. //PREFIX : <http://http://ibl.mdanderson.org/TCGA/>
  54. //PREFIX s3db: <http://www.s3db.org/core#>
  55. //SELECT ?Item, ?Ilabel
  56. //WHERE {?Item rdf:type :C186 .
  57. // ?Item rdfs:label ?Ilabel .}
  58. //
  59. //";
  60. //$mysparql = "
  61. //PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  62. //PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
  63. //PREFIX : <http://http://ibl.mdanderson.org/TCGA/>
  64. //PREFIX s3db: <http://www.s3db.org/core#>
  65. //SELECT ?R,?Rlabel
  66. //WHERE {?R rdfs:subClassOf :P126 .
  67. // ?R rdf:type s3db:s3dbRule .
  68. // ?R rdfs:label ?Rlabel .
  69. // ?R rdf:subject :C186 .}
  70. //
  71. //";
  72. $sparql = ($in['sparql']=='')?$mysparql:urldecode($in['sparql']);
  73. ##Interpret the query
  74. $parsed = s3db_parse($sparql);
  75. exit;
  76. $out = ($in['format']=='')?'HTML':strtoupper($in['format']);
  77. ##serialize the data so that is does not take very long to re-load it
  78. $filename = md5($rdf);
  79. $file_place = $GLOBALS['uploads'].'/';
  80. if(!is_file($file_place.$filename)) {
  81. #read the data into a model
  82. $model = rdf2php($rdf);
  83. if($model=='')
  84. {
  85. echo "dataset could not be parsed";
  86. }
  87. else
  88. {$s_model = serialize($model);
  89. file_put_contents($file_place.$filename, $s_model);
  90. }
  91. }
  92. else {
  93. $model = file_get_contents($file_place.$filename);
  94. $model = unserialize($model);
  95. }
  96. #echo '<pre>';print_r($model);
  97. if(!$in['no_color']){
  98. echo '
  99. <html>
  100. <head>
  101. </head><body>
  102. <form method="GET" action="'.$_SERVER['PHP-SELF'].'" id="sparqlform">
  103. <h5>URL</h5>
  104. <input type = "text" id="url" size = "100%" value="'.$in['url'].'" name="url">
  105. <h5>SPARQL <a href="http://www.w3.org/TR/rdf-sparql-query/" target="_blank">(help!!)</a></h5>
  106. <br />
  107. <textarea cols="100" id="sparql" rows="10" name = "sparql">'.stripslashes($sparql).'</textarea><br />
  108. <input type="submit" value="SPARQL this!" id="submitsparql"></body>
  109. </form>
  110. ';
  111. }
  112. $sparqlQueryFile = md5(urlencode($sparql)).date('Gis');
  113. if(!is_file($file_place.$sparqlQueryFile))
  114. {
  115. if(ereg('XML|HTML', $out))
  116. {
  117. #$incomingSparql = $sparql->_parseSparqlQuery($sparql);
  118. $find=$model->sparqlQuery($sparql, $out);
  119. echo $find;
  120. }
  121. else {
  122. $find=$model->sparqlQuery($sparql);
  123. #echo '<pre>';print_r($find);
  124. }
  125. $s_find = serialize($find);
  126. file_put_contents($file_place.$sparqlQueryFile, $s_find);
  127. }
  128. else {
  129. $s_find = file_get_contents($file_place.$sparqlQueryFile);
  130. $find = unserialize($s_find);
  131. if(ereg('XML|HTML', $out))
  132. {
  133. file_put_contents($GLOBALS['uploads'].'/sparqlResult.'.strtolower($out), $find);
  134. echo $find;
  135. }
  136. else {
  137. echo '<pre>';print_r($find);
  138. }
  139. }
  140. function rdf2php($doc)
  141. {
  142. // Prepare RDF
  143. $rdfInput = $data;
  144. // Show the submitted RDF
  145. // Create a new MemModel
  146. $model = ModelFactory::getDefaultModel();
  147. $model->load($doc);
  148. return ($model);
  149. }
  150. function s3db_parse($queryString)
  151. {
  152. if ($queryString) {
  153. $uncommentedQuery = s3db_uncomment($queryString);
  154. $tokenized = s3db_tokenize($uncommentedQuery);
  155. echo '<pre>';print_r($tokenized);exit;
  156. $parsed = parseQuery($tokenized);
  157. } else {
  158. return ("Querystring is empty.");
  159. }
  160. }
  161. function s3db_uncomment($queryString)
  162. {
  163. // php appears to escape quotes, so unescape them
  164. $queryString = str_replace('\"',"'",$queryString);
  165. $queryString = str_replace("\'",'"',$queryString);
  166. $regex ="/((\"[^\"]*\")|(\'[^\']*\')|(\<[^\>]*\>))|(#.*)/";
  167. return preg_replace($regex,'\1',$queryString);
  168. }
  169. function s3db_tokenize($queryString)
  170. {
  171. $queryString = trim($queryString);
  172. $specialChars = array(" ", "\t", "\r", "\n", ",", "(", ")","{","}",'"',"'",";","[","]");
  173. $len = strlen($queryString);
  174. $this['tokens'][0]='';
  175. $n = 0;
  176. for ($i=0; $i<$len; ++$i) {
  177. if (!in_array($queryString{$i}, $specialChars)) {
  178. $this['tokens'][$n] .= $queryString{$i};
  179. } else {
  180. if ($this['tokens'][$n] != '') {
  181. ++$n;
  182. }
  183. $this['tokens'][$n] = $queryString{$i};
  184. $this['tokens'][++$n] = '';
  185. }
  186. }
  187. return ($this);
  188. }
  189. function parseQuery($tokenizedQuery)
  190. {
  191. do {
  192. switch (strtolower(current($tokenizedQuery['tokens']))) {
  193. case "base":
  194. $this->parseBase();
  195. break;
  196. case "prefix":
  197. $this->parsePrefix();
  198. break;
  199. case "select":
  200. $this->parseSelect();
  201. break;
  202. case "describe":
  203. $this->parseDescribe();
  204. break;
  205. case "ask":
  206. $this->parseAsk('ask');
  207. break;
  208. case "count":
  209. $this->parseAsk('count');
  210. break;
  211. case "from":
  212. $this->parseFrom();
  213. break;
  214. case "construct":
  215. $this->parseConstruct();
  216. break;
  217. case "where":
  218. $this->parseWhere();
  219. $this->parseModifier();
  220. break;
  221. case "{":
  222. prev($this->tokens);
  223. $this->parseWhere();
  224. $this->parseModifier();
  225. break;
  226. }
  227. } while (next($this->tokens));
  228. }
  229. ?>