/**
  * Returns $table's CREATE definition
  *
  * @param string $db            the database name
  * @param string $table         the table name
  * @param string $crlf          the end of line sequence
  * @param string $error_url     the url to go back in case of error
  * @param bool   $do_relation   whether to include relation comments
  * @param bool   $do_comments   whether to include the pmadb-style column
  *                                comments as comments in the structure;
  *                                this is deprecated but the parameter is
  *                                left here because export.php calls
  *                                PMA_exportStructure() also for other
  *                                export types which use this parameter
  * @param bool   $do_mime       whether to include mime comments
  * @param bool   $show_dates    whether to include creation/update/check dates
  * @param bool   $add_semicolon whether to add semicolon and end-of-line
  *                                at the end
  * @param bool   $view          whether we're handling a view
  *
  * @return string resulting schema
  */
 public function getTableDef($db, $table, $crlf, $error_url, $do_relation, $do_comments, $do_mime, $show_dates = false, $add_semicolon = true, $view = false)
 {
     // set $cfgRelation here, because there is a chance that it's modified
     // since the class initialization
     global $cfgRelation;
     $schema_insert = '';
     /**
      * Gets fields properties
      */
     PMA_DBI_select_db($db);
     // Check if we can use Relations
     if ($do_relation && !empty($cfgRelation['relation'])) {
         // Find which tables are related with the current one and write it in
         // an array
         $res_rel = PMA_getForeigners($db, $table);
         if ($res_rel && count($res_rel) > 0) {
             $have_rel = true;
         } else {
             $have_rel = false;
         }
     } else {
         $have_rel = false;
     }
     // end if
     /**
      * Displays the table structure
      */
     $schema_insert .= '<table class="width100" cellspacing="1">';
     $columns_cnt = 4;
     if ($do_relation && $have_rel) {
         $columns_cnt++;
     }
     if ($do_comments && $cfgRelation['commwork']) {
         $columns_cnt++;
     }
     if ($do_mime && $cfgRelation['mimework']) {
         $columns_cnt++;
     }
     $schema_insert .= '<tr class="print-category">';
     $schema_insert .= '<th class="print">' . __('Column') . '</th>';
     $schema_insert .= '<td class="print"><strong>' . __('Type') . '</strong></td>';
     $schema_insert .= '<td class="print"><strong>' . __('Null') . '</strong></td>';
     $schema_insert .= '<td class="print"><strong>' . __('Default') . '</strong></td>';
     if ($do_relation && $have_rel) {
         $schema_insert .= '<td class="print"><strong>' . __('Links to') . '</strong></td>';
     }
     if ($do_comments) {
         $schema_insert .= '<td class="print"><strong>' . __('Comments') . '</strong></td>';
         $comments = PMA_getComments($db, $table);
     }
     if ($do_mime && $cfgRelation['mimework']) {
         $schema_insert .= '<td class="print"><strong>' . htmlspecialchars('MIME') . '</strong></td>';
         $mime_map = PMA_getMIME($db, $table, true);
     }
     $schema_insert .= '</tr>';
     $columns = PMA_DBI_get_columns($db, $table);
     /**
      * Get the unique keys in the table
      */
     $unique_keys = array();
     $keys = PMA_DBI_get_table_indexes($db, $table);
     foreach ($keys as $key) {
         if ($key['Non_unique'] == 0) {
             $unique_keys[] = $key['Column_name'];
         }
     }
     foreach ($columns as $column) {
         $schema_insert .= $this->formatOneColumnDefinition($column, $unique_keys);
         $field_name = $column['Field'];
         if ($do_relation && $have_rel) {
             $schema_insert .= '<td class="print">' . (isset($res_rel[$field_name]) ? htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') : '') . '</td>';
         }
         if ($do_comments && $cfgRelation['commwork']) {
             $schema_insert .= '<td class="print">' . (isset($comments[$field_name]) ? htmlspecialchars($comments[$field_name]) : '') . '</td>';
         }
         if ($do_mime && $cfgRelation['mimework']) {
             $schema_insert .= '<td class="print">' . (isset($mime_map[$field_name]) ? htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) : '') . '</td>';
         }
         $schema_insert .= '</tr>';
     }
     // end foreach
     $schema_insert .= '</table>';
     return $schema_insert;
 }
Exemple #2
0
/**
 * PMA_indexesDiffInTables() compares the source table indexes with target table indexes and keep the indexes to be added in target table in $add_indexes_array
 * indexes to be altered in $alter_indexes_array and indexes to be removed from target table in $remove_indexes_array.
 * Only keyname and uniqueness characteristic of the indexes are altered.
 *
 * @param string $src_db                name of source database
 * @param string $trg_db                name of target database
 * @param mixed  $src_link              connection established with source server
 * @param mixed  $trg_link              connection established with target server
 * @param array  $matching_tables       matching tables name
 * @param array  &$source_indexes       indexes of the source table
 * @param array  &$target_indexes       indexes of the target table
 * @param array  &$add_indexes_array    name of the column on which the index
 *                                      is to be added in the target table
 * @param array  &$alter_indexes_array  key name which needs to be altered
 * @param array  &$remove_indexes_array key name of the index which is to be
 *                                      removed from the target table
 * @param int    $table_counter         number of the matching table
 */
