/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
510 by Suren A. Chilingaryan
Provide metadata services. Currently checks if there are any changes between archive and current databases
1
<?php
2
512 by Suren A. Chilingaryan
Fix HTTP headers for metadata service
3
header("Content-type: text/xml");
4
header("Cache-Control: no-cache, must-revalidate");
510 by Suren A. Chilingaryan
Provide metadata services. Currently checks if there are any changes between archive and current databases
5
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
6
7
class COMPARE {
8
  const EXTRA_ITEMS = 1;                // Fully OK, items added
9
  const MISSING_ITEMS = 2;              // OK. No problems if it is not mistake
10
  const EXTRA_UID = 4;                  // OK. We may have better description in the new archive.
11
  const MISSING_UID = 8;                // Probably not OK. I don't expect there is reason to drop uids. But report a error if names does not match.
12
  const UID_MISTMATCH = 16;             // Not OK.
13
  const NAME_MISTMATCH = 32;            // Probably OK, we may have renames if uids match.
14
  const MISSING_UID_NAME_MATCH = 64;    // OK?
15
  const MISSING_UID_NAME_MISTMATCH = 128;// Not OK
16
    
17
  var $code;
18
  
19
  function __construct($code) {
20
    $this->code = $code;
21
  }
22
  
23
  function Get() {
24
    $code = $this->code;
25
    $uid_mistmatch = $code&(COMPARE::UID_MISTMATCH|COMPARE::MISSING_UID_NAME_MISTMATCH);
26
    $name_mistmatch = $code&COMPARE::NAME_MISTMATCH;
27
    $uids_altered = $code&(COMPARE::EXTRA_UID|COMPARE::MISSING_UID_NAME_MATCH);
28
    
29
    if ($uid_mistmatch) $match = "error";
30
    elseif ($code) $match = "warning";
31
    else $match = "ok";
32
    
33
    return array(
34
        'match' => $match,
35
        'code' => $code,
36
        'uid_mismatch' => $uid_mistmatch?1:0,
37
        'name_mismatch' => $name_mistmatch?1:0,
38
        'uids_altered' => $uids_altered?1:0,
39
        'extra_items' => ($code&COMPARE::EXTRA_ITEMS)?1:0,
40
        'missing_items' => ($code&COMPARE::MISSING_ITEMS)?1:0,
41
    );
42
  }
43
44
  static function CompareGroups($new_req, $old_req, $flags = 0) {
45
    $olist = $old_req->GetItemList($flags);
46
    $nlist = $new_req->GetItemList($flags);
47
    
48
    $res = 0;
49
    foreach ($olist as $i => $info) {
50
        if (!isset($nlist[$i])) {
51
            $res |= COMPARE::MISSING_ITEMS;
52
            break;
53
        }
54
55
        if  (isset($info['uid'])||isset($nlist[$i]['uid'])) {
56
            if (!isset($info['uid'])) 
57
                $res |= COMPARE::EXTRA_UID;
58
            else if (!isset($nlist[$i]['uid'])) {
59
                $res |= COMPARE::MISSING_UID;
60
                if (strcmp($info['name'], $nlist[$i]['name']))
61
                    $res |= MISSING_UID_NAME_MISTMATCH;
62
                else
63
                    $res |= MISSING_UID_NAME_MATCH;
64
            } elseif (strcmp($info['uid'], $nlist[$i]['uid']))
65
                $res |= COMPARE::UID_MISTMATCH;
66
        }
67
        
68
        if (strcmp($info['name'], $nlist[$i]['name']))
69
            $res |= NAME_MISTMATCH;
70
    }
71
    
72
    if (sizeof($nlist) > sizeof($olist))
73
        $res |= COMPARE::EXTRA_ITEMS;
74
    
75
    return new COMPARE($res);
76
  }
77
}
78
79
try {
80
    switch($_GET['target']) {
81
        case "compare_archive":
82
	    if (!isset($_GET['archive'])) {
83
	        echo "here\n";
84
	        $error = translate("Archive is not specified");
85
	        break;
86
            }
87
88
            $props1 = $_GET;
89
            $props2 = $_GET; $props2['db_name'] = $_GET['archive'];
90
            
91
	    $cmp = COMPARE::CompareGroups(new GROUPRequest($props1), new GROUPRequest($props2));
92
	    $info = $cmp->Get();
93
            break;
94
//        case "compare_archvies":      two archives of the same database
95
//        case "compare_groups":        two arbitrary groups
96
	default:
97
	    if (isset($_GET['target'])) $error = translate("Unknown metadata target (%s) is specified", $_GET['target']);
98
	    else $error = translate("The metadata target is not specified");
99
    }
100
} catch (ADEIException $ex) {
101
    $ex->logInfo(NULL, $reader?$reader:new REQUEST());
102
    $error = xml_escape($ex->getInfo());
103
}
104
105
if ($error) echo "<result><Error>$error</Error></result>\n";
106
else {
107
    $extra = "";
108
    foreach ($info as $prop => $value) {
109
	$extra .= " $prop=\"" . xml_escape($value) . "\"";
110
    }
111
    echo "<result$extra/>\n";
112
}
113
114
?>