/**
 * The user has the ability to define more than one database in wtConfig.php.
 * Some modules may want to use their own databases instead of share it with the core.
 * In that case, the module will call wtDBGetConnection with the unique identifier
 * of the database. Now, if this database is defined in wtConfig.php, the function
 * will return it, otherwise the default database will be used.
 *
 * @param $id
 *		the identifier of the desired database.
 * @param $fallback_id
 *    Optional. A fallback connection id which will be used if the first database is not
 *    defined.   
 * @return
 *		the appropriate database connection, if exists, or the base/fallback connection used
 *		by the core.
 */
function wtDBGetConnection($id, $fallback_id = NULL)
{
    global $WT;
    if (isset($WT->DBConnections[$id])) {
        return $WT->DBConnections[$id];
    } else {
        if (!$fallback_id) {
            return $WT->DB;
        } else {
            return wtDBGetConnection($fallback_id);
        }
    }
}
    unset($first);
} else {
    $WT->DB->connect($WT->DBUrl);
}
if (!$WT->DB->connected()) {
    // TODO: Display database connection error message.
}
$WT->ModuleManager = new wtModuleManager();
// Is the core already installed?
$ok = wtRegGetKey("Core/Installed");
if (!$ok || $WT->DevMode) {
    if (wtDBInstall($WT->CoreDir . "include/wtCoreDB.php")) {
        wtUserGroupCreate("administrators");
        wtUserCreate("admin", "admin", "administrators");
        wtRegSetKey("Core/Installed", true);
        if (wtDBInstall($WT->CoreDir . "include/wtCoreModuleDB.php", wtDBGetConnection("Core"))) {
            include $WT->CoreDir . "include/wtModInstall.php";
            wtInstallModulesInDirectory($WT->CoreDir . "modules");
            // Finally, install the core modules.
            wtBuildModuleWeights();
        }
    }
}
unset($ok);
$WT->Theme = wtLoadTheme(NULL);
$WT->ModuleManager->read();
$WT->ModuleManager->preInit();
$WT->ModuleManager->postInit();
if (wtCallHook("Theme/OnCall", NULL) !== FALSE) {
    $WT->Theme->call();
}
 function wtModuleManager()
 {
     $this->mDB =& wtDBGetConnection("Core");
 }
/**
 *	Checks wheter a requirement is available or not. Requirements are used for module dependencies, for example.
 *  A typical requirement string should look like this:
 *
 *  Module:SomeModule>=0.1
 *
 *	This would check if a module named SomeModule is installed in version 0.1 or later.
 *
 *
 * @param $str
 *		The requirement string.
 *	@return
 *		True if the requirement has been met or false if not.
 */
function wtCheckRequirement($reqStr)
{
    $req = wtParseRequirement($reqStr);
    if (!$req) {
        return false;
    }
    global $WT;
    switch ($req['type']) {
        case "Module":
            $db = wtDBGetConnection("Core");
            $r = $WT->ModuleManager->checkModule($req['first'], $req['second']);
            if ($req['type'] == "==" || $req['type'] == "<=" || $req['type'] == ">=" && $r !== TRUE) {
                return false;
            }
            if ($req['type'] == "!=" && $r === TRUE) {
                return false;
            }
            if ($req['type'] == ">" && ($r === TRUE || $r <= 0)) {
                return false;
            }
            if ($req['type'] == "<" && ($r === TRUE || $r >= 0)) {
                return false;
            }
            return true;
            break;
        case "":
            if ($req['first'] == "WTCore") {
                return @eval("return \$WT->Version" . $req['comparison'] . "\$req['second'];");
            }
            break;
    }
    return false;
}