ajax.dataloader.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. define('SAFE_MINIMUM_MEMORY', 32*1024*1024);
  10. require_once('../application/utils.inc.php');
  11. require_once('./setuppage.class.inc.php');
  12. $sMemoryLimit = trim(ini_get('memory_limit'));
  13. if (empty($sMemoryLimit))
  14. {
  15. // On some PHP installations, memory_limit does not exist as a PHP setting!
  16. // (encountered on a 5.2.0 under Windows)
  17. // In that case, ini_set will not work, let's keep track of this and proceed with the data load
  18. SetupWebPage::log_info("No memory limit has been defined in this instance of PHP");
  19. }
  20. else
  21. {
  22. // Check that the limit will allow us to load the data
  23. //
  24. $iMemoryLimit = utils::ConvertToBytes($sMemoryLimit);
  25. if ($iMemoryLimit < SAFE_MINIMUM_MEMORY)
  26. {
  27. if (ini_set('memory_limit', SAFE_MINIMUM_MEMORY) === FALSE)
  28. {
  29. SetupWebPage::log_error("memory_limit is too small: $iMemoryLimit and can not be increased by the script itself.");
  30. }
  31. else
  32. {
  33. SetupWebPage::log_info("memory_limit increased from $iMemoryLimit to ".SAFE_MINIMUM_MEMORY.".");
  34. }
  35. }
  36. }
  37. function FatalErrorCatcher($sOutput)
  38. {
  39. if ( preg_match('|<phpfatalerror>.*</phpfatalerror>|s', $sOutput, $aMatches) )
  40. {
  41. header("HTTP/1.0 500 Internal server error.");
  42. foreach ($aMatches as $sMatch)
  43. {
  44. $errors .= strip_tags($sMatch)."\n";
  45. }
  46. $sOutput = "$errors\n";
  47. // Logging to a file does not work if the whole memory is exhausted...
  48. //SetupWebPage::log_error("Fatal error - in $__FILE__ , $errors");
  49. }
  50. return $sOutput;
  51. }
  52. //Define some bogus, invalid HTML tags that no sane
  53. //person would ever put in an actual document and tell
  54. //PHP to delimit fatal error warnings with them.
  55. ini_set('error_prepend_string', '<phpfatalerror>');
  56. ini_set('error_append_string', '</phpfatalerror>');
  57. // Starts the capture of the ouput, and sets a filter to capture the fatal errors.
  58. ob_start('FatalErrorCatcher'); // Start capturing the output, and pass it through the fatal error catcher
  59. require_once('../core/config.class.inc.php');
  60. require_once('../core/cmdbsource.class.inc.php');
  61. require_once('./xmldataloader.class.inc.php');
  62. define('TMP_CONFIG_FILE', '../tmp-config-itop.php');
  63. //define('FINAL_CONFIG_FILE', '../config-itop.php');
  64. // Never cache this page
  65. header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  66. header("Expires: Fri, 17 Jul 1970 05:00:00 GMT"); // Date in the past
  67. /**
  68. * Main program
  69. */
  70. $sFileName = Utils::ReadParam('file', '');
  71. $sSessionStatus = Utils::ReadParam('session_status', '');
  72. $iPercent = (integer)Utils::ReadParam('percent', 0);
  73. SetupWebPage::log_info("Loading file: $sFileName");
  74. try
  75. {
  76. if (empty($sFileName) || !file_exists($sFileName))
  77. {
  78. throw(new Exception("File $sFileName does not exist"));
  79. }
  80. $oDataLoader = new XMLDataLoader(TMP_CONFIG_FILE); // When called by the wizard, the final config is not yet there
  81. if ($sSessionStatus == 'start')
  82. {
  83. $oChange = MetaModel::NewObject("CMDBChange");
  84. $oChange->Set("date", time());
  85. $oChange->Set("userinfo", "Initialization");
  86. $iChangeId = $oChange->DBInsert();
  87. SetupWebPage::log_info("starting data load session");
  88. $oDataLoader->StartSession($oChange);
  89. }
  90. $oDataLoader->LoadFile($sFileName);
  91. $sResult = sprintf("loading of %s done. (Overall %d %% completed).", basename($sFileName), $iPercent);
  92. echo $sResult;
  93. SetupWebPage::log_info($sResult);
  94. if ($sSessionStatus == 'end')
  95. {
  96. $oDataLoader->EndSession();
  97. SetupWebPage::log_info("ending data load session");
  98. }
  99. }
  100. catch(Exception $e)
  101. {
  102. echo "<p>An error happened while loading the data</p>\n";
  103. echo '<p>'.$e."</p>\n";
  104. SetupWebPage::log_error("An error happened while loading the data. ".$e);
  105. }
  106. if (function_exists('memory_get_peak_usage'))
  107. {
  108. SetupWebPage::log_info("loading file '$sFileName', peak memory usage. ".memory_get_peak_usage());
  109. }
  110. ?>