function PMA_indexesDiffInTables($src_db, $trg_db, $src_link, $trg_link, $matching_tables, &$source_indexes, &$target_indexes, &$add_indexes_array, &$alter_indexes_array, &$remove_indexes_array, $table_counter)
{
    //Gets indexes information for source and target table
    $source_indexes[$table_counter] = PMA_DBI_get_table_indexes($src_db, $matching_tables[$table_counter], $src_link);
    $target_indexes[$table_counter] = PMA_DBI_get_table_indexes($trg_db, $matching_tables[$table_counter], $trg_link);
    for ($a = 0; $a < sizeof($source_indexes[$table_counter]); $a++) {
        $found = false;
        $z = 0;
        //Compares key name and non_unique characteristic of source indexes with target indexes
        /*
         * @todo compare the length of each sub part
         */
        while ($z <= sizeof($target_indexes[$table_counter]) && $found == false) {
            if (isset($source_indexes[$table_counter][$a]) && isset($target_indexes[$table_counter][$z]) && $source_indexes[$table_counter][$a]['Key_name'] == $target_indexes[$table_counter][$z]['Key_name']) {
                $found = true;
                if ($source_indexes[$table_counter][$a]['Column_name'] != $target_indexes[$table_counter][$z]['Column_name'] || $source_indexes[$table_counter][$a]['Non_unique'] != $target_indexes[$table_counter][$z]['Non_unique']) {
                    if (!($source_indexes[$table_counter][$a]['Key_name'] == "PRIMARY" || $target_indexes[$table_counter][$z]['Key_name'] == 'PRIMARY')) {
                        $alter_indexes_array[$table_counter][] = $source_indexes[$table_counter][$a]['Key_name'];
                    }
                }
            }
            $z++;
        }
        if ($found === false) {
            if (!($source_indexes[$table_counter][$a]['Key_name'] == 'PRIMARY')) {
                $add_indexes_array[$table_counter][] = $source_indexes[$table_counter][$a]['Column_name'];
            }
        }
    }
    //Finds indexes that exist on target table but not on source table
    for ($b = 0; $b < sizeof($target_indexes[$table_counter]); $b++) {
        $found = false;
        $c = 0;
        while ($c <= sizeof($source_indexes[$table_counter]) && $found == false) {
            if ($target_indexes[$table_counter][$b]['Column_name'] == $source_indexes[$table_counter][$c]['Column_name']) {
                $found = true;
            }
            $c++;
        }
        if ($found === false) {
            $remove_indexes_array[$table_counter][] = $target_indexes[$table_counter][$b]['Key_name'];
        }
    }
}
Exemple #3
0
    /**
     * Creates tracking version of a table / view
     * (in other words: create a job to track future changes on the table).
     *
     * @param string $dbname       name of database
     * @param string $tablename    name of table
     * @param string $version      version
     * @param string $tracking_set set of tracking statements
     * @param bool   $is_view      if table is a view
     *
     * @static
     *
     * @return int result of version insertion
     */
    static public function createVersion($dbname, $tablename, $version, $tracking_set = '', $is_view = false)
    {
        global $sql_backquotes;

        if ($tracking_set == '') {
            $tracking_set = self::$default_tracking_set;
        }

        include_once './libraries/export/sql.php';

        $sql_backquotes = true;

        $date = date('Y-m-d H:i:s');

        // Get data definition snapshot of table

        $columns = PMA_DBI_get_columns($dbname, $tablename, null, true);
        // int indices to reduce size
        $columns = array_values($columns);
        // remove Privileges to reduce size
        for ($i = 0; $i < count($columns); $i++) {
            unset($columns[$i]['Privileges']);
        }

        $indexes = PMA_DBI_get_table_indexes($dbname, $tablename);

        $snapshot = array('COLUMNS' => $columns, 'INDEXES' => $indexes);
        $snapshot = serialize($snapshot);

        // Get DROP TABLE / DROP VIEW and CREATE TABLE SQL statements
        $sql_backquotes = true;

        $create_sql  = "";

        if (self::$add_drop_table == true && $is_view == false) {
            $create_sql .= self::getLogComment() .
                           'DROP TABLE IF EXISTS ' . PMA_backquote($tablename) . ";\n";

        }

        if (self::$add_drop_view == true && $is_view == true) {
            $create_sql .= self::getLogComment() .
                           'DROP VIEW IF EXISTS ' . PMA_backquote($tablename) . ";\n";
        }

        $create_sql .= self::getLogComment() .
                       PMA_getTableDef($dbname, $tablename, "\n", "");

        // Save version

        $sql_query = "/*NOTRACK*/\n" .
        "INSERT INTO" . self::$pma_table . " (" .
        "db_name, " .
        "table_name, " .
        "version, " .
        "date_created, " .
        "date_updated, " .
        "schema_snapshot, " .
        "schema_sql, " .
        "data_sql, " .
        "tracking " .
        ") " .
        "values (
        '" . PMA_sqlAddSlashes($dbname) . "',
        '" . PMA_sqlAddSlashes($tablename) . "',
        '" . PMA_sqlAddSlashes($version) . "',
        '" . PMA_sqlAddSlashes($date) . "',
        '" . PMA_sqlAddSlashes($date) . "',
        '" . PMA_sqlAddSlashes($snapshot) . "',
        '" . PMA_sqlAddSlashes($create_sql) . "',
        '" . PMA_sqlAddSlashes("\n") . "',
        '" . PMA_sqlAddSlashes(self::transformTrackingSet($tracking_set)) . "' )";

        $result = PMA_query_as_controluser($sql_query);

        if ($result) {
            // Deactivate previous version
            self::deactivateTracking($dbname, $tablename, ($version - 1));
        }

        return $result;
    }
