<commit_before><?php
class popoon_classes_structure2xml {
    
    private $parent;
    private $queries = null;
    private $queryCacheOptions = null;
    
    function __construct($parent) {
        $this->parent = $parent;
        $this->db = $this->parent->db;
    }
    
    private function getAttrib($value) {
        return $this->parent->getAttrib($value);
    }
    
    private  function getParameter($value) {
        return $this->parent->getParameter($value);
    }
    
    
    function showPage ($configXml,$PageOptions = array(), $returnDb2XmlObject = false) 
    {
        
        
        
        $this->api = bx_helpers_simplecache::getInstance();
        // get the queries, either cached from file system or generated
        
        if (is_null($this->queries)) {
            $this->queries = $this->getQueries($configXml,$PageOptions);
        }
        
        $sql2xml = new XML_db2xml($this->db,"bx","Extended");
        
        if ($this->getParameter("contentIsXml")) {
            $sql2xml->setContentIsXml(true);
        }
        
        // i should add this for all options .... later maybe
        if (!(is_null($this->getAttrib("xml_seperator")) ))
        {
            $sql2xml->setOptions(array("user_options" => array("xml_seperator"=>$this->getAttrib("xml_seperator"))));
        }
        $sql2xml->setOptions(array("user_options" => array("print_empty_ids"=>False)));
        
        
        
        if (isset($PageOptions["All"]["user_options"])){
            $sql2xml->setOptions(array("user_options" => $PageOptions["All"]["user_options"]));
        }
        
        if (isset($PageOptions["querystring"])){
            $sql2xml->setOptions(array("user_options"=>array("result_root"=>"querystring")));
            $pageo = array();
            foreach($PageOptions["querystring"] as $key => $value)
            {
                if (!is_array($value))
                {
                    $pageo[$key] = $value;
                }
            }
            $sql2xml->add($pageo);
        }
        if ($array2xml = $this->getParameter("array2xml")) {
            foreach ($array2xml as $name => $array) {
                $sql2xml->setOptions(array("user_options"=>array("result_root"=>$name)));
                $sql2xml->add($array);
            }
        }
        
        if (is_array($this->queries))
        {
            $_maxLastChangedAll = array(0);
            
            foreach ($this->queries as $structureName => $query) {
                if  ($structureName == "_queryInfo") {
                    continue;
                }
                $query['user_options']['result_root'] = $structureName;
                if ($query['type'] == "dbquery"){
                    //caching the sql2xml part
                    $query["query"] = $this->replaceVarsInWhere($query["query"]);
                    if ( $this->parent->st2xmlCaching == "true" ) { 
                        if (! (isset($query["maxLastChanged"]) )) {
                            $this->db->loadModule('extended');
                            $query["maxLastChanged"]  = $this->db->extended->getOne($query['queryLastChanged']);
                            
                        } 
                        if ( $cachedXML = $this->api->simpleCacheCheck("","st2xml_data",$query['query'],"file", $query["maxLastChanged"])) {
                            $sql2xml->addWithInput("File",$cachedXML);
                        } 
                        else {
                            $sql2xml->setOptions(array("user_tableInfo"=>$query['tableInfo'],"user_options"=>$query['user_options']));
                           
                            $sql2xml->add($query['query']);
                            $ctx = new DomXpath($sql2xml->Format->xmldoc);
                            $resultTree = $ctx->query("$structureName",$sql2xml->Format->xmlroot );
                            
                            $this->api->simpleCacheWrite("","st2xml_data",$query['query'],"<?xml version='1.0' ?".">".$sql2xml->Format->xmldoc->saveXML($resultTree->item(0)),"file", $query["maxLastChanged"]);
                        }
                        $_maxLastChangedAll[] = $query["maxLastChanged"];
                        
                    }
                    else
                    {
                        
                        $sql2xml->setOptions(array("user_tableInfo"=>$query['tableInfo'],"user_options"=>$query['user_options']));
                        $sql2xml->add($query['query']);
                        
                    }
                }
                else if ( $query['type'] == "aggregate" ) {
                    if ($this->st2xmlCaching == "true" ) {
                        //http stuff
                        if (strpos($query['query'],"http") === 0 ) {
                            
                            //if maxLastChanged is set, we already checked it in the Validity container..
                            if (! (isset($query["maxLastChanged"]) )) {
                                //check for Last Modified automatically creates the cached file, if not already there
                                $query["maxLastChanged"] = $this->api->simpleCacheHttpLastModified($query['query'], time() - $query['expires'],$this->getParameter ("default","proxy") );
                            }
                            $_maxLastChangedAll[] = $query["maxLastChanged"];
                            $sql2xml->addWithInput("File",$this->api->simpleCacheGenerateName("simpleCacheHttp",$query['query']),array("root"=> $structureName));
                        } 
                        // if not starting with http, it will be a local file... hopefully
                        else {
                            $sql2xml->addWithInput("File",$query['query']);
                        }
                    } 
                    else {
                        // with no caching, just get and add it.. (maybe we should allow http caching without db-caching..)
                        $sql2xml->add($query['query'],array("root"=> $structureName));
                        
                    }
                } 
                else {
                    //quite useless right now... 
                    $sql2xml->add($query['query'],array("root"=> $structureName));
                }
            }
            $this->queries["_queryInfo"]["maxLastChanged"] = max ($_maxLastChangedAll);
            
            // send http cache headers so the sent page expires immediately 
            $lastChangedDate = gmdate("r", $this->queries["_queryInfo"]["maxLastChanged"]);
            if (isset($this->sitemap)) {
                $this->sitemap->setHeader("Last-Modified", $lastChangedDate);
                $this->sitemap->setHeader('Expires', $lastChangedDate);
                
                //var_dump(gmdate("r", $this->queries["_queryInfo"]["maxLastChanged"] ));
                // FIXME: if there is no bitlib2 code anymore, this if can go away
                if (method_exists($this->sitemap,"setLastModified")) {
                    $this->sitemap->setLastModified($this->queries["_queryInfo"]["maxLastChanged"] );
                }
            }
        }
        if ($returnDb2XmlObject) {
            return $sql2xml;   
        } else {
            return $sql2xml->getXmlObject();
        }
        
    }
    
    
    function Structures2Sql ($configFile,$sqlOptions=array(),$rootpath= "/structure")
    {
        
        $configClass = bx_helpers_db::getConfigClass($configFile);
        $dbMainStructure = $configClass->getValues( $rootpath);
        if (is_array($dbMainStructure['children']))
        {
            
            foreach ($dbMainStructure['children'] as $structureName) {
                $dbStructure = $configClass->getValues( "$rootpath/$structureName");
                if (PEAR::isError($dbStructure)) {
                    die($dbStructure->getUserinfo() .  "\n" . $dbStructure->getMessage());
                }
                if (isset($dbStructure["expires"])) {
                    $allqueries[$structureName]['expires'] = time() - strtotime(preg_replace("#access\s+minus\s+#", "-",$dbStructure["expires"]));<commit_msg>throw an exception on PEAR ERROR<commit_after><?php
class popoon_classes_structure2xml {
    
    private $parent;
    private $queries = null;
    private $queryCacheOptions = null;
    
    function __construct($parent) {
        $this->parent = $parent;
        $this->db = $this->parent->db;
    }
    
    private function getAttrib($value) {
        return $this->parent->getAttrib($value);
    }
    
    private  function getParameter($value) {
        return $this->parent->getParameter($value);
    }
    
    
    function showPage ($configXml,$PageOptions = array(), $returnDb2XmlObject = false) 
    {
        
        
        
        $this->api = bx_helpers_simplecache::getInstance();
        // get the queries, either cached from file system or generated
        
        if (is_null($this->queries)) {
            $this->queries = $this->getQueries($configXml,$PageOptions);
        }
         $sql2xml = new XML_db2xml($this->db,"bx","Extended");
        
        if ($this->getParameter("contentIsXml")) {
            $sql2xml->setContentIsXml(true);
        }
        
        // i should add this for all options .... later maybe
        if (!(is_null($this->getAttrib("xml_seperator")) ))
        {
            $sql2xml->setOptions(array("user_options" => array("xml_seperator"=>$this->getAttrib("xml_seperator"))));
        }
        $sql2xml->setOptions(array("user_options" => array("print_empty_ids"=>False)));
        
        
        
        if (isset($PageOptions["All"]["user_options"])){
            $sql2xml->setOptions(array("user_options" => $PageOptions["All"]["user_options"]));
        }
        
        if (isset($PageOptions["querystring"])){
            $sql2xml->setOptions(array("user_options"=>array("result_root"=>"querystring")));
            $pageo = array();
            foreach($PageOptions["querystring"] as $key => $value)
            {
                if (!is_array($value))
                {
                    $pageo[$key] = $value;
                }
            }
            $sql2xml->add($pageo);
        }
        if ($array2xml = $this->getParameter("array2xml")) {
            foreach ($array2xml as $name => $array) {
                $sql2xml->setOptions(array("user_options"=>array("result_root"=>$name)));
                $sql2xml->add($array);
            }
        }
        
        if (is_array($this->queries))
        {
            $_maxLastChangedAll = array(0);
            
            foreach ($this->queries as $structureName => $query) {
                if  ($structureName == "_queryInfo") {
                    continue;
                }
                $query['user_options']['result_root'] = $structureName;
                if ($query['type'] == "dbquery"){
                    //caching the sql2xml part
                    $query["query"] = $this->replaceVarsInWhere($query["query"]);
                    bx_log::log("stucture2xml: ".$query['query']);
                    if ( $this->parent->st2xmlCaching == "true" ) { 
                        if (! (isset($query["maxLastChanged"]) )) {
                            $this->db->loadModule('extended');
                            $query["maxLastChanged"]  = $this->db->extended->getOne($query['queryLastChanged']);
                            
                        } 
                        if ( $cachedXML = $this->api->simpleCacheCheck("","st2xml_data",$query['query'],"file", $query["maxLastChanged"])) {
                            $sql2xml->addWithInput("File",$cachedXML);
                        } 
                        else {
                            $sql2xml->setOptions(array("user_tableInfo"=>$query['tableInfo'],"user_options"=>$query['user_options']));
                            $sql2xml->add($query['query']);
                            $ctx = new DomXpath($sql2xml->Format->xmldoc);
                            $resultTree = $ctx->query("$structureName",$sql2xml->Format->xmlroot );
                            
                            $this->api->simpleCacheWrite("","st2xml_data",$query['query'],"<?xml version='1.0' ?".">".$sql2xml->Format->xmldoc->saveXML($resultTree->item(0)),"file", $query["maxLastChanged"]);
                        }
                        $_maxLastChangedAll[] = $query["maxLastChanged"];
                        
                    }
                    else
                    {
                        
                        $sql2xml->setOptions(array("user_tableInfo"=>$query['tableInfo'],"user_options"=>$query['user_options']));
                        $sql2xml->add($query['query']);
                        
                    }
                }
                else if ( $query['type'] == "aggregate" ) {
                    if ($this->st2xmlCaching == "true" ) {
                        //http stuff
                        if (strpos($query['query'],"http") === 0 ) {
                            
                            //if maxLastChanged is set, we already checked it in the Validity container..
                            if (! (isset($query["maxLastChanged"]) )) {
                                //check for Last Modified automatically creates the cached file, if not already there
                                $query["maxLastChanged"] = $this->api->simpleCacheHttpLastModified($query['query'], time() - $query['expires'],$this->getParameter ("default","proxy") );
                            }
                            $_maxLastChangedAll[] = $query["maxLastChanged"];
                            $sql2xml->addWithInput("File",$this->api->simpleCacheGenerateName("simpleCacheHttp",$query['query']),array("root"=> $structureName));
                        } 
                        // if not starting with http, it will be a local file... hopefully
                        else {
                            $sql2xml->addWithInput("File",$query['query']);
                        }
                    } 
                    else {
                        // with no caching, just get and add it.. (maybe we should allow http caching without db-caching..)
                        $sql2xml->add($query['query'],array("root"=> $structureName));
                        
                    }
                } 
                else {
                    //quite useless right now... 
                    $sql2xml->add($query['query'],array("root"=> $structureName));
                }
            }
            $this->queries["_queryInfo"]["maxLastChanged"] = max ($_maxLastChangedAll);
            
            // send http cache headers so the sent page expires immediately 
            $lastChangedDate = gmdate("r", $this->queries["_queryInfo"]["maxLastChanged"]);
            if (isset($this->sitemap)) {
                $this->sitemap->setHeader("Last-Modified", $lastChangedDate);
                $this->sitemap->setHeader('Expires', $lastChangedDate);
                
                //var_dump(gmdate("r", $this->queries["_queryInfo"]["maxLastChanged"] ));
                // FIXME: if there is no bitlib2 code anymore, this if can go away
                if (method_exists($this->sitemap,"setLastModified")) {
                    $this->sitemap->setLastModified($this->queries["_queryInfo"]["maxLastChanged"] );
                }
            }
        }
        if ($returnDb2XmlObject) {
            return $sql2xml;   
        } else {
            return $sql2xml->getXmlObject();
        }
        
    }
    
    
    function Structures2Sql ($configFile,$sqlOptions=array(),$rootpath= "/structure")
    {
        
        $configClass = bx_helpers_db::getConfigClass($configFile);
        $dbMainStructure = $configClass->getValues( $rootpath);
        if (PEAR::isError($dbMainStructure)) {
            throw new PopoonPEARException($dbMainStructure);
        }
        if (is_array($dbMainStructure['children']))
        {
            
            foreach ($dbMainStructure['children'] as $structureName) {
                $dbStructure = $configClass->getValues( "$rootpath/$structureName");
                if (PEAR::isError($dbStructure)) {
                    die($dbStructure->getUserinfo() .  "\n" . $dbStructure->getMessage());
                }
                if (isset($dbStructure["expires"])) {
                    $allqueries[$structureName]['expires'] = time() - strtotime(preg_replace("#access\s+minus\s+#", "-",$dbStructure["expires"]));
{'fraction_non_alphanumeric': 0.14807021762654804, 'fraction_numerical': 0.005110015630636047, 'mean_word_length': 1.5592307692307692, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 2, '>': 203, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 81}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2100129', 'n_tokens_mistral': 4400, 'n_tokens_neox': 4261, 'n_words': 643}