ITopConsultant.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. * ITop consultant home page
  18. * tool box
  19. * object model analysis
  20. * DB integrity check and repair
  21. *
  22. * @author Erwan Taloc <erwan.taloc@combodo.com>
  23. * @author Romain Quetiez <romain.quetiez@combodo.com>
  24. * @author Denis Flaven <denis.flaven@combodo.com>
  25. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  26. */
  27. function sexyclass($sClass, $sBaseArgs)
  28. {
  29. return "Class <a href=\"?$sBaseArgs&todo=showclass&class=$sClass\">$sClass</a>";
  30. }
  31. function sexyclasslist($aClasses, $sBaseArgs)
  32. {
  33. if (count($aClasses) == 0) return "";
  34. $aRes = array();
  35. foreach($aClasses as $sClass)
  36. {
  37. $aRes[] = sexyclass($sClass, $sBaseArgs);
  38. }
  39. return ("'".implode("', '", $aRes)."'");
  40. }
  41. function ShowClass($sClass, $sBaseArgs)
  42. {
  43. if (!MetaModel::IsValidClass($sClass))
  44. {
  45. echo "Invalid class, expecting a value in {".sexyclasslist(MetaModel::GetClasses(), $sBaseArgs)."}<br/>\n";
  46. return;
  47. }
  48. // en recursif jusque "": MetaModel::GetParentPersistentClass($sClass)
  49. $aProps["Root class"] = MetaModel::GetRootClass($sClass);
  50. $aProps["Parent classes"] = sexyclasslist(MetaModel::EnumParentClasses($sClass), $sBaseArgs);
  51. $aProps["Child classes"] = sexyclasslist(MetaModel::EnumChildClasses($sClass), $sBaseArgs);
  52. $aProps["Subclasses (children + pure PHP)"] = sexyclasslist(MetaModel::GetSubclasses($sClass), $sBaseArgs);
  53. $aProps["Description"] = MetaModel::GetClassDescription($sClass);
  54. $aProps["Autoincrement id?"] = MetaModel::IsAutoIncrementKey($sClass);
  55. $aProps["Key label"] = MetaModel::GetKeyLabel($sClass);
  56. $aProps["Name attribute"] = MetaModel::GetNameAttributeCode($sClass);
  57. $aProps["Reconciliation keys"] = implode(", ", MetaModel::GetReconcKeys($sClass));
  58. $aProps["DB key column"] = MetaModel::DBGetKey($sClass);
  59. $aProps["DB class column"] = MetaModel::DBGetClassField($sClass);
  60. $aProps["Is standalone?"] = MetaModel::IsStandaloneClass($sClass);
  61. foreach (MetaModel::ListAttributeDefs($sClass) as $oAttDef)
  62. {
  63. $aAttProps = array();
  64. $aAttProps["Direct field"] = $oAttDef->IsDirectField();
  65. $aAttProps["External key"] = $oAttDef->IsExternalKey();
  66. $aAttProps["External field"] = $oAttDef->IsExternalField();
  67. $aAttProps["Link set"] = $oAttDef->IsLinkSet();
  68. $aAttProps["Code"] = $oAttDef->GetCode();
  69. $aAttProps["Label"] = $oAttDef->GetLabel();
  70. $aAttProps["Description"] = $oAttDef->GetDescription();
  71. $oValDef = $oAttDef->GetValuesDef();
  72. if (is_object($oValDef))
  73. {
  74. //$aAttProps["Allowed values"] = $oValDef->Describe();
  75. $aAttProps["Allowed values"] = "... object of class ".get_class($oValDef);
  76. }
  77. else
  78. {
  79. $aAttProps["Allowed values"] = "";
  80. }
  81. // MetaModel::IsAttributeInZList($sClass, $sListCode, $sAttCodeOrFltCode)
  82. }
  83. // $aProps["Description"] = MetaModel::DBGetTable($sClass, $sAttCode = null)
  84. $aAttributes = array();
  85. foreach (MetaModel::GetClassFilterDefs($sClass) as $oFilterDef)
  86. {
  87. $aAttProps = array();
  88. $aAttProps["Label"] = $oFilterDef->GetLabel();
  89. $aOpDescs = array();
  90. foreach ($oFilterDef->GetOperators() as $sOpCode => $sOpDescription)
  91. {
  92. $sIsTheLooser = ($sOpCode == $oFilterDef->GetLooseOperator()) ? " (loose search)" : "";
  93. $aOpDescs[] = "$sOpCode ($sOpDescription)$sIsTheLooser";
  94. }
  95. $aAttProps["Operators"] = implode(" / ", $aOpDescs);
  96. $aAttributes[] = $aAttProps;
  97. }
  98. $aProps["Filters"] = MyHelpers::make_table_from_assoc_array($aAttributes);
  99. foreach ($aProps as $sKey => $sDesc)
  100. {
  101. echo "<h4>$sKey</h4>\n";
  102. echo "<p>$sDesc</p>\n";
  103. }
  104. }
  105. function ShowBizModel($sBaseArgs)
  106. {
  107. echo "<ul>\n";
  108. foreach(MetaModel::GetClasses() as $sClass)
  109. {
  110. echo "<li>".sexyclass($sClass, $sBaseArgs)."</li>\n";
  111. }
  112. echo "</ul>\n";
  113. }
  114. function ShowZLists($sBaseArgs)
  115. {
  116. $aData = array();
  117. // 1 row per class, header made after the first row keys
  118. //
  119. foreach(MetaModel::GetClasses() as $sClass)
  120. {
  121. $aRow = array();
  122. $aRow["_"] = $sClass;
  123. foreach (MetaModel::EnumZLists() as $sListCode)
  124. {
  125. $aRow[$sListCode] = implode(", ", MetaModel::GetZListItems($sClass, $sListCode));
  126. }
  127. $aData[] = $aRow;
  128. }
  129. echo MyHelpers::make_table_from_assoc_array($aData);
  130. }
  131. function ShowDatabaseInfo()
  132. {
  133. $aTables = array();
  134. foreach (CMDBSource::EnumTables() as $sTable)
  135. {
  136. $aTableData = array();
  137. $aTableData["Name"] = $sTable;
  138. $aTableDesc = CMDBSource::GetTableInfo($sTable);
  139. $aTableData["Fields"] = MyHelpers::make_table_from_assoc_array($aTableDesc["Fields"]);
  140. $aTables[$sTable] = $aTableData;
  141. }
  142. echo MyHelpers::make_table_from_assoc_array($aTables);
  143. }
  144. function CreateDB()
  145. {
  146. $sRes = "<p>Creating the DB...</p>\n";
  147. if (MetaModel::DBExists(false))
  148. {
  149. $sRes .= "<p>It appears that the DB already exists (at least one table).</p>\n";
  150. }
  151. else
  152. {
  153. MetaModel::DBCreate();
  154. $sRes .= "<p>Done!</p>\n";
  155. }
  156. return $sRes;
  157. }
  158. function DebugQuery($sConfigFile)
  159. {
  160. $sQuery = ReadParam("oql");
  161. if (empty($sQuery))
  162. {
  163. $sQueryTemplate = "SELECT Foo AS f JOIN Dummy AS D ON d.spirit = f.id WHERE f.age * d.height > TO_DAYS(NOW()) OR d.alive";
  164. }
  165. else
  166. {
  167. $sQueryTemplate = $sQuery;
  168. }
  169. echo "<form>\n";
  170. echo "<input type=\"hidden\" name=\"todo\" value=\"debugquery\">\n";
  171. echo "<input type=\"hidden\" name=\"config\" value=\"$sConfigFile\">\n";
  172. echo "<textarea name=\"oql\" rows=\"10\" cols=\"120\" name=\"csvdata\" wrap=\"soft\">$sQueryTemplate</textarea>\n";
  173. echo "<input type=\"submit\" name=\"foo\">\n";
  174. echo "</form>\n";
  175. if (empty($sQuery)) return;
  176. echo "<h1>Testing query</h1>\n";
  177. echo "<p>$sQuery</p>\n";
  178. echo "<h1>Follow up the query build</h1>\n";
  179. MetaModel::StartDebugQuery();
  180. $oFlt = DBObjectSearch::FromOQL($sQuery);
  181. echo "<p>To OQL: ".$oFlt->ToOQL()."</p>";
  182. $sSQL = MetaModel::MakeSelectQuery($oFlt);
  183. MetaModel::StopDebugQuery();
  184. echo "<h1>Explain</h1>\n";
  185. echo "<table border=\"1\">\n";
  186. foreach (CMDBSource::ExplainQuery($sSQL) as $aRow)
  187. {
  188. echo " <tr>\n";
  189. echo " <td>".implode('</td><td>', $aRow)."</td>\n";
  190. echo " </tr>\n";
  191. }
  192. echo "</table>\n";
  193. echo "<h1>Results</h1>\n";
  194. $oSet = new CMDBObjectSet($oFlt);
  195. echo $oSet; // __toString()
  196. }
  197. function DumpDatabase()
  198. {
  199. $aData = MetaModel::DBDump();
  200. foreach ($aData as $sTable => $aRows)
  201. {
  202. echo "<h1>".htmlentities($sTable)."</h1>\n";
  203. if (count($aRows) == 0)
  204. {
  205. echo "<p>no data</p>\n";
  206. }
  207. else
  208. {
  209. echo "<p>".count($aRows)." row(s)</p>\n";
  210. // Table header
  211. echo "<table border=\"1\">\n";
  212. echo "<tr>\n";
  213. foreach (reset($aRows) as $key => $value)
  214. {
  215. echo "<th>".htmlentities($key)."</th>";
  216. }
  217. echo "</tr>\n";
  218. // Table body
  219. foreach ($aRows as $aRow)
  220. {
  221. echo "<tr>\n";
  222. foreach ($aRow as $key => $value)
  223. {
  224. echo "<td>".htmlentities($value)."</td>";
  225. }
  226. echo "</tr>\n";
  227. }
  228. echo "</table>\n";
  229. }
  230. }
  231. }
  232. /////////////////////////////////////////////////////////////////////////////////////
  233. // Helper functions
  234. /////////////////////////////////////////////////////////////////////////////////////
  235. function printMenu($sConfigFile)
  236. {
  237. $sClassCount = count(MetaModel::GetClasses());
  238. $bHasDB = MetaModel::DBExists(false); // no need to be complete to consider that something already exists
  239. $sUrl = "?config=".urlencode($sConfigFile);
  240. echo "<div style=\"background-color:eeeeee; padding:10px;\">\n";
  241. echo "<h2>phpMyORM integration sandbox</h2>\n";
  242. echo "<h4>Target database: $sConfigFile</h4>\n";
  243. echo "<p>$sClassCount classes referenced in the model</p>\n";
  244. echo "<ul>";
  245. echo " <li><a href=\"$sUrl&todo=checkdictionary&categories=bizmodel&outputfilter=NotInDictionary\">Dictionary - missing entries (EN US)</a></li>";
  246. echo " <li><a href=\"$sUrl&todo=dictionarystats\">Dictionary - statistics by language</a></li>";
  247. echo " <li><a href=\"$sUrl&todo=checkmodel\">Biz model consistency</a></li>";
  248. echo " <li><a href=\"$sUrl&todo=showzlists\">Show ZLists</a></li>";
  249. echo " <li><a href=\"$sUrl&todo=showbizmodel\">Browse business model</a></li>";
  250. if ($bHasDB)
  251. {
  252. echo " <li><a href=\"$sUrl&todo=checkmodeltodb\">Concordance between Biz model and DB format</a></li>";
  253. echo " <li><a href=\"$sUrl&todo=checkdb\">DB integrity check</a></li>";
  254. echo " <li><a href=\"$sUrl&todo=userrightssetup\">Setup userrights (init DB)</a></li>";
  255. echo " <li><a href=\"$sUrl&todo=checkall\">Check business model, DB format and data integrity</a></li>";
  256. echo " <li><a href=\"$sUrl&todo=showtables\">Show Tables</a></li>";
  257. echo " <li><a href=\"$sUrl&todo=debugquery\">Test an OQL query (debug)</a></li>";
  258. echo " <li><a href=\"$sUrl&todo=dumpdb\">Dump database</a></li>";
  259. // echo " <li>".htmlentities($sUrl)."&amp;<b>todo=execsql</b>&amp;<b>sql=xxx</b>, to execute a specific sql request</li>";
  260. }
  261. else
  262. {
  263. echo " <li><a href=\"$sUrl&todo=createdb\">Create the DB</a></li>";
  264. }
  265. echo "</ul>";
  266. echo "</div>\n";
  267. }
  268. function printConfigList()
  269. {
  270. echo "<h2>phpMyORM integration sandbox</h2>\n";
  271. echo "<h4>Configuration sumary</h4>\n";
  272. $sBasePath = '..';
  273. $aConfigs = array();
  274. foreach(scandir($sBasePath) as $sFile)
  275. {
  276. if (preg_match('/^config-.+\\.php$/', $sFile)) $aConfigs[] = $sFile;
  277. }
  278. $aConfigDetails = array();
  279. foreach ($aConfigs as $sConfigFile)
  280. {
  281. $sRealPath = $sBasePath.'/'.$sConfigFile;
  282. $oConfig = new Config($sRealPath);
  283. $sAppModules = implode(', ', $oConfig->GetAppModules());
  284. $sDataModels = implode(', ', $oConfig->GetDataModels());
  285. $sAddons = implode(', ', $oConfig->GetAddons());
  286. $sDBSubname = (strlen($oConfig->GetDBSubname()) > 0) ? '('.$oConfig->GetDBSubname().')' : '';
  287. $sUrl = "?config=".urlencode($sRealPath);
  288. $sHLink = "<a href=\"$sUrl\">Manage <b>$sConfigFile</b></a></br>\n";
  289. $aConfigDetails[] = array('Config'=>$sHLink, 'Application'=>$sAppModules, 'Data models'=>$sDataModels, 'Addons'=>$sAddons, 'Database'=>$oConfig->GetDBHost().'/'.$oConfig->GetDBName().$sDBSubname.' as '.$oConfig->GetDBUser());
  290. }
  291. echo MyHelpers::make_table_from_assoc_array($aConfigDetails);
  292. }
  293. function ReadParam($sName, $defaultValue = "")
  294. {
  295. return isset($_REQUEST[$sName]) ? $_REQUEST[$sName] : $defaultValue;
  296. }
  297. function ReadMandatoryParam($sName)
  298. {
  299. $value = ReadParam($sName, null);
  300. if (is_null($value))
  301. {
  302. echo "<p>Missing mandatory argument <b>$sName</b></p>";
  303. exit;
  304. }
  305. return $value;
  306. }
  307. function DisplayDBFormatIssues($aErrors, $aSugFix, $sRepairUrl = "", $sSQLStatementArgName = "")
  308. {
  309. $aSQLFixes = array(); // each and every SQL repair statement
  310. if (count($aErrors) > 0)
  311. {
  312. echo "<div style=\"width:100%;padding:10px;background:#FFAAAA;display:;\">";
  313. echo "<h1>Wrong Database format</h1>\n";
  314. echo "<p>The current database is not consistent with the given business model. Please investigate.</p>\n";
  315. foreach ($aErrors as $sClass => $aTarget)
  316. {
  317. echo "<p>Wrong declaration (or DB format ?) for class <b>$sClass</b></p>\n";
  318. echo "<ul class=\"treeview\">\n";
  319. $i = 0;
  320. foreach ($aTarget as $sTarget => $aMessages)
  321. {
  322. echo "<p>Wrong declaration for attribute <b>$sTarget</b></p>\n";
  323. $sMsg = implode(' AND ', $aMessages);
  324. if (!empty($sRepairUrl))
  325. {
  326. $aSQLFixes = array_merge($aSQLFixes, $aSugFix[$sClass][$sTarget]);
  327. $sSQLFixes = implode('; ', $aSugFix[$sClass][$sTarget]);
  328. $sUrl = "$sRepairUrl&$sSQLStatementArgName=".urlencode($sSQLFixes);
  329. echo "<li>$sMsg (<a href=\"$sUrl\" title=\"".htmlentities($sSQLFixes)."\" target=\"_blank\">fix it now!</a>)</li>\n";
  330. }
  331. else
  332. {
  333. echo "<li>$sMsg (".htmlentities($sSQLFixes).")</li>\n";
  334. }
  335. $i++;
  336. }
  337. echo "</ul>\n";
  338. }
  339. if (count($aSQLFixes) > 1)
  340. {
  341. MetaModel::DBShowApplyForm($sRepairUrl, $sSQLStatementArgName, $aSQLFixes);
  342. }
  343. echo "<p>Aborting...</p>\n";
  344. echo "</div>\n";
  345. exit;
  346. }
  347. }
  348. /////////////////////////////////////////////////////////////////////////////////////////////////
  349. //
  350. // M a i n P r o g r a m
  351. //
  352. /////////////////////////////////////////////////////////////////////////////////////////////////
  353. require_once('../core/cmdbobject.class.inc.php');
  354. $sConfigFile = ReadParam("config", '');
  355. if (empty($sConfigFile))
  356. {
  357. printConfigList();
  358. exit;
  359. }
  360. MetaModel::Startup($sConfigFile, true); // allow missing DB
  361. $sBaseArgs = "config=".urlencode($sConfigFile);
  362. $sTodo = ReadParam("todo", "");
  363. if ($sTodo == 'execsql')
  364. {
  365. $sSql = ReadMandatoryParam("sql");
  366. $aSql = explode("##SEP##", $sSql);
  367. $sConfirm = ReadParam("confirm");
  368. if (empty($sConfirm) || ($sConfirm != "Yes"))
  369. {
  370. echo "<form method=\"post\" action=\"?$sBaseArgs\">\n";
  371. echo "<input type=\"hidden\" name=\"todo\" value=\"execsql\">\n";
  372. echo "<input type=\"hidden\" name=\"sql\" value=\"".htmlentities($sSql)."\">\n";
  373. if (count($aSql) == 1)
  374. {
  375. echo "Do you confirm that you want to execute this command: <b>".htmlentities($aSql[0])."</b> ?</br>\n";
  376. }
  377. else
  378. {
  379. $sAllQueries = "<li>".implode("</li>\n<li>", $aSql)."</li>\n";
  380. echo "Please confirm that you want to execute these commands: <ul style=\"font-size: smaller;\">".$sAllQueries."</ul>\n";
  381. }
  382. echo "<input type=\"submit\" name=\"confirm\" value=\"Yes\">\n";
  383. echo "</form>\n";
  384. }
  385. else
  386. {
  387. foreach ($aSql as $sOneSingleSql)
  388. {
  389. echo "Executing command: <b>$sOneSingleSql</b></br>\n";
  390. CMDBSource::Query($sOneSingleSql);
  391. echo "... done!</br>\n";
  392. }
  393. }
  394. }
  395. else
  396. {
  397. $sBaseUrl = "?$sBaseArgs&todo=execsql";
  398. switch ($sTodo)
  399. {
  400. case "createdb":
  401. // do NOT print the menu, because it will change...
  402. break;
  403. default:
  404. printMenu($sConfigFile);
  405. }
  406. switch ($sTodo)
  407. {
  408. case "showtables":
  409. ShowDatabaseInfo();
  410. break;
  411. case "showbizmodel":
  412. ShowBizModel($sBaseArgs);
  413. break;
  414. case "showclass":
  415. $sClass = ReadMandatoryParam("class");
  416. ShowClass($sClass, $sBaseArgs);
  417. break;
  418. case "showzlists":
  419. ShowZLists($sBaseArgs);
  420. break;
  421. case "debugquery":
  422. DebugQuery($sConfigFile);
  423. break;
  424. case "createdb":
  425. $sRes = CreateDB();
  426. // As the menu depends on the existence of the DB, we have to do display it right after the job is done
  427. printMenu($sConfigFile);
  428. echo $sRes;
  429. break;
  430. case "dictionarystats":
  431. echo "Dictionary: statistics by language<br/>\n";
  432. foreach (Dict::GetLanguages() as $sLanguageCode => $aLanguageData)
  433. {
  434. list($aMissing, $aUnexpected, $aNotTranslated, $aOK) = Dict::MakeStats($sLanguageCode, 'EN US');
  435. echo "<p>Stats for language: $sLanguageCode</p>\n";
  436. echo "<ul><li>Missing:".count($aMissing)."</li><li>Unexpected:".count($aUnexpected)."</li><li>NotTranslated:".count($aNotTranslated)."</li><li>OK:".count($aOK)."</li></ul>\n";
  437. }
  438. break;
  439. case "checkdictionary":
  440. $sCategories = ReadMandatoryParam("categories");
  441. $sOutputFilter = ReadParam("outputfilter", '');
  442. echo "Dictionary: missing entries (categories: $sCategories, output: '$sOutputFilter')</br>\n";
  443. echo "<pre>\n";
  444. echo MetaModel::MakeDictionaryTemplate($sCategories, $sOutputFilter);
  445. echo "</pre>\n";
  446. break;
  447. case "checkmodel":
  448. echo "Check definitions...</br>\n";
  449. MetaModel::CheckDefinitions();
  450. echo "done...</br>\n";
  451. break;
  452. case "checkmodeltodb":
  453. echo "Check DB format...</br>\n";
  454. list($aErrors, $aSugFix) = MetaModel::DBCheckFormat();
  455. DisplayDBFormatIssues($aErrors, $aSugFix, $sBaseUrl, $sSQLStatementArgName = "sql");
  456. echo "done...</br>\n";
  457. break;
  458. case "checkdb":
  459. echo "Check DB integrity...</br>\n";
  460. MetaModel::DBCheckIntegrity($sBaseUrl, "sql");
  461. echo "done...</br>\n";
  462. break;
  463. case "dumpdb":
  464. echo "Dump DB data...</br>\n";
  465. DumpDatabase();
  466. echo "done...</br>\n";
  467. break;
  468. case "userrightssetup":
  469. echo "Setup user rights module (init DB)...</br>\n";
  470. UserRights::Setup();
  471. echo "done...</br>\n";
  472. break;
  473. case "checkall":
  474. echo "Check definitions...</br>\n";
  475. MetaModel::CheckDefinitions();
  476. echo "done...</br>\n";
  477. echo "Check DB format...</br>\n";
  478. list($aErrors, $aSugFix) = MetaModel::DBCheckFormat();
  479. DisplayDBFormatIssues($aErrors, $aSugFix, $sBaseUrl, $sSQLStatementArgName = "sql");
  480. echo "done...</br>\n";
  481. echo "Check DB integrity...</br>\n";
  482. MetaModel::DBCheckIntegrity($sBaseUrl, "sql");
  483. echo "done...</br>\n";
  484. break;
  485. }
  486. }
  487. ?>