<?php
// Enter which index you want to use here
define('INDEX','myIndex');
define('RESULTS_PER_PAGE',10); // for pagination
define('DEBUG_MODE',false); // true or false


$oss=new OSS();


// Accept only search strings with at least n digits
if(isset($_GET['q']) AND strlen($_GET['q'])>=3) $oss->build_query($_GET['q']);

class OSS{

  function __construct(){
    $this->query='';
    $this->info='';
    $this->index=INDEX;   // Index to use
    $this->debug_mode=DEBUG_MODE;
    $this->rpp=RESULTS_PER_PAGE; // Results per page
    $this->collapse_max=1; // In cluster mode, this is always 1
  }
  
  function build_query($q){
    // Assemble the query
    
    $params=array(); // This is where all the parameters are
    
    // Remove tags
    $this->q=trim(strip_tags($q));
        
    
    // Take only the first 80 characters of the string
    $this->q=substr($this->q,0,80);
    

    // Basic parameters
    $params['use']=$this->index;  // zu verwendender Index
    $params['qt']='general';  // Query template to use

    // Collapsing
    $params['collapse.mode']='cluster';
    $params['collapse.field']='host';
    $params['collapse.max']=$this->collapse_max;
    
    
    // Pagination
    if(isset($_GET['page'])) $params['start']=((int) $_GET['page']*10)-9; 
    else $params['start']=1;    
    $params['rows']=$this->rpp;
    
  
    $params['query']=trim($this->q);  // the search term itself
    $params['sort']='score'; // minus prefix for reverse order
    
    
    
    // Override if you want to query only one host
    preg_match("/host:(.*?) /",$this->q.' ',$matches);
    if(isset($matches[1])){
      $params['query']=trim(preg_replace("/{$matches[0]}/",' ',$this->q.' '));
      $params['fq']='host:'.$matches[1];
      unset($params['collapse.mode']);
      unset($params['collapse.field']);
      unset($params['collapse.max']);
    }

    
    $host='http://127.0.0.1:8080/select'; // URL of the OSS host

    // In case someone has injected a 'delete' parameter
    unset($params['delete']);
    
    $this->query=$host.'?'.http_build_query($params);
    
    if($this->debug_mode) echo "Debug mode: ".$this->query;

  }
  
  function show_query(){
    if(isset($this->q)) return preg_replace('/\"/','&quot;',$this->q);
    else return '';
  }
  
  
  function execute(){
    if($this->query=='') return false;
    
    $this->hits=simplexml_load_file($this->query);

    if($this->hits->entry[0]!='Error'){
                list($this->info['name'],
                    $this->info['numFound'],
                    $this->info['collapsedDocCount'],
                    $this->info['start'],
                    $this->info['rows'],
                    $this->info['maxScore'],
                    $this->info['time'])=$this->hits->result->attributes();
      return $this->hits;
    }else{
      // Error. The XML could not be retrieved
      if($this->debug_mode) echo '<pre>'; print_r($this->hits); echo '</pre>';
      return false;
    }
  }
  
  
  function info($key=''){
    if($key=='') return $this->info;
    else return $this->info[$key];
  }
  
  function show_result_count(){
    if($this->info['collapsedDocCount']!=0)
      return 'There were '.$this->info['numFound'].' documents found. '.($this->info['numFound']-$this->info['collapsedDocCount']). ' Results are shown from different hosts.';
    else 
      return 'There were '.$this->info['numFound'].' documents found.';
  }
  
  function uncollapse($doc,$field){
    if(isset($doc->collapseCount)){ ?>
      <a href="?q=<?=$this->show_query();?> host:<?=$field?>">
        Show more results from <?=$field?>
      </a>      
    <?php
    }
  }
  
  function pagination(){
    for($page=1; $page<=ceil(($this->info['numFound']-$this->info['collapsedDocCount'])/$this->rpp)-1; $page++){
      if(!isset($_GET['page'])) $_GET['page']=1;
      
      if($_GET['page']==$page) echo '<strong>'.$page.'</strong> ';
      else echo '<a href="?q='.$this->show_query().'&page='.$page.'">'.$page.'</a> ';
    }
  }
}

?>