backoffice.dataloader.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Does load data from XML files (currently used in the setup and the backoffice data loader utility)
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. /**
  25. * This page is called to load an XML file into the database
  26. * parameters
  27. * 'file' string Name of the file to load
  28. */
  29. define('SAFE_MINIMUM_MEMORY', 256*1024*1024);
  30. require_once('../application/utils.inc.php');
  31. require_once("../application/nicewebpage.class.inc.php");
  32. // required because the class xmldataloader is reporting errors in the setup.log file
  33. require_once('../setup/setuppage.class.inc.php');
  34. function SetMemoryLimit($oP)
  35. {
  36. $sMemoryLimit = trim(ini_get('memory_limit'));
  37. if (empty($sMemoryLimit))
  38. {
  39. // On some PHP installations, memory_limit does not exist as a PHP setting!
  40. // (encountered on a 5.2.0 under Windows)
  41. // In that case, ini_set will not work, let's keep track of this and proceed with the data load
  42. $oP->p("No memory limit has been defined in this instance of PHP");
  43. }
  44. else
  45. {
  46. // Check that the limit will allow us to load the data
  47. //
  48. $iMemoryLimit = utils::ConvertToBytes($sMemoryLimit);
  49. if ($iMemoryLimit < SAFE_MINIMUM_MEMORY)
  50. {
  51. if (ini_set('memory_limit', SAFE_MINIMUM_MEMORY) === FALSE)
  52. {
  53. $oP->p("memory_limit is too small: $iMemoryLimit and can not be increased by the script itself.");
  54. }
  55. else
  56. {
  57. $oP->p("memory_limit increased from $iMemoryLimit to ".SAFE_MINIMUM_MEMORY.".");
  58. }
  59. }
  60. }
  61. }
  62. ////////////////////////////////////////////////////////////////////////////////
  63. //
  64. // Main
  65. //
  66. ////////////////////////////////////////////////////////////////////////////////
  67. require_once('../core/config.class.inc.php');
  68. require_once('../core/log.class.inc.php');
  69. require_once('../core/kpi.class.inc.php');
  70. require_once('../core/cmdbsource.class.inc.php');
  71. require_once('../setup/xmldataloader.class.inc.php');
  72. define('FINAL_CONFIG_FILE', '../config-itop.php');
  73. // Never cache this page
  74. header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  75. header("Expires: Fri, 17 Jul 1970 05:00:00 GMT"); // Date in the past
  76. Utils::SpecifyConfigFile(FINAL_CONFIG_FILE);
  77. /**
  78. * Main program
  79. */
  80. $sFileName = Utils::ReadParam('file', '');
  81. $oP = new WebPage("iTop - Backoffice data loader");
  82. try
  83. {
  84. // Note: the data model must be loaded first
  85. $oDataLoader = new XMLDataLoader(FINAL_CONFIG_FILE); // When called by the wizard, the final config is not yet there
  86. if (empty($sFileName))
  87. {
  88. throw(new Exception("Missing argument 'file'"));
  89. }
  90. if (!file_exists($sFileName))
  91. {
  92. throw(new Exception("File $sFileName does not exist"));
  93. }
  94. SetMemoryLimit($oP);
  95. // The XMLDataLoader constructor has initialized the DB, let's start a transaction
  96. CMDBSource::Query('SET AUTOCOMMIT=0');
  97. CMDBSource::Query('BEGIN WORK');
  98. $oChange = MetaModel::NewObject("CMDBChange");
  99. $oChange->Set("date", time());
  100. $oChange->Set("userinfo", "Initialization");
  101. $iChangeId = $oChange->DBInsert();
  102. $oP->p("Starting data load.");
  103. $oDataLoader->StartSession($oChange);
  104. $oDataLoader->LoadFile($sFileName);
  105. $oP->p("Ending data load session");
  106. if ($oDataLoader->EndSession(true /* strict */))
  107. {
  108. $iCountCreated = $oDataLoader->GetCountCreated();
  109. CMDBSource::Query('COMMIT');
  110. $oP->p("Data successfully written into the DB: $iCountCreated objects created");
  111. }
  112. else
  113. {
  114. CMDBSource::Query('ROLLBACK');
  115. $oP->p("Some issues have been encountered, changes will not be recorded, please review the source data");
  116. $aErrors = $oDataLoader->GetErrors();
  117. if (count($aErrors) > 0)
  118. {
  119. $oP->p('Errors ('.count($aErrors).')');
  120. foreach ($aErrors as $sMsg)
  121. {
  122. $oP->p(' * '.$sMsg);
  123. }
  124. }
  125. $aWarnings = $oDataLoader->GetWarnings();
  126. if (count($aWarnings) > 0)
  127. {
  128. $oP->p('Warnings ('.count($aWarnings).')');
  129. foreach ($aWarnings as $sMsg)
  130. {
  131. $oP->p(' * '.$sMsg);
  132. }
  133. }
  134. }
  135. }
  136. catch(Exception $e)
  137. {
  138. $oP->p("An error happened while loading the data: ".$e->getMessage());
  139. $oP->p("Aborting (no data written)...");
  140. CMDBSource::Query('ROLLBACK');
  141. }
  142. if (function_exists('memory_get_peak_usage'))
  143. {
  144. $oP->p("Information: memory peak usage: ".memory_get_peak_usage());
  145. }
  146. $oP->Output();
  147. ?>