ExportCsv.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * CSV export code
  5. *
  6. * @package PhpMyAdmin-Export
  7. * @subpackage CSV
  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 CSV format
  16. *
  17. * @package PhpMyAdmin-Export
  18. * @subpackage CSV
  19. */
  20. class ExportCsv extends ExportPlugin
  21. {
  22. /**
  23. * The string used to end lines
  24. *
  25. * @var string
  26. */
  27. private $_csvTerminated;
  28. /**
  29. * The string used to separate columns
  30. *
  31. * @var string
  32. */
  33. private $_csvSeparator;
  34. /**
  35. * The string used to enclose columns
  36. *
  37. * @var string
  38. */
  39. private $_csvEnclosed;
  40. /**
  41. * The string used to escape columns
  42. *
  43. * @var string
  44. */
  45. private $_csvEscaped;
  46. /**
  47. * Constructor
  48. */
  49. public function __construct()
  50. {
  51. $this->setProperties();
  52. }
  53. /**
  54. * Sets the export CSV properties
  55. *
  56. * @return void
  57. */
  58. protected function setProperties()
  59. {
  60. $props = 'libraries/properties/';
  61. include_once "$props/plugins/ExportPluginProperties.class.php";
  62. include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
  63. include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
  64. include_once "$props/options/items/TextPropertyItem.class.php";
  65. include_once "$props/options/items/BoolPropertyItem.class.php";
  66. include_once "$props/options/items/HiddenPropertyItem.class.php";
  67. $exportPluginProperties = new ExportPluginProperties();
  68. $exportPluginProperties->setText('CSV');
  69. $exportPluginProperties->setExtension('csv');
  70. $exportPluginProperties->setMimeType('text/comma-separated-values');
  71. $exportPluginProperties->setOptionsText(__('Options'));
  72. // create the root group that will be the options field for
  73. // $exportPluginProperties
  74. // this will be shown as "Format specific options"
  75. $exportSpecificOptions = new OptionsPropertyRootGroup();
  76. $exportSpecificOptions->setName("Format Specific Options");
  77. // general options main group
  78. $generalOptions = new OptionsPropertyMainGroup();
  79. $generalOptions->setName("general_opts");
  80. // create leaf items and add them to the group
  81. $leaf = new TextPropertyItem();
  82. $leaf->setName("separator");
  83. $leaf->setText(__('Columns separated with:'));
  84. $generalOptions->addProperty($leaf);
  85. $leaf = new TextPropertyItem();
  86. $leaf->setName("enclosed");
  87. $leaf->setText(__('Columns enclosed with:'));
  88. $generalOptions->addProperty($leaf);
  89. $leaf = new TextPropertyItem();
  90. $leaf->setName("escaped");
  91. $leaf->setText(__('Columns escaped with:'));
  92. $generalOptions->addProperty($leaf);
  93. $leaf = new TextPropertyItem();
  94. $leaf->setName("terminated");
  95. $leaf->setText(__('Lines terminated with:'));
  96. $generalOptions->addProperty($leaf);
  97. $leaf = new TextPropertyItem();
  98. $leaf->setName('null');
  99. $leaf->setText(__('Replace NULL with:'));
  100. $generalOptions->addProperty($leaf);
  101. $leaf = new BoolPropertyItem();
  102. $leaf->setName('removeCRLF');
  103. $leaf->setText(
  104. __('Remove carriage return/line feed characters within columns')
  105. );
  106. $generalOptions->addProperty($leaf);
  107. $leaf = new BoolPropertyItem();
  108. $leaf->setName('columns');
  109. $leaf->setText(__('Put columns names in the first row'));
  110. $generalOptions->addProperty($leaf);
  111. $leaf = new HiddenPropertyItem();
  112. $leaf->setName('structure_or_data');
  113. $generalOptions->addProperty($leaf);
  114. // add the main group to the root group
  115. $exportSpecificOptions->addProperty($generalOptions);
  116. // set the options for the export plugin property item
  117. $exportPluginProperties->setOptions($exportSpecificOptions);
  118. $this->properties = $exportPluginProperties;
  119. }
  120. /**
  121. * This method is called when any PluginManager to which the observer
  122. * is attached calls PluginManager::notify()
  123. *
  124. * @param SplSubject $subject The PluginManager notifying the observer
  125. * of an update.
  126. *
  127. * @return void
  128. */
  129. public function update (SplSubject $subject)
  130. {
  131. }
  132. /**
  133. * Outputs export header
  134. *
  135. * @return bool Whether it succeeded
  136. */
  137. public function exportHeader ()
  138. {
  139. global $what, $csv_terminated, $csv_separator, $csv_enclosed, $csv_escaped;
  140. // Here we just prepare some values for export
  141. if ($what == 'excel') {
  142. $csv_terminated = "\015\012";
  143. switch($GLOBALS['excel_edition']) {
  144. case 'win':
  145. // as tested on Windows with Excel 2002 and Excel 2007
  146. $csv_separator = ';';
  147. break;
  148. case 'mac_excel2003':
  149. $csv_separator = ';';
  150. break;
  151. case 'mac_excel2008':
  152. $csv_separator = ',';
  153. break;
  154. }
  155. $csv_enclosed = '"';
  156. $csv_escaped = '"';
  157. if (isset($GLOBALS['excel_columns'])) {
  158. $GLOBALS['csv_columns'] = 'yes';
  159. }
  160. } else {
  161. if (empty($csv_terminated) || strtolower($csv_terminated) == 'auto') {
  162. $csv_terminated = $GLOBALS['crlf'];
  163. } else {
  164. $csv_terminated = str_replace('\\r', "\015", $csv_terminated);
  165. $csv_terminated = str_replace('\\n', "\012", $csv_terminated);
  166. $csv_terminated = str_replace('\\t', "\011", $csv_terminated);
  167. } // end if
  168. $csv_separator = str_replace('\\t', "\011", $csv_separator);
  169. }
  170. return true;
  171. }
  172. /**
  173. * Outputs export footer
  174. *
  175. * @return bool Whether it succeeded
  176. */
  177. public function exportFooter ()
  178. {
  179. return true;
  180. }
  181. /**
  182. * Outputs database header
  183. *
  184. * @param string $db Database name
  185. *
  186. * @return bool Whether it succeeded
  187. */
  188. public function exportDBHeader ($db)
  189. {
  190. return true;
  191. }
  192. /**
  193. * Outputs database footer
  194. *
  195. * @param string $db Database name
  196. *
  197. * @return bool Whether it succeeded
  198. */
  199. public function exportDBFooter ($db)
  200. {
  201. return true;
  202. }
  203. /**
  204. * Outputs CREATE DATABASE statement
  205. *
  206. * @param string $db Database name
  207. *
  208. * @return bool Whether it succeeded
  209. */
  210. public function exportDBCreate($db)
  211. {
  212. return true;
  213. }
  214. /**
  215. * Outputs the content of a table in CSV format
  216. *
  217. * @param string $db database name
  218. * @param string $table table name
  219. * @param string $crlf the end of line sequence
  220. * @param string $error_url the url to go back in case of error
  221. * @param string $sql_query SQL query for obtaining data
  222. *
  223. * @return bool Whether it succeeded
  224. */
  225. public function exportData($db, $table, $crlf, $error_url, $sql_query)
  226. {
  227. global $what, $csv_terminated, $csv_separator, $csv_enclosed, $csv_escaped;
  228. // Gets the data from the database
  229. $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
  230. $fields_cnt = PMA_DBI_num_fields($result);
  231. // If required, get fields name at the first line
  232. if (isset($GLOBALS['csv_columns'])) {
  233. $schema_insert = '';
  234. for ($i = 0; $i < $fields_cnt; $i++) {
  235. if ($csv_enclosed == '') {
  236. $schema_insert .= stripslashes(PMA_DBI_field_name($result, $i));
  237. } else {
  238. $schema_insert .= $csv_enclosed
  239. . str_replace(
  240. $csv_enclosed,
  241. $csv_escaped . $csv_enclosed,
  242. stripslashes(PMA_DBI_field_name($result, $i))
  243. )
  244. . $csv_enclosed;
  245. }
  246. $schema_insert .= $csv_separator;
  247. } // end for
  248. $schema_insert = trim(substr($schema_insert, 0, -1));
  249. if (! PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
  250. return false;
  251. }
  252. } // end if
  253. // Format the data
  254. while ($row = PMA_DBI_fetch_row($result)) {
  255. $schema_insert = '';
  256. for ($j = 0; $j < $fields_cnt; $j++) {
  257. if (! isset($row[$j]) || is_null($row[$j])) {
  258. $schema_insert .= $GLOBALS[$what . '_null'];
  259. } elseif ($row[$j] == '0' || $row[$j] != '') {
  260. // always enclose fields
  261. if ($what == 'excel') {
  262. $row[$j] = preg_replace("/\015(\012)?/", "\012", $row[$j]);
  263. }
  264. // remove CRLF characters within field
  265. if (isset($GLOBALS[$what . '_removeCRLF'])
  266. && $GLOBALS[$what . '_removeCRLF']
  267. ) {
  268. $row[$j] = str_replace(
  269. "\n",
  270. "",
  271. str_replace(
  272. "\r",
  273. "",
  274. $row[$j]
  275. )
  276. );
  277. }
  278. if ($csv_enclosed == '') {
  279. $schema_insert .= $row[$j];
  280. } else {
  281. // also double the escape string if found in the data
  282. if ($csv_escaped != $csv_enclosed) {
  283. $schema_insert .= $csv_enclosed
  284. . str_replace(
  285. $csv_enclosed,
  286. $csv_escaped . $csv_enclosed,
  287. str_replace(
  288. $csv_escaped,
  289. $csv_escaped . $csv_escaped,
  290. $row[$j]
  291. )
  292. )
  293. . $csv_enclosed;
  294. } else {
  295. // avoid a problem when escape string equals enclose
  296. $schema_insert .= $csv_enclosed
  297. . str_replace(
  298. $csv_enclosed,
  299. $csv_escaped . $csv_enclosed,
  300. $row[$j]
  301. )
  302. . $csv_enclosed;
  303. }
  304. }
  305. } else {
  306. $schema_insert .= '';
  307. }
  308. if ($j < $fields_cnt-1) {
  309. $schema_insert .= $csv_separator;
  310. }
  311. } // end for
  312. if (! PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
  313. return false;
  314. }
  315. } // end while
  316. PMA_DBI_free_result($result);
  317. return true;
  318. }
  319. }
  320. ?>