/** * Outputs export footer * * @return bool Whether it suceeded * * @access public */ function PMA_exportFooter() { global $workbook; global $tmp_filename; $tmp_filename = tempnam(realpath($GLOBALS['cfg']['TempDir']), 'pma_xlsx_'); $workbookWriter = new PHPExcel_Writer_Excel2007($workbook); $workbookWriter->save($tmp_filename); if (!PMA_exportOutputHandler(file_get_contents($tmp_filename))) { return FALSE; } unlink($tmp_filename); unset($GLOBALS['workbook']); unset($GLOBALS['sheet_index']); return TRUE; }
/** * Outputs export footer * * @return bool Whether it succeeded * * @access public */ function PMA_exportFooter() { $GLOBALS['ods_buffer'] .= '</office:spreadsheet>' . '</office:body>' . '</office:document-content>'; if (!PMA_exportOutputHandler(PMA_createOpenDocument('application/vnd.oasis.opendocument.spreadsheet', $GLOBALS['ods_buffer']))) { return false; } return true; }
/** * Outputs export footer * * @return bool Whether it suceeded * * @access public */ function PMA_exportFooter() { $GLOBALS['odt_buffer'] .= '</office:text>' . '</office:body>' . '</office:document-content>'; if (!PMA_exportOutputHandler(PMA_createOpenDocument('application/vnd.oasis.opendocument.text', $GLOBALS['odt_buffer']))) { return FALSE; } return TRUE; }
/** * Outputs export footer * * @return bool Whether it suceeded * * @access public */ function PMA_exportFooter() { global $workbook; global $tmp_filename; $res = $workbook->close(); if (PEAR::isError($res)) { echo $res->getMessage(); return FALSE; } if (!PMA_exportOutputHandler(file_get_contents($tmp_filename))) { return FALSE; } unlink($tmp_filename); return TRUE; }
/** * Outputs export footer * * @return bool Whether it succeeded */ public function exportFooter() { $pdf = $this->_getPdf(); // instead of $pdf->Output(): if (!PMA_exportOutputHandler($pdf->getPDFData())) { return false; } return true; }
/** * Outputs the content of a table in XML format * * @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 string $sql_query SQL query for obtaining data * * @return bool Whether it succeeded */ public function exportData($db, $table, $crlf, $error_url, $sql_query) { if (isset($GLOBALS['xml_export_contents']) && $GLOBALS['xml_export_contents']) { $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED); $columns_cnt = PMA_DBI_num_fields($result); $columns = array(); for ($i = 0; $i < $columns_cnt; $i++) { $columns[$i] = stripslashes(PMA_DBI_field_name($result, $i)); } unset($i); $buffer = ' <!-- ' . __('Table') . ' ' . $table . ' -->' . $crlf; if (!PMA_exportOutputHandler($buffer)) { return false; } while ($record = PMA_DBI_fetch_row($result)) { $buffer = ' <table name="' . htmlspecialchars($table) . '">' . $crlf; for ($i = 0; $i < $columns_cnt; $i++) { // If a cell is NULL, still export it to preserve // the XML structure if (!isset($record[$i]) || is_null($record[$i])) { $record[$i] = 'NULL'; } $buffer .= ' <column name="' . htmlspecialchars($columns[$i]) . '">' . htmlspecialchars((string) $record[$i]) . '</column>' . $crlf; } $buffer .= ' </table>' . $crlf; if (!PMA_exportOutputHandler($buffer)) { return false; } } PMA_DBI_free_result($result); } return true; }
function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy) { global $cfgRelation; if (!PMA_exportOutputHandler('<h2>' . $GLOBALS['strTableStructure'] . ' ' . $table . '</h2>')) { return FALSE; } /** * Get the unique keys in the table */ $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db); $keys_result = PMA_DBI_query($keys_query); $unique_keys = array(); while ($key = PMA_DBI_fetch_assoc($keys_result)) { if ($key['Non_unique'] == 0) { $unique_keys[] = $key['Column_name']; } } PMA_DBI_free_result($keys_result); /** * Gets fields properties */ PMA_DBI_select_db($db); $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table); $result = PMA_DBI_query($local_query); $fields_cnt = PMA_DBI_num_rows($result); // Check if we can use Relations (Mike Beck) 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">' . htmlspecialchars($GLOBALS['strField']) . '</th>'; $schema_insert .= '<td class="print"><b>' . htmlspecialchars($GLOBALS['strType']) . '</b></td>'; $schema_insert .= '<td class="print"><b>' . htmlspecialchars($GLOBALS['strNull']) . '</b></td>'; $schema_insert .= '<td class="print"><b>' . htmlspecialchars($GLOBALS['strDefault']) . '</b></td>'; if ($do_relation && $have_rel) { $schema_insert .= '<td class="print"><b>' . htmlspecialchars($GLOBALS['strLinksTo']) . '</b></td>'; } if ($do_comments) { $schema_insert .= '<td class="print"><b>' . htmlspecialchars($GLOBALS['strComments']) . '</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; } while ($row = PMA_DBI_fetch_assoc($result)) { $schema_insert = '<tr class="print-category">'; $type = $row['Type']; // reformat mysql query output - staybyte - 9. June 2001 // loic1: set or enum types: slashes single quotes inside options if (preg_match('/^(set|enum)\\((.+)\\)$/i', $type, $tmp)) { $tmp[2] = substr(preg_replace('/([^,])\'\'/', '\\1\\\'', ',' . $tmp[2]), 1); $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')'; $type_nowrap = ''; $binary = 0; $unsigned = 0; $zerofill = 0; } else { $type_nowrap = ' nowrap="nowrap"'; $type = preg_replace('/BINARY/i', '', $type); $type = preg_replace('/ZEROFILL/i', '', $type); $type = preg_replace('/UNSIGNED/i', '', $type); if (empty($type)) { $type = ' '; } $binary = preg_match('/BINARY/i', $row['Type']); $unsigned = preg_match('/UNSIGNED/i', $row['Type']); $zerofill = preg_match('/ZEROFILL/i', $row['Type']); } $strAttribute = ' '; if ($binary) { $strAttribute = 'BINARY'; } if ($unsigned) { $strAttribute = 'UNSIGNED'; } if ($zerofill) { $strAttribute = 'UNSIGNED ZEROFILL'; } if (!isset($row['Default'])) { if ($row['Null'] != 'NO') { $row['Default'] = 'NULL'; } } else { $row['Default'] = $row['Default']; } $fmt_pre = ''; $fmt_post = ''; if (in_array($row['Field'], $unique_keys)) { $fmt_pre = '<b>' . $fmt_pre; $fmt_post = $fmt_post . '</b>'; } if ($row['Key'] == 'PRI') { $fmt_pre = '<i>' . $fmt_pre; $fmt_post = $fmt_post . '</i>'; } $schema_insert .= '<td class="print">' . $fmt_pre . htmlspecialchars($row['Field']) . $fmt_post . '</td>'; $schema_insert .= '<td class="print">' . htmlspecialchars($type) . '</td>'; $schema_insert .= '<td class="print">' . htmlspecialchars($row['Null'] == '' || $row['Null'] == 'NO' ? $GLOBALS['strNo'] : $GLOBALS['strYes']) . '</td>'; $schema_insert .= '<td class="print">' . htmlspecialchars(isset($row['Default']) ? $row['Default'] : '') . '</td>'; $field_name = $row['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 PMA_DBI_free_result($result); return PMA_exportOutputHandler('</table>'); }
/** * Outputs the content of a table in YAML format * * @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 string $sql_query SQL query for obtaining data * @return bool Whether it succeeded * * @access public */ function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) { $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED); $columns_cnt = PMA_DBI_num_fields($result); for ($i = 0; $i < $columns_cnt; $i++) { $columns[$i] = stripslashes(PMA_DBI_field_name($result, $i)); } unset($i); $buffer = ''; $record_cnt = 0; while ($record = PMA_DBI_fetch_row($result)) { $record_cnt++; // Output table name as comment if this is the first record of the table if ($record_cnt == 1) { $buffer = '# ' . $db . '.' . $table . $crlf; $buffer .= '-' . $crlf; } else { $buffer = '-' . $crlf; } for ($i = 0; $i < $columns_cnt; $i++) { if (!isset($record[$i])) { continue; } $column = $columns[$i]; if (is_null($record[$i])) { $buffer .= ' ' . $column . ': null' . $crlf; continue; } if (is_numeric($record[$i])) { $buffer .= ' ' . $column . ': ' . $record[$i] . $crlf; continue; } $record[$i] = str_replace(array('\\', '"', "\n", "\r"), array('\\\\', '\\"', '\\n', '\\r'), $record[$i]); $buffer .= ' ' . $column . ': "' . $record[$i] . '"' . $crlf; } if (!PMA_exportOutputHandler($buffer)) { return false; } } PMA_DBI_free_result($result); return true; }
/** * Outputs the content of a table in MediaWiki format * * @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 string $sql_query SQL query for obtaining data * @param array $aliases Aliases of db/table/columns * * @return bool Whether it succeeded */ public function exportData($db, $table, $crlf, $error_url, $sql_query, $aliases = array()) { $db_alias = $db; $table_alias = $table; $this->initAlias($aliases, $db_alias, $table_alias); // Print data comment $output = $this->_exportComment("Table data for " . PMA\libraries\Util::backquote($table_alias)); // Begin the table construction // Use the "wikitable" class for style // Use the "sortable" class for allowing tables to be sorted by column $output .= "{| class=\"wikitable sortable\" style=\"text-align:center;\"" . $this->_exportCRLF(); // Add the table name if (isset($GLOBALS['mediawiki_caption'])) { $output .= "|+'''" . $table_alias . "'''" . $this->_exportCRLF(); } // Add the table headers if (isset($GLOBALS['mediawiki_headers'])) { // Get column names $column_names = $GLOBALS['dbi']->getColumnNames($db, $table); // Add column names as table headers if (!is_null($column_names)) { // Use '|-' for separating rows $output .= "|-" . $this->_exportCRLF(); // Use '!' for separating table headers foreach ($column_names as $column) { if (!empty($aliases[$db]['tables'][$table]['columns'][$column])) { $column = $aliases[$db]['tables'][$table]['columns'][$column]; } $output .= " ! " . $column . "" . $this->_exportCRLF(); } } } // Get the table data from the database $result = $GLOBALS['dbi']->query($sql_query, null, PMA\libraries\DatabaseInterface::QUERY_UNBUFFERED); $fields_cnt = $GLOBALS['dbi']->numFields($result); while ($row = $GLOBALS['dbi']->fetchRow($result)) { $output .= "|-" . $this->_exportCRLF(); // Use '|' for separating table columns for ($i = 0; $i < $fields_cnt; ++$i) { $output .= " | " . $row[$i] . "" . $this->_exportCRLF(); } } // End table construction $output .= "|}" . str_repeat($this->_exportCRLF(), 2); return PMA_exportOutputHandler($output); }
/** * Outputs the content of a table in CSV format * * @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 string $sql_query SQL query for obtaining data * @param array $aliases Aliases of db/table/columns * * @return bool Whether it succeeded */ public function exportData($db, $table, $crlf, $error_url, $sql_query, $aliases = array()) { global $what, $csv_terminated, $csv_separator, $csv_enclosed, $csv_escaped; $db_alias = $db; $table_alias = $table; $this->initAlias($aliases, $db_alias, $table_alias); // Gets the data from the database $result = $GLOBALS['dbi']->query($sql_query, null, PMA_DatabaseInterface::QUERY_UNBUFFERED); $fields_cnt = $GLOBALS['dbi']->numFields($result); // If required, get fields name at the first line if (isset($GLOBALS['csv_columns'])) { $schema_insert = ''; for ($i = 0; $i < $fields_cnt; $i++) { $col_as = $GLOBALS['dbi']->fieldName($result, $i); if (!empty($aliases[$db]['tables'][$table]['columns'][$col_as])) { $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as]; } $col_as = stripslashes($col_as); if ($csv_enclosed == '') { $schema_insert .= $col_as; } else { $schema_insert .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $col_as) . $csv_enclosed; } $schema_insert .= $csv_separator; } // end for $schema_insert = trim(mb_substr($schema_insert, 0, -1)); if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) { return false; } } // end if // Format the data while ($row = $GLOBALS['dbi']->fetchRow($result)) { $schema_insert = ''; for ($j = 0; $j < $fields_cnt; $j++) { if (!isset($row[$j]) || is_null($row[$j])) { $schema_insert .= $GLOBALS[$what . '_null']; } elseif ($row[$j] == '0' || $row[$j] != '') { // always enclose fields if ($what == 'excel') { $row[$j] = preg_replace("/\r(\n)?/", "\n", $row[$j]); } // remove CRLF characters within field if (isset($GLOBALS[$what . '_removeCRLF']) && $GLOBALS[$what . '_removeCRLF']) { $row[$j] = str_replace("\n", "", str_replace("\r", "", $row[$j])); } if ($csv_enclosed == '') { $schema_insert .= $row[$j]; } else { // also double the escape string if found in the data if ($csv_escaped != $csv_enclosed) { $schema_insert .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, str_replace($csv_escaped, $csv_escaped . $csv_escaped, $row[$j])) . $csv_enclosed; } else { // avoid a problem when escape string equals enclose $schema_insert .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed; } } } else { $schema_insert .= ''; } if ($j < $fields_cnt - 1) { $schema_insert .= $csv_separator; } } // end for if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) { return false; } } // end while $GLOBALS['dbi']->freeResult($result); return true; }
/** * 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 string $export_mode 'create_table', 'triggers', 'create_view', * 'stand_in' * @param string $export_type 'server', 'database', 'table' * @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 $dates whether to include creation/update/check dates * @param array $aliases Aliases of db/table/columns * * @return bool Whether it succeeded */ public function exportStructure($db, $table, $crlf, $error_url, $export_mode, $export_type, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $aliases = array()) { $db_alias = $db; $table_alias = $table; $this->initAlias($aliases, $db_alias, $table_alias); $dump = ''; switch ($export_mode) { case 'create_table': $dump .= '== ' . __('Table structure for table') . ' ' . $table_alias . "\n\n"; $dump .= $this->getTableDef($db, $table, $crlf, $error_url, $do_relation, $do_comments, $do_mime, $dates, true, false, $aliases); break; case 'triggers': $dump = ''; $triggers = $GLOBALS['dbi']->getTriggers($db, $table); if ($triggers) { $dump .= '== ' . __('Triggers') . ' ' . $table_alias . "\n\n"; $dump .= $this->getTriggers($db, $table); } break; case 'create_view': $dump .= '== ' . __('Structure for view') . ' ' . $table_alias . "\n\n"; $dump .= $this->getTableDef($db, $table, $crlf, $error_url, $do_relation, $do_comments, $do_mime, $dates, true, true, $aliases); break; case 'stand_in': $dump .= '== ' . __('Stand-in structure for view') . ' ' . $table . "\n\n"; // export a stand-in definition to resolve view dependencies $dump .= $this->getTableDefStandIn($db, $table, $crlf, $aliases); } // end switch return PMA_exportOutputHandler($dump); }
/** * 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 string $export_mode 'create_table', 'triggers', 'create_view', * 'stand_in' * @param string $export_type 'server', 'database', 'table' * @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 * * @return bool Whether it succeeded */ public function exportStructure($db, $table, $crlf, $error_url, $export_mode, $export_type, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false) { $dump = ''; switch ($export_mode) { case 'create_table': $dump .= '<h2>' . __('Table structure for table') . ' ' . htmlspecialchars($table) . '</h2>'; $dump .= $this->getTableDef($db, $table, $crlf, $error_url, $do_relation, $do_comments, $do_mime, $dates); break; case 'triggers': $dump = ''; $triggers = PMA_DBI_get_triggers($db, $table); if ($triggers) { $dump .= '<h2>' . __('Triggers') . ' ' . htmlspecialchars($table) . '</h2>'; $dump .= $this->getTriggers($db, $table); } break; case 'create_view': $dump .= '<h2>' . __('Structure for view') . ' ' . htmlspecialchars($table) . '</h2>'; $dump .= $this->getTableDef($db, $table, $crlf, $error_url, $do_relation, $do_comments, $do_mime, $dates, true, true); break; case 'stand_in': $dump .= '<h2>' . __('Stand-in structure for view') . ' ' . htmlspecialchars($table) . '</h2>'; // export a stand-in definition to resolve view dependencies $dump .= $this->getTableDefStandIn($db, $table, $crlf); } // end switch return PMA_exportOutputHandler($dump); }
/** * Outputs the content of a table in YAML format * * @param string the database name * @param string the table name * @param string the end of line sequence * @param string the url to go back in case of error * @param string SQL query for obtaining data * * @return bool Whether it suceeded * * @access public */ function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) { $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED); $columns_cnt = PMA_DBI_num_fields($result); for ($i = 0; $i < $columns_cnt; $i++) { $columns[$i] = stripslashes(PMA_DBI_field_name($result, $i)); } unset($i); $buffer = ''; $record_cnt = 0; while ($record = PMA_DBI_fetch_row($result)) { $record_cnt++; // Output table name as comment if this is the first record of the table if ($record_cnt == 1) { $buffer .= $crlf . '// ' . $db . '.' . $table . $crlf; $buffer .= '$' . $table . ' = array(' . $crlf; $buffer .= ' array('; } else { $buffer .= ',' . $crlf . ' array('; } for ($i = 0; $i < $columns_cnt; $i++) { $buffer .= "'" . $columns[$i] . "'=>" . var_export($record[$i], true) . ($i + 1 >= $columns_cnt ? '' : ','); } $buffer .= ')'; } $buffer .= $crlf . ');' . $crlf; if (!PMA_exportOutputHandler($buffer)) { return FALSE; } PMA_DBI_free_result($result); return true; }
/** * Outputs the content of a table in MediaWiki format * * @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 string $sql_query SQL query for obtaining data * @return bool Whether it succeeded * * @access public */ function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) { $columns = PMA_DBI_get_columns($db, $table); $columns = array_values($columns); $row_cnt = count($columns); $output = "{| cellpadding=\"10\" cellspacing=\"0\" border=\"1\" style=\"text-align:center;\"\n"; $output .= "|+'''" . $table . "'''\n"; $output .= "|- style=\"background:#ffdead;\"\n"; $output .= "! style=\"background:#ffffff\" | \n"; for ($i = 0; $i < $row_cnt; ++$i) { $output .= " | " . $columns[$i]['Field']; if ($i + 1 != $row_cnt) { $output .= "\n"; } } $output .= "\n"; $output .= "|- style=\"background:#f9f9f9;\"\n"; $output .= "! style=\"background:#f2f2f2\" | Type\n"; for ($i = 0; $i < $row_cnt; ++$i) { $output .= " | " . $columns[$i]['Type']; if ($i + 1 != $row_cnt) { $output .= "\n"; } } $output .= "\n"; $output .= "|- style=\"background:#f9f9f9;\"\n"; $output .= "! style=\"background:#f2f2f2\" | Null\n"; for ($i = 0; $i < $row_cnt; ++$i) { $output .= " | " . $columns[$i]['Null']; if ($i + 1 != $row_cnt) { $output .= "\n"; } } $output .= "\n"; $output .= "|- style=\"background:#f9f9f9;\"\n"; $output .= "! style=\"background:#f2f2f2\" | Default\n"; for ($i = 0; $i < $row_cnt; ++$i) { $output .= " | " . $columns[$i]['Default']; if ($i + 1 != $row_cnt) { $output .= "\n"; } } $output .= "\n"; $output .= "|- style=\"background:#f9f9f9;\"\n"; $output .= "! style=\"background:#f2f2f2\" | Extra\n"; for ($i = 0; $i < $row_cnt; ++$i) { $output .= " | " . $columns[$i]['Extra']; if ($i + 1 != $row_cnt) { $output .= "\n"; } } $output .= "\n"; $output .= "|}\n\n\n\n"; return PMA_exportOutputHandler($output); }
/** * 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); }
/** * Outputs the content of a table in NHibernate format * * @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 string $sql_query SQL query for obtaining data * * @return bool Whether it succeeded */ public function exportData($db, $table, $crlf, $error_url, $sql_query) { $CG_FORMATS = $this->_getCgFormats(); $CG_HANDLERS = $this->_getCgHandlers(); $format = $GLOBALS['codegen_format']; if (isset($CG_FORMATS[$format])) { return PMA_exportOutputHandler($this->{$CG_HANDLERS}[$format]($db, $table, $crlf)); } return PMA_exportOutputHandler(sprintf("%s is not supported.", $format)); }
/** * Outputs the content of a table * * @param string the database name * @param string the table name * @param string the end of line sequence * @param string the url to go back in case of error * @param string SQL query for obtaining data * * @return bool Whether it suceeded * * @access public */ function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) { global $what; if (isset($GLOBALS[$what . '_export_contents']) && $GLOBALS[$what . '_export_contents']) { $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED); $columns_cnt = PMA_DBI_num_fields($result); for ($i = 0; $i < $columns_cnt; $i++) { $columns[$i] = stripslashes(str_replace(' ', '_', PMA_DBI_field_name($result, $i))); } unset($i); $buffer = ' <!-- ' . $GLOBALS['strTable'] . ' ' . $table . ' -->' . $crlf; if (!PMA_exportOutputHandler($buffer)) { return FALSE; } while ($record = PMA_DBI_fetch_row($result)) { $buffer = ' <table name="' . htmlspecialchars($table) . '">' . $crlf; for ($i = 0; $i < $columns_cnt; $i++) { // If a cell is NULL, still export it to preserve the XML structure if (!isset($record[$i]) || is_null($record[$i])) { $record[$i] = 'NULL'; } $buffer .= ' <column name="' . $columns[$i] . '">' . htmlspecialchars(utf8_encode((string) $record[$i])) . '</column>' . $crlf; } $buffer .= ' </table>' . $crlf; if (!PMA_exportOutputHandler($buffer)) { return FALSE; } } PMA_DBI_free_result($result); } return TRUE; }
/** * Outputs the content of a table in PHP array format * * @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 string $sql_query SQL query for obtaining data * * @return bool Whether it succeeded */ public function exportData($db, $table, $crlf, $error_url, $sql_query) { $result = $GLOBALS['dbi']->query($sql_query, null, PMA_DatabaseInterface::QUERY_UNBUFFERED); $columns_cnt = $GLOBALS['dbi']->numFields($result); for ($i = 0; $i < $columns_cnt; $i++) { $columns[$i] = stripslashes($GLOBALS['dbi']->fieldName($result, $i)); } unset($i); // fix variable names (based on // http://www.php.net/manual/language.variables.basics.php) if (!preg_match('/^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$/', $table)) { // fix invalid characters in variable names by replacing them with // underscores $tablefixed = preg_replace('/[^a-zA-Z0-9_\\x7f-\\xff]/', '_', $table); // variable name must not start with a number or dash... if (preg_match('/^[a-zA-Z_\\x7f-\\xff]/', $tablefixed) == false) { $tablefixed = '_' . $tablefixed; } } else { $tablefixed = $table; } $buffer = ''; $record_cnt = 0; // Output table name as comment $buffer .= $crlf . '// ' . PMA_Util::backquote($db) . '.' . PMA_Util::backquote($table) . $crlf; $buffer .= '$' . $tablefixed . ' = array('; while ($record = $GLOBALS['dbi']->fetchRow($result)) { $record_cnt++; if ($record_cnt == 1) { $buffer .= $crlf . ' array('; } else { $buffer .= ',' . $crlf . ' array('; } for ($i = 0; $i < $columns_cnt; $i++) { $buffer .= var_export($columns[$i], true) . " => " . var_export($record[$i], true) . ($i + 1 >= $columns_cnt ? '' : ','); } $buffer .= ')'; } $buffer .= $crlf . ');' . $crlf; if (!PMA_exportOutputHandler($buffer)) { return false; } $GLOBALS['dbi']->freeResult($result); return true; }
/** * Outputs the content of a table * * @param string the database name * @param string the table name * @param string the end of line sequence * @param string the url to go back in case of error * @param string SQL query for obtaining data * * @return bool Whether it suceeded * * @access public */ function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) { $result = PMA_DBI_query($sql_query, NULL, PMA_DBI_QUERY_UNBUFFERED); $columns_cnt = PMA_DBI_num_fields($result); for ($i = 0; $i < $columns_cnt; $i++) { $columns[$i] = stripslashes(PMA_DBI_field_name($result, $i)); } unset($i); $buffer = ' <!-- ' . $GLOBALS['strTable'] . ' ' . $table . ' -->' . $crlf; if (!PMA_exportOutputHandler($buffer)) { return FALSE; } while ($record = PMA_DBI_fetch_row($result)) { $buffer = ' <' . $table . '>' . $crlf; for ($i = 0; $i < $columns_cnt; $i++) { if (isset($record[$i]) && !is_null($record[$i])) { $buffer .= ' <' . $columns[$i] . '>' . htmlspecialchars($record[$i]) . '</' . $columns[$i] . '>' . $crlf; } } $buffer .= ' </' . $table . '>' . $crlf; if (!PMA_exportOutputHandler($buffer)) { return FALSE; } } PMA_DBI_free_result($result); return TRUE; }
/** * 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 string $export_mode 'create_table', 'triggers', 'create_view', * 'stand_in' * @param string $export_type 'server', 'database', 'table' * @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 * 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 array $aliases Aliases of db/table/columns * * @return bool Whether it succeeded */ public function exportStructure($db, $table, $crlf, $error_url, $export_mode, $export_type, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $aliases = array()) { $db_alias = $db; $table_alias = $table; $this->initAlias($aliases, $db_alias, $table_alias); global $cfgRelation; /* We do not export triggers */ if ($export_mode == 'triggers') { return true; } /** * Get the unique keys in the table */ $unique_keys = array(); $keys = $GLOBALS['dbi']->getTableIndexes($db, $table); foreach ($keys as $key) { if ($key['Non_unique'] == 0) { $unique_keys[] = $key['Column_name']; } } /** * Gets fields properties */ $GLOBALS['dbi']->selectDb($db); // Check if we can use Relations list($res_rel, $have_rel) = PMA_getRelationsAndStatus($do_relation && !empty($cfgRelation['relation']), $db, $table); /** * Displays the table structure */ $buffer = $crlf . '%' . $crlf . '% ' . __('Structure:') . ' ' . $table_alias . $crlf . '%' . $crlf . ' \\begin{longtable}{'; if (!PMA_exportOutputHandler($buffer)) { return false; } $alignment = '|l|c|c|c|'; if ($do_relation && $have_rel) { $alignment .= 'l|'; } if ($do_comments) { $alignment .= 'l|'; } if ($do_mime && $cfgRelation['mimework']) { $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_Util::expandUserString($GLOBALS['latex_structure_caption'], array('texEscape', get_class($this), 'libraries/plugins/export/' . get_class($this) . ".class.php"), array('table' => $table_alias, 'database' => $db_alias)) . '} \\label{' . PMA_Util::expandUserString($GLOBALS['latex_structure_label'], null, array('table' => $table_alias, 'database' => $db_alias)) . '} \\\\' . $crlf; } $buffer .= $header . ' \\\\ \\hline \\hline' . $crlf . '\\endfirsthead' . $crlf; // Table caption on next pages if (isset($GLOBALS['latex_caption'])) { $buffer .= ' \\caption{' . PMA_Util::expandUserString($GLOBALS['latex_structure_continued_caption'], array('texEscape', get_class($this), 'libraries/plugins/export/' . get_class($this) . ".class.php"), array('table' => $table_alias, 'database' => $db_alias)) . '} \\\\ ' . $crlf; } $buffer .= $header . ' \\\\ \\hline \\hline \\endhead \\endfoot ' . $crlf; if (!PMA_exportOutputHandler($buffer)) { return false; } $fields = $GLOBALS['dbi']->getColumns($db, $table); foreach ($fields as $row) { $extracted_columnspec = PMA_Util::extractColumnSpec($row['Type']); $type = $extracted_columnspec['print_type']; if (empty($type)) { $type = ' '; } if (!isset($row['Default'])) { if ($row['Null'] != 'NO') { $row['Default'] = 'NULL'; } } $field_name = $col_as = $row['Field']; if (!empty($aliases[$db]['tables'][$table]['columns'][$col_as])) { $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as]; } $local_buffer = $col_as . "" . $type . "" . ($row['Null'] == '' || $row['Null'] == 'NO' ? __('No') : __('Yes')) . "" . (isset($row['Default']) ? $row['Default'] : ''); if ($do_relation && $have_rel) { $local_buffer .= ""; $local_buffer .= $this->getRelationString($res_rel, $field_name, $db, $aliases); } 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 = self::texEscape($local_buffer); if ($row['Key'] == 'PRI') { $pos = mb_strpos($local_buffer, ""); $local_buffer = '\\textit{' . mb_substr($local_buffer, 0, $pos) . '}' . mb_substr($local_buffer, $pos); } if (in_array($field_name, $unique_keys)) { $pos = mb_strpos($local_buffer, ""); $local_buffer = '\\textbf{' . mb_substr($local_buffer, 0, $pos) . '}' . mb_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); }
/** * Outputs the content of a table in XML format * * @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 string $sql_query SQL query for obtaining data * @param array $aliases Aliases of db/table/columns * * @return bool Whether it succeeded */ public function exportData($db, $table, $crlf, $error_url, $sql_query, $aliases = array()) { $db_alias = $db; $table_alias = $table; $this->initAlias($aliases, $db_alias, $table_alias); if (isset($GLOBALS['xml_export_contents']) && $GLOBALS['xml_export_contents']) { $result = $GLOBALS['dbi']->query($sql_query, null, PMA_DatabaseInterface::QUERY_UNBUFFERED); $columns_cnt = $GLOBALS['dbi']->numFields($result); $columns = array(); for ($i = 0; $i < $columns_cnt; $i++) { $columns[$i] = stripslashes($GLOBALS['dbi']->fieldName($result, $i)); } unset($i); $buffer = ' <!-- ' . __('Table') . ' ' . $table_alias . ' -->' . $crlf; if (!PMA_exportOutputHandler($buffer)) { return false; } while ($record = $GLOBALS['dbi']->fetchRow($result)) { $buffer = ' <table name="' . htmlspecialchars($table_alias) . '">' . $crlf; for ($i = 0; $i < $columns_cnt; $i++) { $col_as = $columns[$i]; if (!empty($aliases[$db]['tables'][$table]['columns'][$col_as])) { $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as]; } // If a cell is NULL, still export it to preserve // the XML structure if (!isset($record[$i]) || is_null($record[$i])) { $record[$i] = 'NULL'; } $buffer .= ' <column name="' . htmlspecialchars($col_as) . '">' . htmlspecialchars((string) $record[$i]) . '</column>' . $crlf; } $buffer .= ' </table>' . $crlf; if (!PMA_exportOutputHandler($buffer)) { return false; } } $GLOBALS['dbi']->freeResult($result); } return true; }
/** * Outputs the content of a table in JSON format * * @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 string $sql_query SQL query for obtaining data * * @return bool Whether it succeeded */ public function exportData($db, $table, $crlf, $error_url, $sql_query) { $result = $GLOBALS['dbi']->query($sql_query, null, PMA_DatabaseInterface::QUERY_UNBUFFERED); $columns_cnt = $GLOBALS['dbi']->numFields($result); for ($i = 0; $i < $columns_cnt; $i++) { $columns[$i] = stripslashes($GLOBALS['dbi']->fieldName($result, $i)); } unset($i); $buffer = ''; $record_cnt = 0; while ($record = $GLOBALS['dbi']->fetchRow($result)) { $record_cnt++; // Output table name as comment if this is the first record of the table if ($record_cnt == 1) { $buffer = '// ' . $db . '.' . $table . $crlf . $crlf; $buffer .= '['; } else { $buffer = ', '; } if (!PMA_exportOutputHandler($buffer)) { return false; } $data = array(); for ($i = 0; $i < $columns_cnt; $i++) { $data[$columns[$i]] = $record[$i]; } if (!PMA_exportOutputHandler(json_encode($data))) { return false; } } if ($record_cnt) { if (!PMA_exportOutputHandler(']')) { return false; } } $GLOBALS['dbi']->freeResult($result); return true; }
/** * Outputs the content of a table in SQL format * * @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 string $sql_query SQL query for obtaining data * * @return bool Whether it succeeded */ public function exportData($db, $table, $crlf, $error_url, $sql_query) { global $current_row, $sql_backquotes; if (isset($GLOBALS['sql_compatibility'])) { $compat = $GLOBALS['sql_compatibility']; } else { $compat = 'NONE'; } $formatted_table_name = isset($GLOBALS['sql_backquotes']) ? PMA_Util::backquoteCompat($table, $compat) : '\'' . $table . '\''; // Do not export data for a VIEW // (For a VIEW, this is called only when exporting a single VIEW) if (PMA_Table::isView($db, $table)) { $head = $this->_possibleCRLF() . $this->_exportComment() . $this->_exportComment('VIEW ' . ' ' . $formatted_table_name) . $this->_exportComment(__('Data') . ': ' . __('None')) . $this->_exportComment() . $this->_possibleCRLF(); if (!PMA_exportOutputHandler($head)) { return false; } return true; } // analyze the query to get the true column names, not the aliases // (this fixes an undefined index, also if Complete inserts // are used, we did not get the true column name in case of aliases) $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($sql_query)); $result = PMA_DBI_try_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED); // a possible error: the table has crashed $tmp_error = PMA_DBI_getError(); if ($tmp_error) { return PMA_exportOutputHandler($this->_exportComment(__('Error reading data:') . ' (' . $tmp_error . ')')); } if ($result != false) { $fields_cnt = PMA_DBI_num_fields($result); // Get field information $fields_meta = PMA_DBI_get_fields_meta($result); $field_flags = array(); for ($j = 0; $j < $fields_cnt; $j++) { $field_flags[$j] = PMA_DBI_field_flags($result, $j); } for ($j = 0; $j < $fields_cnt; $j++) { if (isset($analyzed_sql[0]['select_expr'][$j]['column'])) { $field_set[$j] = PMA_Util::backquoteCompat($analyzed_sql[0]['select_expr'][$j]['column'], $compat, $sql_backquotes); } else { $field_set[$j] = PMA_Util::backquoteCompat($fields_meta[$j]->name, $compat, $sql_backquotes); } } if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'UPDATE') { // update $schema_insert = 'UPDATE '; if (isset($GLOBALS['sql_ignore'])) { $schema_insert .= 'IGNORE '; } // avoid EOL blank $schema_insert .= PMA_Util::backquoteCompat($table, $compat, $sql_backquotes) . ' SET'; } else { // insert or replace if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'REPLACE') { $sql_command = 'REPLACE'; } else { $sql_command = 'INSERT'; } // delayed inserts? if (isset($GLOBALS['sql_delayed'])) { $insert_delayed = ' DELAYED'; } else { $insert_delayed = ''; } // insert ignore? if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'INSERT' && isset($GLOBALS['sql_ignore'])) { $insert_delayed .= ' IGNORE'; } //truncate table before insert if (isset($GLOBALS['sql_truncate']) && $GLOBALS['sql_truncate'] && $sql_command == 'INSERT') { $truncate = 'TRUNCATE TABLE ' . PMA_Util::backquoteCompat($table, $compat, $sql_backquotes) . ";"; $truncatehead = $this->_possibleCRLF() . $this->_exportComment() . $this->_exportComment(__('Truncate table before insert') . ' ' . $formatted_table_name) . $this->_exportComment() . $crlf; PMA_exportOutputHandler($truncatehead); PMA_exportOutputHandler($truncate); } else { $truncate = ''; } // scheme for inserting fields if ($GLOBALS['sql_insert_syntax'] == 'complete' || $GLOBALS['sql_insert_syntax'] == 'both') { $fields = implode(', ', $field_set); $schema_insert = $sql_command . $insert_delayed . ' INTO ' . PMA_Util::backquoteCompat($table, $compat, $sql_backquotes) . ' (' . $fields . ') VALUES'; } else { $schema_insert = $sql_command . $insert_delayed . ' INTO ' . PMA_Util::backquoteCompat($table, $compat, $sql_backquotes) . ' VALUES'; } } //\x08\\x09, not required $search = array("", "\n", "\r", ""); $replace = array('\\0', '\\n', '\\r', '\\Z'); $current_row = 0; $query_size = 0; if (($GLOBALS['sql_insert_syntax'] == 'extended' || $GLOBALS['sql_insert_syntax'] == 'both') && (!isset($GLOBALS['sql_type']) || $GLOBALS['sql_type'] != 'UPDATE')) { $separator = ','; $schema_insert .= $crlf; } else { $separator = ';'; } while ($row = PMA_DBI_fetch_row($result)) { if ($current_row == 0) { $head = $this->_possibleCRLF() . $this->_exportComment() . $this->_exportComment(__('Dumping data for table') . ' ' . $formatted_table_name) . $this->_exportComment() . $crlf; if (!PMA_exportOutputHandler($head)) { return false; } } // We need to SET IDENTITY_INSERT ON for MSSQL if (isset($GLOBALS['sql_compatibility']) && $GLOBALS['sql_compatibility'] == 'MSSQL' && $current_row == 0) { if (!PMA_exportOutputHandler('SET IDENTITY_INSERT ' . PMA_Util::backquoteCompat($table, $compat) . ' ON ;' . $crlf)) { return false; } } $current_row++; for ($j = 0; $j < $fields_cnt; $j++) { // NULL if (!isset($row[$j]) || is_null($row[$j])) { $values[] = 'NULL'; } elseif ($fields_meta[$j]->numeric && $fields_meta[$j]->type != 'timestamp' && !$fields_meta[$j]->blob) { // a number // timestamp is numeric on some MySQL 4.1, BLOBs are // sometimes numeric $values[] = $row[$j]; } elseif (stristr($field_flags[$j], 'BINARY') && $fields_meta[$j]->blob && isset($GLOBALS['sql_hex_for_blob'])) { // a true BLOB // - mysqldump only generates hex data when the --hex-blob // option is used, for fields having the binary attribute // no hex is generated // - a TEXT field returns type blob but a real blob // returns also the 'binary' flag // empty blobs need to be different, but '0' is also empty // :-( if (empty($row[$j]) && $row[$j] != '0') { $values[] = '\'\''; } else { $values[] = '0x' . bin2hex($row[$j]); } } elseif ($fields_meta[$j]->type == 'bit') { // detection of 'bit' works only on mysqli extension $values[] = "b'" . PMA_Util::sqlAddSlashes(PMA_Util::printableBitValue($row[$j], $fields_meta[$j]->length)) . "'"; } else { // something else -> treat as a string $values[] = '\'' . str_replace($search, $replace, PMA_Util::sqlAddSlashes($row[$j])) . '\''; } // end if } // end for // should we make update? if (isset($GLOBALS['sql_type']) && $GLOBALS['sql_type'] == 'UPDATE') { $insert_line = $schema_insert; for ($i = 0; $i < $fields_cnt; $i++) { if (0 == $i) { $insert_line .= ' '; } if ($i > 0) { // avoid EOL blank $insert_line .= ','; } $insert_line .= $field_set[$i] . ' = ' . $values[$i]; } list($tmp_unique_condition, $tmp_clause_is_unique) = PMA_Util::getUniqueCondition($result, $fields_cnt, $fields_meta, $row); $insert_line .= ' WHERE ' . $tmp_unique_condition; unset($tmp_unique_condition, $tmp_clause_is_unique); } else { // Extended inserts case if ($GLOBALS['sql_insert_syntax'] == 'extended' || $GLOBALS['sql_insert_syntax'] == 'both') { if ($current_row == 1) { $insert_line = $schema_insert . '(' . implode(', ', $values) . ')'; } else { $insert_line = '(' . implode(', ', $values) . ')'; $sql_max_size = $GLOBALS['sql_max_query_size']; if (isset($sql_max_size) && $sql_max_size > 0 && $query_size + strlen($insert_line) > $sql_max_size) { if (!PMA_exportOutputHandler(';' . $crlf)) { return false; } $query_size = 0; $current_row = 1; $insert_line = $schema_insert . $insert_line; } } $query_size += strlen($insert_line); // Other inserts case } else { $insert_line = $schema_insert . '(' . implode(', ', $values) . ')'; } } unset($values); if (!PMA_exportOutputHandler(($current_row == 1 ? '' : $separator . $crlf) . $insert_line)) { return false; } } // end while if ($current_row > 0) { if (!PMA_exportOutputHandler(';' . $crlf)) { return false; } } // We need to SET IDENTITY_INSERT OFF for MSSQL if (isset($GLOBALS['sql_compatibility']) && $GLOBALS['sql_compatibility'] == 'MSSQL' && $current_row > 0) { $outputSucceeded = PMA_exportOutputHandler($crlf . 'SET IDENTITY_INSERT ' . PMA_Util::backquoteCompat($table, $compat) . ' OFF;' . $crlf); if (!$outputSucceeded) { return false; } } } // end if ($result != false) PMA_DBI_free_result($result); return true; }
/** * 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 = ' '; } 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>'); }
/** * Outputs the content of a table in CSV format * * @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 string $sql_query SQL query for obtaining data * @return bool Whether it succeeded * * @access public */ function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) { global $what; global $csv_terminated; global $csv_separator; global $csv_enclosed; global $csv_escaped; // Gets the data from the database $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED); $fields_cnt = PMA_DBI_num_fields($result); // If required, get fields name at the first line if (isset($GLOBALS['csv_columns'])) { $schema_insert = ''; for ($i = 0; $i < $fields_cnt; $i++) { if ($csv_enclosed == '') { $schema_insert .= stripslashes(PMA_DBI_field_name($result, $i)); } else { $schema_insert .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(PMA_DBI_field_name($result, $i))) . $csv_enclosed; } $schema_insert .= $csv_separator; } // end for $schema_insert = trim(substr($schema_insert, 0, -1)); if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) { return false; } } // end if // Format the data while ($row = PMA_DBI_fetch_row($result)) { $schema_insert = ''; for ($j = 0; $j < $fields_cnt; $j++) { if (!isset($row[$j]) || is_null($row[$j])) { $schema_insert .= $GLOBALS[$what . '_null']; } elseif ($row[$j] == '0' || $row[$j] != '') { // always enclose fields if ($what == 'excel') { $row[$j] = preg_replace("/\r(\n)?/", "\n", $row[$j]); } // remove CRLF characters within field if (isset($GLOBALS[$what . '_removeCRLF']) && $GLOBALS[$what . '_removeCRLF']) { $row[$j] = str_replace("\n", "", str_replace("\r", "", $row[$j])); } if ($csv_enclosed == '') { $schema_insert .= $row[$j]; } else { // also double the escape string if found in the data if ($csv_escaped != $csv_enclosed) { $schema_insert .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, str_replace($csv_escaped, $csv_escaped . $csv_escaped, $row[$j])) . $csv_enclosed; } else { // avoid a problem when escape string equals enclose $schema_insert .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed; } } } else { $schema_insert .= ''; } if ($j < $fields_cnt - 1) { $schema_insert .= $csv_separator; } } // end for if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) { return false; } } // end while PMA_DBI_free_result($result); return true; }
/** * Outputs the content of a table in CSV format * * @param string the database name * @param string the table name * @param string the end of line sequence * @param string the url to go back in case of error * @param string SQL query for obtaining data * * @return bool Whether it suceeded * * @access public */ function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) { global $what; // Gets the data from the database $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED); $fields_cnt = PMA_DBI_num_fields($result); // If required, get fields name at the first line if (isset($GLOBALS[$what . '_columns'])) { $schema_insert = '<tr>'; for ($i = 0; $i < $fields_cnt; $i++) { $schema_insert .= '<td class=xl2216681 nowrap><b>' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i))) . '</b></td>'; } // end for $schema_insert .= '</tr>'; if (!PMA_exportOutputHandler($schema_insert)) { return FALSE; } } // end if // Format the data while ($row = PMA_DBI_fetch_row($result)) { $schema_insert = '<tr>'; for ($j = 0; $j < $fields_cnt; $j++) { if (!isset($row[$j]) || is_null($row[$j])) { $value = $GLOBALS[$what . '_null']; } elseif ($row[$j] == '0' || $row[$j] != '') { $value = $row[$j]; } else { $value = ''; } $schema_insert .= '<td class=xl2216681 nowrap>' . htmlspecialchars($value) . '</td>'; } // end for $schema_insert .= '</tr>'; if (!PMA_exportOutputHandler($schema_insert)) { return FALSE; } } // end while PMA_DBI_free_result($result); return TRUE; }
/** * Outputs the content of a table in CSV format * * @param string the database name * @param string the table name * @param string the end of line sequence * @param string the url to go back in case of error * @param string SQL query for obtaining data * * @return bool Whether it suceeded * * @access public */ function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) { global $what; global $add_character; global $separator; global $enclosed; global $escaped; // Gets the data from the database $result = PMA_DBI_query($sql_query, NULL, PMA_DBI_QUERY_UNBUFFERED); $fields_cnt = PMA_DBI_num_fields($result); // If required, get fields name at the first line if (isset($GLOBALS['showcsvnames']) && $GLOBALS['showcsvnames'] == 'yes') { $schema_insert = ''; for ($i = 0; $i < $fields_cnt; $i++) { if ($enclosed == '') { $schema_insert .= stripslashes(PMA_DBI_field_name($result, $i)); } else { $schema_insert .= $enclosed . str_replace($enclosed, $escaped . $enclosed, stripslashes(PMA_DBI_field_name($result, $i))) . $enclosed; } $schema_insert .= $separator; } // end for $schema_insert = trim(substr($schema_insert, 0, -1)); if (!PMA_exportOutputHandler($schema_insert . $add_character)) { return FALSE; } } // end if // Format the data while ($row = PMA_DBI_fetch_row($result)) { $schema_insert = ''; for ($j = 0; $j < $fields_cnt; $j++) { if (!isset($row[$j]) || is_null($row[$j])) { $schema_insert .= $GLOBALS[$what . '_replace_null']; } else { if ($row[$j] == '0' || $row[$j] != '') { // loic1 : always enclose fields if ($what == 'excel') { $row[$j] = ereg_replace("\r(\n)?", "\n", $row[$j]); } if ($enclosed == '') { $schema_insert .= $row[$j]; } else { $schema_insert .= $enclosed . str_replace($enclosed, $escaped . $enclosed, $row[$j]) . $enclosed; } } else { $schema_insert .= ''; } } if ($j < $fields_cnt - 1) { $schema_insert .= $separator; } } // end for if (!PMA_exportOutputHandler($schema_insert . $add_character)) { return FALSE; } } // end while PMA_DBI_free_result($result); return TRUE; }
/** * Outputs the content of a table in PDF format * * @todo user-defined page orientation, paper size * @param string the database name * @param string the table name * @param string the end of line sequence * @param string the url to go back in case of error * @param string SQL query for obtaining data * * @return bool Whether it suceeded * * @access public */ function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) { global $what; global $pdf_report_title; $pdf = new PMA_PDF('L', 'pt', 'A3'); $pdf->AddFont('DejaVuSans', '', 'dejavusans.php'); $pdf->AddFont('DejaVuSans', 'B', 'dejavusans-bold.php'); $pdf->AddFont('DejaVuSerif', '', 'dejavuserif.php'); $pdf->AddFont('DejaVuSerif', 'B', 'dejavuserif-bold.php'); $pdf->SetFont(PMA_PDF_FONT, '', 11.5); $pdf->AliasNbPages(); $attr = array('titleFontSize' => 18, 'titleText' => $pdf_report_title); $pdf->mysql_report($sql_query, $attr); // instead of $pdf->Output(): if ($pdf->state < 3) { $pdf->Close(); } if (!PMA_exportOutputHandler($pdf->buffer)) { return FALSE; } return TRUE; }
function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy) { global $cfgRelation; /** * Get the unique keys in the table */ $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db); $keys_result = PMA_DBI_query($keys_query); $unique_keys = array(); while ($key = PMA_DBI_fetch_assoc($keys_result)) { if ($key['Non_unique'] == 0) { $unique_keys[] = $key['Column_name']; } } PMA_DBI_free_result($keys_result); /** * Gets fields properties */ PMA_DBI_select_db($db); $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table); $result = PMA_DBI_query($local_query); $fields_cnt = PMA_DBI_num_rows($result); // Check if we can use Relations (Mike Beck) 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 . '% ' . $GLOBALS['strStructure'] . ': ' . $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 && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) { $columns_cnt++; $alignment .= 'l|'; } if ($do_mime && $cfgRelation['mimework']) { $columns_cnt++; $alignment .= 'l|'; } $buffer = $alignment . '} ' . $crlf; $header = ' \\hline '; $header .= '\\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strField'] . '}} & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strType'] . '}} & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strNull'] . '}} & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strDefault'] . '}}'; if ($do_relation && $have_rel) { $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strLinksTo'] . '}}'; } if ($do_comments && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) { $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strComments'] . '}}'; $comments = PMA_getComments($db, $table); } if ($do_mime && $cfgRelation['mimework']) { $header .= ' & \\multicolumn{1}{|c|}{\\textbf{MIME}}'; $mime_map = PMA_getMIME($db, $table, true); } $local_buffer = PMA_texEscape($table); // Table caption for first page and label if (isset($GLOBALS['latex_caption'])) { $buffer .= ' \\caption{' . str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_caption']) . '} \\label{' . str_replace('__TABLE__', $table, $GLOBALS['latex_structure_label']) . '} \\\\' . $crlf; } $buffer .= $header . ' \\\\ \\hline \\hline' . $crlf . '\\endfirsthead' . $crlf; // Table caption on next pages if (isset($GLOBALS['latex_caption'])) { $buffer .= ' \\caption{' . str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_continued_caption']) . '} \\\\ ' . $crlf; } $buffer .= $header . ' \\\\ \\hline \\hline \\endhead \\endfoot ' . $crlf; if (!PMA_exportOutputHandler($buffer)) { return FALSE; } while ($row = PMA_DBI_fetch_assoc($result)) { $type = $row['Type']; // reformat mysql query output - staybyte - 9. June 2001 // loic1: set or enum types: slashes single quotes inside options if (eregi('^(set|enum)\\((.+)\\)$', $type, $tmp)) { $tmp[2] = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1); $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')'; $type_nowrap = ''; $binary = 0; $unsigned = 0; $zerofill = 0; } else { $type_nowrap = ' nowrap="nowrap"'; $type = eregi_replace('BINARY', '', $type); $type = eregi_replace('ZEROFILL', '', $type); $type = eregi_replace('UNSIGNED', '', $type); if (empty($type)) { $type = ' '; } $binary = eregi('BINARY', $row['Type']); $unsigned = eregi('UNSIGNED', $row['Type']); $zerofill = eregi('ZEROFILL', $row['Type']); } if (!isset($row['Default'])) { if ($row['Null'] != '') { $row['Default'] = 'NULL'; } } else { $row['Default'] = $row['Default']; } $field_name = $row['Field']; $local_buffer = $field_name . "" . $type . "" . ($row['Null'] == '' ? $GLOBALS['strNo'] : $GLOBALS['strYes']) . "" . (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 PMA_DBI_free_result($result); $buffer = ' \\end{longtable}' . $crlf; return PMA_exportOutputHandler($buffer); }
/** * Outputs the content of a table in JSON format * * @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 string $sql_query SQL query for obtaining data * @param array $aliases Aliases of db/table/columns * * @return bool Whether it succeeded */ public function exportData($db, $table, $crlf, $error_url, $sql_query, $aliases = array()) { $db_alias = $db; $table_alias = $table; $this->initAlias($aliases, $db_alias, $table_alias); $result = $GLOBALS['dbi']->query($sql_query, null, PMA\libraries\DatabaseInterface::QUERY_UNBUFFERED); $columns_cnt = $GLOBALS['dbi']->numFields($result); $columns = array(); for ($i = 0; $i < $columns_cnt; $i++) { $col_as = $GLOBALS['dbi']->fieldName($result, $i); if (!empty($aliases[$db]['tables'][$table]['columns'][$col_as])) { $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as]; } $columns[$i] = stripslashes($col_as); } $record_cnt = 0; while ($record = $GLOBALS['dbi']->fetchRow($result)) { $record_cnt++; // Output table name as comment if this is the first record of the table if ($record_cnt == 1) { $buffer = $crlf . '// ' . $db_alias . '.' . $table_alias . $crlf . $crlf; $buffer .= '['; } else { $buffer = ', '; } if (!PMA_exportOutputHandler($buffer)) { return false; } $data = array(); for ($i = 0; $i < $columns_cnt; $i++) { $data[$columns[$i]] = $record[$i]; } if (isset($GLOBALS['json_pretty_print']) && $GLOBALS['json_pretty_print']) { $encoded = json_encode($data, JSON_PRETTY_PRINT); } else { $encoded = json_encode($data); } if (!PMA_exportOutputHandler($encoded)) { return false; } } if ($record_cnt) { if (!PMA_exportOutputHandler(']' . $crlf)) { return false; } } $GLOBALS['dbi']->freeResult($result); return true; }