diff --git a/core/cmdbsource.class.inc.php b/core/cmdbsource.class.inc.php index d25ce4548..cdf31ab80 100644 --- a/core/cmdbsource.class.inc.php +++ b/core/cmdbsource.class.inc.php @@ -410,6 +410,24 @@ class CMDBSource return self::$m_aTablesInfo[strtolower($sTable)]; //return null; } + + public static function DumpTable($sTable) + { + $sSql = "SELECT * FROM `$sTable`"; + $result = mysql_query($sSql, self::$m_resDBLink); + if (!$result) + { + throw new MySQLException('Failed to issue SQL query', array('query' => $sSql)); + } + + $aRows = array(); + while ($aRow = mysql_fetch_array($result, MYSQL_ASSOC)) + { + $aRows[] = $aRow; + } + mysql_free_result($result); + return $aRows; + } } diff --git a/core/metamodel.class.php b/core/metamodel.class.php index 1d5ab7bf9..c05c47399 100644 --- a/core/metamodel.class.php +++ b/core/metamodel.class.php @@ -294,6 +294,28 @@ abstract class MetaModel // This attribute has been inherited (compound objects) return self::DBGetTable(self::$m_aAttribOrigins[$sClass][$sAttCode]); } + + final static protected function DBEnumTables() + { + // This API do not rely on our capability to query the DB and retrieve + // the list of existing tables + // Rather, it uses the list of expected tables, corresponding to the data model + $aTables = array(); + foreach (self::GetClasses() as $sClass) + { + if (self::IsAbstract($sClass)) continue; + $sTable = self::DBGetTable($sClass); + + // Could be completed later with all the classes that are using a given table + if (!array_key_exists($sTable, $aTables)) + { + $aTables[$sTable] = array(); + } + $aTables[$sTable][] = $sClass; + } + return $aTables; + } + final static public function DBGetKey($sClass) { self::_check_subclass($sClass); @@ -1998,6 +2020,17 @@ abstract class MetaModel // $sDoCreateAll = implode(" ; ", $aSQL); } + public static function DBDump() + { + $aDataDump = array(); + foreach (self::DBEnumTables() as $sTable => $aClasses) + { + $aRows = CMDBSource::DumpTable($sTable); + $aDataDump[$sTable] = $aRows; + } + return $aDataDump; + } + public static function DBCheckFormat() { $aErrors = array(); diff --git a/pages/ITopConsultant.php b/pages/ITopConsultant.php index f17ed1933..68dcb6dca 100644 --- a/pages/ITopConsultant.php +++ b/pages/ITopConsultant.php @@ -207,6 +207,46 @@ function DebugQuery($sConfigFile) $oSet = new CMDBObjectSet($oFlt); echo $oSet; // __toString() } + +function DumpDatabase() +{ + $aData = MetaModel::DBDump(); + foreach ($aData as $sTable => $aRows) + { + echo "

".htmlentities($sTable)."

\n"; + + if (count($aRows) == 0) + { + echo "

no data

\n"; + } + else + { + echo "

".count($aRows)." row(s)

\n"; + // Table header + echo "\n"; + echo "\n"; + foreach (reset($aRows) as $key => $value) + { + echo ""; + } + echo "\n"; + + // Table body + foreach ($aRows as $aRow) + { + echo "\n"; + foreach ($aRow as $key => $value) + { + echo ""; + } + echo "\n"; + } + + echo "
".htmlentities($key)."
".htmlentities($value)."
\n"; + } + } +} + ///////////////////////////////////////////////////////////////////////////////////// // Helper functions ///////////////////////////////////////////////////////////////////////////////////// @@ -234,6 +274,7 @@ function printMenu($sConfigFile) echo "
  • Check business model, DB format and data integrity
  • "; echo "
  • Show Tables
  • "; echo "
  • Test an OQL query (debug)
  • "; + echo "
  • Dump database
  • "; // echo "
  • ".htmlentities($sUrl)."&todo=execsql&sql=xxx, to execute a specific sql request
  • "; } else @@ -443,6 +484,11 @@ else MetaModel::DBCheckIntegrity($sBaseUrl, "sql"); echo "done...
    \n"; break; + case "dumpdb": + echo "Dump DB data...
    \n"; + DumpDatabase(); + echo "done...
    \n"; + break; case "userrightssetup": echo "Setup user rights module (init DB)...
    \n"; UserRights::Setup();