<?php
$institutes = array();
$groups = array();
$tokenInst = array();
$x = file_get_contents('http://publik.tuwien.ac.at/searchdb.php?nojava=0&newcall=1');
if (preg_match('/(<select name="inst"[\s\S]*?<\/select>)/', $x, $res)) {
    if (preg_match_all('/<option value="([0-9]+)">(.*?)<\/option>/', $res[1], $inst, PREG_SET_ORDER)) {
        foreach ($inst as $in) {
            $institutes[$in[1]] = $in[2];
            if (preg_match('/(E[0-9]{3}) /', $in[2], $res)) {
                $tokenInst[$res[1]] = $in[1];
            }
            $x = file_get_contents('http://publik.tuwien.ac.at/searchdb.php?nojava=0&newcall=1&inst=' . $in[1]);
            if (preg_match('/(<select name="abt"[\s\S]*?<\/select>)/', $x, $res2)) {
                if (preg_match_all('/<option value="([0-9]+)">(.*?)<\/option>/', $res2[1], $grps, PREG_SET_ORDER)) {
                    foreach ($grps as $grp) {
                        $groups[$grp[1]] = $grp[2];
                    }
                }
            }
        }
    }
}
asort($institutes);
$n = 1;
foreach ($institutes as $code => $name) {
    printf("  \$optionList[%d] = array(0 => '%s', 1 => '%d');\n", $n, utf8_encode($name), $code);
    $n++;
}
asort($groups);
$n = 1;
foreach ($groups as $code => $name) {
    printf("  \$optionList[%d] = array(0 => '%s', 1 => '%d');\n", $n, utf8_encode($name), $code);
    $n++;
}
$institutes2 = array();
print '$Orgoid2PubdbInst = array(' . "\n";
foreach (array(1562, 1589, 1601, 1607, 1616, 1637, 1670, 1644) as $faculty) {
    $x = file_get_contents('https://tiss.tuwien.ac.at/adressbuch/adressbuch/fakultaet/' . $faculty . '.xml');
    if (preg_match_all('/<org_ref oid="([0-9]+)" code="([0-9E]+)">(.*)<\/org_ref>/', $x, $res, PREG_SET_ORDER)) {
        //    print_r($res);
        foreach ($res as $ires) {
            if (array_key_exists($ires[2], $tokenInst)) {
                printf("  %s => %d,\n", $ires[1], $tokenInst[$ires[2]]);
            }
        }
    }
}
print "}\n";
?>
