ajax.dataloader.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * This page is called to load "asynchronously" some xml file into the database
  4. * parameters
  5. * 'file' string Name of the file to load
  6. * 'session_status' string 'start', 'continue' or 'end'
  7. * 'percent' integer 0..100 the percentage of completion once the file has been loaded
  8. */
  9. require_once('../application/utils.inc.php');
  10. require_once('../core/config.class.inc.php');
  11. require_once('../core/cmdbsource.class.inc.php');
  12. require_once('./setuppage.class.inc.php');
  13. require_once('./xmldataloader.class.inc.php');
  14. define('TMP_CONFIG_FILE', '../tmp-config-itop.php');
  15. //define('FINAL_CONFIG_FILE', '../config-itop.php');
  16. // Never cache this page
  17. header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  18. header("Expires: Fri, 17 Jul 1970 05:00:00 GMT"); // Date in the past
  19. /**
  20. * Main program
  21. */
  22. $sFileName = Utils::ReadParam('file', '');
  23. $sSessionStatus = Utils::ReadParam('session_status', '');
  24. $iPercent = (integer)Utils::ReadParam('percent', 0);
  25. setup_web_page::log("Info - Loading file: $sFileName");
  26. try
  27. {
  28. if (empty($sFileName) || !file_exists($sFileName))
  29. {
  30. throw(new Exception("File $sFileName does not exist"));
  31. }
  32. $oDataLoader = new XMLDataLoader(TMP_CONFIG_FILE); // When called by the wizard, the final config is not yet there
  33. if ($sSessionStatus == 'start')
  34. {
  35. $oChange = MetaModel::NewObject("CMDBChange");
  36. $oChange->Set("date", time());
  37. $oChange->Set("userinfo", "Initialization");
  38. $iChangeId = $oChange->DBInsert();
  39. setup_web_page::log("Info - starting data load session");
  40. $oDataLoader->StartSession($oChange);
  41. }
  42. $oDataLoader->LoadFile($sFileName);
  43. $sResult = sprintf("Info - loading of %s done. (Overall %d %% completed).", basename($sFileName), $iPercent);
  44. echo $sResult;
  45. setup_web_page::log($sResult);
  46. if ($sSessionStatus == 'end')
  47. {
  48. $oDataLoader->EndSession();
  49. setup_web_page::log("Info - ending data load session");
  50. }
  51. }
  52. catch(Exception $e)
  53. {
  54. echo "<p>An error happened while loading the data</p>\n";
  55. echo '<p>'.$e."</p>\n";
  56. setup_web_page::log("Error - An error happened while loading the data. ".$e);
  57. }
  58. ?>