Exemple #4
0
 /**
  * Outputs table's structure
  *
  * @param string $db          database name
  * @param string $table       table name
  * @param string $crlf        the end of line sequence
  * @param string $error_url   the url to go back in case of error
  * @param bool   $do_relation whether to include relation comments
  * @param bool   $do_comments whether to include the pmadb-style column comments
  *                            as comments in the structure; this is deprecated
  *                            but the parameter is left here because export.php
  *                            calls PMA_exportStructure() also for other export
  *                            types which use this parameter
  * @param bool   $do_mime     whether to include mime comments
  * @param bool   $dates       whether to include creation/update/check dates
  * @param string $export_mode 'create_table', 'triggers', 'create_view', 'stand_in'
  * @param string $export_type 'server', 'database', 'table'
  *
  * @return bool Whether it succeeded
  *
  * @access public
  */
 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $export_mode, $export_type)
 {
     global $cfgRelation;
     /* We do not export triggers */
     if ($export_mode == 'triggers') {
         return true;
     }
     /**
      * Get the unique keys in the table
      */
     $unique_keys = array();
     $keys = PMA_DBI_get_table_indexes($db, $table);
     foreach ($keys as $key) {
         if ($key['Non_unique'] == 0) {
             $unique_keys[] = $key['Column_name'];
         }
     }
     /**
      * Gets fields properties
      */
     PMA_DBI_select_db($db);
     // Check if we can use Relations
     if ($do_relation && !empty($cfgRelation['relation'])) {
         // Find which tables are related with the current one and write it in
         // an array
         $res_rel = PMA_getForeigners($db, $table);
         if ($res_rel && count($res_rel) > 0) {
             $have_rel = true;
         } else {
             $have_rel = false;
         }
     } else {
         $have_rel = false;
     }
     // end if
     /**
      * Displays the table structure
      */
     $buffer = $crlf . '%' . $crlf . '% ' . __('Structure') . ': ' . $table . $crlf . '%' . $crlf . ' \\begin{longtable}{';
     if (!PMA_exportOutputHandler($buffer)) {
         return false;
     }
     $columns_cnt = 4;
     $alignment = '|l|c|c|c|';
     if ($do_relation && $have_rel) {
         $columns_cnt++;
         $alignment .= 'l|';
     }
     if ($do_comments) {
         $columns_cnt++;
         $alignment .= 'l|';
     }
     if ($do_mime && $cfgRelation['mimework']) {
         $columns_cnt++;
         $alignment .= 'l|';
     }
     $buffer = $alignment . '} ' . $crlf;
     $header = ' \\hline ';
     $header .= '\\multicolumn{1}{|c|}{\\textbf{' . __('Column') . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Type') . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Null') . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Default') . '}}';
     if ($do_relation && $have_rel) {
         $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . __('Links to') . '}}';
     }
     if ($do_comments) {
         $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . __('Comments') . '}}';
         $comments = PMA_getComments($db, $table);
     }
     if ($do_mime && $cfgRelation['mimework']) {
         $header .= ' & \\multicolumn{1}{|c|}{\\textbf{MIME}}';
         $mime_map = PMA_getMIME($db, $table, true);
     }
     // Table caption for first page and label
     if (isset($GLOBALS['latex_caption'])) {
         $buffer .= ' \\caption{' . PMA_expandUserString($GLOBALS['latex_structure_caption'], 'PMA_texEscape', array('table' => $table, 'database' => $db)) . '} \\label{' . PMA_expandUserString($GLOBALS['latex_structure_label'], null, array('table' => $table, 'database' => $db)) . '} \\\\' . $crlf;
     }
     $buffer .= $header . ' \\\\ \\hline \\hline' . $crlf . '\\endfirsthead' . $crlf;
     // Table caption on next pages
     if (isset($GLOBALS['latex_caption'])) {
         $buffer .= ' \\caption{' . PMA_expandUserString($GLOBALS['latex_structure_continued_caption'], 'PMA_texEscape', array('table' => $table, 'database' => $db)) . '} \\\\ ' . $crlf;
     }
     $buffer .= $header . ' \\\\ \\hline \\hline \\endhead \\endfoot ' . $crlf;
     if (!PMA_exportOutputHandler($buffer)) {
         return false;
     }
     $fields = PMA_DBI_get_columns($db, $table);
     foreach ($fields as $row) {
         $extracted_fieldspec = PMA_extractFieldSpec($row['Type']);
         $type = $extracted_fieldspec['print_type'];
         if (empty($type)) {
             $type = ' ';
         }
         if (!isset($row['Default'])) {
             if ($row['Null'] != 'NO') {
                 $row['Default'] = 'NULL';
             }
         }
         $field_name = $row['Field'];
         $local_buffer = $field_name . "" . $type . "" . ($row['Null'] == '' || $row['Null'] == 'NO' ? __('No') : __('Yes')) . "" . (isset($row['Default']) ? $row['Default'] : '');
         if ($do_relation && $have_rel) {
             $local_buffer .= "";
             if (isset($res_rel[$field_name])) {
                 $local_buffer .= $res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')';
             }
         }
         if ($do_comments && $cfgRelation['commwork']) {
             $local_buffer .= "";
             if (isset($comments[$field_name])) {
                 $local_buffer .= $comments[$field_name];
             }
         }
         if ($do_mime && $cfgRelation['mimework']) {
             $local_buffer .= "";
             if (isset($mime_map[$field_name])) {
                 $local_buffer .= str_replace('_', '/', $mime_map[$field_name]['mimetype']);
             }
         }
         $local_buffer = PMA_texEscape($local_buffer);
         if ($row['Key'] == 'PRI') {
             $pos = strpos($local_buffer, "");
             $local_buffer = '\\textit{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos);
         }
         if (in_array($field_name, $unique_keys)) {
             $pos = strpos($local_buffer, "");
             $local_buffer = '\\textbf{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos);
         }
         $buffer = str_replace("", ' & ', $local_buffer);
         $buffer .= ' \\\\ \\hline ' . $crlf;
         if (!PMA_exportOutputHandler($buffer)) {
             return false;
         }
     }
     // end while
     $buffer = ' \\end{longtable}' . $crlf;
     return PMA_exportOutputHandler($buffer);
 }
