ExportOds.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Set of functions used to build OpenDocument Spreadsheet dumps of tables
  5. *
  6. * @package PhpMyAdmin-Export
  7. * @subpackage ODS
  8. */
  9. namespace PhpMyAdmin\Plugins\Export;
  10. use PhpMyAdmin\DatabaseInterface;
  11. use PhpMyAdmin\Export;
  12. use PhpMyAdmin\OpenDocument;
  13. use PhpMyAdmin\Plugins\ExportPlugin;
  14. use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
  15. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
  16. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
  17. use PhpMyAdmin\Properties\Options\Items\BoolPropertyItem;
  18. use PhpMyAdmin\Properties\Options\Items\HiddenPropertyItem;
  19. use PhpMyAdmin\Properties\Options\Items\TextPropertyItem;
  20. /**
  21. * Handles the export for the ODS class
  22. *
  23. * @package PhpMyAdmin-Export
  24. * @subpackage ODS
  25. */
  26. class ExportOds extends ExportPlugin
  27. {
  28. /**
  29. * Constructor
  30. */
  31. public function __construct()
  32. {
  33. $GLOBALS['ods_buffer'] = '';
  34. $this->setProperties();
  35. }
  36. /**
  37. * Sets the export ODS properties
  38. *
  39. * @return void
  40. */
  41. protected function setProperties()
  42. {
  43. $exportPluginProperties = new ExportPluginProperties();
  44. $exportPluginProperties->setText('OpenDocument Spreadsheet');
  45. $exportPluginProperties->setExtension('ods');
  46. $exportPluginProperties->setMimeType(
  47. 'application/vnd.oasis.opendocument.spreadsheet'
  48. );
  49. $exportPluginProperties->setForceFile(true);
  50. $exportPluginProperties->setOptionsText(__('Options'));
  51. // create the root group that will be the options field for
  52. // $exportPluginProperties
  53. // this will be shown as "Format specific options"
  54. $exportSpecificOptions = new OptionsPropertyRootGroup(
  55. "Format Specific Options"
  56. );
  57. // general options main group
  58. $generalOptions = new OptionsPropertyMainGroup("general_opts");
  59. // create primary items and add them to the group
  60. $leaf = new TextPropertyItem(
  61. "null",
  62. __('Replace NULL with:')
  63. );
  64. $generalOptions->addProperty($leaf);
  65. $leaf = new BoolPropertyItem(
  66. "columns",
  67. __('Put columns names in the first row')
  68. );
  69. $generalOptions->addProperty($leaf);
  70. $leaf = new HiddenPropertyItem("structure_or_data");
  71. $generalOptions->addProperty($leaf);
  72. // add the main group to the root group
  73. $exportSpecificOptions->addProperty($generalOptions);
  74. // set the options for the export plugin property item
  75. $exportPluginProperties->setOptions($exportSpecificOptions);
  76. $this->properties = $exportPluginProperties;
  77. }
  78. /**
  79. * Outputs export header
  80. *
  81. * @return bool Whether it succeeded
  82. */
  83. public function exportHeader()
  84. {
  85. $GLOBALS['ods_buffer'] .= '<?xml version="1.0" encoding="utf-8"?' . '>'
  86. . '<office:document-content '
  87. . OpenDocument::NS . ' office:version="1.0">'
  88. . '<office:automatic-styles>'
  89. . '<number:date-style style:name="N37"'
  90. . ' number:automatic-order="true">'
  91. . '<number:month number:style="long"/>'
  92. . '<number:text>/</number:text>'
  93. . '<number:day number:style="long"/>'
  94. . '<number:text>/</number:text>'
  95. . '<number:year/>'
  96. . '</number:date-style>'
  97. . '<number:time-style style:name="N43">'
  98. . '<number:hours number:style="long"/>'
  99. . '<number:text>:</number:text>'
  100. . '<number:minutes number:style="long"/>'
  101. . '<number:text>:</number:text>'
  102. . '<number:seconds number:style="long"/>'
  103. . '<number:text> </number:text>'
  104. . '<number:am-pm/>'
  105. . '</number:time-style>'
  106. . '<number:date-style style:name="N50"'
  107. . ' number:automatic-order="true"'
  108. . ' number:format-source="language">'
  109. . '<number:month/>'
  110. . '<number:text>/</number:text>'
  111. . '<number:day/>'
  112. . '<number:text>/</number:text>'
  113. . '<number:year/>'
  114. . '<number:text> </number:text>'
  115. . '<number:hours number:style="long"/>'
  116. . '<number:text>:</number:text>'
  117. . '<number:minutes number:style="long"/>'
  118. . '<number:text> </number:text>'
  119. . '<number:am-pm/>'
  120. . '</number:date-style>'
  121. . '<style:style style:name="DateCell" style:family="table-cell"'
  122. . ' style:parent-style-name="Default" style:data-style-name="N37"/>'
  123. . '<style:style style:name="TimeCell" style:family="table-cell"'
  124. . ' style:parent-style-name="Default" style:data-style-name="N43"/>'
  125. . '<style:style style:name="DateTimeCell" style:family="table-cell"'
  126. . ' style:parent-style-name="Default" style:data-style-name="N50"/>'
  127. . '</office:automatic-styles>'
  128. . '<office:body>'
  129. . '<office:spreadsheet>';
  130. return true;
  131. }
  132. /**
  133. * Outputs export footer
  134. *
  135. * @return bool Whether it succeeded
  136. */
  137. public function exportFooter()
  138. {
  139. $GLOBALS['ods_buffer'] .= '</office:spreadsheet>'
  140. . '</office:body>'
  141. . '</office:document-content>';
  142. return Export::outputHandler(
  143. OpenDocument::create(
  144. 'application/vnd.oasis.opendocument.spreadsheet',
  145. $GLOBALS['ods_buffer']
  146. )
  147. );
  148. }
  149. /**
  150. * Outputs database header
  151. *
  152. * @param string $db Database name
  153. * @param string $db_alias Aliases of db
  154. *
  155. * @return bool Whether it succeeded
  156. */
  157. public function exportDBHeader($db, $db_alias = '')
  158. {
  159. return true;
  160. }
  161. /**
  162. * Outputs database footer
  163. *
  164. * @param string $db Database name
  165. *
  166. * @return bool Whether it succeeded
  167. */
  168. public function exportDBFooter($db)
  169. {
  170. return true;
  171. }
  172. /**
  173. * Outputs CREATE DATABASE statement
  174. *
  175. * @param string $db Database name
  176. * @param string $export_type 'server', 'database', 'table'
  177. * @param string $db_alias Aliases of db
  178. *
  179. * @return bool Whether it succeeded
  180. */
  181. public function exportDBCreate($db, $export_type, $db_alias = '')
  182. {
  183. return true;
  184. }
  185. /**
  186. * Outputs the content of a table in NHibernate format
  187. *
  188. * @param string $db database name
  189. * @param string $table table name
  190. * @param string $crlf the end of line sequence
  191. * @param string $error_url the url to go back in case of error
  192. * @param string $sql_query SQL query for obtaining data
  193. * @param array $aliases Aliases of db/table/columns
  194. *
  195. * @return bool Whether it succeeded
  196. */
  197. public function exportData(
  198. $db,
  199. $table,
  200. $crlf,
  201. $error_url,
  202. $sql_query,
  203. array $aliases = array()
  204. ) {
  205. global $what;
  206. $db_alias = $db;
  207. $table_alias = $table;
  208. $this->initAlias($aliases, $db_alias, $table_alias);
  209. // Gets the data from the database
  210. $result = $GLOBALS['dbi']->query(
  211. $sql_query,
  212. DatabaseInterface::CONNECT_USER,
  213. DatabaseInterface::QUERY_UNBUFFERED
  214. );
  215. $fields_cnt = $GLOBALS['dbi']->numFields($result);
  216. $fields_meta = $GLOBALS['dbi']->getFieldsMeta($result);
  217. $field_flags = array();
  218. for ($j = 0; $j < $fields_cnt; $j++) {
  219. $field_flags[$j] = $GLOBALS['dbi']->fieldFlags($result, $j);
  220. }
  221. $GLOBALS['ods_buffer']
  222. .= '<table:table table:name="' . htmlspecialchars($table_alias) . '">';
  223. // If required, get fields name at the first line
  224. if (isset($GLOBALS[$what . '_columns'])) {
  225. $GLOBALS['ods_buffer'] .= '<table:table-row>';
  226. for ($i = 0; $i < $fields_cnt; $i++) {
  227. $col_as = $GLOBALS['dbi']->fieldName($result, $i);
  228. if (!empty($aliases[$db]['tables'][$table]['columns'][$col_as])) {
  229. $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as];
  230. }
  231. $GLOBALS['ods_buffer']
  232. .= '<table:table-cell office:value-type="string">'
  233. . '<text:p>'
  234. . htmlspecialchars(
  235. stripslashes($col_as)
  236. )
  237. . '</text:p>'
  238. . '</table:table-cell>';
  239. } // end for
  240. $GLOBALS['ods_buffer'] .= '</table:table-row>';
  241. } // end if
  242. // Format the data
  243. while ($row = $GLOBALS['dbi']->fetchRow($result)) {
  244. $GLOBALS['ods_buffer'] .= '<table:table-row>';
  245. for ($j = 0; $j < $fields_cnt; $j++) {
  246. if ($fields_meta[$j]->type === 'geometry') {
  247. // export GIS types as hex
  248. $row[$j] = '0x' . bin2hex($row[$j]);
  249. }
  250. if (!isset($row[$j]) || is_null($row[$j])) {
  251. $GLOBALS['ods_buffer']
  252. .= '<table:table-cell office:value-type="string">'
  253. . '<text:p>'
  254. . htmlspecialchars($GLOBALS[$what . '_null'])
  255. . '</text:p>'
  256. . '</table:table-cell>';
  257. } elseif (stristr($field_flags[$j], 'BINARY')
  258. && $fields_meta[$j]->blob
  259. ) {
  260. // ignore BLOB
  261. $GLOBALS['ods_buffer']
  262. .= '<table:table-cell office:value-type="string">'
  263. . '<text:p></text:p>'
  264. . '</table:table-cell>';
  265. } elseif ($fields_meta[$j]->type == "date") {
  266. $GLOBALS['ods_buffer']
  267. .= '<table:table-cell office:value-type="date"'
  268. . ' office:date-value="'
  269. . date("Y-m-d", strtotime($row[$j]))
  270. . '" table:style-name="DateCell">'
  271. . '<text:p>'
  272. . htmlspecialchars($row[$j])
  273. . '</text:p>'
  274. . '</table:table-cell>';
  275. } elseif ($fields_meta[$j]->type == "time") {
  276. $GLOBALS['ods_buffer']
  277. .= '<table:table-cell office:value-type="time"'
  278. . ' office:time-value="'
  279. . date("\P\TH\Hi\Ms\S", strtotime($row[$j]))
  280. . '" table:style-name="TimeCell">'
  281. . '<text:p>'
  282. . htmlspecialchars($row[$j])
  283. . '</text:p>'
  284. . '</table:table-cell>';
  285. } elseif ($fields_meta[$j]->type == "datetime") {
  286. $GLOBALS['ods_buffer']
  287. .= '<table:table-cell office:value-type="date"'
  288. . ' office:date-value="'
  289. . date("Y-m-d\TH:i:s", strtotime($row[$j]))
  290. . '" table:style-name="DateTimeCell">'
  291. . '<text:p>'
  292. . htmlspecialchars($row[$j])
  293. . '</text:p>'
  294. . '</table:table-cell>';
  295. } elseif (($fields_meta[$j]->numeric
  296. && $fields_meta[$j]->type != 'timestamp'
  297. && !$fields_meta[$j]->blob)
  298. || $fields_meta[$j]->type == 'real'
  299. ) {
  300. $GLOBALS['ods_buffer']
  301. .= '<table:table-cell office:value-type="float"'
  302. . ' office:value="' . $row[$j] . '" >'
  303. . '<text:p>'
  304. . htmlspecialchars($row[$j])
  305. . '</text:p>'
  306. . '</table:table-cell>';
  307. } else {
  308. $GLOBALS['ods_buffer']
  309. .= '<table:table-cell office:value-type="string">'
  310. . '<text:p>'
  311. . htmlspecialchars($row[$j])
  312. . '</text:p>'
  313. . '</table:table-cell>';
  314. }
  315. } // end for
  316. $GLOBALS['ods_buffer'] .= '</table:table-row>';
  317. } // end while
  318. $GLOBALS['dbi']->freeResult($result);
  319. $GLOBALS['ods_buffer'] .= '</table:table>';
  320. return true;
  321. }
  322. }