<commit_before><?php
/** \file
 * $Header: /cvsroot/tikiwiki/tiki/lib/integrator/integrator.php,v 1.14 2003-11-04 00:03:32 zaufi Exp $
 * 
 * \brief Tiki integrator support class
 *
 */


class TikiIntegrator extends TikiLib
{
    var $c_rep;                         //!< cached value for repository data
    
    function TikiIntegrator($db)
    {
        if (!$db) die("Invalid db object passed to TikiIntegrator constructor");
        $this->db = $db;
        $c_rep = false;
    }
    /// Repository management
    //\{
    /// List all
    function list_repositories($visible_only)
    {
        $cond = ($visible_only == true) ? "where visibility='y'" : '';
        $query = "select * from tiki_integrator_reps ".$cond." order by 'name'";
        $result = $this->query($query);
        $ret = Array();
        while($res = $result->fetchRow(DB_FETCHMODE_ASSOC)) $ret[] = $res;
        return $ret;
    }
    /// Add/Update
    function add_replace_repository($repID, $name, $path, $start, $css, $vis, $cacheable, $descr)
    {
        $name  = addslashes($name);
        $path  = addslashes($path);
        $start = addslashes($start);
        $css   = addslashes($css);
        $descr = addslashes($descr);
        if (strlen($repID) == 0 || $repID == 0)
            $query = "insert into tiki_integrator_reps(name,path,start_page,css_file,visibility,cacheable,description)
                      values('$name','$path','$start','$css','$vis','$cacheable','$descr')";
        else
            $query = "update tiki_integrator_reps 
                      set name='$name',path='$path',start_page='$start',
                      css_file='$css',visibility='$vis',cacheable='$cacheable',
                      description='$descr' where repID='$repID'";
        $result = $this->query($query);
        // Invalidate cached repository if needed
        if (isset($this->c_rep["repID"]) && ($this->c_rep["repID"] == $repID))
            unset($this->c_rep);
    }
    /// Get one entry by ID
    function get_repository($repID)
    {
        // Check if we already cache requested repository info
        if (isset($this->c_rep["repID"]) && ($this->c_rep["repID"] == $repID))
            return $this->c_rep;
        else
        {   // Need to select it...
            $query = "select * from tiki_integrator_reps where repID='$repID'";
            $result = $this->query($query);
            if (!$result->numRows()) return false;
            $res = $result->fetchRow(DB_FETCHMODE_ASSOC);
            $c_rep = $res;
        }
        return $res;
    }
    /// Remove repository and all rules configured for it
    function remove_repository($repID)
    {
        $query = "delete from tiki_integrator_rules where repID=$repID";
        $result = $this->query($query);
        $query = "delete from tiki_integrator_reps where repID=$repID";
        $result = $this->query($query);
        // Check if we remove cached repository
        if (isset($this->c_rep["repID"]) && ($this->c_rep["repID"] == $repID))
            unset($this->c_rep);
        // Clear cached pages for this repository
        $this->clear_cache($repID);
    }
    //\}
    /// Rules management
    //\{
    /// List rules for given repository
    function list_rules($repID)
    {
        $query = "select * from tiki_integrator_rules where repID='$repID' order by 'ord'";
        $result = $this->query($query);
        $ret = Array();
        while($res = $result->fetchRow(DB_FETCHMODE_ASSOC)) $ret[] = $res;
        return $ret;
    }
    /// Add or update rule for repository
    function add_replace_rule($repID, $ruleID, $ord, $srch, $repl, $type, $case, $rxmod, $descr)
    {
        $srch  = addslashes($srch);
        $repl  = addslashes($repl);
        $rxmod = addslashes($rxmod);
        $descr = addslashes($descr);

        if ($ord == 0)
        {
            $query = "select max(ord) from tiki_integrator_rules where repID='$repID'";
            $ord = $this->getOne($query) + 1;
        }
        if (strlen($ruleID) == 0 || $ruleID == 0)
            $query = "insert into tiki_integrator_rules(repID,ord,srch,repl,type,casesense,rxmod,description)
                      values('$repID','$ord','$srch','$repl','$type','$case','$rxmod','$descr')";
        else
            $query = "update tiki_integrator_rules 
                      set repID='$repID',ord='$ord',srch='$srch',repl='$repl',
                      type='$type',casesense='$case',rxmod='$rxmod',description='$descr'
                      where ruleID='$ruleID'";
        $result = $this->query($query);
        // Clear cached pages for this repository
        $this->clear_cache($repID);
    }
    /// Get one entry by ID
    function get_rule($ruleID)
    {
        $query = "select * from tiki_integrator_rules where ruleID='$ruleID'";
        $result = $this->query($query);
        if (!$result->numRows()) return false;
        $res = $result->fetchRow(DB_FETCHMODE_ASSOC);
        return $res;
    }
    /// Remove rule
    function remove_rule($ruleID)
    {
        // Clear cached pages for this repository
        $rule = $this->get_rule($ruleID);
        $this->clear_cache($rule["repID"]);
        // Remove rule
        $query = "delete from tiki_integrator_rules where ruleID=$ruleID";
        $result = $this->query($query);
    }
    /// Apply rule to string
    function apply_rule(&$rep, &$rule, $data)
    {
        // Is there something to search? If no return original data
        if (strlen($rule["srch"]) == 0) return $data;
        // Prepare replace string (subst {path})
        $repl = str_replace('{path}', $rep["path"], $rule["repl"]);
        $repl = str_replace('{repID}', $rep["repID"], $repl);
        //
        $d = $data;
        if ($rule["type"] == 'y')
        {
            // regex rule
            $d = preg_replace('_'.$rule["srch"].'_'.$rule["rxmod"], $repl, $data);
        }
        else
        {<commit_msg>Add emulation of 'g' regex modifier<commit_after><?php
/** \file
 * $Header: /cvsroot/tikiwiki/tiki/lib/integrator/integrator.php,v 1.15 2003-11-08 18:19:09 zaufi Exp $
 * 
 * \brief Tiki integrator support class
 *
 */


class TikiIntegrator extends TikiLib
{
    var $c_rep;                         //!< cached value for repository data
    
    function TikiIntegrator($db)
    {
        if (!$db) die("Invalid db object passed to TikiIntegrator constructor");
        $this->db = $db;
        $c_rep = false;
    }
    /// Repository management
    //\{
    /// List all
    function list_repositories($visible_only)
    {
        $cond = ($visible_only == true) ? "where visibility='y'" : '';
        $query = "select * from tiki_integrator_reps ".$cond." order by 'name'";
        $result = $this->query($query);
        $ret = Array();
        while($res = $result->fetchRow(DB_FETCHMODE_ASSOC)) $ret[] = $res;
        return $ret;
    }
    /// Add/Update
    function add_replace_repository($repID, $name, $path, $start, $css, $vis, $cacheable, $descr)
    {
        $name  = addslashes($name);
        $path  = addslashes($path);
        $start = addslashes($start);
        $css   = addslashes($css);
        $descr = addslashes($descr);
        if (strlen($repID) == 0 || $repID == 0)
            $query = "insert into tiki_integrator_reps(name,path,start_page,css_file,visibility,cacheable,description)
                      values('$name','$path','$start','$css','$vis','$cacheable','$descr')";
        else
            $query = "update tiki_integrator_reps 
                      set name='$name',path='$path',start_page='$start',
                      css_file='$css',visibility='$vis',cacheable='$cacheable',
                      description='$descr' where repID='$repID'";
        $result = $this->query($query);
        // Invalidate cached repository if needed
        if (isset($this->c_rep["repID"]) && ($this->c_rep["repID"] == $repID))
            unset($this->c_rep);
    }
    /// Get one entry by ID
    function get_repository($repID)
    {
        // Check if we already cache requested repository info
        if (isset($this->c_rep["repID"]) && ($this->c_rep["repID"] == $repID))
            return $this->c_rep;
        else
        {   // Need to select it...
            $query = "select * from tiki_integrator_reps where repID='$repID'";
            $result = $this->query($query);
            if (!$result->numRows()) return false;
            $res = $result->fetchRow(DB_FETCHMODE_ASSOC);
            $c_rep = $res;
        }
        return $res;
    }
    /// Remove repository and all rules configured for it
    function remove_repository($repID)
    {
        $query = "delete from tiki_integrator_rules where repID=$repID";
        $result = $this->query($query);
        $query = "delete from tiki_integrator_reps where repID=$repID";
        $result = $this->query($query);
        // Check if we remove cached repository
        if (isset($this->c_rep["repID"]) && ($this->c_rep["repID"] == $repID))
            unset($this->c_rep);
        // Clear cached pages for this repository
        $this->clear_cache($repID);
    }
    //\}
    /// Rules management
    //\{
    /// List rules for given repository
    function list_rules($repID)
    {
        $query = "select * from tiki_integrator_rules where repID='$repID' order by 'ord'";
        $result = $this->query($query);
        $ret = Array();
        while($res = $result->fetchRow(DB_FETCHMODE_ASSOC)) $ret[] = $res;
        return $ret;
    }
    /// Add or update rule for repository
    function add_replace_rule($repID, $ruleID, $ord, $srch, $repl, $type, $case, $rxmod, $descr)
    {
        $srch  = addslashes($srch);
        $repl  = addslashes($repl);
        $rxmod = addslashes($rxmod);
        $descr = addslashes($descr);

        if ($ord == 0)
        {
            $query = "select max(ord) from tiki_integrator_rules where repID='$repID'";
            $ord = $this->getOne($query) + 1;
        }
        if (strlen($ruleID) == 0 || $ruleID == 0)
            $query = "insert into tiki_integrator_rules(repID,ord,srch,repl,type,casesense,rxmod,description)
                      values('$repID','$ord','$srch','$repl','$type','$case','$rxmod','$descr')";
        else
            $query = "update tiki_integrator_rules 
                      set repID='$repID',ord='$ord',srch='$srch',repl='$repl',
                      type='$type',casesense='$case',rxmod='$rxmod',description='$descr'
                      where ruleID='$ruleID'";
        $result = $this->query($query);
        // Clear cached pages for this repository
        $this->clear_cache($repID);
    }
    /// Get one entry by ID
    function get_rule($ruleID)
    {
        $query = "select * from tiki_integrator_rules where ruleID='$ruleID'";
        $result = $this->query($query);
        if (!$result->numRows()) return false;
        $res = $result->fetchRow(DB_FETCHMODE_ASSOC);
        return $res;
    }
    /// Remove rule
    function remove_rule($ruleID)
    {
        // Clear cached pages for this repository
        $rule = $this->get_rule($ruleID);
        $this->clear_cache($rule["repID"]);
        // Remove rule
        $query = "delete from tiki_integrator_rules where ruleID=$ruleID";
        $result = $this->query($query);
    }
    /// Apply rule to string
    function apply_rule(&$rep, &$rule, $data)
    {
        // Is there something to search? If no return original data
        if (strlen($rule["srch"]) == 0) return $data;
        // Prepare replace string (subst {path})
        $repl = str_replace('{path}', $rep["path"], $rule["repl"]);
        $repl = str_replace('{repID}', $rep["repID"], $repl);
        //
        if ($rule["type"] == 'y')
        {
            // regex rule. Do replace 'till we have smth to replace (if 'g' modifier present)...
            $g = !(strpos($rule["rxmod"], 'g') === false);
            $mod = str_replace('g', '', $rule["rxmod"]);
            $d = $data;
            do
            {
                $tmp = $d;
                $d = preg_replace('_'.$rule["srch"].'_'.$mod, $repl, $tmp);
            }
            while ((strcmp($d, $tmp) != 0) && ($g == true));
            unset($tmp);
        }
        else
        {
{'fraction_non_alphanumeric': 0.16620203345359133, 'fraction_numerical': 0.004017710724827812, 'mean_word_length': 2.2038350407144733, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '944150', 'n_tokens_mistral': 3890, 'n_tokens_neox': 3830, 'n_words': 826}