ExportPhparray.class.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Set of functions used to build dumps of tables as PHP Arrays
  5. *
  6. * @package PhpMyAdmin-Export
  7. * @subpackage PHP
  8. */
  9. if (! defined('PHPMYADMIN')) {
  10. exit;
  11. }
  12. /* Get the export interface */
  13. require_once 'libraries/plugins/ExportPlugin.class.php';
  14. /**
  15. * Handles the export for the PHP Array class
  16. *
  17. * @package PhpMyAdmin-Export
  18. * @subpackage PHP
  19. */
  20. class ExportPhparray extends ExportPlugin
  21. {
  22. /**
  23. * Constructor
  24. */
  25. public function __construct()
  26. {
  27. $this->setProperties();
  28. }
  29. /**
  30. * Sets the export PHP Array properties
  31. *
  32. * @return void
  33. */
  34. protected function setProperties()
  35. {
  36. $props = 'libraries/properties/';
  37. include_once "$props/plugins/ExportPluginProperties.class.php";
  38. include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
  39. include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
  40. include_once "$props/options/items/HiddenPropertyItem.class.php";
  41. $exportPluginProperties = new ExportPluginProperties();
  42. $exportPluginProperties->setText('PHP array');
  43. $exportPluginProperties->setExtension('php');
  44. $exportPluginProperties->setMimeType('text/plain');
  45. $exportPluginProperties->setOptionsText(__('Options'));
  46. // create the root group that will be the options field for
  47. // $exportPluginProperties
  48. // this will be shown as "Format specific options"
  49. $exportSpecificOptions = new OptionsPropertyRootGroup();
  50. $exportSpecificOptions->setName("Format Specific Options");
  51. // general options main group
  52. $generalOptions = new OptionsPropertyMainGroup();
  53. $generalOptions->setName("general_opts");
  54. // create primary items and add them to the group
  55. $leaf = new HiddenPropertyItem();
  56. $leaf->setName("structure_or_data");
  57. $generalOptions->addProperty($leaf);
  58. // add the main group to the root group
  59. $exportSpecificOptions->addProperty($generalOptions);
  60. // set the options for the export plugin property item
  61. $exportPluginProperties->setOptions($exportSpecificOptions);
  62. $this->properties = $exportPluginProperties;
  63. }
  64. /**
  65. * This method is called when any PluginManager to which the observer
  66. * is attached calls PluginManager::notify()
  67. *
  68. * @param SplSubject $subject The PluginManager notifying the observer
  69. * of an update.
  70. *
  71. * @return void
  72. */
  73. public function update (SplSubject $subject)
  74. {
  75. }
  76. /**
  77. * Outputs export header
  78. *
  79. * @return bool Whether it succeeded
  80. */
  81. public function exportHeader ()
  82. {
  83. PMA_exportOutputHandler(
  84. '<?php' . $GLOBALS['crlf']
  85. . '/**' . $GLOBALS['crlf']
  86. . ' * Export to PHP Array plugin for PHPMyAdmin' . $GLOBALS['crlf']
  87. . ' * @version 0.2b' . $GLOBALS['crlf']
  88. . ' */' . $GLOBALS['crlf'] . $GLOBALS['crlf']
  89. );
  90. return true;
  91. }
  92. /**
  93. * Outputs export footer
  94. *
  95. * @return bool Whether it succeeded
  96. */
  97. public function exportFooter ()
  98. {
  99. return true;
  100. }
  101. /**
  102. * Outputs database header
  103. *
  104. * @param string $db Database name
  105. *
  106. * @return bool Whether it succeeded
  107. */
  108. public function exportDBHeader ($db)
  109. {
  110. PMA_exportOutputHandler(
  111. '//' . $GLOBALS['crlf']
  112. . '// Database ' . PMA_Util::backquote($db)
  113. . $GLOBALS['crlf'] . '//' . $GLOBALS['crlf']
  114. );
  115. return true;
  116. }
  117. /**
  118. * Outputs database footer
  119. *
  120. * @param string $db Database name
  121. *
  122. * @return bool Whether it succeeded
  123. */
  124. public function exportDBFooter ($db)
  125. {
  126. return true;
  127. }
  128. /**
  129. * Outputs CREATE DATABASE statement
  130. *
  131. * @param string $db Database name
  132. *
  133. * @return bool Whether it succeeded
  134. */
  135. public function exportDBCreate($db)
  136. {
  137. return true;
  138. }
  139. /**
  140. * Outputs the content of a table in PHP array format
  141. *
  142. * @param string $db database name
  143. * @param string $table table name
  144. * @param string $crlf the end of line sequence
  145. * @param string $error_url the url to go back in case of error
  146. * @param string $sql_query SQL query for obtaining data
  147. *
  148. * @return bool Whether it succeeded
  149. */
  150. public function exportData($db, $table, $crlf, $error_url, $sql_query)
  151. {
  152. $result = $GLOBALS['dbi']->query(
  153. $sql_query, null, PMA_DatabaseInterface::QUERY_UNBUFFERED
  154. );
  155. $columns_cnt = $GLOBALS['dbi']->numFields($result);
  156. $columns = array();
  157. for ($i = 0; $i < $columns_cnt; $i++) {
  158. $columns[$i] = stripslashes($GLOBALS['dbi']->fieldName($result, $i));
  159. }
  160. unset($i);
  161. // fix variable names (based on
  162. // http://www.php.net/manual/language.variables.basics.php)
  163. if (! preg_match(
  164. '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/',
  165. $table
  166. )) {
  167. // fix invalid characters in variable names by replacing them with
  168. // underscores
  169. $tablefixed = preg_replace('/[^a-zA-Z0-9_\x7f-\xff]/', '_', $table);
  170. // variable name must not start with a number or dash...
  171. if (preg_match('/^[a-zA-Z_\x7f-\xff]/', $tablefixed) == false) {
  172. $tablefixed = '_' . $tablefixed;
  173. }
  174. } else {
  175. $tablefixed = $table;
  176. }
  177. $buffer = '';
  178. $record_cnt = 0;
  179. // Output table name as comment
  180. $buffer .= $crlf . '// '
  181. . PMA_Util::backquote($db) . '.'
  182. . PMA_Util::backquote($table) . $crlf;
  183. $buffer .= '$' . $tablefixed . ' = array(';
  184. while ($record = $GLOBALS['dbi']->fetchRow($result)) {
  185. $record_cnt++;
  186. if ($record_cnt == 1) {
  187. $buffer .= $crlf . ' array(';
  188. } else {
  189. $buffer .= ',' . $crlf . ' array(';
  190. }
  191. for ($i = 0; $i < $columns_cnt; $i++) {
  192. $buffer .= var_export($columns[$i], true)
  193. . " => " . var_export($record[$i], true)
  194. . (($i + 1 >= $columns_cnt) ? '' : ',');
  195. }
  196. $buffer .= ')';
  197. }
  198. $buffer .= $crlf . ');' . $crlf;
  199. if (! PMA_exportOutputHandler($buffer)) {
  200. return false;
  201. }
  202. $GLOBALS['dbi']->freeResult($result);
  203. return true;
  204. }
  205. }
  206. ?>