modelfactory.class.inc.php 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876
  1. <?php
  2. // Copyright (C) 2010-2012 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. * ModelFactory: in-memory manipulation of the XML MetaModel
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. require_once(APPROOT.'setup/moduleinstaller.class.inc.php');
  25. /**
  26. * ModelFactoryModule: the representation of a Module (i.e. element that can be selected during the setup)
  27. * @package ModelFactory
  28. */
  29. class MFModule
  30. {
  31. protected $sId;
  32. protected $sName;
  33. protected $sVersion;
  34. protected $sRootDir;
  35. protected $sLabel;
  36. protected $aDataModels;
  37. public function __construct($sId, $sRootDir, $sLabel)
  38. {
  39. $this->sId = $sId;
  40. list($this->sName, $this->sVersion) = ModuleDiscovery::GetModuleName($sId);
  41. if (strlen($this->sVersion) == 0)
  42. {
  43. $this->sVersion = '1.0.0';
  44. }
  45. $this->sRootDir = $sRootDir;
  46. $this->sLabel = $sLabel;
  47. $this->aDataModels = array();
  48. // Scan the module's root directory to find the datamodel(*).xml files
  49. if ($hDir = opendir($sRootDir))
  50. {
  51. // This is the correct way to loop over the directory. (according to the documentation)
  52. while (($sFile = readdir($hDir)) !== false)
  53. {
  54. if (preg_match('/^datamodel(.*)\.xml$/i', $sFile, $aMatches))
  55. {
  56. $this->aDataModels[] = $this->sRootDir.'/'.$aMatches[0];
  57. }
  58. }
  59. closedir($hDir);
  60. }
  61. }
  62. public function GetId()
  63. {
  64. return $this->sId;
  65. }
  66. public function GetName()
  67. {
  68. return $this->sName;
  69. }
  70. public function GetVersion()
  71. {
  72. return $this->sVersion;
  73. }
  74. public function GetLabel()
  75. {
  76. return $this->sLabel;
  77. }
  78. public function GetRootDir()
  79. {
  80. return $this->sRootDir;
  81. }
  82. public function GetModuleDir()
  83. {
  84. return basename($this->sRootDir);
  85. }
  86. public function GetDataModelFiles()
  87. {
  88. return $this->aDataModels;
  89. }
  90. /**
  91. * List all classes in this module
  92. */
  93. public function ListClasses()
  94. {
  95. return array();
  96. }
  97. }
  98. /**
  99. * ModelFactory: the class that manages the in-memory representation of the XML MetaModel
  100. * @package ModelFactory
  101. */
  102. class ModelFactory
  103. {
  104. protected $aRootDirs;
  105. protected $oDOMDocument;
  106. protected $oRoot;
  107. protected $oModules;
  108. protected $oClasses;
  109. protected $oMenus;
  110. static protected $aLoadedClasses;
  111. static protected $aWellKnownParents = array('DBObject', 'CMDBObject','cmdbAbstractObject');
  112. // static protected $aWellKnownMenus = array('DataAdministration', 'Catalogs', 'ConfigManagement', 'Contact', 'ConfigManagementCI', 'ConfigManagement:Shortcuts', 'ServiceManagement');
  113. static protected $aLoadedModules;
  114. static protected $aLoadErrors;
  115. public function __construct($aRootDirs, $aRootNodeExtensions = array())
  116. {
  117. $this->aRootDirs = $aRootDirs;
  118. $this->oDOMDocument = new MFDocument();
  119. $this->oRoot = $this->oDOMDocument->CreateElement('itop_design');
  120. $this->oRoot->setAttribute('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance");
  121. $this->oDOMDocument->AppendChild($this->oRoot);
  122. $this->oModules = $this->oDOMDocument->CreateElement('loaded_modules');
  123. $this->oRoot->AppendChild($this->oModules);
  124. $this->oClasses = $this->oDOMDocument->CreateElement('classes');
  125. $this->oRoot->AppendChild($this->oClasses);
  126. foreach (self::$aWellKnownParents as $sWellKnownParent)
  127. {
  128. $this->AddWellKnownParent($sWellKnownParent);
  129. }
  130. $this->oMenus = $this->oDOMDocument->CreateElement('menus');
  131. $this->oRoot->AppendChild($this->oMenus);
  132. foreach($aRootNodeExtensions as $sElementName)
  133. {
  134. $oElement = $this->oDOMDocument->CreateElement($sElementName);
  135. $this->oRoot->AppendChild($oElement);
  136. }
  137. self::$aLoadedModules = array();
  138. self::$aLoadErrors = array();
  139. libxml_use_internal_errors(true);
  140. }
  141. public function Dump($oNode = null, $bReturnRes = false)
  142. {
  143. if (is_null($oNode))
  144. {
  145. $oNode = $this->oRoot;
  146. }
  147. return $oNode->Dump($bReturnRes);
  148. }
  149. public function LoadFromFile($sCacheFile)
  150. {
  151. $this->oDOMDocument->load($sCacheFile);
  152. $this->oRoot = $this->oDOMDocument->firstChild;
  153. $this->oModules = $this->oRoot->getElementsByTagName('loaded_modules')->item(0);
  154. self::$aLoadedModules = array();
  155. foreach($this->oModules->getElementsByTagName('module') as $oModuleNode)
  156. {
  157. $sId = $oModuleNode->getAttribute('id');
  158. $sRootDir = $oModuleNode->GetChildText('root_dir');
  159. $sLabel = $oModuleNode->GetChildText('label');
  160. self::$aLoadedModules[] = new MFModule($sId, $sRootDir, $sLabel);
  161. }
  162. }
  163. public function SaveToFile($sCacheFile)
  164. {
  165. $this->oDOMDocument->save($sCacheFile);
  166. }
  167. /**
  168. * To progressively replace LoadModule
  169. * @param xxx xxx
  170. */
  171. public function LoadDelta(DOMDocument $oDeltaDoc, $oSourceNode, $oTargetParentNode)
  172. {
  173. if (!$oSourceNode instanceof DOMElement) return;
  174. //echo "Load $oSourceNode->tagName::".$oSourceNode->getAttribute('id')." (".$oSourceNode->getAttribute('_delta').")<br/>\n";
  175. $oTarget = $this->oDOMDocument;
  176. if (($oSourceNode->tagName == 'class') && ($oSourceNode->parentNode->tagName == 'classes'))
  177. {
  178. if ($oSourceNode->getAttribute('_delta') == 'define')
  179. {
  180. // This tag is organized in hierarchy: determine the real parent node (as a subnode of the current node)
  181. $sParentId = $oSourceNode->GetChildText('parent');
  182. $oTargetParentNode = $oTarget->GetNodeById('/itop_design/classes//class', $sParentId)->item(0);
  183. if (!$oTargetParentNode)
  184. {
  185. echo "Dumping target doc - looking for '$sParentId'<br/>\n";
  186. $this->oDOMDocument->firstChild->Dump();
  187. throw new Exception("XML datamodel loader: could not find parent node for $oSourceNode->tagName / ".$oSourceNode->getAttribute('id')." with parent id $sParentId");
  188. }
  189. }
  190. else
  191. {
  192. $oTargetNode = $oTarget->GetNodeById('/itop_design/classes//class', $oSourceNode->getAttribute('id'))->item(0);
  193. if (!$oTargetNode)
  194. {
  195. echo "Dumping target doc - looking for '".$oSourceNode->getAttribute('id')."'<br/>\n";
  196. $this->oDOMDocument->firstChild->Dump();
  197. throw new Exception("XML datamodel loader: could not find node for $oSourceNode->tagName/".$oSourceNode->getAttribute('id'));
  198. }
  199. else
  200. {
  201. $oTargetParentNode = $oTargetNode->parentNode;
  202. }
  203. }
  204. }
  205. switch ($oSourceNode->getAttribute('_delta'))
  206. {
  207. case 'must_exist':
  208. case 'merge':
  209. case '':
  210. $bMustExist = ($oSourceNode->getAttribute('_delta') == 'must_exist');
  211. $sSearchId = $oSourceNode->hasAttribute('_rename_from') ? $oSourceNode->getAttribute('_rename_from') : $oSourceNode->getAttribute('id');
  212. $oTargetNode = $oSourceNode->MergeInto($oTargetParentNode, $sSearchId, $bMustExist);
  213. foreach($oSourceNode->childNodes as $oSourceChild)
  214. {
  215. // Continue deeper
  216. $this->LoadDelta($oDeltaDoc, $oSourceChild, $oTargetNode);
  217. }
  218. break;
  219. case 'define_if_not_exists':
  220. $oExistingNode = $oTargetParentNode->FindExistingChildNode($oSourceNode);
  221. if ($oExistingNode == null)
  222. {
  223. // Same as 'define' below
  224. $oTargetNode = $oTarget->ImportNode($oSourceNode, true);
  225. $oTargetParentNode->AddChildNode($oTargetNode);
  226. }
  227. else
  228. {
  229. $oTargetNode = $oExistingNode;
  230. }
  231. break;
  232. case 'define':
  233. // New node - copy child nodes as well
  234. $oTargetNode = $oTarget->ImportNode($oSourceNode, true);
  235. $oTargetParentNode->AddChildNode($oTargetNode);
  236. break;
  237. case 'redefine':
  238. // Replace the existing node by the given node - copy child nodes as well
  239. $oTargetNode = $oTarget->ImportNode($oSourceNode, true);
  240. $sSearchId = $oSourceNode->hasAttribute('_rename_from') ? $oSourceNode->getAttribute('_rename_from') : $oSourceNode->getAttribute('id');
  241. $oTargetParentNode->RedefineChildNode($oTargetNode, $sSearchId);
  242. break;
  243. case 'delete':
  244. $oTargetNode = $oTargetParentNode->FindExistingChildNode($oSourceNode);
  245. $oTargetNode->Delete();
  246. break;
  247. }
  248. if ($oSourceNode->hasAttribute('_rename_from'))
  249. {
  250. $oTargetNode->Rename($oSourceNode->getAttribute('id'));
  251. }
  252. if ($oTargetNode->hasAttribute('_delta'))
  253. {
  254. $oTargetNode->removeAttribute('_delta');
  255. }
  256. }
  257. /**
  258. * Loads the definitions corresponding to the given Module
  259. * @param MFModule $oModule
  260. */
  261. public function LoadModule(MFModule $oModule)
  262. {
  263. $aDataModels = $oModule->GetDataModelFiles();
  264. $sModuleName = $oModule->GetName();
  265. $aClasses = array();
  266. self::$aLoadedModules[] = $oModule;
  267. // For persistence in the cache
  268. $oModuleNode = $this->oDOMDocument->CreateElement('module');
  269. $oModuleNode->setAttribute('id', $oModule->GetId());
  270. $oModuleNode->AppendChild($this->oDOMDocument->CreateElement('root_dir', $oModule->GetRootDir()));
  271. $oModuleNode->AppendChild($this->oDOMDocument->CreateElement('label', $oModule->GetLabel()));
  272. $this->oModules->AppendChild($oModuleNode);
  273. foreach($aDataModels as $sXmlFile)
  274. {
  275. $oDocument = new MFDocument();
  276. libxml_clear_errors();
  277. $oDocument->load($sXmlFile);
  278. //$bValidated = $oDocument->schemaValidate(APPROOT.'setup/itop_design.xsd');
  279. $aErrors = libxml_get_errors();
  280. if (count($aErrors) > 0)
  281. {
  282. self::$aLoadErrors[$sModuleName] = $aErrors;
  283. return;
  284. }
  285. $oXPath = new DOMXPath($oDocument);
  286. $oNodeList = $oXPath->query('/itop_design/classes//class');
  287. foreach($oNodeList as $oNode)
  288. {
  289. if ($oNode->getAttribute('_created_in') == '')
  290. {
  291. $oNode->SetAttribute('_created_in', $sModuleName);
  292. }
  293. }
  294. $oNodeList = $oXPath->query('/itop_design/menus/menu');
  295. foreach($oNodeList as $oNode)
  296. {
  297. if ($oNode->getAttribute('_created_in') == '')
  298. {
  299. $oNode->SetAttribute('_created_in', $sModuleName);
  300. }
  301. }
  302. $oUserRightsNode = $oXPath->query('/itop_design/user_rights')->item(0);
  303. if ($oUserRightsNode)
  304. {
  305. if ($oUserRightsNode->getAttribute('_created_in') == '')
  306. {
  307. $oUserRightsNode->SetAttribute('_created_in', $sModuleName);
  308. }
  309. }
  310. $oDeltaRoot = $oDocument->childNodes->item(0);
  311. $this->LoadDelta($oDocument, $oDeltaRoot, $this->oDOMDocument);
  312. }
  313. }
  314. /**
  315. * XML load errors (XML format and validation)
  316. */
  317. function HasLoadErrors()
  318. {
  319. return (count(self::$aLoadErrors) > 0);
  320. }
  321. function GetLoadErrors()
  322. {
  323. return self::$aLoadErrors;
  324. }
  325. function GetLoadedModules($bExcludeWorkspace = true)
  326. {
  327. if ($bExcludeWorkspace)
  328. {
  329. $aModules = array();
  330. foreach(self::$aLoadedModules as $oModule)
  331. {
  332. if (!$oModule instanceof MFWorkspace)
  333. {
  334. $aModules[] = $oModule;
  335. }
  336. }
  337. }
  338. else
  339. {
  340. $aModules = self::$aLoadedModules;
  341. }
  342. return $aModules;
  343. }
  344. function GetModule($sModuleName)
  345. {
  346. foreach(self::$aLoadedModules as $oModule)
  347. {
  348. if ($oModule->GetName() == $sModuleName) return $oModule;
  349. }
  350. return null;
  351. }
  352. public function CreateElement($sTagName, $sValue = '')
  353. {
  354. return $this->oDOMDocument->createElement($sTagName, $sValue);
  355. }
  356. public function GetNodeById($sXPath, $sId, $oContextNode = null)
  357. {
  358. return $this->oDOMDocument->GetNodeById($sXPath, $sId, $oContextNode);
  359. }
  360. /**
  361. * Apply extensibility rules into the DOM
  362. * @param array aRestrictionRules Array of array ('selectors' => array of XPaths, 'rules' => array of rules)
  363. * @return void
  364. */
  365. public function RestrictExtensibility($aRestrictionRules)
  366. {
  367. foreach ($aRestrictionRules as $aRestriction)
  368. {
  369. foreach ($aRestriction['selectors'] as $sSelector)
  370. {
  371. foreach($this->GetNodes($sSelector) as $oNode)
  372. {
  373. $oNode->RestrictExtensibility($aRestriction['rules']);
  374. }
  375. }
  376. }
  377. }
  378. /**
  379. * Check if the class specified by the given node already exists in the loaded DOM
  380. * @param DOMNode $oClassNode The node corresponding to the class to load
  381. * @throws Exception
  382. * @return bool True if the class exists, false otherwise
  383. */
  384. protected function ClassExists(DOMNode $oClassNode)
  385. {
  386. assert(false);
  387. if ($oClassNode->hasAttribute('id'))
  388. {
  389. $sClassName = $oClassNode->GetAttribute('id');
  390. }
  391. else
  392. {
  393. throw new Exception('ModelFactory::AddClass: Cannot add a class with no name');
  394. }
  395. return (array_key_exists($sClassName, self::$aLoadedClasses));
  396. }
  397. /**
  398. * Check if the class specified by the given name already exists in the loaded DOM
  399. * @param string $sClassName The node corresponding to the class to load
  400. * @throws Exception
  401. * @return bool True if the class exists, false otherwise
  402. */
  403. protected function ClassNameExists($sClassName, $bFlattenLayers = true)
  404. {
  405. return !is_null($this->GetClass($sClassName, $bFlattenLayers));
  406. }
  407. /**
  408. * Add the given class to the DOM
  409. * @param DOMNode $oClassNode
  410. * @param string $sModuleName The name of the module in which this class is declared
  411. * @throws Exception
  412. */
  413. public function AddClass(DOMNode $oClassNode, $sModuleName)
  414. {
  415. if ($oClassNode->hasAttribute('id'))
  416. {
  417. $sClassName = $oClassNode->GetAttribute('id');
  418. }
  419. else
  420. {
  421. throw new Exception('ModelFactory::AddClass: Cannot add a class with no name');
  422. }
  423. if ($this->ClassNameExists($oClassNode->getAttribute('id')))
  424. {
  425. throw new Exception("ModelFactory::AddClass: Cannot add the already existing class $sClassName");
  426. }
  427. $sParentClass = $oClassNode->GetChildText('parent', '');
  428. $oParentNode = $this->GetClass($sParentClass);
  429. if ($oParentNode == null)
  430. {
  431. throw new Exception("ModelFactory::AddClass: Cannot find the parent class of '$sClassName': '$sParentClass'");
  432. }
  433. else
  434. {
  435. if ($sModuleName != '')
  436. {
  437. $oClassNode->SetAttribute('_created_in', $sModuleName);
  438. }
  439. $oParentNode->AddChildNode($this->oDOMDocument->importNode($oClassNode, true));
  440. if (substr($sParentClass, 0, 1) == '/') // Convention for well known parent classes
  441. {
  442. // Remove the leading slash character
  443. $oParentNameNode = $oClassNode->GetOptionalElement('parent')->firstChild; // Get the DOMCharacterData node
  444. $oParentNameNode->data = substr($sParentClass, 1);
  445. }
  446. }
  447. }
  448. /**
  449. * Remove a class from the DOM
  450. * @param string $sClass
  451. * @throws Exception
  452. */
  453. public function RemoveClass($sClass)
  454. {
  455. $oClassNode = $this->GetClass($sClass);
  456. if ($oClassNode == null)
  457. {
  458. throw new Exception("ModelFactory::RemoveClass: Cannot remove the non existing class $sClass");
  459. }
  460. // Note: the child classes are removed entirely
  461. $oClassNode->Delete();
  462. }
  463. /**
  464. * Modify a class within the DOM
  465. * @param string $sMenuId
  466. * @param DOMNode $oMenuNode
  467. * @throws Exception
  468. */
  469. public function AlterClass($sClassName, DOMNode $oClassNode)
  470. {
  471. $sOriginalName = $sClassName;
  472. if ($this->ClassNameExists($sClassName))
  473. {
  474. $oDestNode = self::$aLoadedClasses[$sClassName];
  475. }
  476. else
  477. {
  478. $sOriginalName = $oClassNode->getAttribute('_original_name');
  479. if ($this->ClassNameExists($sOriginalName))
  480. {
  481. // Class was renamed !
  482. $oDestNode = self::$aLoadedClasses[$sOriginalName];
  483. }
  484. else
  485. {
  486. throw new Exception("ModelFactory::AddClass: Cannot alter the non-existing class $sClassName / $sOriginalName");
  487. }
  488. }
  489. $this->_priv_AlterNode($oDestNode, $oClassNode);
  490. $sClassName = $oDestNode->getAttribute('id');
  491. if ($sOriginalName != $sClassName)
  492. {
  493. unset(self::$aLoadedClasses[$sOriginalName]);
  494. self::$aLoadedClasses[$sClassName] = $oDestNode;
  495. }
  496. $this->_priv_SetFlag($oDestNode, 'modified');
  497. }
  498. public function GetClassXMLTemplate($sName, $sIcon)
  499. {
  500. $sHeader = '<?xml version="1.0" encoding="utf-8"?'.'>';
  501. return
  502. <<<EOF
  503. $sHeader
  504. <class id="$sName">
  505. <comment/>
  506. <properties>
  507. </properties>
  508. <naming format=""><attributes/></naming>
  509. <reconciliation><attributes/></reconciliation>
  510. <display_template/>
  511. <icon>$sIcon</icon>
  512. </properties>
  513. <fields/>
  514. <lifecycle/>
  515. <methods/>
  516. <presentation>
  517. <details><items/></details>
  518. <search><items/></search>
  519. <list><items/></list>
  520. </presentation>
  521. </class>
  522. EOF
  523. ;
  524. }
  525. /**
  526. * List all classes from the DOM, for a given module
  527. * @param string $sModuleNale
  528. * @param bool $bFlattenLayers
  529. * @throws Exception
  530. */
  531. public function ListClasses($sModuleName, $bFlattenLayers = true)
  532. {
  533. $sXPath = "/itop_design/classes//class[@_created_in='$sModuleName']";
  534. if ($bFlattenLayers)
  535. {
  536. $sXPath = "/itop_design/classes//class[@_created_in='$sModuleName' and (not(@_alteration) or @_alteration!='removed')]";
  537. }
  538. return $this->GetNodes($sXPath);
  539. }
  540. /**
  541. * List all classes from the DOM, for a given module
  542. * @param string $sModuleNale
  543. * @param bool $bFlattenLayers
  544. * @throws Exception
  545. */
  546. public function ListAllClasses($bFlattenLayers = true)
  547. {
  548. $sXPath = "/itop_design/classes//class";
  549. if ($bFlattenLayers)
  550. {
  551. $sXPath = "/itop_design/classes//class[not(@_alteration) or @_alteration!='removed']";
  552. }
  553. return $this->GetNodes($sXPath);
  554. }
  555. /**
  556. * List top level (non abstract) classes having child classes
  557. * @param bool $bFlattenLayers
  558. * @throws Exception
  559. */
  560. public function ListRootClasses($bFlattenLayers = true)
  561. {
  562. $sXPath = "/itop_design/classes/class/class/class/..";
  563. if ($bFlattenLayers)
  564. {
  565. $sXPath = "/itop_design/classes/class/class[not(@_alteration) or @_alteration!='removed']/class[not(@_alteration) or @_alteration!='removed']/..";
  566. }
  567. return $this->GetNodes($sXPath);
  568. }
  569. public function GetClass($sClassName, $bFlattenLayers = true)
  570. {
  571. $oClassNode = $this->GetNodes("/itop_design/classes//class[@id='$sClassName']")->item(0);
  572. if (($oClassNode != null) && ($bFlattenLayers))
  573. {
  574. $sOperation = $oClassNode->getAttribute('_alteration');
  575. if ($sOperation == 'removed')
  576. {
  577. $oClassNode = null;
  578. }
  579. }
  580. return $oClassNode;
  581. }
  582. public function AddWellKnownParent($sWellKnownParent)
  583. {
  584. $oWKClass = $this->oDOMDocument->CreateElement('class');
  585. $oWKClass->setAttribute('id', $sWellKnownParent);
  586. $this->oClasses->AppendChild($oWKClass);
  587. return $oWKClass;
  588. }
  589. public function GetChildClasses($oClassNode, $bFlattenLayers = true)
  590. {
  591. $sXPath = "class";
  592. if ($bFlattenLayers)
  593. {
  594. $sXPath = "class[(@_operation!='removed')]";
  595. }
  596. return $this->GetNodes($sXPath, $oClassNode);
  597. }
  598. public function GetField($sClassName, $sAttCode, $bFlattenLayers = true)
  599. {
  600. if (!$this->ClassNameExists($sClassName))
  601. {
  602. return null;
  603. }
  604. $oClassNode = self::$aLoadedClasses[$sClassName];
  605. if ($bFlattenLayers)
  606. {
  607. $sOperation = $oClassNode->getAttribute('_operation');
  608. if ($sOperation == 'removed')
  609. {
  610. $oClassNode = null;
  611. }
  612. }
  613. $sXPath = "fields/field[@id='$sAttCode']";
  614. if ($bFlattenLayers)
  615. {
  616. $sXPath = "fields/field[(@id='$sAttCode' and (not(@_operation) or @_operation!='removed'))]";
  617. }
  618. $oFieldNode = $this->GetNodes($sXPath, $oClassNode)->item(0);
  619. if (($oFieldNode == null) && ($sParentClass = $oClassNode->GetChildText('parent')))
  620. {
  621. return $this->GetField($sParentClass, $sAttCode, $bFlattenLayers);
  622. }
  623. return $oFieldNode;
  624. }
  625. /**
  626. * List all classes from the DOM
  627. * @param bool $bFlattenLayers
  628. * @throws Exception
  629. */
  630. public function ListFields(DOMNode $oClassNode, $bFlattenLayers = true)
  631. {
  632. $sXPath = "fields/field";
  633. if ($bFlattenLayers)
  634. {
  635. $sXPath = "fields/field[not(@_alteration) or @_alteration!='removed']";
  636. }
  637. return $this->GetNodes($sXPath, $oClassNode);
  638. }
  639. public function AddField(DOMNode $oClassNode, $sFieldCode, $sFieldType, $sSQL, $defaultValue, $bIsNullAllowed, $aExtraParams)
  640. {
  641. $oNewField = $this->oDOMDocument->createElement('field');
  642. $oNewField->setAttribute('id', $sFieldCode);
  643. $this->_priv_AlterField($oNewField, $sFieldType, $sSQL, $defaultValue, $bIsNullAllowed, $aExtraParams);
  644. $oFields = $oClassNode->getElementsByTagName('fields')->item(0);
  645. $oFields->AppendChild($oNewField);
  646. $this->_priv_SetFlag($oNewField, 'added');
  647. }
  648. public function RemoveField(DOMNode $oClassNode, $sFieldCode)
  649. {
  650. $sXPath = "fields/field[@id='$sFieldCode']";
  651. $oFieldNodes = $this->GetNodes($sXPath, $oClassNode);
  652. if (is_object($oFieldNodes) && (is_object($oFieldNodes->item(0))))
  653. {
  654. $oFieldNode = $oFieldNodes->item(0);
  655. $sOpCode = $oFieldNode->getAttribute('_operation');
  656. if ($oFieldNode->getAttribute('_operation') == 'added')
  657. {
  658. $oFieldNode->parentNode->removeChild($oFieldNode);
  659. }
  660. else
  661. {
  662. $this->_priv_SetFlag($oFieldNode, 'removed');
  663. }
  664. }
  665. }
  666. public function AlterField(DOMNode $oClassNode, $sFieldCode, $sFieldType, $sSQL, $defaultValue, $bIsNullAllowed, $aExtraParams)
  667. {
  668. $sXPath = "fields/field[@id='$sFieldCode']";
  669. $oFieldNodes = $this->GetNodes($sXPath, $oClassNode);
  670. if (is_object($oFieldNodes) && (is_object($oFieldNodes->item(0))))
  671. {
  672. $oFieldNode = $oFieldNodes->item(0);
  673. //@@TODO: if the field was 'added' => then let it as 'added'
  674. $sOpCode = $oFieldNode->getAttribute('_operation');
  675. switch($sOpCode)
  676. {
  677. case 'added':
  678. case 'modified':
  679. // added or modified, let it as it is
  680. break;
  681. default:
  682. $this->_priv_SetFlag($oFieldNodes->item(0), 'modified');
  683. }
  684. $this->_priv_AlterField($oFieldNodes->item(0), $sFieldType, $sSQL, $defaultValue, $bIsNullAllowed, $aExtraParams);
  685. }
  686. }
  687. protected function _priv_AlterField(DOMNode $oFieldNode, $sFieldType, $sSQL, $defaultValue, $bIsNullAllowed, $aExtraParams)
  688. {
  689. switch($sFieldType)
  690. {
  691. case 'Blob':
  692. case 'Boolean':
  693. case 'CaseLog':
  694. case 'Deadline':
  695. case 'Duration':
  696. case 'EmailAddress':
  697. case 'EncryptedString':
  698. case 'HTML':
  699. case 'IPAddress':
  700. case 'LongText':
  701. case 'OQL':
  702. case 'OneWayPassword':
  703. case 'Password':
  704. case 'Percentage':
  705. case 'String':
  706. case 'Text':
  707. case 'Text':
  708. case 'TemplateHTML':
  709. case 'TemplateString':
  710. case 'TemplateText':
  711. case 'URL':
  712. case 'Date':
  713. case 'DateTime':
  714. case 'Decimal':
  715. case 'Integer':
  716. break;
  717. case 'ExternalKey':
  718. $this->_priv_AddFieldAttribute($oFieldNode, 'target_class', $aExtraParams);
  719. // Fall through
  720. case 'HierarchicalKey':
  721. $this->_priv_AddFieldAttribute($oFieldNode, 'on_target_delete', $aExtraParams);
  722. $this->_priv_AddFieldAttribute($oFieldNode, 'filter', $aExtraParams);
  723. break;
  724. case 'ExternalField':
  725. $this->_priv_AddFieldAttribute($oFieldNode, 'extkey_attcode', $aExtraParams);
  726. $this->_priv_AddFieldAttribute($oFieldNode, 'target_attcode', $aExtraParams);
  727. break;
  728. case 'Enum':
  729. $this->_priv_SetFieldValues($oFieldNode, $aExtraParams);
  730. break;
  731. case 'LinkedSetIndirect':
  732. $this->_priv_AddFieldAttribute($oFieldNode, 'ext_key_to_remote', $aExtraParams);
  733. // Fall through
  734. case 'LinkedSet':
  735. $this->_priv_AddFieldAttribute($oFieldNode, 'linked_class', $aExtraParams);
  736. $this->_priv_AddFieldAttribute($oFieldNode, 'ext_key_to_me', $aExtraParams);
  737. $this->_priv_AddFieldAttribute($oFieldNode, 'count_min', $aExtraParams);
  738. $this->_priv_AddFieldAttribute($oFieldNode, 'count_max', $aExtraParams);
  739. break;
  740. default:
  741. throw(new Exception('Unsupported type of field: '.$sFieldType));
  742. }
  743. $this->_priv_SetFieldDependencies($oFieldNode, $aExtraParams);
  744. $oFieldNode->setAttribute('type', $sFieldType);
  745. $oFieldNode->setAttribute('sql', $sSQL);
  746. $oFieldNode->setAttribute('default_value', $defaultValue);
  747. $oFieldNode->setAttribute('is_null_alllowed', $bIsNullAllowed ? 'true' : 'false');
  748. }
  749. protected function _priv_AddFieldAttribute(DOMNode $oFieldNode, $sAttributeCode, $aExtraParams, $bMandatory = false)
  750. {
  751. $value = array_key_exists($sAttributeCode, $aExtraParams) ? $aExtraParams[$sAttributeCode] : '';
  752. if (($value == '') && (!$bMandatory)) return;
  753. $oFieldNode->setAttribute($sAttributeCode, $value);
  754. }
  755. protected function _priv_SetFieldDependencies($oFieldNode, $aExtraParams)
  756. {
  757. $aDeps = array_key_exists('dependencies', $aExtraParams) ? $aExtraParams['dependencies'] : '';
  758. $oDependencies = $oFieldNode->getElementsByTagName('dependencies')->item(0);
  759. // No dependencies before, and no dependencies to add, exit
  760. if (($oDependencies == null) && ($aDeps == '')) return;
  761. // Remove the previous dependencies
  762. $oFieldNode->removeChild($oDependencies);
  763. // If no dependencies, exit
  764. if ($aDeps == '') return;
  765. // Build the new list of dependencies
  766. $oDependencies = $this->oDOMDocument->createElement('dependencies');
  767. foreach($aDeps as $sAttCode)
  768. {
  769. $oDep = $this->oDOMDocument->createElement('attribute');
  770. $oDep->setAttribute('id', $sAttCode);
  771. $oDependencies->addChild($oDep);
  772. }
  773. $oFieldNode->addChild($oDependencies);
  774. }
  775. protected function _priv_SetFieldValues($oFieldNode, $aExtraParams)
  776. {
  777. $aVals = array_key_exists('values', $aExtraParams) ? $aExtraParams['values'] : '';
  778. $oValues = $oFieldNode->getElementsByTagName('values')->item(0);
  779. // No dependencies before, and no dependencies to add, exit
  780. if (($oValues == null) && ($aVals == '')) return;
  781. // Remove the previous dependencies
  782. $oFieldNode->removeChild($oValues);
  783. // If no dependencies, exit
  784. if ($aVals == '') return;
  785. // Build the new list of dependencies
  786. $oValues = $this->oDOMDocument->createElement('values');
  787. foreach($aVals as $sValue)
  788. {
  789. $oVal = $this->oDOMDocument->createElement('value', $sValue);
  790. $oValues->appendChild($oVal);
  791. }
  792. $oFieldNode->appendChild($oValues);
  793. }
  794. public function SetPresentation(DOMNode $oClassNode, $sPresentationCode, $aPresentation)
  795. {
  796. $oPresentation = $oClassNode->getElementsByTagName('presentation')->item(0);
  797. if (!is_object($oPresentation))
  798. {
  799. $oPresentation = $this->oDOMDocument->createElement('presentation');
  800. $oClassNode->appendChild($oPresentation);
  801. }
  802. $oZlist = $oPresentation->getElementsByTagName($sPresentationCode)->item(0);
  803. if (is_object($oZlist))
  804. {
  805. // Remove the previous Zlist
  806. $oPresentation->removeChild($oZlist);
  807. }
  808. // Create the ZList anew
  809. $oZlist = $this->oDOMDocument->createElement($sPresentationCode);
  810. $oPresentation->appendChild($oZlist);
  811. $this->AddZListItem($oZlist, $aPresentation);
  812. $this->_priv_SetFlag($oZlist, 'replaced');
  813. }
  814. protected function AddZListItem($oXMLNode, $value)
  815. {
  816. if (is_array($value))
  817. {
  818. $oXmlItems = $this->oDOMDocument->CreateElement('items');
  819. $oXMLNode->appendChild($oXmlItems);
  820. foreach($value as $key => $item)
  821. {
  822. $oXmlItem = $this->oDOMDocument->CreateElement('item');
  823. $oXmlItems->appendChild($oXmlItem);
  824. if (is_string($key))
  825. {
  826. $oXmlItem->SetAttribute('key', $key);
  827. }
  828. $this->AddZListItem($oXmlItem, $item);
  829. }
  830. }
  831. else
  832. {
  833. $oXmlText = $this->oDOMDocument->CreateTextNode((string) $value);
  834. $oXMLNode->appendChild($oXmlText);
  835. }
  836. }
  837. /**
  838. * List all transitions from a given state
  839. * @param DOMNode $oStateNode The state
  840. * @param bool $bFlattenLayers
  841. * @throws Exception
  842. */
  843. public function ListTransitions(DOMNode $oStateNode, $bFlattenLayers = true)
  844. {
  845. $sXPath = "transitions/transition";
  846. if ($bFlattenLayers)
  847. {
  848. //$sXPath = "transitions/transition[@_operation!='removed']";
  849. }
  850. return $this->GetNodes($sXPath, $oStateNode);
  851. }
  852. /**
  853. * List all states of a given class
  854. * @param DOMNode $oClassNode The class
  855. * @param bool $bFlattenLayers
  856. * @throws Exception
  857. */
  858. public function ListStates(DOMNode $oClassNode, $bFlattenLayers = true)
  859. {
  860. $sXPath = "lifecycle/states/state";
  861. if ($bFlattenLayers)
  862. {
  863. //$sXPath = "lifecycle/states/state[@_operation!='removed']";
  864. }
  865. return $this->GetNodes($sXPath, $oClassNode);
  866. }
  867. /**
  868. * List Zlists from the DOM for a given class
  869. * @param bool $bFlattenLayers
  870. * @throws Exception
  871. */
  872. public function ListZLists(DOMNode $oClassNode, $bFlattenLayers = true)
  873. {
  874. // Not yet implemented !!!
  875. return array();
  876. }
  877. public function ApplyChanges()
  878. {
  879. $oNodes = $this->ListChanges();
  880. foreach($oNodes as $oNode)
  881. {
  882. $sOperation = $oNode->GetAttribute('_alteration');
  883. switch($sOperation)
  884. {
  885. case 'added':
  886. case 'replaced':
  887. // marked as added or modified, just reset the flag
  888. $oNode->removeAttribute('_alteration');
  889. break;
  890. case 'removed':
  891. // marked as deleted, let's remove the node from the tree
  892. $oNode->parentNode->removeChild($oNode);
  893. // TODO!!!!!!!
  894. //unset(self::$aLoadedClasses[$sClass]);
  895. break;
  896. }
  897. if ($oNode->hasAttribute('_old_id'))
  898. {
  899. $oNode->removeAttribute('_old_id');
  900. }
  901. }
  902. }
  903. public function ListChanges()
  904. {
  905. return $this->GetNodes('//*[@_alteration or @_old_id]');
  906. }
  907. /**
  908. * Create path for the delta
  909. * @param DOMDocument oTargetDoc Where to attach the top of the hierarchy
  910. * @param MFElement oNode The node to import with its path
  911. */
  912. protected function ImportNodeAndPathDelta($oTargetDoc, $oNode)
  913. {
  914. // Preliminary: skip the parent if this node is organized hierarchically into the DOM
  915. // Only class nodes are organized this way
  916. $oParent = $oNode->parentNode;
  917. if ($oNode->tagName == 'class')
  918. {
  919. while (($oParent instanceof DOMElement) && ($oParent->tagName == $oNode->tagName) && $oParent->hasAttribute('id'))
  920. {
  921. $oParent = $oParent->parentNode;
  922. }
  923. }
  924. // Recursively create the path for the parent
  925. if ($oParent instanceof DOMElement)
  926. {
  927. $oParentClone = $this->ImportNodeAndPathDelta($oTargetDoc, $oParent);
  928. }
  929. else
  930. {
  931. // We've reached the top let's add the node into the root recipient
  932. $oParentClone = $oTargetDoc;
  933. }
  934. // Look for the node into the parent node
  935. // Note: this is an identified weakness of the algorithm,
  936. // because for each node modified, and each node of its path
  937. // we will have to lookup for the existing entry
  938. // Anyhow, this loop is quite quick to execute because in the delta
  939. // the number of nodes is limited
  940. $oNodeClone = null;
  941. foreach ($oParentClone->childNodes as $oChild)
  942. {
  943. if (($oChild instanceof DOMElement) && ($oChild->tagName == $oNode->tagName))
  944. {
  945. if (!$oNode->hasAttribute('id') || ($oNode->getAttribute('id') == $oChild->getAttribute('id')))
  946. {
  947. $oNodeClone = $oChild;
  948. break;
  949. }
  950. }
  951. }
  952. if (!$oNodeClone)
  953. {
  954. $sAlteration = $oNode->getAttribute('_alteration');
  955. $bCopyContents = ($sAlteration == 'replaced') || ($sAlteration == 'added');
  956. $oNodeClone = $oTargetDoc->importNode($oNode->cloneNode($bCopyContents), $bCopyContents);
  957. $oNodeClone->removeAttribute('_alteration');
  958. if ($oNodeClone->hasAttribute('_old_id'))
  959. {
  960. $oNodeClone->setAttribute('_rename_from', $oNodeClone->getAttribute('_old_id'));
  961. $oNodeClone->removeAttribute('_old_id');
  962. }
  963. switch ($sAlteration)
  964. {
  965. case '':
  966. if ($oNodeClone->hasAttribute('id'))
  967. {
  968. $oNodeClone->setAttribute('_delta', 'must_exist');
  969. }
  970. break;
  971. case 'added':
  972. $oNodeClone->setAttribute('_delta', 'define');
  973. break;
  974. case 'replaced':
  975. $oNodeClone->setAttribute('_delta', 'redefine');
  976. break;
  977. case 'removed':
  978. $oNodeClone->setAttribute('_delta', 'delete');
  979. break;
  980. }
  981. $oParentClone->appendChild($oNodeClone);
  982. }
  983. return $oNodeClone;
  984. }
  985. /**
  986. * Get the text/XML version of the delta
  987. */
  988. public function GetDelta()
  989. {
  990. $oDelta = new MFDocument();
  991. foreach($this->ListChanges() as $oAlteredNode)
  992. {
  993. $this->ImportNodeAndPathDelta($oDelta, $oAlteredNode);
  994. }
  995. return $oDelta->saveXML();
  996. }
  997. /**
  998. * Searches on disk in the root directories for module description files
  999. * and returns an array of MFModules
  1000. * @return array Array of MFModules
  1001. */
  1002. public function FindModules()
  1003. {
  1004. $aAvailableModules = ModuleDiscovery::GetAvailableModules($this->aRootDirs);
  1005. $aResult = array();
  1006. foreach($aAvailableModules as $sId => $aModule)
  1007. {
  1008. $aResult[] = new MFModule($sId, $aModule['root_dir'], $aModule['label']);
  1009. }
  1010. return $aResult;
  1011. }
  1012. public function TestAlteration()
  1013. {
  1014. try
  1015. {
  1016. $sHeader = '<?xml version="1.0" encoding="utf-8"?'.'>';
  1017. $sOriginalXML =
  1018. <<<EOF
  1019. $sHeader
  1020. <itop_design>
  1021. <a id="first a">
  1022. <b>Text</b>
  1023. <c id="1">
  1024. <d>D1</d>
  1025. <d>D2</d>
  1026. </c>
  1027. </a>
  1028. <a id="second a">
  1029. <parent>first a</parent>
  1030. </a>
  1031. <a id="third a">
  1032. <parent>first a</parent>
  1033. <x>blah</x>
  1034. </a>
  1035. </itop_design>
  1036. EOF;
  1037. $this->oDOMDocument = new MFDocument();
  1038. $this->oDOMDocument->loadXML($sOriginalXML);
  1039. // DOM Get the original values, then modify its contents by the mean of the API
  1040. $oRoot = $this->GetNodes('//itop_design')->item(0);
  1041. //$oRoot->Dump();
  1042. $sDOMOriginal = $oRoot->Dump(true);
  1043. $oNode = $oRoot->GetNodes('a/b')->item(0);
  1044. $oNew = $this->oDOMDocument->CreateElement('b', 'New text');
  1045. $oNode->parentNode->RedefineChildNode($oNew);
  1046. $oNode = $oRoot->GetNodes('a/c')->item(0);
  1047. $oNewC = $this->oDOMDocument->CreateElement('c');
  1048. $oNewC->setAttribute('id', '1');
  1049. $oNode->parentNode->RedefineChildNode($oNewC);
  1050. $oNewC->appendChild($this->oDOMDocument->CreateElement('d', 'x'));
  1051. $oNewC->appendChild($this->oDOMDocument->CreateElement('d', 'y'));
  1052. $oNewC->appendChild($this->oDOMDocument->CreateElement('d', 'z'));
  1053. $oNamedNode = $this->oDOMDocument->CreateElement('z');
  1054. $oNamedNode->setAttribute('id', 'abc');
  1055. $oNewC->AddChildNode($oNamedNode);
  1056. $oNewC->AddChildNode($this->oDOMDocument->CreateElement('r', 'to be replaced'));
  1057. // Alter this "modified node", no flag should be set in its subnodes
  1058. $oNewC->Rename('blah');
  1059. $oNewC->Rename('foo');
  1060. $oNewC->AddChildNode($this->oDOMDocument->CreateElement('y', '(no flag)'));
  1061. $oNewC->AddChildNode($this->oDOMDocument->CreateElement('x', 'To delete programatically'));
  1062. $oSubNode = $oNewC->GetUniqueElement('z');
  1063. $oSubNode->Rename('abcdef');
  1064. $oSubNode = $oNewC->GetUniqueElement('x');
  1065. $oSubNode->Delete();
  1066. $oNewC->RedefineChildNode($this->oDOMDocument->CreateElement('r', 'replacement'));
  1067. $oNode = $oRoot->GetNodes("//a[@id='second a']")->item(0);
  1068. $oNode->Rename('el 2o A');
  1069. $oNode->Rename('el secundo A');
  1070. $oNew = $this->oDOMDocument->CreateElement('e', 'Something new here');
  1071. $oNode->AddChildNode($oNew);
  1072. $oNewA = $this->oDOMDocument->CreateElement('a');
  1073. $oNewA->setAttribute('id', 'new a');
  1074. $oNode->AddChildNode($oNewA);
  1075. $oSubnode = $this->oDOMDocument->CreateElement('parent', 'el secundo A');
  1076. $oSubnode->setAttribute('id', 'to be changed');
  1077. $oNewA->AddChildNode($oSubnode);
  1078. $oNewA->AddChildNode($this->oDOMDocument->CreateElement('f', 'Welcome to the newcomer'));
  1079. $oNewA->AddChildNode($this->oDOMDocument->CreateElement('x', 'To delete programatically'));
  1080. // Alter this "new a", as it is new, no flag should be set
  1081. $oNewA->Rename('new_a');
  1082. $oSubNode = $oNewA->GetUniqueElement('parent');
  1083. $oSubNode->Rename('alter ego');
  1084. $oNewA->RedefineChildNode($this->oDOMDocument->CreateElement('f', 'dummy data'));
  1085. $oSubNode = $oNewA->GetUniqueElement('x');
  1086. $oSubNode->Delete();
  1087. $oNode = $oRoot->GetNodes("//a[@id='third a']")->item(0);
  1088. $oNode->Delete();
  1089. //$oRoot->Dump();
  1090. $sDOMModified = $oRoot->Dump(true);
  1091. // Compute the delta
  1092. //
  1093. $sDeltaXML = $this->GetDelta();
  1094. //echo "<pre>\n";
  1095. //echo htmlentities($sDeltaXML);
  1096. //echo "</pre>\n";
  1097. // Reiterating - try to remake the DOM by applying the computed delta
  1098. //
  1099. $this->oDOMDocument = new MFDocument();
  1100. $this->oDOMDocument->loadXML($sOriginalXML);
  1101. $oRoot = $this->GetNodes('//itop_design')->item(0);
  1102. //$oRoot->Dump();
  1103. echo "<h4>Rebuild the DOM - Delta applied...</h4>\n";
  1104. $oDeltaDoc = new MFDocument();
  1105. $oDeltaDoc->loadXML($sDeltaXML);
  1106. //$oDeltaDoc->Dump();
  1107. //$this->oDOMDocument->Dump();
  1108. $oDeltaRoot = $oDeltaDoc->childNodes->item(0);
  1109. $this->LoadDelta($oDeltaDoc, $oDeltaRoot, $this->oDOMDocument);
  1110. //$oRoot->Dump();
  1111. $sDOMRebuilt = $oRoot->Dump(true);
  1112. }
  1113. catch (Exception $e)
  1114. {
  1115. echo "<h1>Exception: ".$e->getMessage()."</h1>\n";
  1116. echo "<pre>\n";
  1117. debug_print_backtrace();
  1118. echo "</pre>\n";
  1119. }
  1120. $sArrStyle = "font-size: 40;";
  1121. echo "<table>\n";
  1122. echo " <tr>\n";
  1123. echo " <td width=\"50%\">\n";
  1124. echo " <h4>DOM - Original values</h4>\n";
  1125. echo " <pre>".htmlentities($sDOMOriginal)."</pre>\n";
  1126. echo " </td>\n";
  1127. echo " <td width=\"50%\" align=\"left\" valign=\"center\"><span style=\"$sArrStyle\">&rArr; &rArr; &rArr;</span></td>\n";
  1128. echo " </tr>\n";
  1129. echo " <tr><td align=\"center\"><span style=\"$sArrStyle\">&dArr;</div></td><td align=\"center\"><span style=\"$sArrStyle\"><span style=\"$sArrStyle\">&dArr;</div></div></td></tr>\n";
  1130. echo " <tr>\n";
  1131. echo " <td width=\"50%\">\n";
  1132. echo " <h4>DOM - Altered with various changes</h4>\n";
  1133. echo " <pre>".htmlentities($sDOMModified)."</pre>\n";
  1134. echo " </td>\n";
  1135. echo " <td width=\"50%\">\n";
  1136. echo " <h4>DOM - Rebuilt from the Delta</h4>\n";
  1137. echo " <pre>".htmlentities($sDOMRebuilt)."</pre>\n";
  1138. echo " </td>\n";
  1139. echo " </tr>\n";
  1140. echo " <tr><td align=\"center\"><span style=\"$sArrStyle\">&dArr;</div></td><td align=\"center\"><span style=\"$sArrStyle\">&uArr;</div></td></tr>\n";
  1141. echo " <td width=\"50%\">\n";
  1142. echo " <h4>Delta (Computed by ModelFactory)</h4>\n";
  1143. echo " <pre>".htmlentities($sDeltaXML)."</pre>\n";
  1144. echo " </td>\n";
  1145. echo " <td width=\"50%\" align=\"left\" valign=\"center\"><span style=\"$sArrStyle\">&rArr; &rArr; &rArr;</span></td>\n";
  1146. echo " </tr>\n";
  1147. echo "</table>\n";
  1148. } // TEST !
  1149. /**
  1150. * Extracts some nodes from the DOM
  1151. * @param string $sXPath A XPath expression
  1152. * @return DOMNodeList
  1153. */
  1154. public function GetNodes($sXPath, $oContextNode = null)
  1155. {
  1156. return $this->oDOMDocument->GetNodes($sXPath, $oContextNode);
  1157. }
  1158. public function ListActiveChildNodes($sContextXPath, $sTagName)
  1159. {
  1160. $oContextPath = $this->oRoot->GetNodes($sContextXPath)->item(0);
  1161. return $oContextPath->ListActiveChildNodes($sTagName);
  1162. }
  1163. }
  1164. /**
  1165. * MFElement: helper to read/change the DOM
  1166. * @package ModelFactory
  1167. */
  1168. class MFElement extends DOMElement
  1169. {
  1170. /**
  1171. * Extracts some nodes from the DOM
  1172. * @param string $sXPath A XPath expression
  1173. * @return DOMNodeList
  1174. */
  1175. public function GetNodes($sXPath)
  1176. {
  1177. return $this->ownerDocument->GetNodes($sXPath, $this);
  1178. }
  1179. /**
  1180. * For debugging purposes - but this is currently buggy: the whole document is rendered
  1181. */
  1182. public function Dump($bReturnRes = false)
  1183. {
  1184. $sXml = $this->ownerDocument->saveXML($this);
  1185. if ($bReturnRes)
  1186. {
  1187. return $sXml;
  1188. }
  1189. else
  1190. {
  1191. echo "<pre>\n";
  1192. echo htmlentities($sXml);
  1193. echo "</pre>\n";
  1194. }
  1195. }
  1196. /**
  1197. * Returns the node directly under the given node
  1198. */
  1199. public function GetUniqueElement($sTagName, $bMustExist = true)
  1200. {
  1201. $oNode = null;
  1202. foreach($this->childNodes as $oChildNode)
  1203. {
  1204. if ($oChildNode->nodeName == $sTagName)
  1205. {
  1206. $oNode = $oChildNode;
  1207. break;
  1208. }
  1209. }
  1210. if ($bMustExist && is_null($oNode))
  1211. {
  1212. throw new DOMFormatException('Missing unique tag: '.$sTagName);
  1213. }
  1214. return $oNode;
  1215. }
  1216. /**
  1217. * Returns the node directly under the current node, or null if missing
  1218. */
  1219. public function GetOptionalElement($sTagName)
  1220. {
  1221. return $this->GetUniqueElement($sTagName, false);
  1222. }
  1223. /**
  1224. * Returns the TEXT of the current node (possibly from several subnodes)
  1225. */
  1226. public function GetText($sDefault = null)
  1227. {
  1228. $sText = null;
  1229. foreach($this->childNodes as $oChildNode)
  1230. {
  1231. if ($oChildNode instanceof DOMCharacterData) // Base class of DOMText and DOMCdataSection
  1232. {
  1233. if (is_null($sText)) $sText = '';
  1234. $sText .= $oChildNode->wholeText;
  1235. }
  1236. }
  1237. if (is_null($sText))
  1238. {
  1239. return $sDefault;
  1240. }
  1241. else
  1242. {
  1243. return $sText;
  1244. }
  1245. }
  1246. /**
  1247. * Get the TEXT value from the child node
  1248. */
  1249. public function GetChildText($sTagName, $sDefault = null)
  1250. {
  1251. $sRet = $sDefault;
  1252. if ($oChild = $this->GetOptionalElement($sTagName))
  1253. {
  1254. $sRet = $oChild->GetText($sDefault);
  1255. }
  1256. return $sRet;
  1257. }
  1258. /**
  1259. * Assumes the current node to be either a text or
  1260. * <items>
  1261. * <item [key]="..."]>value<item>
  1262. * <item [key]="..."]>value<item>
  1263. * </items>
  1264. * where value can be the either a text or an array of items... recursively
  1265. * Returns a PHP array
  1266. */
  1267. public function GetNodeAsArrayOfItems($sElementName = 'items')
  1268. {
  1269. $oItems = $this->GetOptionalElement($sElementName);
  1270. if ($oItems)
  1271. {
  1272. $res = array();
  1273. $aRanks = array();
  1274. foreach($oItems->childNodes as $oItem)
  1275. {
  1276. // When an attribute is missing
  1277. if ($oItem->hasAttribute('id'))
  1278. {
  1279. $key = $oItem->getAttribute('id');
  1280. if (array_key_exists($key, $res))
  1281. {
  1282. // Houston!
  1283. throw new DOMFormatException("Tag ".$oItem->getNodePath().", id '$key' already used!!!");
  1284. }
  1285. $res[$key] = $oItem->GetNodeAsArrayOfItems();
  1286. }
  1287. else
  1288. {
  1289. $res[] = $oItem->GetNodeAsArrayOfItems();
  1290. }
  1291. $sRank = $oItem->GetChildText('rank');
  1292. if ($sRank != '')
  1293. {
  1294. $aRanks[] = (float) $sRank;
  1295. }
  1296. else
  1297. {
  1298. $aRanks[] = count($aRanks) > 0 ? max($aRanks) + 1 : 0;
  1299. }
  1300. array_multisort($aRanks, $res);
  1301. }
  1302. }
  1303. else
  1304. {
  1305. $res = $this->GetText();
  1306. }
  1307. return $res;
  1308. }
  1309. public function SetNodeAsArrayOfItems($aList)
  1310. {
  1311. $oNewNode = $this->ownerDocument->CreateElement($this->tagName);
  1312. if ($this->getAttribute('id') != '')
  1313. {
  1314. $oNewNode->setAttribute('id', $this->getAttribute('id'));
  1315. }
  1316. self::AddItemToNode($this->ownerDocument, $oNewNode, $aList);
  1317. $this->parentNode->RedefineChildNode($oNewNode);
  1318. }
  1319. protected static function AddItemToNode($oXmlDoc, $oXMLNode, $itemValue)
  1320. {
  1321. if (is_array($itemValue))
  1322. {
  1323. $oXmlItems = $oXmlDoc->CreateElement('items');
  1324. $oXMLNode->AppendChild($oXmlItems);
  1325. foreach($itemValue as $key => $item)
  1326. {
  1327. $oXmlItem = $oXmlDoc->CreateElement('item');
  1328. $oXmlItems->AppendChild($oXmlItem);
  1329. if (is_string($key))
  1330. {
  1331. $oXmlItem->SetAttribute('key', $key);
  1332. }
  1333. self::AddItemToNode($oXmlDoc, $oXmlItem, $item);
  1334. }
  1335. }
  1336. else
  1337. {
  1338. $oXmlText = $oXmlDoc->CreateTextNode((string) $itemValue);
  1339. $oXMLNode->AppendChild($oXmlText);
  1340. }
  1341. }
  1342. /**
  1343. * Helper to remove child nodes
  1344. */
  1345. public function DeleteChildren()
  1346. {
  1347. while (isset($this->firstChild))
  1348. {
  1349. if ($this->firstChild instanceof MFElement)
  1350. {
  1351. $this->firstChild->DeleteChildren();
  1352. }
  1353. $this->removeChild($this->firstChild);
  1354. }
  1355. }
  1356. /**
  1357. * Find the child node matching the given node
  1358. * @param MFElement $oRefNode The node to search for
  1359. * @param bool $sSearchId substitutes to the value of the 'id' attribute
  1360. */
  1361. public function FindExistingChildNode(MFElement $oRefNode, $sSearchId = null)
  1362. {
  1363. return self::FindNode($this, $oRefNode, $sSearchId);
  1364. }
  1365. /**
  1366. * Seems to work fine (and is about 10 times faster than above) EXCEPT for menus !!!!
  1367. * @param unknown_type $oParent
  1368. * @param unknown_type $oRefNode
  1369. * @param unknown_type $sSearchId
  1370. * @throws Exception
  1371. */
  1372. public static function FindNode(DOMNode $oParent, MFElement $oRefNode, $sSearchId = null)
  1373. {
  1374. $oRes = null;
  1375. if ($oParent instanceof DOMDocument)
  1376. {
  1377. $oDoc = $oParent->firstChild->ownerDocument;
  1378. $oRoot = $oParent;
  1379. }
  1380. else
  1381. {
  1382. $oDoc = $oParent->ownerDocument;
  1383. $oRoot = $oParent;
  1384. }
  1385. $oXPath = new DOMXPath($oDoc);
  1386. if ($oRefNode->hasAttribute('id'))
  1387. {
  1388. // Find the first element having the same tag name and id
  1389. if (!$sSearchId)
  1390. {
  1391. $sSearchId = $oRefNode->getAttribute('id');
  1392. }
  1393. $sXPath = './'.$oRefNode->tagName."[@id='$sSearchId']";
  1394. $oRes = $oXPath->query($sXPath, $oRoot)->item(0);
  1395. }
  1396. else
  1397. {
  1398. // Get the first one having the same tag name (ignore others)
  1399. $sXPath = './'.$oRefNode->tagName;
  1400. $oRes = $oXPath->query($sXPath, $oRoot)->item(0);
  1401. }
  1402. return $oRes;
  1403. }
  1404. public function ListActiveChildNodes($sTagName)
  1405. {
  1406. $sXPath = $sTagName."[not(@_alteration) or @_alteration!='removed']";
  1407. return $this->GetNodes($sXPath);
  1408. }
  1409. /**
  1410. * Check if the current node is under a node 'added' or 'altered'
  1411. * Usage: In such a case, the change must not be tracked
  1412. */
  1413. protected function IsInDefinition()
  1414. {
  1415. // Iterate through the parents: reset the flag if any of them has a flag set
  1416. for($oParent = $this ; $oParent instanceof MFElement ; $oParent = $oParent->parentNode)
  1417. {
  1418. if ($oParent->getAttribute('_alteration') != '')
  1419. {
  1420. return true;
  1421. }
  1422. }
  1423. return false;
  1424. }
  1425. /**
  1426. * Add a node and set the flags that will be used to compute the delta
  1427. * @param MFElement $oNode The node (including all subnodes) to add
  1428. */
  1429. public function AddChildNode(MFElement $oNode)
  1430. {
  1431. $oExisting = $this->FindExistingChildNode($oNode);
  1432. if ($oExisting)
  1433. {
  1434. if ($oExisting->getAttribute('_alteration') != 'removed')
  1435. {
  1436. throw new Exception("Attempting to add a node that already exists: $oNode->tagName (id: ".$oNode->getAttribute('id')."");
  1437. }
  1438. $oExisting->ReplaceWith($oNode);
  1439. $sFlag = 'replaced';
  1440. }
  1441. else
  1442. {
  1443. $this->appendChild($oNode);
  1444. $sFlag = 'added';
  1445. }
  1446. if (!$this->IsInDefinition())
  1447. {
  1448. $oNode->setAttribute('_alteration', $sFlag);
  1449. }
  1450. }
  1451. /**
  1452. * Modify a node and set the flags that will be used to compute the delta
  1453. * @param MFElement $oNode The node (including all subnodes) to set
  1454. */
  1455. public function RedefineChildNode(MFElement $oNode, $sSearchId = null)
  1456. {
  1457. $oExisting = $this->FindExistingChildNode($oNode, $sSearchId);
  1458. if (!$oExisting)
  1459. {
  1460. throw new Exception("Attempting to modify a non existing node: $oNode->tagName (id: ".$oNode->getAttribute('id').")");
  1461. }
  1462. if ($oExisting->getAttribute('_alteration') == 'removed')
  1463. {
  1464. throw new Exception("Attempting to modify a deleted node: $oNode->tagName (id: ".$oNode->getAttribute('id')."");
  1465. }
  1466. $oExisting->ReplaceWith($oNode);
  1467. if (!$this->IsInDefinition())
  1468. {
  1469. $oNode->setAttribute('_alteration', 'replaced');
  1470. }
  1471. }
  1472. /**
  1473. * Replaces a node by another one, making sure that recursive nodes are preserved
  1474. * @param MFElement $oNewNode The replacement
  1475. */
  1476. protected function ReplaceWith($oNewNode)
  1477. {
  1478. // Move the classes from the old node into the new one
  1479. foreach($this->GetNodes('class') as $oChild)
  1480. {
  1481. $oNewNode->appendChild($oChild);
  1482. }
  1483. $oParentNode = $this->parentNode;
  1484. $oParentNode->replaceChild($oNewNode, $this);
  1485. }
  1486. /**
  1487. * Remove a node and set the flags that will be used to compute the delta
  1488. */
  1489. public function Delete()
  1490. {
  1491. $oParent = $this->parentNode;
  1492. switch ($this->getAttribute('_alteration'))
  1493. {
  1494. case 'replaced':
  1495. $sFlag = 'removed';
  1496. break;
  1497. case 'added':
  1498. $sFlag = null;
  1499. break;
  1500. case 'removed':
  1501. throw new Exception("Attempting to remove a deleted node: $this->tagName (id: ".$this->getAttribute('id')."");
  1502. default:
  1503. $sFlag = 'removed';
  1504. if ($this->IsInDefinition())
  1505. {
  1506. $sFlag = null;
  1507. break;
  1508. }
  1509. }
  1510. if ($sFlag)
  1511. {
  1512. $this->setAttribute('_alteration', $sFlag);
  1513. $this->DeleteChildren();
  1514. }
  1515. else
  1516. {
  1517. // Remove the node entirely
  1518. $this->parentNode->removeChild($this);
  1519. }
  1520. }
  1521. /**
  1522. * Merge the current node into the given container
  1523. *
  1524. * @param DOMNode $oContainer An element or a document
  1525. * @param string $sSearchId The id to consider (could be blank)
  1526. * @param bool $bMustExist Throw an exception if the node must already be found (and not marked as deleted!)
  1527. */
  1528. public function MergeInto($oContainer, $sSearchId, $bMustExist)
  1529. {
  1530. $oTargetNode = $oContainer->FindExistingChildNode($this, $sSearchId);
  1531. if ($oTargetNode)
  1532. {
  1533. if ($oTargetNode->getAttribute('_alteration') == 'removed')
  1534. {
  1535. if ($bMustExist)
  1536. {
  1537. throw new Exception("XML datamodel loader: found mandatory node $this->tagName/$sSearchId marked as deleted in $oContainer->tagName");
  1538. }
  1539. // Beware: ImportNode(xxx, false) DOES NOT copy the node's attribute on *some* PHP versions (<5.2.17)
  1540. // So use this workaround to import a node and its attributes on *any* PHP version
  1541. $oTargetNode = $oContainer->ownerDocument->ImportNode($this->cloneNode(false), true);
  1542. $oContainer->AddChildNode($oTargetNode);
  1543. }
  1544. }
  1545. else
  1546. {
  1547. if ($bMustExist)
  1548. {
  1549. echo "Dumping parent node<br/>\n";
  1550. $oContainer->Dump();
  1551. throw new Exception("XML datamodel loader: could not find $this->tagName/$sSearchId in $oContainer->tagName");
  1552. }
  1553. // Beware: ImportNode(xxx, false) DOES NOT copy the node's attribute on *some* PHP versions (<5.2.17)
  1554. // So use this workaround to import a node and its attributes on *any* PHP version
  1555. $oTargetNode = $oContainer->ownerDocument->ImportNode($this->cloneNode(false), true);
  1556. $oContainer->AddChildNode($oTargetNode);
  1557. }
  1558. return $oTargetNode;
  1559. }
  1560. /**
  1561. * Renames a node and set the flags that will be used to compute the delta
  1562. * @param String $sNewId The new id
  1563. */
  1564. public function Rename($sId)
  1565. {
  1566. if (($this->getAttribute('_alteration') == 'replaced') || !$this->IsInDefinition())
  1567. {
  1568. $sOriginalId = $this->getAttribute('_old_id');
  1569. if ($sOriginalId == '')
  1570. {
  1571. $this->setAttribute('_old_id', $this->getAttribute('id'));
  1572. }
  1573. else if($sOriginalId == $sId)
  1574. {
  1575. $this->removeAttribute('_old_id');
  1576. }
  1577. }
  1578. $this->setAttribute('id', $sId);
  1579. }
  1580. /**
  1581. * Apply extensibility rules onto this node
  1582. * @param array aRules Array of rules (strings)
  1583. * @return void
  1584. */
  1585. public function RestrictExtensibility($aRules)
  1586. {
  1587. $oRulesNode = $this->GetOptionalElement('rules');
  1588. if ($oRulesNode)
  1589. {
  1590. $aCurrentRules = $oRulesNode->GetNodeAsArrayOfItems();
  1591. $aCurrentRules = array_merge($aCurrentRules, $aRules);
  1592. $oRulesNode->SetNodeAsArrayOfItems($aCurrentRules);
  1593. }
  1594. else
  1595. {
  1596. $oNewNode = $this->ownerDocument->CreateElement('rules');
  1597. $this->appendChild($oNewNode);
  1598. $oNewNode->SetNodeAsArrayOfItems($aRules);
  1599. }
  1600. }
  1601. /**
  1602. * Read extensibility rules for this node
  1603. * @return Array of rules (strings)
  1604. */
  1605. public function GetExtensibilityRules()
  1606. {
  1607. $aCurrentRules = array();
  1608. $oRulesNode = $this->GetOptionalElement('rules');
  1609. if ($oRulesNode)
  1610. {
  1611. $aCurrentRules = $oRulesNode->GetNodeAsArrayOfItems();
  1612. }
  1613. return $aCurrentRules;
  1614. }
  1615. }
  1616. /**
  1617. * MFDocument - formating rules for XML input/output
  1618. * @package ModelFactory
  1619. */
  1620. class MFDocument extends DOMDocument
  1621. {
  1622. public function __construct()
  1623. {
  1624. parent::__construct('1.0', 'UTF-8');
  1625. $this->registerNodeClass('DOMElement', 'MFElement');
  1626. $this->formatOutput = true; // indent (must be loaded with option LIBXML_NOBLANKS)
  1627. $this->preserveWhiteSpace = true; // otherwise the formatOutput option would have no effect
  1628. }
  1629. /**
  1630. * Overload of the standard API
  1631. */
  1632. public function load($filename, $options = 0)
  1633. {
  1634. parent::load($filename, LIBXML_NOBLANKS);
  1635. }
  1636. /**
  1637. * Overload of the standard API
  1638. */
  1639. public function loadXML($source, $options = 0)
  1640. {
  1641. parent::loadXML($source, LIBXML_NOBLANKS);
  1642. }
  1643. /**
  1644. * Overload the standard API
  1645. */
  1646. public function saveXML(DOMNode $node = null, $options = 0)
  1647. {
  1648. $oRootNode = $this->firstChild;
  1649. if (!$oRootNode)
  1650. {
  1651. $oRootNode = $this->createElement('itop_design'); // make sure that the document is not empty
  1652. $oRootNode->setAttribute('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance");
  1653. $this->appendChild($oRootNode);
  1654. }
  1655. return parent::saveXML();
  1656. }
  1657. /**
  1658. * For debugging purposes
  1659. */
  1660. public function Dump($bReturnRes = false)
  1661. {
  1662. $sXml = $this->saveXML();
  1663. if ($bReturnRes)
  1664. {
  1665. return $sXml;
  1666. }
  1667. else
  1668. {
  1669. echo "<pre>\n";
  1670. echo htmlentities($sXml);
  1671. echo "</pre>\n";
  1672. }
  1673. }
  1674. /**
  1675. * Find the child node matching the given node
  1676. * @param MFElement $oRefNode The node to search for
  1677. * @param bool $sSearchId substitutes to the value of the 'id' attribute
  1678. */
  1679. public function FindExistingChildNode(MFElement $oRefNode, $sSearchId = null)
  1680. {
  1681. return MFElement::FindNode($this, $oRefNode, $sSearchId);
  1682. }
  1683. /**
  1684. * Extracts some nodes from the DOM
  1685. * @param string $sXPath A XPath expression
  1686. * @return DOMNodeList
  1687. */
  1688. public function GetNodes($sXPath, $oContextNode = null)
  1689. {
  1690. $oXPath = new DOMXPath($this);
  1691. if (is_null($oContextNode))
  1692. {
  1693. return $oXPath->query($sXPath);
  1694. }
  1695. else
  1696. {
  1697. return $oXPath->query($sXPath, $oContextNode);
  1698. }
  1699. }
  1700. public function GetNodeById($sXPath, $sId, $oContextNode = null)
  1701. {
  1702. $oXPath = new DOMXPath($this);
  1703. $sXPath .= "[@id='$sId' and(not(@_alteration) or @_alteration!='removed')]";
  1704. if (is_null($oContextNode))
  1705. {
  1706. return $oXPath->query($sXPath);
  1707. }
  1708. else
  1709. {
  1710. return $oXPath->query($sXPath, $oContextNode);
  1711. }
  1712. }
  1713. }