oqlexception.class.inc.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Special handling for OQL syntax errors
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. class OQLException extends CoreException
  25. {
  26. public function __construct($sIssue, $sInput, $iLine, $iCol, $sUnexpected, $aExpecting = null)
  27. {
  28. $this->m_MyIssue = $sIssue;
  29. $this->m_sInput = $sInput;
  30. $this->m_iLine = $iLine;
  31. $this->m_iCol = $iCol;
  32. $this->m_sUnexpected = $sUnexpected;
  33. $this->m_aExpecting = $aExpecting;
  34. if (is_null($this->m_aExpecting) || (count($this->m_aExpecting) == 0))
  35. {
  36. $sMessage = "$sIssue - found '{$this->m_sUnexpected}' at $iCol in '$sInput'";
  37. }
  38. else
  39. {
  40. $sExpectations = '{'.implode(', ', $this->m_aExpecting).'}';
  41. $sSuggest = self::FindClosestString($this->m_sUnexpected, $this->m_aExpecting);
  42. $sMessage = "$sIssue - found '{$this->m_sUnexpected}' at $iCol in '$sInput', expecting $sExpectations, I would suggest to use '$sSuggest'";
  43. }
  44. // make sure everything is assigned properly
  45. parent::__construct($sMessage, 0);
  46. }
  47. public function GetUserFriendlyDescription()
  48. {
  49. // Todo - translate all errors!
  50. return $this->getMessage();
  51. }
  52. public function getHtmlDesc($sHighlightHtmlBegin = '<span style="font-weight: bolder">', $sHighlightHtmlEnd = '</span>')
  53. {
  54. $sRet = htmlentities($this->m_MyIssue.", found '".$this->m_sUnexpected."' in: ", ENT_QUOTES, 'UTF-8');
  55. $sRet .= htmlentities(substr($this->m_sInput, 0, $this->m_iCol), ENT_QUOTES, 'UTF-8');
  56. $sRet .= $sHighlightHtmlBegin.htmlentities(substr($this->m_sInput, $this->m_iCol, strlen($this->m_sUnexpected)), ENT_QUOTES, 'UTF-8').$sHighlightHtmlEnd;
  57. $sRet .= htmlentities(substr($this->m_sInput, $this->m_iCol + strlen($this->m_sUnexpected)), ENT_QUOTES, 'UTF-8');
  58. if (!is_null($this->m_aExpecting) && (count($this->m_aExpecting) > 0))
  59. {
  60. if (count($this->m_aExpecting) < 30)
  61. {
  62. $sExpectations = '{'.implode(', ', $this->m_aExpecting).'}';
  63. $sRet .= ", expecting ".htmlentities($sExpectations, ENT_QUOTES, 'UTF-8');
  64. }
  65. $sSuggest = self::FindClosestString($this->m_sUnexpected, $this->m_aExpecting);
  66. if (strlen($sSuggest) > 0)
  67. {
  68. $sRet .= ", I would suggest to use '$sHighlightHtmlBegin".htmlentities($sSuggest, ENT_QUOTES, 'UTF-8')."$sHighlightHtmlEnd'";
  69. }
  70. }
  71. return $sRet;
  72. }
  73. public function GetIssue()
  74. {
  75. return $this->m_MyIssue;
  76. }
  77. public function GetSuggestions()
  78. {
  79. return $this->m_aExpecting;
  80. }
  81. public function GetWrongWord()
  82. {
  83. return $this->m_sUnexpected;
  84. }
  85. public function GetColumn()
  86. {
  87. return $this->m_iCol;
  88. }
  89. static public function FindClosestString($sInput, $aDictionary)
  90. {
  91. // no shortest distance found, yet
  92. $fShortest = -1;
  93. $sRet = '';
  94. // loop through words to find the closest
  95. foreach ($aDictionary as $sSuggestion)
  96. {
  97. // calculate the distance between the input string and the suggested one
  98. $fDist = levenshtein($sInput, $sSuggestion);
  99. if ($fDist == 0)
  100. {
  101. // Exact match
  102. return $sSuggestion;
  103. }
  104. if (($fDist <= 3) && ($fShortest < 0 || $fDist <= $fShortest))
  105. {
  106. // set the closest match, and shortest distance
  107. $sRet = $sSuggestion;
  108. $fShortest = $fDist;
  109. }
  110. }
  111. return $sRet;
  112. }
  113. }
  114. ?>