Exemple #5
0
 /**
  * Outputs table's structure
  *
  * @param string  $db           database name
  * @param string  $table        table name
  * @param string  $crlf         the end of line sequence
  * @param string  $error_url    the url to go back in case of error
  * @param bool    $do_relation  whether to include relation comments
  * @param bool    $do_comments  whether to include the pmadb-style column comments
  *                                as comments in the structure; this is deprecated
  *                                but the parameter is left here because export.php
  *                                calls PMA_exportStructure() also for other export
  *                                types which use this parameter
  * @param bool    $do_mime      whether to include mime comments
  * @param bool    $dates        whether to include creation/update/check dates
  * @param string  $export_mode  'create_table', 'triggers', 'create_view', 'stand_in'
  * @param string  $export_type  'server', 'database', 'table'
  * @return  bool      Whether it succeeded
  *
  * @access  public
  */
 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $export_mode, $export_type)
 {
     global $cfgRelation;
     if (!PMA_exportOutputHandler('<h2>' . __('Table structure for table') . ' ' . htmlspecialchars($table) . '</h2>')) {
         return false;
     }
     /**
      * Get the unique keys in the table
      */
     $unique_keys = array();
     $keys = PMA_DBI_get_table_indexes($db, $table);
     foreach ($keys as $key) {
         if ($key['Non_unique'] == 0) {
             $unique_keys[] = $key['Column_name'];
         }
     }
     /**
      * Gets fields properties
      */
     PMA_DBI_select_db($db);
     // Check if we can use Relations
     if ($do_relation && !empty($cfgRelation['relation'])) {
         // Find which tables are related with the current one and write it in
         // an array
         $res_rel = PMA_getForeigners($db, $table);
         if ($res_rel && count($res_rel) > 0) {
             $have_rel = true;
         } else {
             $have_rel = false;
         }
     } else {
         $have_rel = false;
     }
     // end if
     /**
      * Displays the table structure
      */
     if (!PMA_exportOutputHandler('<table class="width100" cellspacing="1">')) {
         return false;
     }
     $columns_cnt = 4;
     if ($do_relation && $have_rel) {
         $columns_cnt++;
     }
     if ($do_comments && $cfgRelation['commwork']) {
         $columns_cnt++;
     }
     if ($do_mime && $cfgRelation['mimework']) {
         $columns_cnt++;
     }
     $schema_insert = '<tr class="print-category">';
     $schema_insert .= '<th class="print">' . __('Column') . '</th>';
     $schema_insert .= '<td class="print"><b>' . __('Type') . '</b></td>';
     $schema_insert .= '<td class="print"><b>' . __('Null') . '</b></td>';
     $schema_insert .= '<td class="print"><b>' . __('Default') . '</b></td>';
     if ($do_relation && $have_rel) {
         $schema_insert .= '<td class="print"><b>' . __('Links to') . '</b></td>';
     }
     if ($do_comments) {
         $schema_insert .= '<td class="print"><b>' . __('Comments') . '</b></td>';
         $comments = PMA_getComments($db, $table);
     }
     if ($do_mime && $cfgRelation['mimework']) {
         $schema_insert .= '<td class="print"><b>' . htmlspecialchars('MIME') . '</b></td>';
         $mime_map = PMA_getMIME($db, $table, true);
     }
     $schema_insert .= '</tr>';
     if (!PMA_exportOutputHandler($schema_insert)) {
         return false;
     }
     $columns = PMA_DBI_get_columns($db, $table);
     foreach ($columns as $column) {
         $schema_insert = '<tr class="print-category">';
         $extracted_fieldspec = PMA_extractFieldSpec($column['Type']);
         $type = htmlspecialchars($extracted_fieldspec['print_type']);
         if (empty($type)) {
             $type = '&nbsp;';
         }
         if (!isset($column['Default'])) {
             if ($column['Null'] != 'NO') {
                 $column['Default'] = 'NULL';
             }
         }
         $fmt_pre = '';
         $fmt_post = '';
         if (in_array($column['Field'], $unique_keys)) {
             $fmt_pre = '<b>' . $fmt_pre;
             $fmt_post = $fmt_post . '</b>';
         }
         if ($column['Key'] == 'PRI') {
             $fmt_pre = '<i>' . $fmt_pre;
             $fmt_post = $fmt_post . '</i>';
         }
         $schema_insert .= '<td class="print">' . $fmt_pre . htmlspecialchars($column['Field']) . $fmt_post . '</td>';
         $schema_insert .= '<td class="print">' . htmlspecialchars($type) . '</td>';
         $schema_insert .= '<td class="print">' . ($column['Null'] == '' || $column['Null'] == 'NO' ? __('No') : __('Yes')) . '</td>';
         $schema_insert .= '<td class="print">' . htmlspecialchars(isset($column['Default']) ? $column['Default'] : '') . '</td>';
         $field_name = $column['Field'];
         if ($do_relation && $have_rel) {
             $schema_insert .= '<td class="print">' . (isset($res_rel[$field_name]) ? htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') : '') . '</td>';
         }
         if ($do_comments && $cfgRelation['commwork']) {
             $schema_insert .= '<td class="print">' . (isset($comments[$field_name]) ? htmlspecialchars($comments[$field_name]) : '') . '</td>';
         }
         if ($do_mime && $cfgRelation['mimework']) {
             $schema_insert .= '<td class="print">' . (isset($mime_map[$field_name]) ? htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) : '') . '</td>';
         }
         $schema_insert .= '</tr>';
         if (!PMA_exportOutputHandler($schema_insert)) {
             return false;
         }
     }
     // end while
     return PMA_exportOutputHandler('</table>');
 }
