modelfactory.class.inc.php 52 KB

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