// Main
//
////////////////////////////////////////////////////////////////////////////////
// Never cache this page
header("Cache-Control: no-cache, must-revalidate");
// HTTP/1.1
header("Expires: Fri, 17 Jul 1970 05:00:00 GMT");
// Date in the past
/**
 * Main program
 */
$sFileName = Utils::ReadParam('file', '', false, 'raw_data');
$oP = new WebPage("iTop - Backoffice data loader");
try {
    // Note: the data model must be loaded first
    $oDataLoader = new XMLDataLoader();
    if (empty($sFileName)) {
        throw new Exception("Missing argument 'file'");
    }
    if (!file_exists($sFileName)) {
        throw new Exception("File {$sFileName} does not exist");
    }
    SetMemoryLimit($oP);
    // The XMLDataLoader constructor has initialized the DB, let's start a transaction
    CMDBSource::Query('START TRANSACTION');
    $oChange = MetaModel::NewObject("CMDBChange");
    $oChange->Set("date", time());
    $oChange->Set("userinfo", "Initialization");
    $iChangeId = $oChange->DBInsert();
    $oP->p("Starting data load.");
    $oDataLoader->StartSession($oChange);
 protected static function DoLoadFiles($aSelectedModules, $sModulesDir, $sDBServer, $sDBUser, $sDBPwd, $sDBName, $sDBPrefix, $sTargetEnvironment = '', $bOldAddon = false, $bSampleData = false)
 {
     $aParamValues = array('db_server' => $sDBServer, 'db_user' => $sDBUser, 'db_pwd' => $sDBPwd, 'db_name' => $sDBName, 'new_db_name' => $sDBName, 'db_prefix' => $sDBPrefix);
     $oConfig = new Config();
     $oConfig->UpdateFromParams($aParamValues, $sModulesDir);
     if ($bOldAddon) {
         // Old version of the add-on for backward compatibility with pre-2.0 data models
         $oConfig->SetAddons(array('user rights' => 'addons/userrights/userrightsprofile.db.class.inc.php'));
     }
     //Load the MetaModel if needed (asynchronous mode)
     if (!self::$bMetaModelStarted) {
         $oProductionEnv = new RunTimeEnvironment($sTargetEnvironment);
         $oProductionEnv->InitDataModel($oConfig, false);
         // load data model and connect to the database
         self::$bMetaModelStarted = true;
         // No need to reload the final MetaModel in case the installer runs synchronously
     }
     $oDataLoader = new XMLDataLoader();
     CMDBObject::SetTrackInfo("Initialization");
     $oMyChange = CMDBObject::GetCurrentChange();
     SetupPage::log_info("starting data load session");
     $oDataLoader->StartSession($oMyChange);
     $aFiles = array();
     $aPreviouslyLoadedFiles = array();
     $oProductionEnv = new RunTimeEnvironment();
     $aAvailableModules = $oProductionEnv->AnalyzeInstallation($oConfig, APPROOT . $sModulesDir);
     foreach ($aAvailableModules as $sModuleId => $aModule) {
         if ($sModuleId != ROOT_MODULE) {
             // Load data only for selected AND newly installed modules
             if (in_array($sModuleId, $aSelectedModules)) {
                 if ($aModule['version_db'] != '') {
                     // Simulate the load of the previously loaded XML files to get the mapping of the keys
                     if ($bSampleData) {
                         $aPreviouslyLoadedFiles = array_merge($aPreviouslyLoadedFiles, $aAvailableModules[$sModuleId]['data.struct'], $aAvailableModules[$sModuleId]['data.sample']);
                     } else {
                         // Load only structural data
                         $aPreviouslyLoadedFiles = array_merge($aPreviouslyLoadedFiles, $aAvailableModules[$sModuleId]['data.struct']);
                     }
                 } else {
                     if ($bSampleData) {
                         $aFiles = array_merge($aFiles, $aAvailableModules[$sModuleId]['data.struct'], $aAvailableModules[$sModuleId]['data.sample']);
                     } else {
                         // Load only structural data
                         $aFiles = array_merge($aFiles, $aAvailableModules[$sModuleId]['data.struct']);
                     }
                 }
             }
         }
     }
     // Simulate the load of the previously loaded files, in order to initialize
     // the mapping between the identifiers in the XML and the actual identifiers
     // in the current database
     foreach ($aPreviouslyLoadedFiles as $sFileRelativePath) {
         $sFileName = APPROOT . $sFileRelativePath;
         SetupPage::log_info("Loading file: {$sFileName} (just to get the keys mapping)");
         if (empty($sFileName) || !file_exists($sFileName)) {
             throw new Exception("File {$sFileName} does not exist");
         }
         $oDataLoader->LoadFile($sFileName, true);
         $sResult = sprintf("loading of %s done.", basename($sFileName));
         SetupPage::log_info($sResult);
     }
     foreach ($aFiles as $sFileRelativePath) {
         $sFileName = APPROOT . $sFileRelativePath;
         SetupPage::log_info("Loading file: {$sFileName}");
         if (empty($sFileName) || !file_exists($sFileName)) {
             throw new Exception("File {$sFileName} does not exist");
         }
         $oDataLoader->LoadFile($sFileName);
         $sResult = sprintf("loading of %s done.", basename($sFileName));
         SetupPage::log_info($sResult);
     }
     $oDataLoader->EndSession();
     SetupPage::log_info("ending data load session");
 }