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