modelfactory.class.inc.php 52 KB

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