expression.class.inc.php 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. <?php
  2. // Copyright (C) 2010-2016 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. * General definition of an expression tree (could be OQL, SQL or whatever)
  20. *
  21. * @copyright Copyright (C) 2010-2016 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. class MissingQueryArgument extends CoreException
  25. {
  26. }
  27. abstract class Expression
  28. {
  29. /**
  30. * Perform a deep clone (as opposed to "clone" which does copy a reference to the underlying objects)
  31. **/
  32. public function DeepClone()
  33. {
  34. return unserialize(serialize($this));
  35. }
  36. // recursive translation of identifiers
  37. abstract public function GetUnresolvedFields($sAlias, &$aUnresolved);
  38. abstract public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true);
  39. // recursive rendering (aArgs used as input by default, or used as output if bRetrofitParams set to True
  40. abstract public function Render(&$aArgs = null, $bRetrofitParams = false);
  41. abstract public function ApplyParameters($aArgs);
  42. // recursively builds an array of class => fieldname
  43. abstract public function ListRequiredFields();
  44. // recursively list field parents ($aTable = array of sParent => dummy)
  45. abstract public function CollectUsedParents(&$aTable);
  46. abstract public function IsTrue();
  47. // recursively builds an array of [classAlias][fieldName] => value
  48. abstract public function ListConstantFields();
  49. public function RequiresField($sClass, $sFieldName)
  50. {
  51. // #@# todo - optimize : this is called quite often when building a single query !
  52. $aRequired = $this->ListRequiredFields();
  53. if (!in_array($sClass.'.'.$sFieldName, $aRequired)) return false;
  54. return true;
  55. }
  56. public function serialize()
  57. {
  58. return base64_encode($this->Render());
  59. }
  60. static public function unserialize($sValue)
  61. {
  62. return self::FromOQL(base64_decode($sValue));
  63. }
  64. static public function FromOQL($sConditionExpr)
  65. {
  66. $oOql = new OqlInterpreter($sConditionExpr);
  67. $oExpression = $oOql->ParseExpression();
  68. return $oExpression;
  69. }
  70. static public function FromSQL($sSQL)
  71. {
  72. $oSql = new SQLExpression($sSQL);
  73. return $oSql;
  74. }
  75. public function LogAnd($oExpr)
  76. {
  77. if ($this->IsTrue()) return clone $oExpr;
  78. if ($oExpr->IsTrue()) return clone $this;
  79. return new BinaryExpression($this, 'AND', $oExpr);
  80. }
  81. public function LogOr($oExpr)
  82. {
  83. return new BinaryExpression($this, 'OR', $oExpr);
  84. }
  85. abstract public function RenameParam($sOldName, $sNewName);
  86. abstract public function RenameAlias($sOldName, $sNewName);
  87. /**
  88. * Make the most relevant label, given the value of the expression
  89. *
  90. * @param DBSearch oFilter The context in which this expression has been used
  91. * @param string sValue The value returned by the query, for this expression
  92. * @param string sDefault The default value if no relevant label could be computed
  93. * @return The label
  94. */
  95. public function MakeValueLabel($oFilter, $sValue, $sDefault)
  96. {
  97. return $sDefault;
  98. }
  99. }
  100. class SQLExpression extends Expression
  101. {
  102. protected $m_sSQL;
  103. public function __construct($sSQL)
  104. {
  105. $this->m_sSQL = $sSQL;
  106. }
  107. public function IsTrue()
  108. {
  109. return false;
  110. }
  111. // recursive rendering
  112. public function Render(&$aArgs = null, $bRetrofitParams = false)
  113. {
  114. return $this->m_sSQL;
  115. }
  116. public function ApplyParameters($aArgs)
  117. {
  118. }
  119. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  120. {
  121. }
  122. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  123. {
  124. return clone $this;
  125. }
  126. public function ListRequiredFields()
  127. {
  128. return array();
  129. }
  130. public function CollectUsedParents(&$aTable)
  131. {
  132. }
  133. public function ListConstantFields()
  134. {
  135. return array();
  136. }
  137. public function RenameParam($sOldName, $sNewName)
  138. {
  139. // Do nothing, since there is nothing to rename
  140. }
  141. public function RenameAlias($sOldName, $sNewName)
  142. {
  143. // Do nothing, since there is nothing to rename
  144. }
  145. }
  146. class BinaryExpression extends Expression
  147. {
  148. protected $m_oLeftExpr; // filter code or an SQL expression (later?)
  149. protected $m_oRightExpr;
  150. protected $m_sOperator;
  151. public function __construct($oLeftExpr, $sOperator, $oRightExpr)
  152. {
  153. if (!is_object($oLeftExpr))
  154. {
  155. throw new CoreException('Expecting an Expression object on the left hand', array('found_type' => gettype($oLeftExpr)));
  156. }
  157. if (!is_object($oRightExpr))
  158. {
  159. throw new CoreException('Expecting an Expression object on the right hand', array('found_type' => gettype($oRightExpr)));
  160. }
  161. if (!$oLeftExpr instanceof Expression)
  162. {
  163. throw new CoreException('Expecting an Expression object on the left hand', array('found_class' => get_class($oLeftExpr)));
  164. }
  165. if (!$oRightExpr instanceof Expression)
  166. {
  167. throw new CoreException('Expecting an Expression object on the right hand', array('found_class' => get_class($oRightExpr)));
  168. }
  169. $this->m_oLeftExpr = $oLeftExpr;
  170. $this->m_oRightExpr = $oRightExpr;
  171. $this->m_sOperator = $sOperator;
  172. }
  173. public function IsTrue()
  174. {
  175. // return true if we are certain that it will be true
  176. if ($this->m_sOperator == 'AND')
  177. {
  178. if ($this->m_oLeftExpr->IsTrue() && $this->m_oRightExpr->IsTrue()) return true;
  179. }
  180. elseif ($this->m_sOperator == 'OR')
  181. {
  182. if ($this->m_oLeftExpr->IsTrue() || $this->m_oRightExpr->IsTrue()) return true;
  183. }
  184. return false;
  185. }
  186. public function GetLeftExpr()
  187. {
  188. return $this->m_oLeftExpr;
  189. }
  190. public function GetRightExpr()
  191. {
  192. return $this->m_oRightExpr;
  193. }
  194. public function GetOperator()
  195. {
  196. return $this->m_sOperator;
  197. }
  198. // recursive rendering
  199. public function Render(&$aArgs = null, $bRetrofitParams = false)
  200. {
  201. $sOperator = $this->GetOperator();
  202. $sLeft = $this->GetLeftExpr()->Render($aArgs, $bRetrofitParams);
  203. $sRight = $this->GetRightExpr()->Render($aArgs, $bRetrofitParams);
  204. return "($sLeft $sOperator $sRight)";
  205. }
  206. public function ApplyParameters($aArgs)
  207. {
  208. if ($this->m_oLeftExpr instanceof VariableExpression)
  209. {
  210. $this->m_oLeftExpr = $this->m_oLeftExpr->GetAsScalar($aArgs);
  211. }
  212. else //if ($this->m_oLeftExpr instanceof Expression)
  213. {
  214. $this->m_oLeftExpr->ApplyParameters($aArgs);
  215. }
  216. if ($this->m_oRightExpr instanceof VariableExpression)
  217. {
  218. $this->m_oRightExpr = $this->m_oRightExpr->GetAsScalar($aArgs);
  219. }
  220. else //if ($this->m_oRightExpr instanceof Expression)
  221. {
  222. $this->m_oRightExpr->ApplyParameters($aArgs);
  223. }
  224. }
  225. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  226. {
  227. $this->GetLeftExpr()->GetUnresolvedFields($sAlias, $aUnresolved);
  228. $this->GetRightExpr()->GetUnresolvedFields($sAlias, $aUnresolved);
  229. }
  230. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  231. {
  232. $oLeft = $this->GetLeftExpr()->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  233. $oRight = $this->GetRightExpr()->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  234. return new BinaryExpression($oLeft, $this->GetOperator(), $oRight);
  235. }
  236. public function ListRequiredFields()
  237. {
  238. $aLeft = $this->GetLeftExpr()->ListRequiredFields();
  239. $aRight = $this->GetRightExpr()->ListRequiredFields();
  240. return array_merge($aLeft, $aRight);
  241. }
  242. public function CollectUsedParents(&$aTable)
  243. {
  244. $this->GetLeftExpr()->CollectUsedParents($aTable);
  245. $this->GetRightExpr()->CollectUsedParents($aTable);
  246. }
  247. /**
  248. * List all constant expression of the form <field> = <scalar> or <field> = :<variable>
  249. * Could be extended to support <field> = <function><constant_expression>
  250. */
  251. public function ListConstantFields()
  252. {
  253. $aResult = array();
  254. if ($this->m_sOperator == '=')
  255. {
  256. if (($this->m_oLeftExpr instanceof FieldExpression) && ($this->m_oRightExpr instanceof ScalarExpression))
  257. {
  258. $aResult[$this->m_oLeftExpr->GetParent()][$this->m_oLeftExpr->GetName()] = $this->m_oRightExpr;
  259. }
  260. else if (($this->m_oRightExpr instanceof FieldExpression) && ($this->m_oLeftExpr instanceof ScalarExpression))
  261. {
  262. $aResult[$this->m_oRightExpr->GetParent()][$this->m_oRightExpr->GetName()] = $this->m_oLeftExpr;
  263. }
  264. else if (($this->m_oLeftExpr instanceof FieldExpression) && ($this->m_oRightExpr instanceof VariableExpression))
  265. {
  266. $aResult[$this->m_oLeftExpr->GetParent()][$this->m_oLeftExpr->GetName()] = $this->m_oRightExpr;
  267. }
  268. else if (($this->m_oRightExpr instanceof FieldExpression) && ($this->m_oLeftExpr instanceof VariableExpression))
  269. {
  270. $aResult[$this->m_oRightExpr->GetParent()][$this->m_oRightExpr->GetName()] = $this->m_oLeftExpr;
  271. }
  272. }
  273. else if ($this->m_sOperator == 'AND')
  274. {
  275. // Strictly, this should be done only for the AND operator
  276. $aResult = array_merge_recursive($this->m_oRightExpr->ListConstantFields(), $this->m_oLeftExpr->ListConstantFields());
  277. }
  278. return $aResult;
  279. }
  280. public function RenameParam($sOldName, $sNewName)
  281. {
  282. $this->GetLeftExpr()->RenameParam($sOldName, $sNewName);
  283. $this->GetRightExpr()->RenameParam($sOldName, $sNewName);
  284. }
  285. public function RenameAlias($sOldName, $sNewName)
  286. {
  287. $this->GetLeftExpr()->RenameAlias($sOldName, $sNewName);
  288. $this->GetRightExpr()->RenameAlias($sOldName, $sNewName);
  289. }
  290. }
  291. class UnaryExpression extends Expression
  292. {
  293. protected $m_value;
  294. public function __construct($value)
  295. {
  296. $this->m_value = $value;
  297. }
  298. public function IsTrue()
  299. {
  300. // return true if we are certain that it will be true
  301. return ($this->m_value == 1);
  302. }
  303. public function GetValue()
  304. {
  305. return $this->m_value;
  306. }
  307. // recursive rendering
  308. public function Render(&$aArgs = null, $bRetrofitParams = false)
  309. {
  310. return CMDBSource::Quote($this->m_value);
  311. }
  312. public function ApplyParameters($aArgs)
  313. {
  314. }
  315. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  316. {
  317. }
  318. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  319. {
  320. return clone $this;
  321. }
  322. public function ListRequiredFields()
  323. {
  324. return array();
  325. }
  326. public function CollectUsedParents(&$aTable)
  327. {
  328. }
  329. public function ListConstantFields()
  330. {
  331. return array();
  332. }
  333. public function RenameParam($sOldName, $sNewName)
  334. {
  335. // Do nothing
  336. // really ? what about :param{$iParamIndex} ??
  337. }
  338. public function RenameAlias($sOldName, $sNewName)
  339. {
  340. // Do nothing
  341. }
  342. }
  343. class ScalarExpression extends UnaryExpression
  344. {
  345. public function __construct($value)
  346. {
  347. if (!is_scalar($value) && !is_null($value) && (!$value instanceof OqlHexValue))
  348. {
  349. throw new CoreException('Attempt to create a scalar expression from a non scalar', array('var_type'=>gettype($value)));
  350. }
  351. parent::__construct($value);
  352. }
  353. // recursive rendering
  354. public function Render(&$aArgs = null, $bRetrofitParams = false)
  355. {
  356. if (is_null($this->m_value))
  357. {
  358. $sRet = 'NULL';
  359. }
  360. else
  361. {
  362. $sRet = CMDBSource::Quote($this->m_value);
  363. }
  364. return $sRet;
  365. }
  366. public function GetAsScalar($aArgs)
  367. {
  368. return clone $this;
  369. }
  370. }
  371. class TrueExpression extends ScalarExpression
  372. {
  373. public function __construct()
  374. {
  375. parent::__construct(1);
  376. }
  377. public function IsTrue()
  378. {
  379. return true;
  380. }
  381. }
  382. class FalseExpression extends ScalarExpression
  383. {
  384. public function __construct()
  385. {
  386. parent::__construct(0);
  387. }
  388. public function IsTrue()
  389. {
  390. return false;
  391. }
  392. }
  393. class FieldExpression extends UnaryExpression
  394. {
  395. protected $m_sParent;
  396. protected $m_sName;
  397. public function __construct($sName, $sParent = '')
  398. {
  399. parent::__construct("$sParent.$sName");
  400. $this->m_sParent = $sParent;
  401. $this->m_sName = $sName;
  402. }
  403. public function IsTrue()
  404. {
  405. // return true if we are certain that it will be true
  406. return false;
  407. }
  408. public function GetParent() {return $this->m_sParent;}
  409. public function GetName() {return $this->m_sName;}
  410. // recursive rendering
  411. public function Render(&$aArgs = null, $bRetrofitParams = false)
  412. {
  413. if (empty($this->m_sParent))
  414. {
  415. return "`{$this->m_sName}`";
  416. }
  417. return "`{$this->m_sParent}`.`{$this->m_sName}`";
  418. }
  419. public function ListRequiredFields()
  420. {
  421. return array($this->m_sParent.'.'.$this->m_sName);
  422. }
  423. public function CollectUsedParents(&$aTable)
  424. {
  425. $aTable[$this->m_sParent] = true;
  426. }
  427. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  428. {
  429. if ($this->m_sParent == $sAlias)
  430. {
  431. // Add a reference to the field
  432. $aUnresolved[$this->m_sName] = $this;
  433. }
  434. elseif ($sAlias == '')
  435. {
  436. // An empty alias means "any alias"
  437. // In such a case, the results are indexed differently
  438. $aUnresolved[$this->m_sParent][$this->m_sName] = $this;
  439. }
  440. }
  441. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  442. {
  443. if (!array_key_exists($this->m_sParent, $aTranslationData))
  444. {
  445. if ($bMatchAll) throw new CoreException('Unknown parent id in translation table', array('parent_id' => $this->m_sParent, 'translation_table' => array_keys($aTranslationData)));
  446. return clone $this;
  447. }
  448. if (!array_key_exists($this->m_sName, $aTranslationData[$this->m_sParent]))
  449. {
  450. if (!array_key_exists('*', $aTranslationData[$this->m_sParent]))
  451. {
  452. // #@# debug - if ($bMatchAll) MyHelpers::var_dump_html($aTranslationData, true);
  453. if ($bMatchAll) throw new CoreException('Unknown name in translation table', array('name' => $this->m_sName, 'parent_id' => $this->m_sParent, 'translation_table' => array_keys($aTranslationData[$this->m_sParent])));
  454. return clone $this;
  455. }
  456. $sNewParent = $aTranslationData[$this->m_sParent]['*'];
  457. $sNewName = $this->m_sName;
  458. if ($bMarkFieldsAsResolved)
  459. {
  460. $oRet = new FieldExpressionResolved($sNewName, $sNewParent);
  461. }
  462. else
  463. {
  464. $oRet = new FieldExpression($sNewName, $sNewParent);
  465. }
  466. }
  467. else
  468. {
  469. $oRet = $aTranslationData[$this->m_sParent][$this->m_sName];
  470. }
  471. return $oRet;
  472. }
  473. /**
  474. * Make the most relevant label, given the value of the expression
  475. *
  476. * @param DBSearch oFilter The context in which this expression has been used
  477. * @param string sValue The value returned by the query, for this expression
  478. * @param string sDefault The default value if no relevant label could be computed
  479. * @return The label
  480. */
  481. public function MakeValueLabel($oFilter, $sValue, $sDefault)
  482. {
  483. $sAttCode = $this->GetName();
  484. $sParentAlias = $this->GetParent();
  485. $aSelectedClasses = $oFilter->GetSelectedClasses();
  486. $sClass = $aSelectedClasses[$sParentAlias];
  487. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  488. // Set a default value for the general case
  489. $sRes = $oAttDef->GetAsHtml($sValue);
  490. // Exceptions...
  491. if ($oAttDef->IsExternalKey())
  492. {
  493. $sObjClass = $oAttDef->GetTargetClass();
  494. $iObjKey = (int)$sValue;
  495. if ($iObjKey > 0)
  496. {
  497. $oObject = MetaModel::GetObject($sObjClass, $iObjKey);
  498. $sRes = $oObject->GetHyperlink();
  499. }
  500. else
  501. {
  502. // Undefined
  503. $sRes = DBObject::MakeHyperLink($sObjClass, 0);
  504. }
  505. }
  506. elseif ($oAttDef->IsExternalField())
  507. {
  508. if (is_null($sValue))
  509. {
  510. $sRes = Dict::S('UI:UndefinedObject');
  511. }
  512. }
  513. return $sRes;
  514. }
  515. public function RenameAlias($sOldName, $sNewName)
  516. {
  517. if ($this->m_sParent == $sOldName)
  518. {
  519. $this->m_sParent = $sNewName;
  520. }
  521. }
  522. }
  523. // Has been resolved into an SQL expression
  524. class FieldExpressionResolved extends FieldExpression
  525. {
  526. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  527. {
  528. }
  529. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  530. {
  531. return clone $this;
  532. }
  533. }
  534. class VariableExpression extends UnaryExpression
  535. {
  536. protected $m_sName;
  537. public function __construct($sName)
  538. {
  539. parent::__construct($sName);
  540. $this->m_sName = $sName;
  541. }
  542. public function IsTrue()
  543. {
  544. // return true if we are certain that it will be true
  545. return false;
  546. }
  547. public function GetName() {return $this->m_sName;}
  548. // recursive rendering
  549. public function Render(&$aArgs = null, $bRetrofitParams = false)
  550. {
  551. if (is_null($aArgs))
  552. {
  553. return ':'.$this->m_sName;
  554. }
  555. elseif (array_key_exists($this->m_sName, $aArgs))
  556. {
  557. $res = CMDBSource::Quote($aArgs[$this->m_sName]);
  558. if (is_array($res))
  559. {
  560. $res = implode(', ', $res);
  561. }
  562. return $res;
  563. }
  564. elseif (($iPos = strpos($this->m_sName, '->')) !== false)
  565. {
  566. $sParamName = substr($this->m_sName, 0, $iPos);
  567. if (array_key_exists($sParamName.'->object()', $aArgs))
  568. {
  569. $sAttCode = substr($this->m_sName, $iPos + 2);
  570. $oObj = $aArgs[$sParamName.'->object()'];
  571. if ($sAttCode == 'id')
  572. {
  573. return CMDBSource::Quote($oObj->GetKey());
  574. }
  575. return CMDBSource::Quote($oObj->Get($sAttCode));
  576. }
  577. }
  578. if ($bRetrofitParams)
  579. {
  580. $aArgs[$this->m_sName] = null;
  581. return ':'.$this->m_sName;
  582. }
  583. else
  584. {
  585. throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>array_keys($aArgs)));
  586. }
  587. }
  588. public function RenameParam($sOldName, $sNewName)
  589. {
  590. if ($this->m_sName == $sOldName)
  591. {
  592. $this->m_sName = $sNewName;
  593. }
  594. }
  595. public function GetAsScalar($aArgs)
  596. {
  597. $oRet = null;
  598. if (array_key_exists($this->m_sName, $aArgs))
  599. {
  600. $oRet = new ScalarExpression($aArgs[$this->m_sName]);
  601. }
  602. elseif (($iPos = strpos($this->m_sName, '->')) !== false)
  603. {
  604. $sParamName = substr($this->m_sName, 0, $iPos);
  605. if (array_key_exists($sParamName.'->object()', $aArgs))
  606. {
  607. $sAttCode = substr($this->m_sName, $iPos + 2);
  608. $oObj = $aArgs[$sParamName.'->object()'];
  609. if ($sAttCode == 'id')
  610. {
  611. $oRet = new ScalarExpression($oObj->GetKey());
  612. }
  613. elseif (MetaModel::IsValidAttCode(get_class($oObj), $sAttCode))
  614. {
  615. $oRet = new ScalarExpression($oObj->Get($sAttCode));
  616. }
  617. else
  618. {
  619. throw new CoreException("Query argument {$this->m_sName} not matching any attribute of class ".get_class($oObj));
  620. }
  621. }
  622. }
  623. if (is_null($oRet))
  624. {
  625. throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>array_keys($aArgs)));
  626. }
  627. return $oRet;
  628. }
  629. }
  630. // Temporary, until we implement functions and expression casting!
  631. // ... or until we implement a real full text search based in the MATCH() expression
  632. class ListExpression extends Expression
  633. {
  634. protected $m_aExpressions;
  635. public function __construct($aExpressions)
  636. {
  637. $this->m_aExpressions = $aExpressions;
  638. }
  639. public static function FromScalars($aScalars)
  640. {
  641. $aExpressions = array();
  642. foreach($aScalars as $value)
  643. {
  644. $aExpressions[] = new ScalarExpression($value);
  645. }
  646. return new ListExpression($aExpressions);
  647. }
  648. public function IsTrue()
  649. {
  650. // return true if we are certain that it will be true
  651. return false;
  652. }
  653. public function GetItems()
  654. {
  655. return $this->m_aExpressions;
  656. }
  657. // recursive rendering
  658. public function Render(&$aArgs = null, $bRetrofitParams = false)
  659. {
  660. $aRes = array();
  661. foreach ($this->m_aExpressions as $oExpr)
  662. {
  663. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  664. }
  665. return '('.implode(', ', $aRes).')';
  666. }
  667. public function ApplyParameters($aArgs)
  668. {
  669. $aRes = array();
  670. foreach ($this->m_aExpressions as $idx => $oExpr)
  671. {
  672. if ($oExpr instanceof VariableExpression)
  673. {
  674. $this->m_aExpressions[$idx] = $oExpr->GetAsScalar();
  675. }
  676. else
  677. {
  678. $oExpr->ApplyParameters($aArgs);
  679. }
  680. }
  681. }
  682. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  683. {
  684. foreach ($this->m_aExpressions as $oExpr)
  685. {
  686. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  687. }
  688. }
  689. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  690. {
  691. $aRes = array();
  692. foreach ($this->m_aExpressions as $oExpr)
  693. {
  694. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  695. }
  696. return new ListExpression($aRes);
  697. }
  698. public function ListRequiredFields()
  699. {
  700. $aRes = array();
  701. foreach ($this->m_aExpressions as $oExpr)
  702. {
  703. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  704. }
  705. return $aRes;
  706. }
  707. public function CollectUsedParents(&$aTable)
  708. {
  709. foreach ($this->m_aExpressions as $oExpr)
  710. {
  711. $oExpr->CollectUsedParents($aTable);
  712. }
  713. }
  714. public function ListConstantFields()
  715. {
  716. $aRes = array();
  717. foreach ($this->m_aExpressions as $oExpr)
  718. {
  719. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  720. }
  721. return $aRes;
  722. }
  723. public function RenameParam($sOldName, $sNewName)
  724. {
  725. $aRes = array();
  726. foreach ($this->m_aExpressions as $key => $oExpr)
  727. {
  728. $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  729. }
  730. }
  731. public function RenameAlias($sOldName, $sNewName)
  732. {
  733. $aRes = array();
  734. foreach ($this->m_aExpressions as $key => $oExpr)
  735. {
  736. $oExpr->RenameAlias($sOldName, $sNewName);
  737. }
  738. }
  739. }
  740. class FunctionExpression extends Expression
  741. {
  742. protected $m_sVerb;
  743. protected $m_aArgs; // array of expressions
  744. public function __construct($sVerb, $aArgExpressions)
  745. {
  746. $this->m_sVerb = $sVerb;
  747. $this->m_aArgs = $aArgExpressions;
  748. }
  749. public function IsTrue()
  750. {
  751. // return true if we are certain that it will be true
  752. return false;
  753. }
  754. public function GetVerb()
  755. {
  756. return $this->m_sVerb;
  757. }
  758. public function GetArgs()
  759. {
  760. return $this->m_aArgs;
  761. }
  762. // recursive rendering
  763. public function Render(&$aArgs = null, $bRetrofitParams = false)
  764. {
  765. $aRes = array();
  766. foreach ($this->m_aArgs as $iPos => $oExpr)
  767. {
  768. $aRes[] = $oExpr->Render($aArgs, $bRetrofitParams);
  769. }
  770. return $this->m_sVerb.'('.implode(', ', $aRes).')';
  771. }
  772. public function ApplyParameters($aArgs)
  773. {
  774. $aRes = array();
  775. foreach ($this->m_aArgs as $idx => $oExpr)
  776. {
  777. if ($oExpr instanceof VariableExpression)
  778. {
  779. $this->m_aArgs[$idx] = $oExpr->GetAsScalar($aArgs);
  780. }
  781. else
  782. {
  783. $oExpr->ApplyParameters($aArgs);
  784. }
  785. }
  786. }
  787. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  788. {
  789. foreach ($this->m_aArgs as $oExpr)
  790. {
  791. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  792. }
  793. }
  794. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  795. {
  796. $aRes = array();
  797. foreach ($this->m_aArgs as $oExpr)
  798. {
  799. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  800. }
  801. return new FunctionExpression($this->m_sVerb, $aRes);
  802. }
  803. public function ListRequiredFields()
  804. {
  805. $aRes = array();
  806. foreach ($this->m_aArgs as $oExpr)
  807. {
  808. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  809. }
  810. return $aRes;
  811. }
  812. public function CollectUsedParents(&$aTable)
  813. {
  814. foreach ($this->m_aArgs as $oExpr)
  815. {
  816. $oExpr->CollectUsedParents($aTable);
  817. }
  818. }
  819. public function ListConstantFields()
  820. {
  821. $aRes = array();
  822. foreach ($this->m_aArgs as $oExpr)
  823. {
  824. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  825. }
  826. return $aRes;
  827. }
  828. public function RenameParam($sOldName, $sNewName)
  829. {
  830. foreach ($this->m_aArgs as $key => $oExpr)
  831. {
  832. $this->m_aArgs[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  833. }
  834. }
  835. public function RenameAlias($sOldName, $sNewName)
  836. {
  837. foreach ($this->m_aArgs as $key => $oExpr)
  838. {
  839. $oExpr->RenameAlias($sOldName, $sNewName);
  840. }
  841. }
  842. /**
  843. * Make the most relevant label, given the value of the expression
  844. *
  845. * @param DBSearch oFilter The context in which this expression has been used
  846. * @param string sValue The value returned by the query, for this expression
  847. * @param string sDefault The default value if no relevant label could be computed
  848. * @return The label
  849. */
  850. public function MakeValueLabel($oFilter, $sValue, $sDefault)
  851. {
  852. static $aWeekDayToString = null;
  853. if (is_null($aWeekDayToString))
  854. {
  855. // Init the correspondance table
  856. $aWeekDayToString = array(
  857. 0 => Dict::S('DayOfWeek-Sunday'),
  858. 1 => Dict::S('DayOfWeek-Monday'),
  859. 2 => Dict::S('DayOfWeek-Tuesday'),
  860. 3 => Dict::S('DayOfWeek-Wednesday'),
  861. 4 => Dict::S('DayOfWeek-Thursday'),
  862. 5 => Dict::S('DayOfWeek-Friday'),
  863. 6 => Dict::S('DayOfWeek-Saturday')
  864. );
  865. }
  866. static $aMonthToString = null;
  867. if (is_null($aMonthToString))
  868. {
  869. // Init the correspondance table
  870. $aMonthToString = array(
  871. 1 => Dict::S('Month-01'),
  872. 2 => Dict::S('Month-02'),
  873. 3 => Dict::S('Month-03'),
  874. 4 => Dict::S('Month-04'),
  875. 5 => Dict::S('Month-05'),
  876. 6 => Dict::S('Month-06'),
  877. 7 => Dict::S('Month-07'),
  878. 8 => Dict::S('Month-08'),
  879. 9 => Dict::S('Month-09'),
  880. 10 => Dict::S('Month-10'),
  881. 11 => Dict::S('Month-11'),
  882. 12 => Dict::S('Month-12'),
  883. );
  884. }
  885. $sRes = $sDefault;
  886. if (strtolower($this->m_sVerb) == 'date_format')
  887. {
  888. $oFormatExpr = $this->m_aArgs[1];
  889. if ($oFormatExpr->Render() == "'%w'")
  890. {
  891. if (isset($aWeekDayToString[(int)$sValue]))
  892. {
  893. $sRes = $aWeekDayToString[(int)$sValue];
  894. }
  895. }
  896. elseif ($oFormatExpr->Render() == "'%Y-%m'")
  897. {
  898. // yyyy-mm => "yyyy month"
  899. $iMonth = (int) substr($sValue, -2); // the two last chars
  900. $sRes = substr($sValue, 0, 4).' '.$aMonthToString[$iMonth];
  901. }
  902. elseif ($oFormatExpr->Render() == "'%Y-%m-%d'")
  903. {
  904. // yyyy-mm-dd => "month d"
  905. $iMonth = (int) substr($sValue, 5, 2);
  906. $sRes = $aMonthToString[$iMonth].' '.(int)substr($sValue, -2);
  907. }
  908. }
  909. return $sRes;
  910. }
  911. }
  912. class IntervalExpression extends Expression
  913. {
  914. protected $m_oValue; // expression
  915. protected $m_sUnit;
  916. public function __construct($oValue, $sUnit)
  917. {
  918. $this->m_oValue = $oValue;
  919. $this->m_sUnit = $sUnit;
  920. }
  921. public function IsTrue()
  922. {
  923. // return true if we are certain that it will be true
  924. return false;
  925. }
  926. public function GetValue()
  927. {
  928. return $this->m_oValue;
  929. }
  930. public function GetUnit()
  931. {
  932. return $this->m_sUnit;
  933. }
  934. // recursive rendering
  935. public function Render(&$aArgs = null, $bRetrofitParams = false)
  936. {
  937. return 'INTERVAL '.$this->m_oValue->Render($aArgs, $bRetrofitParams).' '.$this->m_sUnit;
  938. }
  939. public function ApplyParameters($aArgs)
  940. {
  941. if ($this->m_oValue instanceof VariableExpression)
  942. {
  943. $this->m_oValue = $this->m_oValue->GetAsScalar($aArgs);
  944. }
  945. else
  946. {
  947. $this->m_oValue->ApplyParameters($aArgs);
  948. }
  949. }
  950. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  951. {
  952. $this->m_oValue->GetUnresolvedFields($sAlias, $aUnresolved);
  953. }
  954. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  955. {
  956. return new IntervalExpression($this->m_oValue->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved), $this->m_sUnit);
  957. }
  958. public function ListRequiredFields()
  959. {
  960. return array();
  961. }
  962. public function CollectUsedParents(&$aTable)
  963. {
  964. }
  965. public function ListConstantFields()
  966. {
  967. return array();
  968. }
  969. public function RenameParam($sOldName, $sNewName)
  970. {
  971. $this->m_oValue->RenameParam($sOldName, $sNewName);
  972. }
  973. public function RenameAlias($sOldName, $sNewName)
  974. {
  975. $this->m_oValue->RenameAlias($sOldName, $sNewName);
  976. }
  977. }
  978. class CharConcatExpression extends Expression
  979. {
  980. protected $m_aExpressions;
  981. public function __construct($aExpressions)
  982. {
  983. $this->m_aExpressions = $aExpressions;
  984. }
  985. public function IsTrue()
  986. {
  987. // return true if we are certain that it will be true
  988. return false;
  989. }
  990. public function GetItems()
  991. {
  992. return $this->m_aExpressions;
  993. }
  994. // recursive rendering
  995. public function Render(&$aArgs = null, $bRetrofitParams = false)
  996. {
  997. $aRes = array();
  998. foreach ($this->m_aExpressions as $oExpr)
  999. {
  1000. $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
  1001. // Concat will be globally NULL if one single argument is null !
  1002. $aRes[] = "COALESCE($sCol, '')";
  1003. }
  1004. return "CAST(CONCAT(".implode(', ', $aRes).") AS CHAR)";
  1005. }
  1006. public function ApplyParameters($aArgs)
  1007. {
  1008. $aRes = array();
  1009. foreach ($this->m_aExpressions as $idx => $oExpr)
  1010. {
  1011. if ($oExpr instanceof VariableExpression)
  1012. {
  1013. $this->m_aExpressions[$idx] = $oExpr->GetAsScalar();
  1014. }
  1015. else
  1016. {
  1017. $this->m_aExpressions->ApplyParameters($aArgs);
  1018. }
  1019. }
  1020. }
  1021. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  1022. {
  1023. foreach ($this->m_aExpressions as $oExpr)
  1024. {
  1025. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  1026. }
  1027. }
  1028. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  1029. {
  1030. $aRes = array();
  1031. foreach ($this->m_aExpressions as $oExpr)
  1032. {
  1033. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1034. }
  1035. return new CharConcatExpression($aRes);
  1036. }
  1037. public function ListRequiredFields()
  1038. {
  1039. $aRes = array();
  1040. foreach ($this->m_aExpressions as $oExpr)
  1041. {
  1042. $aRes = array_merge($aRes, $oExpr->ListRequiredFields());
  1043. }
  1044. return $aRes;
  1045. }
  1046. public function CollectUsedParents(&$aTable)
  1047. {
  1048. foreach ($this->m_aExpressions as $oExpr)
  1049. {
  1050. $oExpr->CollectUsedParents($aTable);
  1051. }
  1052. }
  1053. public function ListConstantFields()
  1054. {
  1055. $aRes = array();
  1056. foreach ($this->m_aExpressions as $oExpr)
  1057. {
  1058. $aRes = array_merge($aRes, $oExpr->ListConstantFields());
  1059. }
  1060. return $aRes;
  1061. }
  1062. public function RenameParam($sOldName, $sNewName)
  1063. {
  1064. foreach ($this->m_aExpressions as $key => $oExpr)
  1065. {
  1066. $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName);
  1067. }
  1068. }
  1069. public function RenameAlias($sOldName, $sNewName)
  1070. {
  1071. foreach ($this->m_aExpressions as $key => $oExpr)
  1072. {
  1073. $oExpr->RenameAlias($sOldName, $sNewName);
  1074. }
  1075. }
  1076. }
  1077. class CharConcatWSExpression extends CharConcatExpression
  1078. {
  1079. protected $m_separator;
  1080. public function __construct($separator, $aExpressions)
  1081. {
  1082. $this->m_separator = $separator;
  1083. parent::__construct($aExpressions);
  1084. }
  1085. // recursive rendering
  1086. public function Render(&$aArgs = null, $bRetrofitParams = false)
  1087. {
  1088. $aRes = array();
  1089. foreach ($this->m_aExpressions as $oExpr)
  1090. {
  1091. $sCol = $oExpr->Render($aArgs, $bRetrofitParams);
  1092. // Concat will be globally NULL if one single argument is null !
  1093. $aRes[] = "COALESCE($sCol, '')";
  1094. }
  1095. $sSep = CMDBSource::Quote($this->m_separator);
  1096. return "CAST(CONCAT_WS($sSep, ".implode(', ', $aRes).") AS CHAR)";
  1097. }
  1098. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  1099. {
  1100. $aRes = array();
  1101. foreach ($this->m_aExpressions as $oExpr)
  1102. {
  1103. $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1104. }
  1105. return new CharConcatWSExpression($this->m_separator, $aRes);
  1106. }
  1107. }
  1108. class QueryBuilderExpressions
  1109. {
  1110. protected $m_oConditionExpr;
  1111. protected $m_aSelectExpr;
  1112. protected $m_aGroupByExpr;
  1113. protected $m_aJoinFields;
  1114. protected $m_aClassIds;
  1115. public function __construct($oSearch, $aGroupByExpr = null)
  1116. {
  1117. $this->m_oConditionExpr = $oSearch->GetCriteria();
  1118. $this->m_aSelectExpr = array();
  1119. $this->m_aGroupByExpr = $aGroupByExpr;
  1120. $this->m_aJoinFields = array();
  1121. $this->m_aClassIds = array();
  1122. foreach($oSearch->GetJoinedClasses() as $sClassAlias => $sClass)
  1123. {
  1124. $this->m_aClassIds[$sClassAlias] = new FieldExpression('id', $sClassAlias);
  1125. }
  1126. }
  1127. public function GetSelect()
  1128. {
  1129. return $this->m_aSelectExpr;
  1130. }
  1131. public function GetGroupBy()
  1132. {
  1133. return $this->m_aGroupByExpr;
  1134. }
  1135. public function GetCondition()
  1136. {
  1137. return $this->m_oConditionExpr;
  1138. }
  1139. public function PopJoinField()
  1140. {
  1141. return array_pop($this->m_aJoinFields);
  1142. }
  1143. public function AddSelect($sAttAlias, $oExpression)
  1144. {
  1145. $this->m_aSelectExpr[$sAttAlias] = $oExpression;
  1146. }
  1147. //$oConditionTree = $oConditionTree->LogAnd($oFinalClassRestriction);
  1148. public function AddCondition($oExpression)
  1149. {
  1150. $this->m_oConditionExpr = $this->m_oConditionExpr->LogAnd($oExpression);
  1151. }
  1152. public function PushJoinField($oExpression)
  1153. {
  1154. array_push($this->m_aJoinFields, $oExpression);
  1155. }
  1156. /**
  1157. * Get tables representing the queried objects
  1158. * Could be further optimized: when the first join is an outer join, then the rest can be omitted
  1159. */
  1160. public function GetMandatoryTables(&$aTables = null)
  1161. {
  1162. if (is_null($aTables)) $aTables = array();
  1163. foreach($this->m_aClassIds as $sClass => $oExpression)
  1164. {
  1165. $oExpression->CollectUsedParents($aTables);
  1166. }
  1167. }
  1168. public function GetUnresolvedFields($sAlias, &$aUnresolved)
  1169. {
  1170. $this->m_oConditionExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  1171. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  1172. {
  1173. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  1174. }
  1175. if ($this->m_aGroupByExpr)
  1176. {
  1177. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  1178. {
  1179. $oExpr->GetUnresolvedFields($sAlias, $aUnresolved);
  1180. }
  1181. }
  1182. foreach($this->m_aJoinFields as $oExpression)
  1183. {
  1184. $oExpression->GetUnresolvedFields($sAlias, $aUnresolved);
  1185. }
  1186. }
  1187. public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true)
  1188. {
  1189. $this->m_oConditionExpr = $this->m_oConditionExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1190. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  1191. {
  1192. $this->m_aSelectExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1193. }
  1194. if ($this->m_aGroupByExpr)
  1195. {
  1196. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  1197. {
  1198. $this->m_aGroupByExpr[$sColAlias] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1199. }
  1200. }
  1201. foreach($this->m_aJoinFields as $index => $oExpression)
  1202. {
  1203. $this->m_aJoinFields[$index] = $oExpression->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1204. }
  1205. foreach($this->m_aClassIds as $sClass => $oExpression)
  1206. {
  1207. $this->m_aClassIds[$sClass] = $oExpression->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved);
  1208. }
  1209. }
  1210. public function RenameParam($sOldName, $sNewName)
  1211. {
  1212. $this->m_oConditionExpr->RenameParam($sOldName, $sNewName);
  1213. foreach($this->m_aSelectExpr as $sColAlias => $oExpr)
  1214. {
  1215. $this->m_aSelectExpr[$sColAlias] = $oExpr->RenameParam($sOldName, $sNewName);
  1216. }
  1217. if ($this->m_aGroupByExpr)
  1218. {
  1219. foreach($this->m_aGroupByExpr as $sColAlias => $oExpr)
  1220. {
  1221. $this->m_aGroupByExpr[$sColAlias] = $oExpr->RenameParam($sOldName, $sNewName);
  1222. }
  1223. }
  1224. foreach($this->m_aJoinFields as $index => $oExpression)
  1225. {
  1226. $this->m_aJoinFields[$index] = $oExpression->RenameParam($sOldName, $sNewName);
  1227. }
  1228. }
  1229. }
  1230. ?>