<?php
// Xinfeng Chen, 2002.12.13
// require_once('base_array_file.php');
// require_once('class_forum_base.php');

class CSearchEngine extends CBaseArrayFile {
 var $_keyword, $_finished;
 var $id, $refid;

 function CSearchEngine(){
  global $HTTP_SESSION_VARS;
  if(!isset($HTTP_SESSION_VARS['sv_searchResult'])){
   $HTTP_SESSION_VARS['sv_searchResult']=array();
   $HTTP_SESSION_VARS['sv_keyword']='';
   $HTTP_SESSION_VARS['sv_finished']=false;
  }
  $this->_rawlist=&$HTTP_SESSION_VARS['sv_searchResult'];
  $this->_size=count($this->_rawlist);
  $this->_keyword=&$HTTP_SESSION_VARS['sv_keyword'];
  $this->_finished=&$HTTP_SESSION_VARS['sv_finished'];
 }

 function _explodeRecord($a_line){
  if($a_line){
   list($this->id,$this->refid)=explode(SP, $a_line);
  }else{
   unset($this->id,$this->refid);
  }
 }

 function _composeRecord($a_line){
  $a_line=$this->id.SP.$this->refid;
 }

 function search($a_list,$a_keyword,$a_lines){

  if(($a_keyword!=$this->_keyword)||($a_lines<=0)){
   $this->reset();
   $this->_keyword=$a_keyword;
   $this->_finished=false;
  }

  //continue last search
  $tmpLine=$this->itemCount();
  if($tmpLine>0){
   $this->moveTo($tmpLine-1);
   if(!$a_list->setAbsolutePosition($this->refid)){$this->_finished=true;}
   if(!$a_list->id != $this->id){$this->_finished=true;}
   if(!$a_list->moveNext()){$this->_finished=true;}
  }else{
   if(!$a_list->moveFirst()){$this->_finished=true;}
  }

  while(!$this->_finished && ($tmpLine < $a_lines)){
   $fname=CForumBase::getFilePath('TOPIC',$a_list->bid,$a_list->id);
   $contents=@file($fname);
   if(stristr($contents[0],$a_keyword)){
    $tmpLine++;
    $this->appendNew();
    $this->id=$a_list->id; $this->refid=$a_list->GetAbsolutePosition();
    $this->update();
    if($tmpLine >= 1000){$this->_finished=true;}
   }

   if(!$a_list->moveNext()){$this->_finished=true;}
  }
  
  return ($a_lines <= $this->itemCount());
 }

}
?>