deletionplan.class.inc.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. // Copyright (C) 2010-2013 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. * Algorithm to delete object(s) and maintain data integrity
  20. *
  21. * @copyright Copyright (C) 2010-2013 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. class DeleteException extends CoreException
  25. {
  26. }
  27. /**
  28. * Deletion plan (other objects to be deleted/modified, eventual issues, etc.)
  29. *
  30. * @package iTopORM
  31. */
  32. class DeletionPlan
  33. {
  34. //protected $m_aIssues;
  35. protected $m_bFoundStopper;
  36. protected $m_bFoundSecurityIssue;
  37. protected $m_bFoundManualDelete;
  38. protected $m_bFoundManualOperation;
  39. protected $m_iToDelete;
  40. protected $m_iToUpdate;
  41. protected $m_aToDelete;
  42. protected $m_aToUpdate;
  43. protected static $m_aModeUpdate = array(
  44. DEL_SILENT => array(
  45. DEL_SILENT => DEL_SILENT,
  46. DEL_AUTO => DEL_AUTO,
  47. DEL_MANUAL => DEL_MANUAL
  48. ),
  49. DEL_MANUAL => array(
  50. DEL_SILENT => DEL_MANUAL,
  51. DEL_AUTO => DEL_AUTO,
  52. DEL_MANUAL => DEL_MANUAL
  53. ),
  54. DEL_AUTO => array(
  55. DEL_SILENT => DEL_AUTO,
  56. DEL_AUTO => DEL_AUTO,
  57. DEL_MANUAL => DEL_AUTO
  58. )
  59. );
  60. public function __construct()
  61. {
  62. $this->m_iToDelete = 0;
  63. $this->m_iToUpdate = 0;
  64. $this->m_aToDelete = array();
  65. $this->m_aToUpdate = array();
  66. $this->m_bFoundStopper = false;
  67. $this->m_bFoundSecurityIssue = false;
  68. $this->m_bFoundManualDelete = false;
  69. $this->m_bFoundManualOperation = false;
  70. }
  71. public function ComputeResults()
  72. {
  73. $this->m_iToDelete = 0;
  74. $this->m_iToUpdate = 0;
  75. foreach($this->m_aToDelete as $sClass => $aToDelete)
  76. {
  77. foreach($aToDelete as $iId => $aData)
  78. {
  79. $this->m_iToDelete++;
  80. if (isset($aData['issue']))
  81. {
  82. $this->m_bFoundStopper = true;
  83. $this->m_bFoundManualOperation = true;
  84. if (isset($aData['issue_security']))
  85. {
  86. $this->m_bFoundSecurityIssue = true;
  87. }
  88. }
  89. if ($aData['mode'] == DEL_MANUAL)
  90. {
  91. $this->m_bFoundStopper = true;
  92. $this->m_bFoundManualDelete = true;
  93. }
  94. }
  95. }
  96. // Getting and setting time limit are not symetric:
  97. // www.php.net/manual/fr/function.set-time-limit.php#72305
  98. $iPreviousTimeLimit = ini_get('max_execution_time');
  99. $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop');
  100. foreach($this->m_aToUpdate as $sClass => $aToUpdate)
  101. {
  102. foreach($aToUpdate as $iId => $aData)
  103. {
  104. set_time_limit($iLoopTimeLimit);
  105. $this->m_iToUpdate++;
  106. $oObject = $aData['to_reset'];
  107. $aExtKeyLabels = array();
  108. foreach ($aData['attributes'] as $sRemoteExtKey => $aRemoteAttDef)
  109. {
  110. $oObject->Set($sRemoteExtKey, $aData['values'][$sRemoteExtKey]);
  111. $aExtKeyLabels[] = $aRemoteAttDef->GetLabel();
  112. }
  113. $this->m_aToUpdate[$sClass][$iId]['attributes_list'] = implode(', ', $aExtKeyLabels);
  114. list($bRes, $aIssues, $bSecurityIssues) = $oObject->CheckToWrite();
  115. if (!$bRes)
  116. {
  117. $this->m_aToUpdate[$sClass][$iId]['issue'] = implode(', ', $aIssues);
  118. $this->m_bFoundStopper = true;
  119. if ($bSecurityIssues)
  120. {
  121. $this->m_aToUpdate[$sClass][$iId]['issue_security'] = true;
  122. $this->m_bFoundSecurityIssue = true;
  123. }
  124. }
  125. }
  126. }
  127. set_time_limit($iPreviousTimeLimit);
  128. }
  129. public function GetIssues()
  130. {
  131. $aIssues = array();
  132. foreach ($this->m_aToDelete as $sClass => $aToDelete)
  133. {
  134. foreach ($aToDelete as $iId => $aData)
  135. {
  136. if (isset($aData['issue']))
  137. {
  138. $aIssues[] = $aData['issue'];
  139. }
  140. }
  141. }
  142. foreach ($this->m_aToUpdate as $sClass => $aToUpdate)
  143. {
  144. foreach ($aToUpdate as $iId => $aData)
  145. {
  146. if (isset($aData['issue']))
  147. {
  148. $aIssues[] = $aData['issue'];
  149. }
  150. }
  151. }
  152. return $aIssues;
  153. }
  154. public function ListDeletes()
  155. {
  156. return $this->m_aToDelete;
  157. }
  158. public function ListUpdates()
  159. {
  160. return $this->m_aToUpdate;
  161. }
  162. public function GetTargetCount()
  163. {
  164. return $this->m_iToDelete + $this->m_iToUpdate;
  165. }
  166. public function FoundStopper()
  167. {
  168. return $this->m_bFoundStopper;
  169. }
  170. public function FoundSecurityIssue()
  171. {
  172. return $this->m_bFoundSecurityIssue;
  173. }
  174. public function FoundManualOperation()
  175. {
  176. return $this->m_bFoundManualOperation;
  177. }
  178. public function FoundManualDelete()
  179. {
  180. return $this->m_bFoundManualDelete;
  181. }
  182. public function FoundManualUpdate()
  183. {
  184. }
  185. public function AddToDelete($oObject, $iDeletionMode = null)
  186. {
  187. if (is_null($iDeletionMode))
  188. {
  189. $bRequestedExplicitely = true;
  190. $iDeletionMode = DEL_AUTO;
  191. }
  192. else
  193. {
  194. $bRequestedExplicitely = false;
  195. }
  196. $sClass = get_class($oObject);
  197. $iId = $oObject->GetKey();
  198. if (isset($this->m_aToUpdate[$sClass][$iId]))
  199. {
  200. unset($this->m_aToUpdate[$sClass][$iId]);
  201. }
  202. if (isset($this->m_aToDelete[$sClass][$iId]))
  203. {
  204. if ($this->m_aToDelete[$sClass][$iId]['requested_explicitely'])
  205. {
  206. // No change: let it in mode DEL_AUTO
  207. }
  208. else
  209. {
  210. $iPrevDeletionMode = $this->m_aToDelete[$sClass][$iId]['mode'];
  211. $iNewDeletionMode = self::$m_aModeUpdate[$iPrevDeletionMode][$iDeletionMode];
  212. $this->m_aToDelete[$sClass][$iId]['mode'] = $iNewDeletionMode;
  213. if ($bRequestedExplicitely)
  214. {
  215. // This object was in the root list
  216. $this->m_aToDelete[$sClass][$iId]['requested_explicitely'] = true;
  217. $this->m_aToDelete[$sClass][$iId]['mode'] = DEL_AUTO;
  218. }
  219. }
  220. }
  221. else
  222. {
  223. $this->m_aToDelete[$sClass][$iId] = array(
  224. 'to_delete' => $oObject,
  225. 'mode' => $iDeletionMode,
  226. 'requested_explicitely' => $bRequestedExplicitely,
  227. );
  228. }
  229. }
  230. public function SetDeletionIssues($oObject, $aIssues, $bSecurityIssue)
  231. {
  232. if (count($aIssues) > 0)
  233. {
  234. $sClass = get_class($oObject);
  235. $iId = $oObject->GetKey();
  236. $this->m_aToDelete[$sClass][$iId]['issue'] = implode(', ', $aIssues);
  237. if ($bSecurityIssue)
  238. {
  239. $this->m_aToDelete[$sClass][$iId]['issue_security'] = true;
  240. }
  241. }
  242. }
  243. public function AddToUpdate($oObject, $oAttDef, $value = 0)
  244. {
  245. $sClass = get_class($oObject);
  246. $iId = $oObject->GetKey();
  247. if (isset($this->m_aToDelete[$sClass][$iId]))
  248. {
  249. // skip... it should be deleted anyhow !
  250. }
  251. else
  252. {
  253. if (!isset($this->m_aToUpdate[$sClass][$iId]))
  254. {
  255. $this->m_aToUpdate[$sClass][$iId] = array(
  256. 'to_reset' => $oObject,
  257. );
  258. }
  259. $this->m_aToUpdate[$sClass][$iId]['attributes'][$oAttDef->GetCode()] = $oAttDef;
  260. $this->m_aToUpdate[$sClass][$iId]['values'][$oAttDef->GetCode()] = $value;
  261. }
  262. }
  263. }
  264. ?>