ajax.dataloader.php 3.6 KB

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