3.4.0-rc1
[phpmyadmin/thilanka.git] / libraries / import / docsql.php
bloba64a09410638ff34854d3f1465f782a5c610fb4d
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * DocSQL import plugin for phpMyAdmin
6 * @package phpMyAdmin-Import
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * Load relations.
15 $cfgRelation = PMA_getRelationsParam();
17 /**
18 * We need relations enabled and we work only on database
20 if ($plugin_param !== 'database' || $GLOBALS['num_tables'] < 1
21 || ! $cfgRelation['relwork'] || ! $cfgRelation['commwork']) {
22 return;
25 if (isset($plugin_list)) {
26 $plugin_list['docsql'] = array( // set name of your plugin
27 'text' => __('DocSQL'), // text to be displayed as choice
28 'extension' => '', // extension this plugin can handle
29 'options' => array( // array of options for your plugin (optional)
30 array('type' => 'begin_group', 'name' => 'general_opts'),
31 array('type' => 'text', 'name' => 'table', 'text' => __('Table name')),
32 array('type' => 'end_group')
34 'options_text' => __('Options'), // text to describe plugin options (must be set if options are used)
36 /* We do not define function when plugin is just queried for information above */
37 return;
40 $tab = $_POST['docsql_table'];
41 $buffer = '';
42 /* Read whole buffer, we except it is small enough */
43 while (!$finished && !$error && !$timeout_passed) {
44 $data = PMA_importGetNextChunk();
45 if ($data === FALSE) {
46 // subtract data we didn't handle yet and stop processing
47 break;
48 } elseif ($data === TRUE) {
49 // nothing to read
50 break;
51 } else {
52 // Append new data to buffer
53 $buffer .= $data;
55 } // End of import loop
56 /* Process the data */
57 if ($data === TRUE && !$error && !$timeout_passed) {
58 $buffer = str_replace("\r\n", "\n", $buffer);
59 $buffer = str_replace("\r", "\n", $buffer);
60 $lines = explode("\n", $buffer);
61 foreach ($lines AS $lkey => $line) {
62 //echo '<p>' . $line . '</p>';
63 $inf = explode('|', $line);
64 if (!empty($inf[1]) && strlen(trim($inf[1])) > 0) {
65 $qry = '
66 INSERT INTO
67 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
68 (db_name, table_name, column_name, ' . PMA_backquote('comment') . ')
69 VALUES (
70 \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\',
71 \'' . PMA_sqlAddslashes(trim($tab)) . '\',
72 \'' . PMA_sqlAddslashes(trim($inf[0])) . '\',
73 \'' . PMA_sqlAddslashes(trim($inf[1])) . '\')';
74 PMA_importRunQuery($qry, $qry . '-- ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]), true);
75 } // end inf[1] exists
76 if (!empty($inf[2]) && strlen(trim($inf[2])) > 0) {
77 $for = explode('->', $inf[2]);
78 $qry = '
79 INSERT INTO
80 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation']) . '
81 (master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)
82 VALUES (
83 \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\',
84 \'' . PMA_sqlAddslashes(trim($tab)) . '\',
85 \'' . PMA_sqlAddslashes(trim($inf[0])) . '\',
86 \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\',
87 \'' . PMA_sqlAddslashes(trim($for[0])) . '\',
88 \'' . PMA_sqlAddslashes(trim($for[1])) . '\')';
89 PMA_importRunQuery($qry, $qry . '-- ' . htmlspecialchars($tab) . '.' . htmlspecialchars($inf[0]) . '(' . htmlspecialchars($inf[2]) . ')', true);
90 } // end inf[2] exists
91 } // End lines loop
92 } // End import
93 // Commit any possible data in buffers
94 PMA_importRunQuery();