backoffice.dataloader.php 5.0 KB

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