/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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php

class SOURCETree {
 var $list;
 var $flags;
 
 var $props_cast;
 
 const IGNORE_UIDS = 0x80000000;
 const IGNORE_BADITEMS = 0x40000000;
 const EXPAND_SERVERS =  0x20000000;

 function __construct($srctree, $optimize = false, $flags = 0) {
    if (is_array($srctree)) $this->list = $srctree;
    else $this->list = $this->Parse($srctree, $optimize, $flags);

    $this->props_cast = false;
    $this->flags = $flags;
 }
 
 
 function GetRequests(MASK $mask = NULL, $props_cast = false, $type = "GROUPRequest") {
    $list = $this->ApplyMask($mask, $this->list);
    
    $plist = array();
    foreach ($list as $key => &$gr) {
	$plist[$key] = $gr['props'];
    }

    if ($props_cast) {
	array_walk($plist, $props_cast);
    }

    return new REQUESTList($props = array(), $plist, $type);
 }
 
 function GetSourceRequests(MASK $mask = NULL) {
     $list = $this->ApplyMask($mask, $this->list);

    $plist = array();
    foreach ($list as &$gr) {
        $key = $gr['props']['db_server'] . "__" . $gr['props']['db_name'];
        if ($plist[$key]) continue;
        
	$plist[$key] = array(
	    'db_server' => $gr['props']['db_server'],
	    'db_name' => $gr['props']['db_name']
	);
    }
    
    return new REQUESTList($props = array(), $plist, "SOURCERequest");
 }
 
 function GetGroupRequest(LOGGROUP $grp, MASK $mask = NULL) {
    if (!isset($this->list[$grp->gid])) {
	throw new ADEIException(translate("Unknown group (%s) is requested in SOURCETree::GetGroupRequest", $grp->gid));
    }

    $info = $this->list[$grp->gid];
    if (($mask)&&(is_array($mask->ids))) {
	if ($info['items'] === false) {
	    $info['props']['db_mask'] = $mask->GetProp();
	} else {
	    $ires = array();
	    foreach ($mask->ids as $id) {
		if (isset($info['items'][$id])) {
		    array_push($ires, $info['items'][$id]);
	        } else {
		    throw new ADEIException(translate("Invalid mask, item (%s) is not present in group", $id));
	        }
	    }
	
	    if ($ires) {
		$info['props']['db_mask'] = implode(",", $ires);
	    } else {
		$info['props']['db_mask'] = false;
	    }
	}
    }

/*
    if ($this->props_cast) {
	call_user_func($this->props_cast, $info['props'], $grp->gid);
    }
*/    
    return new GROUPRequest($info['props']);
 }

 static function ApplyMask(MASK $mask = NULL, $list) {
    if ((!$mask)||(!is_array($mask->ids))) return $list;

    $list_pos = 0;

    $res = array();
        
    $ids = $mask->ids;
    sort($ids);

    if ($ids) $curid = array_shift($ids);
    else return $res;

    foreach($list as $key => &$gr) {
	if (is_array($gr['items'])) {
	    $items = $gr['items'];
	    $size = sizeof($items);
	} else {
	    $req = new GROUPRequest($gr['props']);
	    $rdr = $req->CreateReader();
	    $size = $rdr->GetGroupSize();
	    unset($rdr);
	    unset($req);
	    $items = range(0, $size - 1);
	}
	
	$ires = array(); $ireal = array();
	while ($list_pos + $size > $curid) {
	    array_push($ires, $items[$curid - $list_pos]);
	    array_push($ireal, $curid - $list_pos);
	    
	    if ($ids) $curid = array_shift($ids);
	    else {
		$curid = -1;
		break;
	    }
	}
	
	if ($ires) {
	    $props = $gr['props'];
	    $props['db_mask'] = implode(",", $ires);
	    $props['real_mask'] = implode(",", $ireal);
	    $res[$key] = array(
		'props' => $props,
		'items' => $ires
	    );
	}
	
	$list_pos += $size;
	
	if ($curid < 0) break;
    }

    return $res;
 }

