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

/s3db3.5.10/frames/sparqlForm.php

https://code.google.com/p/s3db/
PHP | 271 lines | 156 code | 34 blank | 81 comment | 10 complexity | fb972527343f3c1039b39ff291003f00 MD5 | raw file
  1. <?php
  2. /**
  3. * @author Helena F Deus <helenadeus@gmail.com>
  4. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
  5. * @package S3DB http://www.s3db.org
  6. */
  7. ##Display a form for submitting a sparql query and a target deployment
  8. ini_set('display_errors',0);
  9. if($_REQUEST['su3d'])
  10. ini_set('display_errors',1);
  11. if(file_exists('../config.inc.php'))
  12. {
  13. include('../config.inc.php');
  14. }
  15. else
  16. {
  17. Header('Location: ../index.php');
  18. exit;
  19. }
  20. $section_num = '5';
  21. $website_title = $GLOBALS['s3db_info']['server']['site_title'].' - S3DB SPARQL endpoint';
  22. $content_width='80%';
  23. #include(S3DB_SERVER_ROOT.'/dbstruct.php');
  24. include_once(S3DB_SERVER_ROOT.'/core.header.php');
  25. include_once(S3DB_SERVER_ROOT.'/rdfheader.inc.php');
  26. include_once(S3DB_SERVER_ROOT.'/s3dbcore/sparql_read.php');
  27. ##Add some style into the page
  28. include(S3DB_SERVER_ROOT.'/s3style.php');
  29. if(is_array($argv))
  30. foreach ($argv as $argin) {
  31. if(ereg('(format|url|noHTML)=(.*)', $argin, $zz))
  32. $in[$zz[1]] = $zz[2];
  33. }
  34. else {
  35. $in = $_REQUEST;
  36. }
  37. $start = strtotime(date('His'));
  38. #
  39. ##This is the default query. It will immediatelly execute once the user clicks on the "SPARQL" tab
  40. #
  41. $in['Did'] = ($in['Did']!='')?$in['Did']:S3DB_URI_BASE;
  42. $in['url'] = $in['Did'];
  43. $prefixes = ($in['prefixes']!='')?$in['prefixes']:'PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  44. PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
  45. PREFIX s3db: <http://www.s3db.org/core.owl#>
  46. PREFIX : <'.$in['Did'].((substr($in['Did'], strlen($in['Did'])-1, 1)=='/')?'':'/').'>';
  47. $target = ($in['Did']!='')?$in['Did']:S3DB_URI_BASE;
  48. $in['from'] = explode(',',$target);
  49. foreach ($in['from'] as $fromURI) {
  50. $targets .= "FROM <".trim($fromURI)."> ";
  51. }
  52. $in['select'] = ($in['sparql']!='')?$in['sparql']:'
  53. SELECT * WHERE {
  54. ?project_id a s3db:s3dbProject .
  55. OPTIONAL { ?project_id rdfs:label ?ProjectName . }
  56. OPTIONAL { ?project_id rdfs:comment ?ProjectNotes . }
  57. }';
  58. $in['query'] = $prefixes.$in['select'];
  59. $in['format']='php';
  60. $I = compact('in', 'user_id','db');
  61. $sparqlOutput = sparql($I);
  62. $sparqlData = unserialize($sparqlOutput);
  63. if(is_array($sparqlData)){
  64. $cols = array_keys($sparqlData[0]);
  65. $data2display = s3db_display($sparqlData, $cols);
  66. #$z = array('data'=>$sparqlData,'cols'=>$cols, 'format'=>$in['format']);
  67. #$data2display = outputFormat($z);
  68. }
  69. echo '
  70. <html>
  71. <head>
  72. </head>
  73. <body>
  74. <form method="GET" action="sparqlForm.php" id="sparqlform">
  75. <table class="top">
  76. <tr class="oddbold">
  77. <td>SPARQL <a href="http://www.w3.org/TR/rdf-sparql-query/" target="_blank">(help!!)</a></td>
  78. </tr>
  79. <tr class="box">
  80. <td><textarea cols="100" id="sparql" rows="10" name = "sparql">'.stripslashes($in['select']).'</textarea><br /></td>
  81. </tr>
  82. <tr>
  83. <td><br /></td>
  84. </tr>
  85. <tr class="oddbold">
  86. <td><a href="" title="You may specify where you want your query to be targeted or, by default, local deployment will be used. Use commas to separate all your target deployments (for example http://xxx/s3db, http://yyy/s3db)">Target Deployment</a></td>
  87. </tr>
  88. <tr class="box">
  89. <td><input type="hidden" name="key" value="'.$in['key'].'"/>
  90. <input type="hidden" name="format" value="'.$in['format'].'"/>
  91. <input type = "text" id="Did" size = "100%" value="'.$in['Did'].'" name="Did">
  92. </td>
  93. </tr>
  94. <tr>
  95. <td><br /></td>
  96. </tr>
  97. <tr class="oddbold">
  98. <td><a href="#" title="Prefixes may be used in the query as short surrogates for long URI. You may add your own prefixes here">Prefixes</a></td>
  99. </tr>
  100. <tr class="box">
  101. <td>
  102. <textarea id="prefixes" cols = "100" rows="3" name="prefixes" style="background-color: #F4F4F4">'.$prefixes.'
  103. </textarea>
  104. </td>
  105. </tr>
  106. <tr>
  107. <td><br /></td>
  108. </tr>
  109. <tr>
  110. <td><input type="submit" value="SPARQL this!" id="submitsparql"></td>
  111. </tr>
  112. <tr>
  113. <td><br /></td>
  114. </tr>
  115. <tr>
  116. <td>Query took '.(strtotime(date('His'))-$start).' sec</td>
  117. </tr>
  118. <tr>
  119. <td>'.$data2display.'</td>
  120. </tr>
  121. </table>
  122. ';
  123. if($_REQUEST['callback'])##serialize and put in a javscript var
  124. {
  125. ##
  126. $varName =ereg_replace('^\?','',$_REQUEST['callback']);
  127. ##Create a JSON variable that can be read and interpreted by the javascript in the other frame
  128. if(is_array($sparqlData)){
  129. $newLeaf = array();
  130. foreach ($sparqlData as $key=>$data) {
  131. $coreVar = isS3DBCore($data[$_REQUEST['callback']]);
  132. $forJS = $coreVar['letter'].$coreVar['value'];
  133. switch ($coreVar['letter']) {
  134. case 'D':
  135. $nextCallback = '?project_id';
  136. $nextVar = ereg_replace('^\?','',$nextCallback);
  137. $newLeaf[$key][1] = '[ "'.$forJS.'", 0 ]';
  138. $newLeaf[$key][2] = '[ "Projects", "../frames/sparqlForm.php?query=select distinct ?project_id where { '.$nextCallback.' a s3db:s3dbProject . }&callback=?project_id&leadInd='.$key.'"];';
  139. break;
  140. case 'P':
  141. $nextCallback = '?rule_id';
  142. $nextVar = ereg_replace('^\?','',$nextCallback);
  143. $newLeaf[$key][1] = '[ "'.$forJS.'", 0 ]';
  144. $newLeaf[$key][2] = '[ "Collections", "../frames/sparqlForm.php?query=select distinct ?collection_id where { ?collection_id a s3db:s3dbCollection . ?collection_id rdfs:subClassOf :'.$forJS.' . }&callback=?collection_id&leadInd='.$key.'"];';
  145. $newLeaf[$key][3] = '[ "Rules", "../frames/sparqlForm.php?query=select distinct ?rule_id where { ?rule_id a s3db:s3dbRule . ?rule_id rdfs:subClassOf :'.$forJS.' . }&callback=?rule_id&leadInd='.$key.'"];';
  146. break;
  147. case 'C':
  148. $nextCallback = '?item_id';
  149. $nextVar = ereg_replace('^\?','',$nextCallback);
  150. $newLeaf[$key][1] = '[ "'.$forJS.'", "../frames/sparqlForm.php?query='.urlencode('select * where { '.$nextCallback.' a s3db:s3db'.$GLOBALS['COREidsInv'][$nextVar].' . }').'&callback='.$nextCallback.'&leadInd='.$key.'" ]';
  151. $newLeaf[$key][2] = '[ "Items", "../frames/sparqlForm.php?query=select distinct ?item_id where { ?item_id a s3db:s3dbItem . ?item_id rdf:type :'.$forJS.' . }&callback=?item_id&leadInd='.$key.'"];';
  152. $newLeaf[$key][3] = '[ "Rules", "../frames/sparqlForm.php?query=select distinct ?rule_id where { ?rule_id a s3db:s3dbRule . ?rule_id rdfs:subject :'.$forJS.' . }&callback=?statement_id&leadInd='.$key.'""];';
  153. break;
  154. default :
  155. }
  156. if($key!=max(array_keys($sparqlData))) $newLeaf[$key][1] .= ';';
  157. }
  158. $_SESSION[$_REQUEST['callback']] = $newLeaf;
  159. #$newVar = 'zzz.js';
  160. #file_put_contents('../tigre_tree/'.$newVar,serialize($newLeaf));
  161. #chmod('../tigre_tree/'.$newVar,0777);
  162. #echo $leaf;exit;
  163. echo '<script type="text/javascript">parent.sparqltrial.location = (\'../tigre_tree/sparqlTrial.php?newLeaf='.base64_encode(serialize($newLeaf)).'&callback='.$_REQUEST['callback'].'\')</script>';
  164. }
  165. /*
  166. if(is_array($sparqlData)){
  167. $leaf = 'var newLeaf = [];';
  168. foreach ($sparqlData as $key=>$data) {
  169. $coreVar = isS3DBCore($data[$_REQUEST['callback']]);
  170. $forJS = $coreVar['letter'].$coreVar['value'];
  171. $leaf .= 'newLeaf['.$key.'] = [ "'.$forJS.'", "../frames/sparqlForm.php?query='.urlencode('select * where { ?project_id a s3db:s3dbProject . }').'&callback='.$nextCallback.'&leadInd='.$key.'" ]';
  172. if($key!=max(array_keys($sparqlData))) $leaf .= ';';
  173. }
  174. $newVar = 'zzz.js';
  175. file_put_contents('../tigre_tree/'.$newVar,$leaf);
  176. chmod('../tigre_tree/'.$newVar,0777);
  177. #echo $leaf;exit;
  178. echo '<script type="text/javascript">parent.sparqltrial.location = (\'../tigre_tree/sparqlTrial.php?newLeaf='.$newVar.'&callback='.$_REQUEST['callback'].'\')</script>';
  179. }
  180. */
  181. ##Create a javascript var that will be sent to the other frame
  182. //echo '<script type="text/javascript" src="../tigre_tree/coreTree.js"></script>';
  183. //
  184. //
  185. //
  186. //foreach ($sparqlData as $key=>$data) {
  187. // $coreVar = isS3DBCore($data[$_REQUEST['callback']]);
  188. // $forJS = $coreVar['letter'].$coreVar['value'];
  189. //
  190. // #Depending on the callback, we'll be changing diferent portions of the tree
  191. // #Now reload the other frame with the new tree
  192. // switch ($varName) {##deployment_id corresponds to index
  193. // case 'deployment_id':
  194. // $Branch = 0;
  195. // $FirstLeaf = 3;
  196. // $Leaf = '["'.$forJS.'", "../frames/sparqlForm.php?query=select * where { ?project_id a s3db:s3dbProject . ?project_id rdfs:subClassOf :'.$forJS.' . }&callback=?project_id"]';
  197. // break;
  198. //
  199. // default :
  200. // }
  201. //echo '<script type="text/javascript">TREE_ITEMS['.$Branch.']['.$FirstLeaf.'] = '.$Leaf.'</script>';
  202. //
  203. //$FirstLeaf++;
  204. //}
  205. //
  206. //echo '<script type="text/javascript">setCookie("TREE_ITEMS",TREE_ITEMS,7);</script>';
  207. //
  208. //$treePhp ='<script type="text/javascript">alert(TREE_ITEMS);</script>';
  209. //file_put_contents('../tigre_tree/sparqlNewTree.js',$treePhp);
  210. //chmod('../tigre_tree/sparqlNewTree.js',0777);
  211. //
  212. //##And now reload the tree with the new sparql tree
  213. //#Iclude tree_tpl
  214. //echo '<script language="JavaScript" src="../tigre_tree/sparql_tree.js"></script>';
  215. //echo '<script language="JavaScript" src="../tigre_tree/tree.js"></script>';
  216. //#echo '<script type="text/javascript">parent.sparqltrial.TREE_ITEMS = parent.sparqlform.TREE_ITEMS</script>';
  217. //echo '<script type="text/javascript">parent.sparqltrial.location = (\'../tigre_tree/sparqlTrial.php?newTree=sparqlNewTree.js\')</script>';
  218. #echo '<script type="text/javascript">var php=js2php (TREE_ITEMS); </script>';
  219. #echo '<script type="text/javascript">setCookie("treePHP",php,7);</script>';
  220. #echo '<pre>';print_r($_COOKIE);
  221. #echo '<pre>';print_r($treePhp);
  222. }
  223. #echo s3db_display($sparqlData, $cols);
  224. exit;
  225. ?>