modelfactory.class.inc.php 52 KB

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