ExportPhparray.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. * Removes end of comment from a string
  78. *
  79. * @param string $string String to replace
  80. *
  81. * @return string
  82. */
  83. public function commentString($string)
  84. {
  85. return strtr($string, '*/', '-');
  86. }
  87. /**
  88. * Outputs export header
  89. *
  90. * @return bool Whether it succeeded
  91. */
  92. public function exportHeader ()
  93. {
  94. PMA_exportOutputHandler(
  95. '<?php' . $GLOBALS['crlf']
  96. . '/**' . $GLOBALS['crlf']
  97. . ' * Export to PHP Array plugin for PHPMyAdmin' . $GLOBALS['crlf']
  98. . ' * @version 0.2b' . $GLOBALS['crlf']
  99. . ' */' . $GLOBALS['crlf'] . $GLOBALS['crlf']
  100. );
  101. return true;
  102. }
  103. /**
  104. * Outputs export footer
  105. *
  106. * @return bool Whether it succeeded
  107. */
  108. public function exportFooter ()
  109. {
  110. return true;
  111. }
  112. /**
  113. * Outputs database header
  114. *
  115. * @param string $db Database name
  116. *
  117. * @return bool Whether it succeeded
  118. */
  119. public function exportDBHeader ($db)
  120. {
  121. PMA_exportOutputHandler(
  122. '/**' . $GLOBALS['crlf']
  123. . ' * Database ' . $this->commentString(PMA_Util::backquote($db))
  124. . $GLOBALS['crlf'] . ' */' . $GLOBALS['crlf']
  125. );
  126. return true;
  127. }
  128. /**
  129. * Outputs database footer
  130. *
  131. * @param string $db Database name
  132. *
  133. * @return bool Whether it succeeded
  134. */
  135. public function exportDBFooter ($db)
  136. {
  137. return true;
  138. }
  139. /**
  140. * Outputs CREATE DATABASE statement
  141. *
  142. * @param string $db Database name
  143. *
  144. * @return bool Whether it succeeded
  145. */
  146. public function exportDBCreate($db)
  147. {
  148. return true;
  149. }
  150. /**
  151. * Outputs the content of a table in NHibernate format
  152. *
  153. * @param string $db database name
  154. * @param string $table table name
  155. * @param string $crlf the end of line sequence
  156. * @param string $error_url the url to go back in case of error
  157. * @param string $sql_query SQL query for obtaining data
  158. *
  159. * @return bool Whether it succeeded
  160. */
  161. public function exportData($db, $table, $crlf, $error_url, $sql_query)
  162. {
  163. $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
  164. $columns_cnt = PMA_DBI_num_fields($result);
  165. for ($i = 0; $i < $columns_cnt; $i++) {
  166. $columns[$i] = stripslashes(PMA_DBI_field_name($result, $i));
  167. }
  168. unset($i);
  169. // fix variable names (based on
  170. // http://www.php.net/manual/language.variables.basics.php)
  171. if (! preg_match(
  172. '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/',
  173. $table
  174. )) {
  175. // fix invalid characters in variable names by replacing them with
  176. // underscores
  177. $tablefixed = preg_replace('/[^a-zA-Z0-9_\x7f-\xff]/', '_', $table);
  178. // variable name must not start with a number or dash...
  179. if (preg_match('/^[a-zA-Z_\x7f-\xff]/', $tablefixed) == false) {
  180. $tablefixed = '_' . $tablefixed;
  181. }
  182. } else {
  183. $tablefixed = $table;
  184. }
  185. $buffer = '';
  186. $record_cnt = 0;
  187. // Output table name as comment
  188. $buffer .= $crlf . '/* '
  189. . $this->commentString(PMA_Util::backquote($db)) . '.'
  190. . $this->commentString(PMA_Util::backquote($table)) . ' */' . $crlf;
  191. $buffer .= '$' . $tablefixed . ' = array(';
  192. while ($record = PMA_DBI_fetch_row($result)) {
  193. $record_cnt++;
  194. if ($record_cnt == 1) {
  195. $buffer .= $crlf . ' array(';
  196. } else {
  197. $buffer .= ',' . $crlf . ' array(';
  198. }
  199. for ($i = 0; $i < $columns_cnt; $i++) {
  200. $buffer .= var_export($columns[$i], true)
  201. . " => " . var_export($record[$i], true)
  202. . (($i + 1 >= $columns_cnt) ? '' : ',');
  203. }
  204. $buffer .= ')';
  205. }
  206. $buffer .= $crlf . ');' . $crlf;
  207. if (! PMA_exportOutputHandler($buffer)) {
  208. return false;
  209. }
  210. PMA_DBI_free_result($result);
  211. return true;
  212. }
  213. }
  214. ?>