priv_sync_chunk.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Internal: synchronize part of the records - cannot be invoked separately
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. if (!defined('__DIR__')) define('__DIR__', dirname(__FILE__));
  25. require_once(__DIR__.'/../approot.inc.php');
  26. require_once(APPROOT.'/application/application.inc.php');
  27. require_once(APPROOT.'/application/webpage.class.inc.php');
  28. require_once(APPROOT.'/application/csvpage.class.inc.php');
  29. require_once(APPROOT.'/application/clipage.class.inc.php');
  30. require_once(APPROOT.'/application/startup.inc.php');
  31. function ReadMandatoryParam($oP, $sParam, $sSanitizationFilter = 'parameter')
  32. {
  33. $sValue = utils::ReadParam($sParam, null, true /* Allow CLI */, $sSanitizationFilter);
  34. if (is_null($sValue))
  35. {
  36. $oP->p("ERROR: Missing argument '$sParam'\n");
  37. exit(29);
  38. }
  39. return trim($sValue);
  40. }
  41. /////////////////////////////////
  42. // Main program
  43. if (!utils::IsModeCLI())
  44. {
  45. $oP = new WebPage(Dict::S("TitleSynchroExecution"));
  46. $oP->p("This page is used internally by iTop");
  47. $oP->output();
  48. exit -2;
  49. }
  50. $oP = new CLIPage(Dict::S("TitleSynchroExecution"));
  51. try
  52. {
  53. utils::UseParamFile();
  54. }
  55. catch(Exception $e)
  56. {
  57. $oP->p("Error: ".$e->GetMessage());
  58. $oP->output();
  59. exit -2;
  60. }
  61. // Next steps:
  62. // specific arguments: 'csvfile'
  63. //
  64. $sAuthUser = ReadMandatoryParam($oP, 'auth_user', 'raw_data');
  65. $sAuthPwd = ReadMandatoryParam($oP, 'auth_pwd', 'raw_data');
  66. if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd))
  67. {
  68. UserRights::Login($sAuthUser); // Login & set the user's language
  69. }
  70. else
  71. {
  72. $oP->p("Access restricted or wrong credentials ('$sAuthUser')");
  73. $oP->output();
  74. exit -1;
  75. }
  76. $iStepCount = ReadMandatoryParam($oP, 'step_count');
  77. $oP->p('Executing a partial synchro - step '.$iStepCount);
  78. $iSource = ReadMandatoryParam($oP, 'source');
  79. $iStatLog = ReadMandatoryParam($oP, 'log');
  80. $iChange = ReadMandatoryParam($oP, 'change');
  81. $sLastFullLoad = ReadMandatoryParam($oP, 'last_full_load', 'raw_data');
  82. $iChunkSize = ReadMandatoryParam($oP, 'chunk');
  83. $oP->p('Last full load: '.$sLastFullLoad);
  84. $oP->p('Chunk size: '.$iChunkSize);
  85. $oP->p('Source: '.$iSource);
  86. try
  87. {
  88. $oSynchroDataSource = MetaModel::GetObject('SynchroDataSource', $iSource);
  89. $oLog = MetaModel::GetObject('SynchroLog', $iStatLog);
  90. $oChange = MetaModel::GetObject('CMDBChange', $iChange);
  91. if (strlen($sLastFullLoad) > 0)
  92. {
  93. $oLastFullLoad = new DateTime($sLastFullLoad);
  94. $oSynchroExec = new SynchroExecution($oSynchroDataSource, $oLastFullLoad);
  95. }
  96. else
  97. {
  98. $oSynchroExec = new SynchroExecution($oSynchroDataSource);
  99. }
  100. if ($oSynchroExec->DoSynchronizeChunk($oLog, $oChange, $iChunkSize))
  101. {
  102. // The last line MUST follow this convention
  103. $oP->p("continue");
  104. }
  105. else
  106. {
  107. // The last line MUST follow this convention
  108. $oP->p("finished");
  109. }
  110. $oP->output();
  111. }
  112. catch(Exception $e)
  113. {
  114. $oP->p("Error: ".$e->GetMessage());
  115. $oP->add($e->getTraceAsString());
  116. $oP->output();
  117. exit(28);
  118. }
  119. ?>