backoffice.dataloader.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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('../approot.inc.php');
  31. require_once(APPROOT.'/application/utils.inc.php');
  32. require_once(APPROOT."/application/nicewebpage.class.inc.php");
  33. // required because the class xmldataloader is reporting errors in the setup.log file
  34. require_once(APPROOT.'/setup/setuppage.class.inc.php');
  35. function SetMemoryLimit($oP)
  36. {
  37. $sMemoryLimit = trim(ini_get('memory_limit'));
  38. if (empty($sMemoryLimit))
  39. {
  40. // On some PHP installations, memory_limit does not exist as a PHP setting!
  41. // (encountered on a 5.2.0 under Windows)
  42. // In that case, ini_set will not work, let's keep track of this and proceed with the data load
  43. $oP->p("No memory limit has been defined in this instance of PHP");
  44. }
  45. else
  46. {
  47. // Check that the limit will allow us to load the data
  48. //
  49. $iMemoryLimit = utils::ConvertToBytes($sMemoryLimit);
  50. if ($iMemoryLimit < SAFE_MINIMUM_MEMORY)
  51. {
  52. if (ini_set('memory_limit', SAFE_MINIMUM_MEMORY) === FALSE)
  53. {
  54. $oP->p("memory_limit is too small: $iMemoryLimit and can not be increased by the script itself.");
  55. }
  56. else
  57. {
  58. $oP->p("memory_limit increased from $iMemoryLimit to ".SAFE_MINIMUM_MEMORY.".");
  59. }
  60. }
  61. }
  62. }
  63. ////////////////////////////////////////////////////////////////////////////////
  64. //
  65. // Main
  66. //
  67. ////////////////////////////////////////////////////////////////////////////////
  68. require_once(APPROOT.'/core/config.class.inc.php');
  69. require_once(APPROOT.'/core/log.class.inc.php');
  70. require_once(APPROOT.'/core/kpi.class.inc.php');
  71. require_once(APPROOT.'/core/cmdbsource.class.inc.php');
  72. require_once(APPROOT.'/setup/xmldataloader.class.inc.php');
  73. define('FINAL_CONFIG_FILE', APPROOT.'/config-itop.php');
  74. // Never cache this page
  75. header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  76. header("Expires: Fri, 17 Jul 1970 05:00:00 GMT"); // Date in the past
  77. Utils::SpecifyConfigFile(FINAL_CONFIG_FILE);
  78. /**
  79. * Main program
  80. */
  81. $sFileName = Utils::ReadParam('file', '');
  82. $oP = new WebPage("iTop - Backoffice data loader");
  83. try
  84. {
  85. // Note: the data model must be loaded first
  86. $oDataLoader = new XMLDataLoader(FINAL_CONFIG_FILE); // When called by the wizard, the final config is not yet there
  87. if (empty($sFileName))
  88. {
  89. throw(new Exception("Missing argument 'file'"));
  90. }
  91. if (!file_exists($sFileName))
  92. {
  93. throw(new Exception("File $sFileName does not exist"));
  94. }
  95. SetMemoryLimit($oP);
  96. // The XMLDataLoader constructor has initialized the DB, let's start a transaction
  97. CMDBSource::Query('SET AUTOCOMMIT=0');
  98. CMDBSource::Query('BEGIN WORK');
  99. $oChange = MetaModel::NewObject("CMDBChange");
  100. $oChange->Set("date", time());
  101. $oChange->Set("userinfo", "Initialization");
  102. $iChangeId = $oChange->DBInsert();
  103. $oP->p("Starting data load.");
  104. $oDataLoader->StartSession($oChange);
  105. $oDataLoader->LoadFile($sFileName);
  106. $oP->p("Ending data load session");
  107. if ($oDataLoader->EndSession(true /* strict */))
  108. {
  109. $iCountCreated = $oDataLoader->GetCountCreated();
  110. CMDBSource::Query('COMMIT');
  111. $oP->p("Data successfully written into the DB: $iCountCreated objects created");
  112. }
  113. else
  114. {
  115. CMDBSource::Query('ROLLBACK');
  116. $oP->p("Some issues have been encountered, changes will not be recorded, please review the source data");
  117. $aErrors = $oDataLoader->GetErrors();
  118. if (count($aErrors) > 0)
  119. {
  120. $oP->p('Errors ('.count($aErrors).')');
  121. foreach ($aErrors as $sMsg)
  122. {
  123. $oP->p(' * '.$sMsg);
  124. }
  125. }
  126. $aWarnings = $oDataLoader->GetWarnings();
  127. if (count($aWarnings) > 0)
  128. {
  129. $oP->p('Warnings ('.count($aWarnings).')');
  130. foreach ($aWarnings as $sMsg)
  131. {
  132. $oP->p(' * '.$sMsg);
  133. }
  134. }
  135. }
  136. }
  137. catch(Exception $e)
  138. {
  139. $oP->p("An error happened while loading the data: ".$e->getMessage());
  140. $oP->p("Aborting (no data written)...");
  141. CMDBSource::Query('ROLLBACK');
  142. }
  143. if (function_exists('memory_get_peak_usage'))
  144. {
  145. $oP->p("Information: memory peak usage: ".memory_get_peak_usage());
  146. }
  147. $oP->Output();
  148. ?>