【php开源代码栏目提醒】:网学会员在php开源代码频道为大家收集整理了db_details_importdocsql.php提供大家参考,希望对大家有所帮助!
<?php
/* $Id: db_details_importdocsql.php,v 2.11 2006/01/17 17:02:28 cybot_tm Exp $ */
// vim: expandtab sw=4 ts=4 sts=4:
require_once('./libraries/common.lib.php');
/**
* This script imports relation infos from docSQL (www.databay.de)
*/
/**
* Get the values of the variables posted or sent to this script and display
* the headers
*/
require_once('./libraries/read_dump.lib.php');
require_once('./libraries/header.inc.php');
// Check parameters
PMA_checkParameters(array('db'));
// We do any work, only if docSQL import was enabled in config
if (isset($cfg['docSQLDir']) && !empty($cfg['docSQLDir'])) {
if (substr($cfg['docSQLDir'], -1) != '/') {
$cfg['docSQLDir'] .= '/';
}
/**
* Imports docSQL files
*
* @param string the basepath
* @param string the filename
* @param string the complete filename
* @param string the content of a file
*
* @return boolean always true
*
* @global array GLOBAL variables
*/
function docsql_check($docpath = '', $file = '', $filename = '', $content = 'none') {
global $GLOBALS;
if (preg_match('@^(.*)_field_comment\.(txt|zip|bz2|bzip).*$@i', $filename)) {
$tab = preg_replace('@^(.*)_field_comment\.(txt|zip|bz2|bzip).*@si', '\1', $filename);
//echo '<h1>Working on Table ' . $_tab . '</h1>';
if ($content == 'none') {
$lines = array();
$fd = fopen($docpath . $file, 'r');
if ($fd) {
while (!feof($fd)) {
$lines[] = fgets($fd, 4096);
}
}
} else {
$content = str_replace("\r\n", "\n", $content);
$content = str_replace("\r", "\n", $content);
$lines = explode("\n", $content);
}
if (isset($lines) && is_array($lines) && count($lines) > 0) {
foreach ($lines AS $lkey => $line) {
//echo '<p>' . $line . '</p>';
$inf = explode('|', $line);
if (!empty($inf[1]) && strlen(trim($inf[1])) > 0) {
$qry = '
INSERT INTO
' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['column_info']) . '
( db_name, table_name, column_name, ' . PMA_backquote('comment') . ' )
VALUES (
\'' . PMA_sqlAddslashes($GLOBALS['db']) . '\',
\'' . PMA_sqlAddslashes(trim($tab)) . '\',
\'' . PMA_sqlAddslashes(trim($inf[0])) . '\',
\'' . PMA_sqlAddslashes(trim($inf[1])) . '\')';
if (PMA_query_as_cu($qry)) {
echo '<p>' . $GLOBALS['strAddedColumnComment'] . ' ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]) . '</p>';
} else {
echo '<p>' . $GLOBALS['strWritingCommentNotPossible'] . '</p>';
}
echo "\n";
} // end inf[1] exists
if (!empty($inf[2]) && strlen(trim($inf[2])) > 0) {
$for = explode('->', $inf[2]);
$qry = '
INSERT INTO
' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['relation']) . '
( master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)
VALUES (
\'' . PMA_sqlAddslashes($GLOBALS['db']) . '\',
\'' . PMA_sqlAddslashes(trim($tab)) . '\',
\'' . PMA_sqlAddslashes(trim($inf[0])) . '\',
\'' . PMA_sqlAddslashes($GLOBALS['db']) . '\',
\'' . PMA_sqlAddslashes(trim($for[0])) . '\',
\'' . PMA_sqlAddslashes(trim($for[1])) . '\')';
if (PMA_query_as_cu($qry)) {
echo '<p>' . $GLOBALS['strAddedColumnRelation'] . ' ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]) . ' to ' . htmlspecialchars($inf[2]) . '</p>';
} else {
echo '<p>' . $GLOBALS['strWritingRelationNotPossible'] . '</p>';
}
echo "\n";
} // end inf[2] exists
}
echo '<p><font color="green">' . sprintf($GLOBALS['strImportSuccessfullyFinished'], count($lines)) . '</font></p>' . "\n";
} else {
echo '<p><font color="red">' . $GLOBALS['strFileCouldNotBeRead'] . '</font></p>' . "\n";
}
return 1;
} else {
if ($content != 'none') {
echo '<p><font color="orange">' . sprintf($GLOBALS['strIgnoringFile'], ' ' . htmlspecialchars($file)) . '</font></p>' . "\n";
} else {
// garvin: disabled. Shouldn't impose ANY non-submitted files ever.
echo '<p><font color="orange">' . sprintf($GLOBALS['strIgnoringFile'], ' ' . '...') . '</font></p>' . "\n";
}
return 0;
} // end working on table
}
/**
* Try to get the "$DOCUMENT_ROOT" variable whatever is the register_globals
* value
*/
if (empty($DOCUMENT_ROOT)) {
if (!empty($_SERVER) && isset($_SERVER['DOCUMENT_ROOT'])) {
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
} elseif (!empty($_ENV) && isset($_ENV['DOCUMENT_ROOT'])) {
$DOCUMENT_ROOT = $_ENV['DOCUMENT_ROOT'];
} elseif (@getenv('DOCUMENT_ROOT')) {
$DOCUMENT_ROOT = getenv('DOCUMENT_ROOT');
} else {
$DOCUMENT_ROOT = '.';
}
} // end if
/**
* Executes import if required
*/
if (isset($do) && $do == 'import') {
$orig_do
cpath = $docpath;
if (empty($sql_file)) {
$sql_file = 'none';
}
// Get relation settings
require_once('./libraries/relation.lib.php');
$cfgRelation = PMA_getRelationsParam();
// G