include_once("schema_loader.php5");
class query_object {
//String Vars that are suppplied through search form
public $project;
public $query_type;
public $max_conditions;
public $search_stripped_fields;
public $schema;
public $stylesheet;
public $page;
public $max_results;
public $pre_processing_script;
public $post_processing_script;
public $sort_order; //must be preprocessed to deal with indexing of database columns
//Array Vars supplied through search form
public $db_cols;
public $reg_exps;
public $strings;
public $connectives;
public $user_char_classes;
public $xml_char_classes;
//Vars that are produced aftery querying database
public $query;
public $link;
public $xml;
public $main_ids;
public $string_var_names;
function __construct() {
$this->project = "";
$this->max_conditions = "";
$this->max_results = "";
$this->page = "";
$this->pre_processing_script = "";
$this->post_processing_script = "";
$this->query_type = "";
$this->schema = "";
$this->search_stripped_fields = false;
$this->stylesheet = "";
$this->xml_char_classes = "";
$this->string_var_names = array("max_conditions","max_results","pre_processing_script",
"post_processing_script","query_type","schema",
"search_stripped_fields","stylesheet");
$this->sort_order = "";
$this->connectives = array();
$this->db_cols = array();
$this->reg_exps = array();
$this->strings = array();
$this->user_char_classes = array();
$this->main_ids = array();
$this->query = "";
$this->xml = "";
$this->link = "";
}
/******************************* project ****************************/
function set_project($project) {
$this->project = $project;
}
function get_project() {
return $this->project;
}
/******************************* query_type ****************************/
function set_query_type($query_type) {
$this->query_type = $query_type;
}
function get_query_type() {
return $this->query_type;
}
/******************************* max_conditions ****************************/
function set_max_conditions($max_conditions) {
$this->max_conditions = $max_conditions;
}
function get_max_conditions() {
return $this->max_conditions;
}
/******************************* condition_set ****************************/
function reset_condition_set() {
$this->db_cols = array();
$this->reg_exps = array();
$this->strings = array();
$this->connectives = array();
}
function add_condition_set($db_cols,$reg_exp,$string) {
$this->db_cols[] = $db_cols;
$this->reg_exps[] = $reg_exp;
$this->strings[] = $string;
}
/******************************* db_cols ****************************/
function get_db_cols($i) {
if(count($this->db_cols) > $i) {
return $this->db_cols[$i];
}
else
return false;
}
function set_db_cols($db_cols) {
$this->db_cols = $db_cols;
}
/******************************* reg_exps ****************************/
function get_reg_exp($i) {
if(count($this->reg_exps) > $i) {
return $this->reg_exps[$i];
}
else
return false;
}
function set_reg_exps($reg_exps) {
$this->reg_exps = $reg_exps;
}
/******************************* strings ****************************/
function get_string($i) {
if(count($this->strings) > $i) {
return $this->strings[$i];
}
else
return false;
}
function set_strings($strings) {
$this->strings = $strings;
}
/******************************* connnectives ****************************/
function add_connective($connective) {
$this->connectives[] = $connective;
}
function get_connective($i) {
if(count($this->connectives) > $i) {
return $this->connectives[$i];
}
else
return false;
}
function get_last_connective() {
return end($this->connectives);
}
/******************************* search_stripped_fields ****************************/
function get_search_stripped_fields() {
return $this->search_stripped_fields;
}
function set_search_stripped_fields($search_stripped_fields) {
$this->search_stripped_fields = $search_stripped_fields;
}
/******************************* stylesheet ****************************/
function get_stylesheet() {
return $this->stylesheet;
}
function set_stylesheet($stylesheet) {
$this->stylesheet = $stylesheet;
}
/******************************* schema ****************************/
function get_schema() {
return $this->schema;
}
function set_schema($schema) {
$this->schema = $schema;
}
/******************************* sort_order ****************************/
function get_sort_order() {
return $this->sort_order;
}
/**
* For now, this function takes a comma delimited string of non-indexed fields, e.g. lxa,lxo not lxa_0,lxo_0;
*/
function set_sort_order($sort_order) {
$sort_fields = explode(",",$sort_order);
if(count($sort_fields) != 0) {
$sort_order = "";
$sl = new schema_loader();
$schema = $this->schema;
if($this->schema == "") {
$schema = "../$this->project/schema.xml";
}
else {
$schema = "../$this->project/$schema";
}
if(!file_exists($schema)) {
die("No schema file in project folder $this->project");
}
$group = $sl->create_group_structure($schema,"");
foreach($sort_fields as $field) {
$field = trim($field);
if(array_key_exists($field,$group->fields)) {
$num_fields = $group->fields[$field];
for($i=0;$i<$num_fields;$i++) {
$sort_order .= $field."_".$i.",";
}
}
}
$this->sort_order = substr($sort_order,0,-1);
}
else
$this->sort_order = "";
}
/******************************* char_classes ****************************/
function get_char_classes() {
$char_classes = array();
if((count($this->xml_char_classes) != 0) || (count($this->user_char_classes) != 0)) {
$char_classes = array_merge($this->xml_char_classes, $this->user_char_classes);
}
return $char_classes;
}
/******************************* user_char_classes ****************************/
function load_user_char_classes($user_char_classes) {
if($user_char_classes != "") {
$lines = explode("\n",$user_char_classes);
foreach($lines as $line) {
$vals = explode(" = ",$line);
$this->user_char_classes[$vals[0]] = $vals[1];
}
}
}
/******************************* xml_char_classes ****************************/
function get_xml_char_classes() {
return $this->xml_char_classes;
}
function set_xml_char_classes($xml_char_classes) {
$this->xml_char_classes = "../".$this->get_project()."/".$xml_char_classes;
}
function load_xml_char_classes($xml_char_classes) {
if($xml_char_classes != "") {
$xcc = array();
$char_xml = simplexml_load_file($xml_char_classes);
foreach($char_xml->children() as $class) {
$xcc[(string)$class->NAME] = (string)$class->VALUE;
}
$this->xml_char_classes = $xcc;
}
}
/******************************* page ****************************/
function get_page() {
return $this->page;
}
function set_page($page) {
$this->page = $page;
}
/******************************* main_ids ****************************/
function get_main_ids() {
return $this->main_ids;
}
function set_main_ids($main_ids) {
$this->main_ids = $main_ids;
}
/******************************* max_results ****************************/
function get_max_results() {
return $this->max_results;
}
function set_max_results($max_results) {
$this->max_results = $max_results;
}
/******************************* xml ****************************/
function get_xml() {
return $this->xml;
}
function set_xml($xml) {
$this->xml = $xml;
}
/******************************* pre_processing_script ****************************/
function get_pre_processing_script() {
return $this->pre_processing_script;
}
function set_pre_processing_script($pre_processing_script) {
$this->pre_processing_script = $pre_processing_script;
}
/******************************* post_processing_script ****************************/
function get_post_processing_script() {
return $this->post_processing_script;
}
function set_post_processing_script($post_processing_script) {
$this->post_processing_script = $post_processing_script;
}
/******************************* num_conditions ****************************/
function get_num_conditions() {
return count($this->strings);
}
/******************************* subqueries ****************************/
function get_query() {
return $this->query;
}
function set_query($query) {
$this->query = $query;
}
/******************************* link ****************************/
function get_link() {
return $this->link;
}
function set_link($link) {
$this->link = $link;
}
/******************************* group ****************************/
function get_group() {
$project = $this->get_project();
$schema = $this->get_schema();
if($schema == "")
$schema = "../$project/schema.xml";
else
$schema = "../$project/$schema";
$sl = new schema_loader();
$group = $sl->create_group_structure($schema,"");
return $group;
}
/******************************* print_values ****************************/
function print_values() {
echo "Project: $this->project
";
echo "Query Type: $this->query_type
";
echo "Max Conditions: $this->max_conditions
";
echo "Db_cols:
";
foreach($this->db_cols as $i=>$cols) {
echo "$i: $cols
";
}
echo "Reg_exps:
";
foreach($this->reg_exps as $i=>$reg) {
echo "$i: $reg
";
}
echo "Strings:
";
foreach($this->strings as $i=>$string) {
echo "$i: $string
";
}
echo "Connectives:
";
foreach($this->connectives as $i=>$con) {
echo "$i: $con
";
}
echo "Search Stripped Fields: $this->search_stripped_fields
";
echo "Schema: $this->schema
";
echo "Sort Order: $this->sort_order
";
echo "Stylesheet: $this->stylesheet
";
echo "XML Char Classes:
";
foreach($this->xml_char_classes as $k=>$c) {
echo "$k: $c
";
}
echo "User Char Classes:
";
foreach($this->user_char_classes as $k=>$c) {
echo "$k: $c
";
}
echo "Page: $this->page
";
echo "Main Ids:
";
foreach($this->main_ids as $id) {
echo "$id ";
}
echo "
";
echo "Max Results: $this->max_results
";
echo "xml: $this->xml
";
echo "Pre-processing Script: $this->pre_processing_script
";
echo "Post-processing Script: $this->post_processing_script
";
echo "Query: $this->query
";
echo "Link: $this->link
";
}
/******************************* assign_value ****************************/
function assign_value($form_vars, $defaults, $var_name) {
$var = trim($form_vars[$var_name]);
if($var == "") {
$function = "get_$var_name";
$var = $this->$function();
if($var == "") {
$var = $defaults[$var_name];
}
}
$function = "set_$var_name";
$this->$function($var);
}
/******************************* get_project_defaults ****************************/
function get_project_defaults($defaults_file) {
$defaults = array();
$defaults_xml = simplexml_load_file($defaults_file);
foreach($defaults_xml->children() as $tag=>$value) {
$defaults["".$tag] = "".$value;
}
return $defaults;
}
/******************************* fill_query_object ****************************/
//Order of precedence: $form_vars > current > defaults
function fill_query_object($form_vars,$defaults) {
$project = $form_vars["project"];
$this->set_project($project);
if((count($defaults) == 0) || ($defaults=="")) {
$defaults_file = "../$project/defaults.xml";
$defaults = array();
if(file_exists($defaults_file)) {
$defaults = $this->get_project_defaults($defaults_file);
}
}
foreach($this->string_var_names as $var_name) {
$this->assign_value($form_vars,$defaults,$var_name);
}
$page = trim($form_vars["page"]);
$this->set_page($page);
if($defaults["xml_char_classes"] != "") {
$xml_char_classes = "../$project/".$defaults["xml_char_classes"];
$this->load_xml_char_classes($xml_char_classes);
}
if($form_vars["user_char_classes"] != "") {
$user_char_classes = $form_vars["user_char_classes"];
$this->load_user_char_classes($user_char_classes);
}
$sort_order = strtolower(trim($form_vars["sort_order"]));
if($sort_order == "") {
$sort_order = $defaults["sort_order"];
}
$this->set_sort_order($sort_order);
$reg_exps = array();
$xml = simplexml_load_file("../$project/reg_exps.xml");
foreach($xml->children() as $set) {
$english = "".$set->ENGLISH;
$value = "".$set->VALUE;
$reg_exps[$english] = $value;
}
$xml = simplexml_load_file("../$project/phrases_and_fields.xml");
$phrases_and_fields = array();
foreach($xml->children() as $set) {
$phrases_and_fields["".$set->ENGLISH] = "".$set->VALUE;
}
$address = getenv("SERVER_NAME");
$script = getenv("REQUEST_URI");
$script = ereg_replace("[?].*","",$script);
$link = "http://".$address.$script."?project=$project";
$max_conditions = $this->get_max_conditions();
$this->reset_condition_set();
for($i=0;$i<$max_conditions;$i++) {
$db_cols_var = "db_cols_".$i;
$db_cols = trim($form_vars[$db_cols_var]);
if(array_key_exists($db_cols,$phrases_and_fields)) {
$db_cols = $phrases_and_fields[$db_cols];
}
$reg_exp_var = "reg_exp_".$i;
$reg_exp = stripslashes($form_vars[$reg_exp_var]);
if(array_key_exists($reg_exp,$reg_exps)) {
$reg_exp = $reg_exps[$reg_exp];
}
$string_var = "string_".$i;
$string = trim($form_vars[$string_var]);
$string = ereg_replace("[+]","\\\+",$string);
$connective_var = "connective_".($i);
$connective = trim($form_vars[$connective_var]);
if($string != "" || ereg("<>", $reg_exp)) {
$link .= "&$db_cols_var=$db_cols&$reg_exp_var=$reg_exp&$string_var=$string&$connective_var=$connective";
$this->add_condition_set($db_cols,$reg_exp,$string);
$this->add_connective($connective);
}
}
$link = ereg_replace("&","&",$link);
$link = ereg_replace("<","<",$link);
$link = ereg_replace(">",">",$link);
$this->set_link($link);
}
}
?>