deletionplan.class.inc.php 7.2 KB

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