server/src/app/Libraries/Sparql/SparqlQueryAnalyser.php
changeset 387 7fba86fa8604
parent 386 c731ab9b934d
equal deleted inserted replaced
386:c731ab9b934d 387:7fba86fa8604
    45             }
    45             }
    46         }
    46         }
    47         return $this->queryType;
    47         return $this->queryType;
    48     }
    48     }
    49 
    49 
    50     private function extractPrefix() {
    50     private function extractPrefixLimit() {
    51         $prefixes = [];
    51         $this->prefixes = [];
    52         $rawPrefixes = [];
    52         $this->rawPrefixes = [];
    53         $res = preg_replace_callback("%".self::SPARQL_PREFIX_BASE_REGEXP."%iu", function($m) use (&$prefixes, &$rawPrefixes) {
    53         $res = preg_replace_callback("%".self::SPARQL_PREFIX_BASE_REGEXP."%iu", function($m) {
    54             $rawPrefixes[] = trim($m[0]);
    54             $this->rawPrefixes[] = trim($m[0]);
    55             $prefixes[$m[3]?$m[3]:""] = $m[4];
    55             $this->prefixes[$m[3]?$m[3]:""] = $m[4];
    56             return "";
    56             return "";
    57         }, $this->query);
    57         }, $this->query);
       
    58         $res = preg_replace_callback("%".self::SPARQL_LIMIT_OFFSET_QUERY_REGEXP."%iu", function($m) {
       
    59             for($i=0;$i<(count($m)-1)/2;$i++) {
       
    60                 if(Utils::startsWith(strtolower($m[2*$i+1]), "limit")) {
       
    61                     $this->limit = intval($m[$i*2+2]);
       
    62                 } elseif (Utils::startsWith(strtolower($m[2*$i+1]), "offset")) {
       
    63                     $this->offset = intval($m[$i*2+2]);
       
    64                 }
       
    65             }
       
    66         }, $res);
    58 
    67 
    59         return [$rawPrefixes, $prefixes, trim($res)];
    68         $this->rawQuery = trim($res);
    60     }
    69     }
    61 
    70 
    62     public function getRawPrefixes() {
    71     public function getRawPrefixes() {
    63         if($this->rawPrefixes === false) {
    72         if($this->rawPrefixes === false) {
    64             list($this->rawPrefixes, $this->prefixes, $this->rawQuery) = $this->extractPrefix();
    73             $this->extractPrefixLimit();
    65         }
    74         }
    66         return $this->rawPrefixes;
    75         return $this->rawPrefixes;
    67     }
    76     }
    68 
    77 
    69     public function getPrefixes() {
    78     public function getPrefixes() {
    70         if($this->prefixes === false) {
    79         if($this->prefixes === false) {
    71             list($this->rawPrefixes, $this->prefixes, $this->rawQuery) = $this->extractPrefix();
    80             $this->extractPrefixLimit();
    72         }
    81         }
    73         return $this->prefixes;
    82         return $this->prefixes;
    74     }
    83     }
    75 
    84 
    76     public function getRawQuery() {
    85     public function getRawQuery() {
    77         if($this->rawQuery === false) {
    86         if($this->rawQuery === false) {
    78             list($this->rawPrefixes, $this->prefixes, $this->rawQuery) = $this->extractPrefix();
    87             $this->extractPrefixLimit();
    79         }
    88         }
    80         return $this->rawQuery;
    89         return $this->rawQuery;
    81     }
    90     }
    82 
    91 
    83     public function getCountVar() {
    92     public function getCountVar() {
    90     public function getCountQuery() {
    99     public function getCountQuery() {
    91         return implode(" ", $this->getRawPrefixes())." select (count(*) as ".$this->getCountVar().") { ".$this->getRawQuery()." }";
   100         return implode(" ", $this->getRawPrefixes())." select (count(*) as ".$this->getCountVar().") { ".$this->getRawQuery()." }";
    92     }
   101     }
    93 
   102 
    94     private function setLimitOffset() {
   103     private function setLimitOffset() {
    95         if(preg_match("%".self::SPARQL_LIMIT_OFFSET_QUERY_REGEXP."%iu", $this->query, $m) === 1) {
   104         $this->extractPrefixLimit();
    96             for($i=0;$i<(count($m)-1)/2;$i++) {
       
    97                 if(Utils::startsWith(strtolower($m[2*$i+1]), "limit")) {
       
    98                     $this->limit = intval($m[$i*2+2]);
       
    99                 } elseif (Utils::startsWith(strtolower($m[2*$i+1]), "offset")) {
       
   100                     $this->offset = intval($m[$i*2+2]);
       
   101                 }
       
   102             }
       
   103         }
       
   104         if($this->limit === false) {
   105         if($this->limit === false) {
   105             $this->limit = null;
   106             $this->limit = null;
   106         }
   107         }
   107         if($this->offset === false) {
   108         if($this->offset === false) {
   108             $this->offset = null;
   109             $this->offset = null;