Exemple #6
0
 /**
  * Outputs table's structure
  *
  * @param string $db          database name
  * @param string $table       table name
  * @param string $crlf        the end of line sequence
  * @param string $error_url   the url to go back in case of error
  * @param bool   $do_relation whether to include relation comments
  * @param bool   $do_comments whether to include the pmadb-style column comments
  *                            as comments in the structure; this is deprecated
  *                            but the parameter is left here because export.php
  *                            calls PMA_exportStructure() also for other export
  *                            types which use this parameter
  * @param bool   $do_mime     whether to include mime comments
  * @param bool   $dates       whether to include creation/update/check dates
  * @param string $export_mode 'create_table', 'triggers', 'create_view', 'stand_in'
  * @param string $export_type 'server', 'database', 'table'
  *
  * @return bool Whether it succeeded
  *
  * @access public
  */
 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $export_mode, $export_type)
 {
     global $cfgRelation;
     /* We do not export triggers */
     if ($export_mode == 'triggers') {
         return true;
     }
     /* Heading */
     $GLOBALS['odt_buffer'] .= '<text:h text:outline-level="2" text:style-name="Heading_2" text:is-list-header="true">' . __('Table structure for table') . ' ' . htmlspecialchars($table) . '</text:h>';
     /**
      * Get the unique keys in the table
      */
     $unique_keys = array();
     $keys = PMA_DBI_get_table_indexes($db, $table);
     foreach ($keys as $key) {
         if ($key['Non_unique'] == 0) {
             $unique_keys[] = $key['Column_name'];
         }
     }
     /**
      * Gets fields properties
      */
     PMA_DBI_select_db($db);
     // Check if we can use Relations
     if ($do_relation && !empty($cfgRelation['relation'])) {
         // Find which tables are related with the current one and write it in
         // an array
         $res_rel = PMA_getForeigners($db, $table);
         if ($res_rel && count($res_rel) > 0) {
             $have_rel = true;
         } else {
             $have_rel = false;
         }
     } else {
         $have_rel = false;
     }
     // end if
     /**
      * Displays the table structure
      */
     $GLOBALS['odt_buffer'] .= '<table:table table:name="' . htmlspecialchars($table) . '_data">';
     $columns_cnt = 4;
     if ($do_relation && $have_rel) {
         $columns_cnt++;
     }
     if ($do_comments) {
         $columns_cnt++;
     }
     if ($do_mime && $cfgRelation['mimework']) {
         $columns_cnt++;
     }
     $GLOBALS['odt_buffer'] .= '<table:table-column table:number-columns-repeated="' . $columns_cnt . '"/>';
     /* Header */
     $GLOBALS['odt_buffer'] .= '<table:table-row>';
     $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">' . '<text:p>' . __('Column') . '</text:p>' . '</table:table-cell>';
     $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">' . '<text:p>' . __('Type') . '</text:p>' . '</table:table-cell>';
     $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">' . '<text:p>' . __('Null') . '</text:p>' . '</table:table-cell>';
     $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">' . '<text:p>' . __('Default') . '</text:p>' . '</table:table-cell>';
     if ($do_relation && $have_rel) {
         $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">' . '<text:p>' . __('Links to') . '</text:p>' . '</table:table-cell>';
     }
     if ($do_comments) {
         $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">' . '<text:p>' . __('Comments') . '</text:p>' . '</table:table-cell>';
         $comments = PMA_getComments($db, $table);
     }
     if ($do_mime && $cfgRelation['mimework']) {
         $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">' . '<text:p>' . __('MIME type') . '</text:p>' . '</table:table-cell>';
         $mime_map = PMA_getMIME($db, $table, true);
     }
     $GLOBALS['odt_buffer'] .= '</table:table-row>';
     $columns = PMA_DBI_get_columns($db, $table);
     foreach ($columns as $column) {
         $field_name = $column['Field'];
         $GLOBALS['odt_buffer'] .= '<table:table-row>';
         $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">' . '<text:p>' . htmlspecialchars($field_name) . '</text:p>' . '</table:table-cell>';
         $extracted_fieldspec = PMA_extractFieldSpec($column['Type']);
         $type = htmlspecialchars($extracted_fieldspec['print_type']);
         if (empty($type)) {
             $type = '&nbsp;';
         }
         $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">' . '<text:p>' . htmlspecialchars($type) . '</text:p>' . '</table:table-cell>';
         if (!isset($column['Default'])) {
             if ($column['Null'] != 'NO') {
                 $column['Default'] = 'NULL';
             } else {
                 $column['Default'] = '';
             }
         } else {
             $column['Default'] = $column['Default'];
         }
         $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">' . '<text:p>' . ($column['Null'] == '' || $column['Null'] == 'NO' ? __('No') : __('Yes')) . '</text:p>' . '</table:table-cell>';
         $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">' . '<text:p>' . htmlspecialchars($column['Default']) . '</text:p>' . '</table:table-cell>';
         if ($do_relation && $have_rel) {
             if (isset($res_rel[$field_name])) {
                 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">' . '<text:p>' . htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') . '</text:p>' . '</table:table-cell>';
             }
         }
         if ($do_comments) {
             if (isset($comments[$field_name])) {
                 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">' . '<text:p>' . htmlspecialchars($comments[$field_name]) . '</text:p>' . '</table:table-cell>';
             } else {
                 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">' . '<text:p></text:p>' . '</table:table-cell>';
             }
         }
         if ($do_mime && $cfgRelation['mimework']) {
             if (isset($mime_map[$field_name])) {
                 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">' . '<text:p>' . htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) . '</text:p>' . '</table:table-cell>';
             } else {
                 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">' . '<text:p></text:p>' . '</table:table-cell>';
             }
         }
         $GLOBALS['odt_buffer'] .= '</table:table-row>';
     }
     // end while
     $GLOBALS['odt_buffer'] .= '</table:table>';
     return true;
 }