 static function Parse($srctree, $optimize = false, $flags = 0) { 
    global $ADEI;
    global $ADEI_SRCTREE_EXTRA;
    
    $elements = explode(",", 
	preg_replace_callback(
	    "/(\([^)]+,[^)]+\))/", 
	    create_function('$matches', '
		return preg_replace("/,/", ";", $matches[0]);
	    '),
	    $srctree
	)
    );
    
    $curid = false;
    $res = array();
    $idnum = array();
    $extras = array();
    
    if ((!$elements)||(!$elements[0])) $elements = array();

    if ($flags&SOURCETree::EXPAND_SERVERS) {
	$remove_dublicates = false;
	
	$new_elements = array();
	foreach ($elements as $element) {
	    if (preg_match("/^(.*)\(([^)]+)\)$/", $element, $m)) {
		array_push($new_elements, $element);
	    } else {
	        $parts = explode("__", $element, 4);
		if (sizeof($parts) == 1) {
		    $req = new SERVERRequest($props = array(
			"db_server" => $parts[0]
		    ));
		    $list = $req->GetDatabaseList();
		    foreach ($list as $id => $info) {
			array_push($new_elements, $parts[0] . "__" . $id);
		    }

		    $remove_dublicates = true;
		} else {
		    if ((sizeof($parts) > 2)&&($parts[0] == "virtual")) {
		        $extra = $parts[1];
                        if (!$extras[$extra]) {
	                    ADEI::RequireClass("srctree/{$ADEI_SRCTREE_EXTRA[$extra]['class']}");
	                    $extras[$extra] = new $ADEI_SRCTREE_EXTRA[$extra]['class']($req, $ADEI_SRCTREE_EXTRA[$extra]['options']);
		        }
	                $parts = explode("__", $element, 3);
		        $list = $extras[$extra]->Parse($parts[2]);
		        foreach ($list as $item) {
		            array_push($new_elements, $item);
		        }
		        
		        $remove_dublicates = true;
		    } else {
		        array_push($new_elements, $element);
		    }
		}
	    }
	}

	$elements = array();
	foreach ($new_elements as $element) {
	    if (preg_match("/^(.*)\(([^)]+)\)$/", $element, $m)) {
		array_push($elements, $element);
	    } else {
	        $parts = explode("__", $element, 4);
		if (sizeof($parts) == 2) {
		    $req = new SOURCERequest($props = array(
			"db_server" => $parts[0],
			"db_name" => $parts[1]
		    ));
		    $list = $req->GetGroupList();
		    foreach ($list as $gid => $info) {
			array_push($elements, $parts[0] . "__" . $parts[1] . "__" . $gid);
		    }

		    $remove_dublicates = true;
		} else {
		    array_push($elements, $element);
		}
	    }
	    
	}
	
	if ($remove_dublicates) {
	    if ($optimize) $elements = array_unique($elements);
	    else {
		$new_elements = array_unique($elements);
		ksort($new_elements);
		$elements = array_values($new_elements);
	    }
	}

    }
    
    foreach ($elements as $element) {
	if (preg_match("/^(.*)\(([^)]+)\)$/", $element, $m)) {
	    $id = $m[1];

	    $parts = explode("__", $m[1], 3);
	    
	    if (sizeof($parts)<3) {
		throw new ADEIException(translate("Unsupported element (%s) in the source tree", $m[1]));
	    }

	    $items = explode(";", $m[2]);
	} else {
	    $parts = explode("__", $element, 4);

	    if ((sizeof($parts) == 1)&&(($flags&SOURCETree::IGNORE_UIDS)==0)) {
		$item_props = $ADEI->ResolveUID($parts[0]);	// Controls are not supported at the moment
		if ($item_props) $parts = explode("__", SOURCETree::PropsToItem($item_props));
		else if (($flags&SOURCETree::IGNORE_BADITEMS)==0) throw new ADEIException(translate("UID (%s) is not available", $parts[0]));
		else continue;
	    }

	    if (sizeof($parts)<3) {
		if (($flags&SOURCETree::IGNORE_BADITEMS)==0) throw new ADEIException(translate("Unsupported element (%s) of source tree", $element));
		else continue;
		
	    }
	    $id = $parts[0] . "__" . $parts[1] . "__" . $parts[2];

	    if (sizeof($parts) == 4) {
		$items = array($parts[3]);
	    } else {
		$items = false;
	    }
	}
    
	if ($optimize) {
	    $realid = $id;
	} else {
	    if (strcmp($id, $curid)) {
		if ($idnum[$id]) {
		    $realid = $id . "__" . (++$idnum[$id]);
		} else {
		    $realid = $id;
		    $idnum[$id] = 1;
		}
		$curid = $id;
	    }
	}
	
	if (!isset($res[$realid])) {
	    $res[$realid] = array(
		'props' => array(
		    'db_server' => $parts[0],
		    'db_name' => $parts[1],
		    'db_group' => $parts[2]
		),
		'items' => array()
	    );
	}
	
	if ($items === false) {
	    $res[$realid]['items'] = false;
	    unset($res[$realid]['props']['db_mask']);
	} else if (is_array($res[$realid]['items'])) {
	    $res[$realid]['items'] = array_merge($res[$realid]['items'], $items);
	    $res[$realid]['props']['db_mask'] = implode(",", $res[$realid]['items']);
	}

    }
    
    return $res;
 }

 static function Create($list, $flags = 0) {
    $res = array();
    foreach ($list as $gr) {
	$item = "{$gr['props']['db_server']}__{$gr['props']['db_name']}__{$gr['props']['db_group']}";
	if ($gr['items']) {
	    $item .= "(" . implode(";", $gr['items']) . ")";
	}
	array_push($res, $item);
    }
    return implode(",", $res);
 }

 static function Optimize($srctree, $flags = 0) {
    return SOURCETree::Create(SOURCETree::Parse($srctree, true, $flags), $flags);
 }
 
 static function GetGroupID($props) {
    return "{$props['db_server']}__{$props['db_name']}__{$props['db_group']}"; 
 }
 
 static function ItemToProps($item) {
    $arr = explode("__", $item, 4);
    if (sizeof($arr) < 4) 
	throw new ADEIException(translate("Invalid item specification (%s) is supplied", $item));
    
    $props = array(
        'id' => $item,
	'db_server' => $arr[0],
	'db_name' => $arr[1],
	'db_group' => $arr[2],
	'db_mask' => $arr[3]
    );
    return $props;    
 }

 static function PropsToItem(array &$props) {
    if (is_numeric($props['db_mask'])) {
	return "{$props['db_server']}__{$props['db_name']}__{$props['db_group']}__{$props['db_mask']}"; 
    } else {
	return "{$props['db_server']}__{$props['db_name']}__{$props['db_group']}({$props['db_mask']})"; 
    }
 }
 
 static function PropsCmp($p1, $p2) {
    if (sizeof($p1) != sizeof($p2)) return 1;
    foreach ($p1 as $key => $value) {
	if ((!isset($p2[$key]))||(strcmp($p2[$key], $value))) return 1;
    }
    return 0;
 }
}


?>