run_query.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Tools to design OQL queries and test them
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once('../approot.inc.php');
  25. require_once(APPROOT.'/application/application.inc.php');
  26. require_once(APPROOT.'/application/itopwebpage.class.inc.php');
  27. require_once(APPROOT.'/application/startup.inc.php');
  28. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  29. LoginWebPage::DoLogin(true); // Check user rights and prompt if needed (must be admin)
  30. function ShowExamples($oP, $sExpression)
  31. {
  32. $bUsingExample = false;
  33. $aExamples = array(
  34. 'Pedagogic examples' => array(
  35. "Applications" => "SELECT Application",
  36. "Person having an 'A' in their name" => "SELECT Person AS B WHERE B.name LIKE '%A%'",
  37. "Servers having a name like dbserver1.demo.com or dbserver023.foo.fr" => "SELECT Server WHERE name REGEXP '^dbserver[0-9]+\\\\..+\\\\.[a-z]{2,3}$'",
  38. "Changes planned on new year's day" => "SELECT Change AS ch WHERE ch.start_date >= '2009-12-31' AND ch.end_date <= '2010-01-01'",
  39. "IPs in a range" => "SELECT InfrastructureCI AS dev WHERE INET_ATON(dev.management_ip) > INET_ATON('10.22.32.224') AND INET_ATON(dev.management_ip) < INET_ATON('10.22.32.255')"
  40. ),
  41. 'Usefull examples' => array(
  42. "NW interfaces of equipment in production for customer 'Demo'" => "SELECT NetworkInterface AS if JOIN InfrastructureCI AS dev ON if.device_id = dev.id WHERE if.status = 'production' AND dev.status = 'production' AND dev.owner_name = 'Demo' AND if.physical_type = 'ethernet'",
  43. "My tickets" => "SELECT Incident AS i WHERE i.agent_id = :current_contact_id",
  44. "People being owner of an active ticket" => "SELECT Person AS p JOIN Incident AS i ON i.agent_id = p.id WHERE i.status != 'Closed'",
  45. "Contracts terminating in the next thirty days" => "SELECT Contract AS c WHERE c.end_date > NOW() AND c.end_date < DATE_ADD(NOW(), INTERVAL 30 DAY)",
  46. "Orphan tickets (opened one hour ago, still not assigned)" => "SELECT Incident AS i WHERE i.start_date < DATE_SUB(NOW(), INTERVAL 60 MINUTE) AND i.status = 'New'",
  47. "Long lasting incidents (duration > 8 hours)" => "SELECT Incident AS i WHERE i.close_date > DATE_ADD(i.start_date, INTERVAL 8 HOUR)",
  48. ),
  49. );
  50. $aDisplayData = array();
  51. foreach($aExamples as $sTopic => $aQueries)
  52. {
  53. foreach($aQueries as $sDescription => $sOql)
  54. {
  55. $sHighlight = '';
  56. $sDisable = '';
  57. if ($sOql == $sExpression)
  58. {
  59. // this one is currently being tested, highlight it
  60. $sHighlight = "background-color:yellow;";
  61. $sDisable = 'disabled';
  62. // and remember we are testing a query of the list
  63. $bUsingExample = true;
  64. }
  65. //$aDisplayData[$sTopic][] = array(
  66. $aDisplayData[Dict::S('UI:RunQuery:QueryExamples')][] = array(
  67. 'desc' => "<div style=\"$sHighlight\">".htmlentities($sDescription, ENT_QUOTES, 'UTF-8')."</div>",
  68. 'oql' => "<div style=\"$sHighlight\">".htmlentities($sOql, ENT_QUOTES, 'UTF-8')."</div>",
  69. 'go' => "<form method=\"get\"><input type=\"hidden\" name=\"expression\" value=\"$sOql\"><input type=\"submit\" value=\"".Dict::S('UI:Button:Test')."\" $sDisable></form>\n",
  70. );
  71. }
  72. }
  73. $aDisplayConfig = array();
  74. $aDisplayConfig['desc'] = array('label' => Dict::S('UI:RunQuery:HeaderPurpose'), 'description' => Dict::S('UI:RunQuery:HeaderPurpose+'));
  75. $aDisplayConfig['oql'] = array('label' => Dict::S('UI:RunQuery:HeaderOQLExpression'), 'description' => Dict::S('UI:RunQuery:HeaderOQLExpression+'));
  76. $aDisplayConfig['go'] = array('label' => '', 'description' => '');
  77. foreach ($aDisplayData as $sTopic => $aQueriesDisplayData)
  78. {
  79. $bShowOpened = $bUsingExample;
  80. $oP->StartCollapsibleSection($sTopic, $bShowOpened);
  81. $oP->table($aDisplayConfig, $aQueriesDisplayData);
  82. $oP->EndCollapsibleSection();
  83. }
  84. }
  85. $sOperation = utils::ReadParam('operation', 'menu');
  86. $oAppContext = new ApplicationContext();
  87. $oP = new iTopWebPage(Dict::S('UI:RunQuery:Title'));
  88. // Main program
  89. $sExpression = utils::ReadParam('expression', '');
  90. $sEncoding = utils::ReadParam('encoding', 'oql');
  91. ShowExamples($oP, $sExpression);
  92. try
  93. {
  94. if ($sEncoding == 'crypted')
  95. {
  96. // Translate $sExpression into a oql expression
  97. $sClearText = base64_decode($sExpression);
  98. echo "<strong>FYI: '$sClearText'</strong><br/>\n";
  99. $oFilter = DBObjectSearch::unserialize($sExpression);
  100. $sExpression = $oFilter->ToOQL();
  101. }
  102. else
  103. {
  104. // leave $sExpression as is
  105. }
  106. $oP->add("<form method=\"get\">\n");
  107. $oP->add(Dict::S('UI:RunQuery:ExpressionToEvaluate')."<br/>\n");
  108. $oP->add("<textarea cols=\"120\" rows=\"8\" name=\"expression\">$sExpression</textarea>\n");
  109. $oP->add("<input type=\"submit\" value=\"".Dict::S('UI:Button:Evaluate')."\">\n");
  110. $oP->add("</form>\n");
  111. if (!empty($sExpression))
  112. {
  113. $oFilter = DBObjectSearch::FromOQL($sExpression);
  114. if ($oFilter)
  115. {
  116. $oP->add("<h3>Query results</h3>\n");
  117. $oResultBlock = new DisplayBlock($oFilter, 'list', false);
  118. $oResultBlock->Display($oP, 'runquery');
  119. $oP->p('');
  120. $oP->StartCollapsibleSection(Dict::S('UI:RunQuery:MoreInfo'), false);
  121. $oP->p(Dict::S('UI:RunQuery:DevelopedQuery').$oFilter->ToOQL());
  122. $oP->p(Dict::S('UI:RunQuery:SerializedFilter').$oFilter->serialize());
  123. $oP->EndCollapsibleSection();
  124. }
  125. }
  126. }
  127. catch(CoreException $e)
  128. {
  129. $oP->p('<b>'.Dict::Format('UI:RunQuery:Error', $e->getHtmlDesc()).'</b>');
  130. }
  131. catch(Exception $e)
  132. {
  133. $oP->p('<b>'.Dict::Format('UI:RunQuery:Error', $e->getMessage()).'</b>');
  134. }
  135. $oP->output();
  136. ?>