modelfactory.class.inc.php 51 KB

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