cmdbobject.class.inc.php 15 KB

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