PMA_DBI_select_db($db);
$tables = PMA_DBI_get_tables($db);
$count = 0;
foreach ($tables as $table) {
    $comments = PMA_getComments($db, $table);
    echo '<div>' . "\n";
    echo '<h2>' . htmlspecialchars($table) . '</h2>' . "\n";
    /**
     * Gets table informations
     */
    $show_comment = PMA_Table::sGetStatusInfo($db, $table, 'TABLE_COMMENT');
    /**
     * Gets table keys and retains them
     */
    PMA_DBI_select_db($db);
    $indexes = PMA_DBI_get_table_indexes($db, $table);
    $primary = '';
    $indexes = array();
    $lastIndex = '';
    $indexes_info = array();
    $indexes_data = array();
    $pk_array = array();
    // will be use to emphasis prim. keys in the table
    // view
    foreach ($indexes as $row) {
        // Backups the list of primary keys
        if ($row['Key_name'] == 'PRIMARY') {
            $primary .= $row['Column_name'] . ', ';
            $pk_array[$row['Column_name']] = 1;
        }
        // Retains keys informations
 /**
  * Provides UNIQUE columns and INDEX columns present in criteria tables
  *
  * @param array $all_tables           Tables involved in the search
  * @param array $all_columns          Columns involved in the search
  * @param array $where_clause_columns Columns having criteria where clause
  *
  * @return array having UNIQUE and INDEX columns
  */
 private function _getIndexes($all_tables, $all_columns, $where_clause_columns)
 {
     $unique_columns = array();
     $index_columns = array();
     foreach ($all_tables as $table) {
         $indexes = PMA_DBI_get_table_indexes($this->_db, $table);
         foreach ($indexes as $index) {
             $column = $table . '.' . $index['Column_name'];
             if (isset($all_columns[$column])) {
                 if ($index['Non_unique'] == 0) {
                     if (isset($where_clause_columns[$column])) {
                         $unique_columns[$column] = 'Y';
                     } else {
                         $unique_columns[$column] = 'N';
                     }
                 } else {
                     if (isset($where_clause_columns[$column])) {
                         $index_columns[$column] = 'Y';
                     } else {
                         $index_columns[$column] = 'N';
                     }
                 }
             }
         }
         // end while (each index of a table)
     }
     // end while (each table)
     return array('unique' => $unique_columns, 'index' => $index_columns);
 }
 /**
  * Load index data for table
  *
  * @param string $table  table
  * @param string $schema schema
  *
  * @return boolean whether loading was successful
  */
 private static function _loadIndexes($table, $schema)
 {
     if (isset(PMA_Index::$_registry[$schema][$table])) {
         return true;
     }
     $_raw_indexes = PMA_DBI_get_table_indexes($schema, $table);
     foreach ($_raw_indexes as $_each_index) {
         $_each_index['Schema'] = $schema;
         if (!isset(PMA_Index::$_registry[$schema][$table][$_each_index['Key_name']])) {
             $key = new PMA_Index($_each_index);
             PMA_Index::$_registry[$schema][$table][$_each_index['Key_name']] = $key;
         } else {
             $key = PMA_Index::$_registry[$schema][$table][$_each_index['Key_name']];
         }
         $key->addColumn($_each_index);
     }
     return true;
 }
 /**
  * Returns $table's CREATE definition
  *
  * @param string $db            the database name
  * @param string $table         the table name
  * @param string $crlf          the end of line sequence
  * @param string $error_url     the url to go back in case of error
  * @param bool   $do_relation   whether to include relation comments
  * @param bool   $do_comments   whether to include the pmadb-style column
  *                                comments as comments in the structure;
  *                                this is deprecated but the parameter is
  *                                left here because export.php calls
  *                                $this->exportStructure() also for other
  *                                export types which use this parameter
  * @param bool   $do_mime       whether to include mime comments
  * @param bool   $show_dates    whether to include creation/update/check dates
  * @param bool   $add_semicolon whether to add semicolon and end-of-line
  *                              at the end
  * @param bool   $view          whether we're handling a view
  *
  * @return string resulting schema
  */
 function getTableDef($db, $table, $crlf, $error_url, $do_relation, $do_comments, $do_mime, $show_dates = false, $add_semicolon = true, $view = false)
 {
     global $cfgRelation;
     $text_output = '';
     /**
      * Get the unique keys in the table
      */
     $unique_keys = array();
     $keys = PMA_DBI_get_table_indexes($db, $table);
     foreach ($keys as $key) {
         if ($key['Non_unique'] == 0) {
             $unique_keys[] = $key['Column_name'];
         }
     }
     /**
      * Gets fields properties
      */
     PMA_DBI_select_db($db);
     // Check if we can use Relations
     if ($do_relation && !empty($cfgRelation['relation'])) {
         // Find which tables are related with the current one and write it in
         // an array
         $res_rel = PMA_getForeigners($db, $table);
         if ($res_rel && count($res_rel) > 0) {
             $have_rel = true;
         } else {
             $have_rel = false;
         }
     } else {
         $have_rel = false;
     }
     // end if
     /**
      * Displays the table structure
      */
     $columns_cnt = 4;
     if ($do_relation && $have_rel) {
         $columns_cnt++;
     }
     if ($do_comments && $cfgRelation['commwork']) {
         $columns_cnt++;
     }
     if ($do_mime && $cfgRelation['mimework']) {
         $columns_cnt++;
     }
     $text_output .= "|------\n";
     $text_output .= '|' . __('Column');
     $text_output .= '|' . __('Type');
     $text_output .= '|' . __('Null');
     $text_output .= '|' . __('Default');
     if ($do_relation && $have_rel) {
         $text_output .= '|' . __('Links to');
     }
     if ($do_comments) {
         $text_output .= '|' . __('Comments');
         $comments = PMA_getComments($db, $table);
     }
     if ($do_mime && $cfgRelation['mimework']) {
         $text_output .= '|' . htmlspecialchars('MIME');
         $mime_map = PMA_getMIME($db, $table, true);
     }
     $text_output .= "\n|------\n";
     $columns = PMA_DBI_get_columns($db, $table);
     foreach ($columns as $column) {
         $text_output .= $this->formatOneColumnDefinition($column, $unique_keys);
         $field_name = $column['Field'];
         if ($do_relation && $have_rel) {
             $text_output .= '|' . (isset($res_rel[$field_name]) ? htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') : '');
         }
         if ($do_comments && $cfgRelation['commwork']) {
             $text_output .= '|' . (isset($comments[$field_name]) ? htmlspecialchars($comments[$field_name]) : '');
         }
         if ($do_mime && $cfgRelation['mimework']) {
             $text_output .= '|' . (isset($mime_map[$field_name]) ? htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) : '');
         }
         $text_output .= "\n";
     }
     // end foreach
     return $text_output;
 }
    /**
     * Creates tracking version of a table / view
     * (in other words: create a job to track future changes on the table).
     *
     * @param string $dbname       name of database
     * @param string $tablename    name of table
     * @param string $version      version
     * @param string $tracking_set set of tracking statements
     * @param bool   $is_view      if table is a view
     *
     * @static
     *
     * @return int result of version insertion
     */
    static public function createVersion($dbname, $tablename, $version,
        $tracking_set = '', $is_view = false
    ) {
        global $sql_backquotes, $export_type;

        $common_functions = PMA_CommonFunctions::getInstance();
        
        if ($tracking_set == '') {
            $tracking_set = self::$default_tracking_set;
        }

        // get Export SQL instance
        require_once "libraries/plugin_interface.lib.php";
        $export_sql_plugin = PMA_getPlugin(
            "export",
            "sql",    
            'libraries/plugins/export/',
            array(
                'export_type' => $export_type,
                'single_table' => isset($single_table)
            )
        );

        $sql_backquotes = true;

        $date = date('Y-m-d H:i:s');

        // Get data definition snapshot of table

        $columns = PMA_DBI_get_columns($dbname, $tablename, null, true);
        // int indices to reduce size
        $columns = array_values($columns);
        // remove Privileges to reduce size
        for ($i = 0; $i < count($columns); $i++) {
            unset($columns[$i]['Privileges']);
        }

        $indexes = PMA_DBI_get_table_indexes($dbname, $tablename);

        $snapshot = array('COLUMNS' => $columns, 'INDEXES' => $indexes);
        $snapshot = serialize($snapshot);

        // Get DROP TABLE / DROP VIEW and CREATE TABLE SQL statements
        $sql_backquotes = true;

        $create_sql  = "";

        if (self::$add_drop_table == true && $is_view == false) {
            $create_sql .= self::getLogComment()
                . 'DROP TABLE IF EXISTS ' . $common_functions->backquote($tablename) . ";\n";

        }

        if (self::$add_drop_view == true && $is_view == true) {
            $create_sql .= self::getLogComment()
                . 'DROP VIEW IF EXISTS ' . $common_functions->backquote($tablename) . ";\n";
        }

        $create_sql .= self::getLogComment() .
            $export_sql_plugin->getTableDef($dbname, $tablename, "\n", "");

        // Save version

        $sql_query = "/*NOTRACK*/\n" .
        "INSERT INTO" . self::$pma_table . " (" .
        "db_name, " .
        "table_name, " .
        "version, " .
        "date_created, " .
        "date_updated, " .
        "schema_snapshot, " .
        "schema_sql, " .
        "data_sql, " .
        "tracking " .
        ") " .
        "values (
        '" . $common_functions->sqlAddSlashes($dbname) . "',
        '" . $common_functions->sqlAddSlashes($tablename) . "',
        '" . $common_functions->sqlAddSlashes($version) . "',
        '" . $common_functions->sqlAddSlashes($date) . "',
        '" . $common_functions->sqlAddSlashes($date) . "',
        '" . $common_functions->sqlAddSlashes($snapshot) . "',
        '" . $common_functions->sqlAddSlashes($create_sql) . "',
        '" . $common_functions->sqlAddSlashes("\n") . "',
        '" . $common_functions->sqlAddSlashes(self::_transformTrackingSet($tracking_set)) . "' )";

        $result = PMA_queryAsControlUser($sql_query);

        if ($result) {
            // Deactivate previous version
            self::deactivateTracking($dbname, $tablename, ($version - 1));
        }

        return $result;
    }