All pastes #523704 Raw Edit

arc console

public text v1 · immutable
#523704 ·published 2007-05-30 21:27 UTC
rendered paste body
<?php
//You need an ARC sparql store to connect to,  you need to edit the connection details to it, and you can create a file called prefixes.txt containing the PREFIX statements that you would use ina sparql query.

$arc_require_path = '../lib/arc/';
require_once($arc_require_path."ARC_api.php");

/*Instantiate API */

$api_args = array(
  "inc_path" => $arc_require_path,
  "config_path"=>$arc_require_path."arc_config.php"
);

$api = new ARC_api($api_args);

$api->db_connect();

function readline($prompt="") {
if(strlen($in) > 0 )
        echo $prompt;
        $o = "";
        $c = "";
        while ($c!="\r"&&$c!="\n") {
        $o.= $c;
        $c = fread(STDIN, 1);
            }
        fgetc(STDIN);
    return $o;
}

while(true){
$in = readline('>> ');
        if(strlen($in) > 0 )
        {
                preg_match_all('/\?[a-zA-Z]+/',$in,$sparql_vars);

                $prefixes = file_get_contents($arc_require_path."prefixes.txt")." ";
                $select = " SELECT ".implode(' ', $sparql_vars[0])." ";
                $where = " WHERE { $in }";
                $jsonc = array();
                foreach($sparql_vars[0] as $sp) $jsonc[]=substr($sp,1);                                       

                $args=array(
                "result_type"=>"rows", // (rows|json|xml|single|rows_n_count|row_count|sql)
                "query"=>$prefixes.$select.$where,                                                            
                "jsonc"=>$jsonc,                                                                              
                        );                                                                                    
                                                                                                              
                $qr=$api->query($args);                                                                       
                                                                                                              
                if($rows=$qr["result"])
                {
                        echo "------------Results-------------\r\n\r\n";
                        foreach($rows as $row)                                                                
                        {                                                                                     
                                foreach($jsonc as $k) echo "{$k}\t|\t{$row[$k]}\r\n";
                                echo "--------------------------\r\n";                                        
                        }                                                                                     
                }
                else
                { echo "Error ".implode("\r\n",$qr); }
        }
}
?>