/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php

require($ADEI_ROOTDIR . "/classes/searchengine.php");
require($ADEI_ROOTDIR . "/classes/searchfilter.php");
require($ADEI_ROOTDIR . "/classes/searchresults.php");

class SEARCH {
 var $req;
 var $engines;
 var $modules;

 const EXACT_MATCH = 1;
 const WORD_MATCH = 2;
 const FUZZY_MATCH = 3;
 const REGEX_MATCH = 4;
 
 function __construct(REQUEST $req = NULL) {
    global $ADEI;
    global $SEARCH_ENGINES;

    if ($req) $this->req = $req;
    else $this->req = new REQUEST();

    $this->modules = array();
    $this->engines = array();
    
    foreach ($SEARCH_ENGINES as $cl => &$opts) {
	try {
	    $ADEI->RequireClass("search/$cl", true);
	} catch (ADEIException $ae) {
	    throw new ADEIException(translate("Unsupported search engine is configured: \"%s\"", $cl));
	}
    
    	$engine = new $cl($req, $opts);
	$modules = $engine->GetModules();
	
	foreach ($modules as $module) {
	    if (isset($this->modules[$module])) {
		throw new ADEIException(translate("SEARCH engines are conflicting. Module (%s) is provided by two different engines: \"" . get_class($this->modules[$module]) . "\" and \"$cl\""));
	    }
	    $this->modules[$module] = $engine;
	}
	
	$this->engines[$cl] = $engine;
    }
 }

 function DetectModules($string) {

    foreach ($this->engines as $engine) {
	$res = $engine->DetectModules($string);
	if ($res !== false) return $res;
    }

    return false;
 } 

 function Search($string = false, $modules = false, $threshold = false, $limits = false, $opts = false) {
    if ($string === false) $string = $this->req->props['search'];
    
    if (preg_match("/^\s*{([^}]*)}\s*(.*)$/", $string, $m)) {
	$modules = $m[1];
	$string = $m[2];
    }

    if (!$opts) $opts = array();
    
    if (preg_match("/^\s*\\[([^\\]]*)\\]\s*(.*)$/", $string, $m)) {
	$opts_string = $m[1];
	if (stripos($opts_string, "=") !== false) {
	    $opts['match'] = SEARCH::EXACT_MATCH;
	} else if (stripos($opts_string, "w") !== false) {
	    $opts['match'] = SEARCH::WORD_MATCH;
	} else if (stripos($opts_string, "~") !== false) {
	    $opts['match'] = SEARCH::FUZZY_MATCH;
	}
    }

    if (($threshold === false)&&(isset($this->req->props['search_threshold']))) {
	$threshold = $this->req->props['search_threshold'];
    }

    if (($modules === false)&&($this->req->props['search_modules'])) {
	$modules = $this->req->props['search_modules'];
    } else {
	// try detection
    }


	// Extracting limits part    
    if (!is_array($limits)) $limits = array();

    $parts = preg_split("/:/", $string);
    if (sizeof($parts) > 1) {
	if (!preg_match("/^(.*)\s*(\b[\w\d_]+)$/", $parts[0], $matches)) {
	    throw new ADEIException(translate("Invalid search string %s", $string));
	}
    
	$key = $matches[2];

	for ($i = 1; $i < sizeof($parts) - 1; $i++) {
	    if (!preg_match("/^(.*)\s*(\b[\w\d_]+)$/", $parts[$i], $m)) {
	        throw new ADEIException(translate("Invalid search string %s", $string));
	    }

	    $limits[$key] = $m[1];	    
	    
	    $key = $m[2];
	}
	
	$limits[$key] = $parts[$i];

	$string = $matches[1];

    }

    $filter = new SEARCHFilter($threshold, $limits);

	// extracting module information    
    if (($modules)&&(is_string($modules))) {
	if (strpos($modules, "{") === false) {
	    $mstr = explode(",", $modules);
	    $modules = array();
	    foreach ($mstr as $mod) {
		if (preg_match("/^([\w\d_]+)(\((.*)\))?$/", $mod, $m)) {
		    $module = $m[1];
		    $modules[$module] = array();
		    
		    if ($m[2]) {
			$params = explode(";", $m[3]);
			foreach ($params as $param) {
			    if (preg_match("/^([^=]+)=(.*)$/", $param, $m)) {
				$modules[$module][$m[1]] = $m[2];
			    } else {
				$modules[$module][$param] = true;
			    }
			}
		    }
		}
	    }
	} else {
	    $modules = json_decode($modules);
	}
    }
    
    if (!$modules) $modules = $this->DetectModules($string);
    
    $result = new SEARCHResults();
    
    if ($modules) {
	foreach ($modules as $module => $opts) {
	    if (isset($this->modules[$module])) {
	        $modres  = $this->modules[$module]->Search($string, $module, $filter, $opts);
		if ($modres) $result->Combine($modres);
		
	    } else {
		throw new ADEIException(translate("The requested search module (%s) is not provided by any of search engines", $module));
	    }
	}
    } else {
	    // Detect what is needed
	    
	foreach ($this->engines as $engine) {
	    $modres = $engine->EngineSearch($string, $filter, $opts);
	    if ($modres) $result->Combine($modres);
	}
    }
    
    if ($result->HaveResults()) {
	return $result;
    }
    
    return false;
 }
 
}

?>