<?php
define('idDiGIR_flt','$Id: DiGIR_flt.php,v 1.4 2004/05/18 19:05:15 rdg Exp $');
if (defined('idDIGIR_VERSION_REQUEST'))
    return;
/*
* $Id: DiGIR_flt.php,v 1.4 2004/05/18 19:05:15 rdg Exp $
*/


include_once("DiGIR_globals.php");
include_once("DiGIR_utils.php");
include_once("DiGIR_errors.php");
include_once("DiGIR_fltLOP.php");
include_once("DiGIR_fltCOP.php");
include_once("DiGIR_fltBuilder.php");

/**
* The filter class is a wrapper for filter operations.
*/
class filter
{
    //m_filter is the actual filter structure
    var $m_filter;
    //m_local indicates if the filter is local (from the provider
    //configuration) or not (from regular requests)
    var $m_local;

    //construct an empty filter structure
    function filter($local=false)
    {
        $this->m_filter = NULL;
        $this->m_local = $local;
    }

    //parse a YAZ RPN string into a filter structure
    function parseRPN($rpn)
    {
		$szq = trim($rpn);
		$aElements = split("@",$szq);
	    DebugMsg("aElements = ".dumpArray($aElements));
	    if (count($aElements) > 1)
        {
	        $s = trim($aElements[1]);
	        $atmp = split(" ",$s);
	        $opt = opType($atmp[0]);
        }
        switch ($opt)
        {
	        case op_AND:
	        case op_OR:
                DebugMsg("1. add filter LOP");
                $this->m_filter = new filterLOP($opt,NULL);
                $this->m_filter->parseRPN(array_slice($aElements,2));
                break;
	        case op_ATTR:
                DebugMsg("1. add filter COP");
                $this->m_filter = new filterCOP(cp_undefined);
                $this->m_filter->parseRPN(array_slice($aElements,1));
                break;
        }
	}

	//parses an XML filter structure ala DiGIR
	function parseXML($xml,&$namespaces)
	{
		$xparser = new XPath();
		$xparser->setVerbose(0);
		$xparser->setXmlOption(XML_OPTION_CASE_FOLDING,false);
		$xparser->setXmlOption(XML_OPTION_SKIP_WHITE,true);

		if (isURL($xml))
		{
			$xparser->importFromFile($xml);
		}
		else
		{
			$xparser->importFromString($xml);
		}

		$fb = new filterBuilderXP($xparser,$namespaces,$this->m_local);
		$fb->parse($xparser);

		$this->m_filter = $fb->filter;
	}
	
	function parse($filterString,&$namespaces)
	{
		if (strncmp($filterString,"@",1) == 0)
		{
			//PQN filter string
			$this->parseRPN($filterString);
		}
		else
		{
			//DiGIR XML filter
			$this->parseXML($filterString,$namespaces);
		}	
	}

	//dump the contents for debuggin purposes.
	function Dump()
	{
        if ($this->m_filter != NULL)
	        $this->m_filter->Dump(0);
	}

    //Generates an SQL statement based on the filter structure
    function toSQL($aConcepts)
    {
        $sql = "";
        if ($this->m_filter != NULL)
        {
            $sql .= "(".$this->m_filter->toSQL($aConcepts).")";
        }
        else
        {
            if (!$this->m_local)
            {
                AppendError(DC_BADQUERY,"no filter structure #1",DIAG_ERROR);
            }
        }
        return $sql;
    }

    function toRPN($aConcepts)
    {
        $rpn = "";
        if ($this->m_filter != NULL)
        {
            $rpn = $this->m_filter->toRPN();
        }
        else
        {
            if (!$this->m_local)
            {
                AppendError(DC_BADQUERY,"no filter structure #2",DIAG_ERROR);
            }
        }
        return $rpn;
    }
    
    function validQuery()
    {
    	if ($this->m_filter == NULL)
    		return false;
    	return true;
    }
}

?>