cmdbobject.class.inc.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. * Class cmdbObject
  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. * cmdbObjectClass
  26. * the file to include, then the core is yours
  27. *
  28. * @package iTopORM
  29. */
  30. require_once('coreexception.class.inc.php');
  31. require_once('config.class.inc.php');
  32. require_once('log.class.inc.php');
  33. require_once('dict.class.inc.php');
  34. require_once('attributedef.class.inc.php');
  35. require_once('filterdef.class.inc.php');
  36. require_once('stimulus.class.inc.php');
  37. require_once('valuesetdef.class.inc.php');
  38. require_once('MyHelpers.class.inc.php');
  39. require_once('expression.class.inc.php');
  40. require_once('cmdbsource.class.inc.php');
  41. require_once('sqlquery.class.inc.php');
  42. require_once('oql/oqlquery.class.inc.php');
  43. require_once('oql/oqlexception.class.inc.php');
  44. require_once('oql/oql-parser.php');
  45. require_once('oql/oql-lexer.php');
  46. require_once('oql/oqlinterpreter.class.inc.php');
  47. require_once('dbobject.class.php');
  48. require_once('dbobjectsearch.class.php');
  49. require_once('dbobjectset.class.php');
  50. // db change tracking data model
  51. require_once('cmdbchange.class.inc.php');
  52. require_once('cmdbchangeop.class.inc.php');
  53. // customization data model
  54. // Romain: temporary moved into application.inc.php (see explanations there)
  55. //require_once('trigger.class.inc.php');
  56. //require_once('action.class.inc.php');
  57. // application log
  58. // Romain: temporary moved into application.inc.php (see explanations there)
  59. //require_once('event.class.inc.php');
  60. require_once('csvparser.class.inc.php');
  61. require_once('bulkchange.class.inc.php');
  62. //
  63. // Error handling
  64. // To be finalized... or removed ?
  65. //
  66. function cmdbErrorHandler($errno, $errstr, $errfile, $errline)
  67. {
  68. // font-family: Courier-New, Courier, Arial, Helevtica;
  69. $sErrorStyle = "
  70. background-color: #ffaaaa;
  71. color: #000000;
  72. border: 1px dashed #000000;
  73. padding: 0.25em;
  74. margin-top: 1em;
  75. ";
  76. $sCallStackStyle = "
  77. font-size: smaller;
  78. background-color: #ffcccc;
  79. color: #000000;
  80. border: 1px dashed #000000;
  81. padding: 0.25em;
  82. margin-top: 1em;
  83. ";
  84. switch ($errno)
  85. {
  86. case E_USER_ERROR:
  87. case E_ERROR:
  88. echo "<div style=\"$sErrorStyle\">\n";
  89. echo "<b>Error</b> [$errno] $errstr<br />\n";
  90. echo "<div style=\"$sCallStackStyle\">\n";
  91. MyHelpers::dump_callstack(1);
  92. echo "</div>\n";
  93. echo "Hereafter the biz model internals:<br />\n";
  94. echo "<pre>\n";
  95. MetaModel::static_var_dump();
  96. echo "</pre>\n";
  97. echo "Aborting...<br />\n";
  98. echo "</div>\n";
  99. exit(1);
  100. break;
  101. case E_USER_WARNING:
  102. case E_WARNING:
  103. echo "<div style=\"background-color:#FAA;\">\n";
  104. echo "<b>Warning</b> [$errno] $errstr<br />\n";
  105. echo "<div style=\"background-color:#FCC;\">\n";
  106. MyHelpers::dump_callstack(1);
  107. echo "</div>\n";
  108. echo "</div>\n";
  109. break;
  110. case E_USER_NOTICE:
  111. case E_NOTICE:
  112. echo "<div style=\"background-color:#FAA;\">\n";
  113. echo "<b>Notice</b> [$errno] $errstr<br />\n";
  114. echo "<div style=\"background-color:#FCC;\">\n";
  115. MyHelpers::dump_callstack(1);
  116. echo "</div>\n";
  117. echo "</div>\n";
  118. break;
  119. default:
  120. echo "Unknown error type: [$errno] $errstr<br />\n";
  121. MyHelpers::dump_callstack(1);
  122. break;
  123. }
  124. }
  125. error_reporting(E_ALL | E_STRICT);
  126. //set_error_handler("cmdbErrorHandler");
  127. //
  128. //
  129. //
  130. /**
  131. * A persistent object, which changes are accurately recorded
  132. *
  133. * @package iTopORM
  134. */
  135. abstract class CMDBObject extends DBObject
  136. {
  137. protected $m_datCreated;
  138. protected $m_datUpdated;
  139. protected static $m_oCurrChange = null;
  140. private function RecordObjCreation(CMDBChange $oChange)
  141. {
  142. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpCreate");
  143. $oMyChangeOp->Set("change", $oChange->GetKey());
  144. $oMyChangeOp->Set("objclass", get_class($this));
  145. $oMyChangeOp->Set("objkey", $this->GetKey());
  146. $iId = $oMyChangeOp->DBInsertNoReload();
  147. }
  148. private function RecordObjDeletion(CMDBChange $oChange, $objkey)
  149. {
  150. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpDelete");
  151. $oMyChangeOp->Set("change", $oChange->GetKey());
  152. $oMyChangeOp->Set("objclass", get_class($this));
  153. $oMyChangeOp->Set("objkey", $objkey);
  154. $iId = $oMyChangeOp->DBInsertNoReload();
  155. }
  156. private function RecordAttChanges(CMDBChange $oChange, array $aValues, array $aOrigValues)
  157. {
  158. // $aValues is an array of $sAttCode => $value
  159. //
  160. foreach ($aValues as $sAttCode=> $value)
  161. {
  162. $oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
  163. if ($oAttDef->IsLinkSet()) continue; // #@# temporary
  164. if ($oAttDef instanceOf AttributeBlob)
  165. {
  166. // Data blobs
  167. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeBlob");
  168. $oMyChangeOp->Set("change", $oChange->GetKey());
  169. $oMyChangeOp->Set("objclass", get_class($this));
  170. $oMyChangeOp->Set("objkey", $this->GetKey());
  171. $oMyChangeOp->Set("attcode", $sAttCode);
  172. if (array_key_exists($sAttCode, $aOrigValues))
  173. {
  174. $original = $aOrigValues[$sAttCode];
  175. }
  176. else
  177. {
  178. $original = new ormDocument();
  179. }
  180. $oMyChangeOp->Set("prevdata", $original);
  181. $iId = $oMyChangeOp->DBInsertNoReload();
  182. }
  183. elseif ($oAttDef instanceOf AttributeText)
  184. {
  185. // Data blobs
  186. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeText");
  187. $oMyChangeOp->Set("change", $oChange->GetKey());
  188. $oMyChangeOp->Set("objclass", get_class($this));
  189. $oMyChangeOp->Set("objkey", $this->GetKey());
  190. $oMyChangeOp->Set("attcode", $sAttCode);
  191. if (array_key_exists($sAttCode, $aOrigValues))
  192. {
  193. $original = $aOrigValues[$sAttCode];
  194. }
  195. else
  196. {
  197. $original = null;
  198. }
  199. $oMyChangeOp->Set("prevdata", $original);
  200. $iId = $oMyChangeOp->DBInsertNoReload();
  201. }
  202. else
  203. {
  204. // Scalars
  205. //
  206. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpSetAttributeScalar");
  207. $oMyChangeOp->Set("change", $oChange->GetKey());
  208. $oMyChangeOp->Set("objclass", get_class($this));
  209. $oMyChangeOp->Set("objkey", $this->GetKey());
  210. $oMyChangeOp->Set("attcode", $sAttCode);
  211. if (array_key_exists($sAttCode, $aOrigValues))
  212. {
  213. $sOriginalValue = $aOrigValues[$sAttCode];
  214. }
  215. else
  216. {
  217. $sOriginalValue = 'undefined';
  218. }
  219. $oMyChangeOp->Set("oldvalue", $sOriginalValue);
  220. $oMyChangeOp->Set("newvalue", $value);
  221. $iId = $oMyChangeOp->DBInsertNoReload();
  222. }
  223. }
  224. }
  225. public function DBInsert()
  226. {
  227. if(!is_object(self::$m_oCurrChange))
  228. {
  229. throw new CoreException("DBInsert() could not be used here, please use DBInsertTracked() instead");
  230. }
  231. return $this->DBInsertTracked_Internal();
  232. }
  233. public function DBInsertTracked(CMDBChange $oChange)
  234. {
  235. self::$m_oCurrChange = $oChange;
  236. $ret = $this->DBInsertTracked_Internal();
  237. self::$m_oCurrChange = null;
  238. return $ret;
  239. }
  240. public function DBInsertTrackedNoReload(CMDBChange $oChange)
  241. {
  242. self::$m_oCurrChange = $oChange;
  243. $ret = $this->DBInsertTracked_Internal(true);
  244. self::$m_oCurrChange = null;
  245. return $ret;
  246. }
  247. protected function DBInsertTracked_Internal($bDoNotReload = false)
  248. {
  249. if ($bDoNotReload)
  250. {
  251. $ret = parent::DBInsertNoReload();
  252. }
  253. else
  254. {
  255. $ret = parent::DBInsert();
  256. }
  257. $this->RecordObjCreation(self::$m_oCurrChange);
  258. return $ret;
  259. }
  260. public function DBClone($newKey = null)
  261. {
  262. if(!self::$m_oCurrChange)
  263. {
  264. throw new CoreException("DBClone() could not be used here, please use DBCloneTracked() instead");
  265. }
  266. return $this->DBCloneTracked_Internal();
  267. }
  268. public function DBCloneTracked(CMDBChange $oChange, $newKey = null)
  269. {
  270. self::$m_oCurrChange = $oChange;
  271. $this->DBCloneTracked_Internal($newKey);
  272. self::$m_oCurrChange = null;
  273. }
  274. protected function DBCloneTracked_Internal($newKey = null)
  275. {
  276. $newKey = parent::DBClone($newKey);
  277. $oClone = MetaModel::GetObject(get_class($this), $newKey);
  278. $oClone->RecordObjCreation(self::$m_oCurrChange);
  279. return $newKey;
  280. }
  281. public function DBUpdate()
  282. {
  283. if(!self::$m_oCurrChange)
  284. {
  285. throw new CoreException("DBUpdate() could not be used here, please use DBUpdateTracked() instead");
  286. }
  287. return $this->DBUpdateTracked_internal();
  288. }
  289. public function DBUpdateTracked(CMDBChange $oChange)
  290. {
  291. self::$m_oCurrChange = $oChange;
  292. $this->DBUpdateTracked_Internal();
  293. self::$m_oCurrChange = null;
  294. }
  295. protected function DBUpdateTracked_Internal()
  296. {
  297. // Copy the changes list before the update (the list should be reset afterwards)
  298. $aChanges = $this->ListChanges();
  299. if (count($aChanges) == 0)
  300. {
  301. //throw new CoreWarning("Attempting to update an unchanged object");
  302. return;
  303. }
  304. // Save the original values (will be reset to the new values when the object get written to the DB)
  305. $aOriginalValues = $this->m_aOrigValues;
  306. $ret = parent::DBUpdate();
  307. $this->RecordAttChanges(self::$m_oCurrChange, $aChanges, $aOriginalValues);
  308. return $ret;
  309. }
  310. public function DBDelete()
  311. {
  312. if(!self::$m_oCurrChange)
  313. {
  314. throw new CoreException("DBDelete() could not be used here, please use DBDeleteTracked() instead");
  315. }
  316. return $this->DBDeleteTracked_Internal();
  317. }
  318. public function DBDeleteTracked(CMDBChange $oChange)
  319. {
  320. self::$m_oCurrChange = $oChange;
  321. $this->DBDeleteTracked_Internal();
  322. self::$m_oCurrChange = null;
  323. }
  324. protected function DBDeleteTracked_Internal()
  325. {
  326. $prevkey = $this->GetKey();
  327. $ret = parent::DBDelete();
  328. $this->RecordObjDeletion(self::$m_oCurrChange, $prevkey);
  329. return $ret;
  330. }
  331. public static function BulkDelete(DBObjectSearch $oFilter)
  332. {
  333. if(!self::$m_oCurrChange)
  334. {
  335. throw new CoreException("BulkDelete() could not be used here, please use BulkDeleteTracked() instead");
  336. }
  337. return $this->BulkDeleteTracked_Internal($oFilter);
  338. }
  339. public static function BulkDeleteTracked(CMDBChange $oChange, DBObjectSearch $oFilter)
  340. {
  341. self::$m_oCurrChange = $oChange;
  342. $this->BulkDeleteTracked_Internal($oFilter);
  343. self::$m_oCurrChange = null;
  344. }
  345. protected static function BulkDeleteTracked_Internal(DBObjectSearch $oFilter)
  346. {
  347. throw new CoreWarning("Change tracking not tested for bulk operations");
  348. // Get the list of objects to delete (and record data before deleting the DB records)
  349. $oObjSet = new CMDBObjectSet($oFilter);
  350. $aObjAndKeys = array(); // array of id=>object
  351. while ($oItem = $oObjSet->Fetch())
  352. {
  353. $aObjAndKeys[$oItem->GetKey()] = $oItem;
  354. }
  355. $oObjSet->FreeResult();
  356. // Delete in one single efficient query
  357. $ret = parent::BulkDelete($oFilter);
  358. // Record... in many queries !!!
  359. foreach($aObjAndKeys as $prevkey=>$oItem)
  360. {
  361. $oItem->RecordObjDeletion(self::$m_oCurrChange, $prevkey);
  362. }
  363. return $ret;
  364. }
  365. public static function BulkUpdate(DBObjectSearch $oFilter, array $aValues)
  366. {
  367. if(!self::$m_oCurrChange)
  368. {
  369. throw new CoreException("BulkUpdate() could not be used here, please use BulkUpdateTracked() instead");
  370. }
  371. return $this->BulkUpdateTracked_Internal($oFilter, $aValues);
  372. }
  373. public static function BulkUpdateTracked(CMDBChange $oChange, DBObjectSearch $oFilter, array $aValues)
  374. {
  375. self::$m_oCurrChange = $oChange;
  376. $this->BulkUpdateTracked_Internal($oFilter, $aValues);
  377. self::$m_oCurrChange = null;
  378. }
  379. protected static function BulkUpdateTracked_Internal(DBObjectSearch $oFilter, array $aValues)
  380. {
  381. // $aValues is an array of $sAttCode => $value
  382. // Get the list of objects to update (and load it before doing the change)
  383. $oObjSet = new CMDBObjectSet($oFilter);
  384. $oObjSet->Load();
  385. // Keep track of the previous values (will be overwritten when the objects are synchronized with the DB)
  386. $aOriginalValues = array();
  387. $oObjSet->Rewind();
  388. while ($oItem = $oObjSet->Fetch())
  389. {
  390. $aOriginalValues[$oItem->GetKey()] = $oItem->m_aOrigValues;
  391. }
  392. // Update in one single efficient query
  393. $ret = parent::BulkUpdate($oFilter, $aValues);
  394. // Record... in many queries !!!
  395. $oObjSet->Rewind();
  396. while ($oItem = $oObjSet->Fetch())
  397. {
  398. $aChangedValues = $oItem->ListChangedValues($aValues);
  399. $oItem->RecordAttChanges(self::$m_oCurrChange, $aChangedValues, $aOriginalValues[$oItem->GetKey()]);
  400. }
  401. return $ret;
  402. }
  403. }
  404. /**
  405. * TODO: investigate how to get rid of this class that was made to workaround some language limitation... or a poor design!
  406. *
  407. * @package iTopORM
  408. */
  409. class CMDBObjectSet extends DBObjectSet
  410. {
  411. // this is the public interface (?)
  412. // I have to define those constructors here... :-(
  413. // just to get the right object class in return.
  414. // I have to think again to those things: maybe it will work fine if a have a constructor define here (?)
  415. static public function FromScratch($sClass)
  416. {
  417. $oFilter = new CMDBSearchFilter($sClass);
  418. $oRetSet = new CMDBObjectSet($oFilter); // THE ONLY DIFF IS HERE
  419. // NOTE: THIS DOES NOT WORK IF m_bLoaded is private...
  420. // BUT IT THAT CASE YOU DO NOT GET ANY ERROR !!!!!
  421. $oRetSet->m_bLoaded = true; // no DB load
  422. return $oRetSet;
  423. }
  424. static public function FromArray($sClass, $aObjects)
  425. {
  426. $oFilter = new CMDBSearchFilter($sClass);
  427. $oRetSet = new CMDBObjectSet($oFilter); // THE ONLY DIFF IS HERE
  428. // NOTE: THIS DOES NOT WORK IF m_bLoaded is private...
  429. // BUT IT THAT CASE YOU DO NOT GET ANY ERROR !!!!!
  430. $oRetSet->m_bLoaded = true; // no DB load
  431. $oRetSet->AddObjectArray($aObjects);
  432. return $oRetSet;
  433. }
  434. static public function FromArrayAssoc($aClasses, $aObjects)
  435. {
  436. // In a perfect world, we should create a complete tree of DBObjectSearch,
  437. // but as we lack most of the information related to the objects,
  438. // let's create one search definition
  439. $sClass = reset($aClasses);
  440. $sAlias = key($aClasses);
  441. $oFilter = new CMDBSearchFilter($sClass, $sAlias);
  442. $oRetSet = new CMDBObjectSet($oFilter);
  443. $oRetSet->m_bLoaded = true; // no DB load
  444. foreach($aObjects as $rowIndex => $aObjectsByClassAlias)
  445. {
  446. $oRetSet->AddObjectExtended($aObjectsByClassAlias);
  447. }
  448. return $oRetSet;
  449. }
  450. }
  451. /**
  452. * TODO: investigate how to get rid of this class that was made to workaround some language limitation... or a poor design!
  453. *
  454. * @package iTopORM
  455. */
  456. class CMDBSearchFilter extends DBObjectSearch
  457. {
  458. // this is the public interface (?)
  459. }
  460. ?>