deletionplan.class.inc.php 7.0 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. foreach($this->m_aToUpdate as $sClass => $aToUpdate)
  100. {
  101. foreach($aToUpdate as $iId => $aData)
  102. {
  103. set_time_limit(5);
  104. $this->m_iToUpdate++;
  105. $oObject = $aData['to_reset'];
  106. $aExtKeyLabels = array();
  107. foreach ($aData['attributes'] as $sRemoteExtKey => $aRemoteAttDef)
  108. {
  109. $oObject->Set($sRemoteExtKey, $aData['values'][$sRemoteExtKey]);
  110. $aExtKeyLabels[] = $aRemoteAttDef->GetLabel();
  111. }
  112. $this->m_aToUpdate[$sClass][$iId]['attributes_list'] = implode(', ', $aExtKeyLabels);
  113. list($bRes, $aIssues, $bSecurityIssues) = $oObject->CheckToWrite();
  114. if (!$bRes)
  115. {
  116. $this->m_aToUpdate[$sClass][$iId]['issue'] = implode(', ', $aIssues);
  117. $this->m_bFoundStopper = true;
  118. if ($bSecurityIssues)
  119. {
  120. $this->m_aToUpdate[$sClass][$iId]['issue_security'] = true;
  121. $this->m_bFoundSecurityIssue = true;
  122. }
  123. }
  124. }
  125. }
  126. set_time_limit($iPreviousTimeLimit);
  127. }
  128. public function GetIssues()
  129. {
  130. $aIssues = array();
  131. foreach ($this->m_aToDelete as $sClass => $aToDelete)
  132. {
  133. foreach ($aToDelete as $iId => $aData)
  134. {
  135. if (isset($aData['issue']))
  136. {
  137. $aIssues[] = $aData['issue'];
  138. }
  139. }
  140. }
  141. foreach ($this->m_aToUpdate as $sClass => $aToUpdate)
  142. {
  143. foreach ($aToUpdate as $iId => $aData)
  144. {
  145. if (isset($aData['issue']))
  146. {
  147. $aIssues[] = $aData['issue'];
  148. }
  149. }
  150. }
  151. return $aIssues;
  152. }
  153. public function ListDeletes()
  154. {
  155. return $this->m_aToDelete;
  156. }
  157. public function ListUpdates()
  158. {
  159. return $this->m_aToUpdate;
  160. }
  161. public function GetTargetCount()
  162. {
  163. return $this->m_iToDelete + $this->m_iToUpdate;
  164. }
  165. public function FoundStopper()
  166. {
  167. return $this->m_bFoundStopper;
  168. }
  169. public function FoundSecurityIssue()
  170. {
  171. return $this->m_bFoundSecurityIssue;
  172. }
  173. public function FoundManualOperation()
  174. {
  175. return $this->m_bFoundManualOperation;
  176. }
  177. public function FoundManualDelete()
  178. {
  179. return $this->m_bFoundManualDelete;
  180. }
  181. public function FoundManualUpdate()
  182. {
  183. }
  184. public function AddToDelete($oObject, $iDeletionMode = null)
  185. {
  186. if (is_null($iDeletionMode))
  187. {
  188. $bRequestedExplicitely = true;
  189. $iDeletionMode = DEL_AUTO;
  190. }
  191. else
  192. {
  193. $bRequestedExplicitely = false;
  194. }
  195. $sClass = get_class($oObject);
  196. $iId = $oObject->GetKey();
  197. if (isset($this->m_aToUpdate[$sClass][$iId]))
  198. {
  199. unset($this->m_aToUpdate[$sClass][$iId]);
  200. }
  201. if (isset($this->m_aToDelete[$sClass][$iId]))
  202. {
  203. if ($this->m_aToDelete[$sClass][$iId]['requested_explicitely'])
  204. {
  205. // No change: let it in mode DEL_AUTO
  206. }
  207. else
  208. {
  209. $iPrevDeletionMode = $this->m_aToDelete[$sClass][$iId]['mode'];
  210. $iNewDeletionMode = self::$m_aModeUpdate[$iPrevDeletionMode][$iDeletionMode];
  211. $this->m_aToDelete[$sClass][$iId]['mode'] = $iNewDeletionMode;
  212. if ($bRequestedExplicitely)
  213. {
  214. // This object was in the root list
  215. $this->m_aToDelete[$sClass][$iId]['requested_explicitely'] = true;
  216. $this->m_aToDelete[$sClass][$iId]['mode'] = DEL_AUTO;
  217. }
  218. }
  219. }
  220. else
  221. {
  222. $this->m_aToDelete[$sClass][$iId] = array(
  223. 'to_delete' => $oObject,
  224. 'mode' => $iDeletionMode,
  225. 'requested_explicitely' => $bRequestedExplicitely,
  226. );
  227. }
  228. }
  229. public function SetDeletionIssues($oObject, $aIssues, $bSecurityIssue)
  230. {
  231. if (count($aIssues) > 0)
  232. {
  233. $sClass = get_class($oObject);
  234. $iId = $oObject->GetKey();
  235. $this->m_aToDelete[$sClass][$iId]['issue'] = implode(', ', $aIssues);
  236. if ($bSecurityIssue)
  237. {
  238. $this->m_aToDelete[$sClass][$iId]['issue_security'] = true;
  239. }
  240. }
  241. }
  242. public function AddToUpdate($oObject, $oAttDef, $value = 0)
  243. {
  244. $sClass = get_class($oObject);
  245. $iId = $oObject->GetKey();
  246. if (isset($this->m_aToDelete[$sClass][$iId]))
  247. {
  248. // skip... it should be deleted anyhow !
  249. }
  250. else
  251. {
  252. if (!isset($this->m_aToUpdate[$sClass][$iId]))
  253. {
  254. $this->m_aToUpdate[$sClass][$iId] = array(
  255. 'to_reset' => $oObject,
  256. );
  257. }
  258. $this->m_aToUpdate[$sClass][$iId]['attributes'][$oAttDef->GetCode()] = $oAttDef;
  259. $this->m_aToUpdate[$sClass][$iId]['values'][$oAttDef->GetCode()] = $value;
  260. }
  261. }
  262. }
  263. ?>