Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / plugins / export / ExportTexytext.class.php
blob3aa609c34572a47b8f5d6f05943de781503e8904
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Export to Texy! text.
6 * @package PhpMyAdmin-Export
7 * @subpackage Texy!text
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /* Get the export interface */
14 require_once 'libraries/plugins/ExportPlugin.class.php';
16 /**
17 * Handles the export for the Texy! text class
19 * @package PhpMyAdmin-Export
20 * @subpackage Texy!text
22 class ExportTexytext extends ExportPlugin
24 /**
25 * Constructor
27 public function __construct()
29 $this->setProperties();
32 /**
33 * Sets the export Texy! text properties
35 * @return void
37 protected function setProperties()
39 $props = 'libraries/properties/';
40 include_once "$props/plugins/ExportPluginProperties.class.php";
41 include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
42 include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
43 include_once "$props/options/items/RadioPropertyItem.class.php";
44 include_once "$props/options/items/BoolPropertyItem.class.php";
45 include_once "$props/options/items/TextPropertyItem.class.php";
47 $exportPluginProperties = new ExportPluginProperties();
48 $exportPluginProperties->setText('Texy! text');
49 $exportPluginProperties->setExtension('txt');
50 $exportPluginProperties->setMimeType('text/plain');
51 $exportPluginProperties->setOptionsText(__('Options'));
53 // create the root group that will be the options field for
54 // $exportPluginProperties
55 // this will be shown as "Format specific options"
56 $exportSpecificOptions = new OptionsPropertyRootGroup();
57 $exportSpecificOptions->setName("Format Specific Options");
59 // what to dump (structure/data/both) main group
60 $dumpWhat = new OptionsPropertyMainGroup();
61 $dumpWhat->setName("general_opts");
62 $dumpWhat->setText(__('Dump table'));
63 // create primary items and add them to the group
64 $leaf = new RadioPropertyItem();
65 $leaf->setName("structure_or_data");
66 $leaf->setValues(
67 array(
68 'structure' => __('structure'),
69 'data' => __('data'),
70 'structure_and_data' => __('structure and data')
73 $dumpWhat->addProperty($leaf);
74 // add the main group to the root group
75 $exportSpecificOptions->addProperty($dumpWhat);
77 // data options main group
78 $dataOptions = new OptionsPropertyMainGroup();
79 $dataOptions->setName("data");
80 $dataOptions->setText(__('Data dump options'));
81 $dataOptions->setForce('structure');
82 // create primary items and add them to the group
83 $leaf = new BoolPropertyItem();
84 $leaf->setName("columns");
85 $leaf->setText(__('Put columns names in the first row'));
86 $dataOptions->addProperty($leaf);
87 $leaf = new TextPropertyItem();
88 $leaf->setName('null');
89 $leaf->setText(__('Replace NULL with:'));
90 $dataOptions->addProperty($leaf);
91 // add the main group to the root group
92 $exportSpecificOptions->addProperty($dataOptions);
94 // set the options for the export plugin property item
95 $exportPluginProperties->setOptions($exportSpecificOptions);
96 $this->properties = $exportPluginProperties;
99 /**
100 * This method is called when any PluginManager to which the observer
101 * is attached calls PluginManager::notify()
103 * @param SplSubject $subject The PluginManager notifying the observer
104 * of an update.
106 * @return void
108 public function update (SplSubject $subject)
113 * Outputs export header
115 * @return bool Whether it succeeded
117 public function exportHeader ()
119 return true;
123 * Outputs export footer
125 * @return bool Whether it succeeded
127 public function exportFooter ()
129 return true;
133 * Outputs database header
135 * @param string $db Database name
137 * @return bool Whether it succeeded
139 public function exportDBHeader ($db)
141 return PMA_exportOutputHandler(
142 '===' . __('Database') . ' ' . $db . "\n\n"
147 * Outputs database footer
149 * @param string $db Database name
151 * @return bool Whether it succeeded
153 public function exportDBFooter ($db)
155 return true;
159 * Outputs CREATE DATABASE statement
161 * @param string $db Database name
163 * @return bool Whether it succeeded
165 public function exportDBCreate($db)
167 return true;
170 * Outputs the content of a table in NHibernate format
172 * @param string $db database name
173 * @param string $table table name
174 * @param string $crlf the end of line sequence
175 * @param string $error_url the url to go back in case of error
176 * @param string $sql_query SQL query for obtaining data
178 * @return bool Whether it succeeded
180 public function exportData($db, $table, $crlf, $error_url, $sql_query)
182 global $what;
184 if (! PMA_exportOutputHandler(
185 '== ' . __('Dumping data for table') . ' ' . $table . "\n\n"
186 )) {
187 return false;
190 // Gets the data from the database
191 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
192 $fields_cnt = PMA_DBI_num_fields($result);
194 // If required, get fields name at the first line
195 if (isset($GLOBALS[$what . '_columns'])) {
196 $text_output = "|------\n";
197 for ($i = 0; $i < $fields_cnt; $i++) {
198 $text_output .= '|'
199 . htmlspecialchars(
200 stripslashes(PMA_DBI_field_name($result, $i))
202 } // end for
203 $text_output .= "\n|------\n";
204 if (! PMA_exportOutputHandler($text_output)) {
205 return false;
207 } // end if
209 // Format the data
210 while ($row = PMA_DBI_fetch_row($result)) {
211 $text_output = '';
212 for ($j = 0; $j < $fields_cnt; $j++) {
213 if (! isset($row[$j]) || is_null($row[$j])) {
214 $value = $GLOBALS[$what . '_null'];
215 } elseif ($row[$j] == '0' || $row[$j] != '') {
216 $value = $row[$j];
217 } else {
218 $value = ' ';
220 $text_output .= '|'
221 . str_replace(
222 '|', '&#124;', htmlspecialchars($value)
224 } // end for
225 $text_output .= "\n";
226 if (! PMA_exportOutputHandler($text_output)) {
227 return false;
229 } // end while
230 PMA_DBI_free_result($result);
232 return true;
236 * Returns a stand-in CREATE definition to resolve view dependencies
238 * @param string $db the database name
239 * @param string $view the view name
240 * @param string $crlf the end of line sequence
242 * @return string resulting definition
244 function getTableDefStandIn($db, $view, $crlf)
246 $text_output = '';
249 * Get the unique keys in the table
251 $unique_keys = array();
252 $keys = PMA_DBI_get_table_indexes($db, $view);
253 foreach ($keys as $key) {
254 if ($key['Non_unique'] == 0) {
255 $unique_keys[] = $key['Column_name'];
260 * Gets fields properties
262 PMA_DBI_select_db($db);
265 * Displays the table structure
268 $text_output .= "|------\n"
269 . '|' . __('Column')
270 . '|' . __('Type')
271 . '|' . __('Null')
272 . '|' . __('Default')
273 . "\n|------\n";
275 $columns = PMA_DBI_get_columns($db, $view);
276 foreach ($columns as $column) {
277 $text_output .= $this->formatOneColumnDefinition($column, $unique_keys);
278 $text_output .= "\n";
279 } // end foreach
281 return $text_output;
285 * Returns $table's CREATE definition
287 * @param string $db the database name
288 * @param string $table the table name
289 * @param string $crlf the end of line sequence
290 * @param string $error_url the url to go back in case of error
291 * @param bool $do_relation whether to include relation comments
292 * @param bool $do_comments whether to include the pmadb-style column
293 * comments as comments in the structure;
294 * this is deprecated but the parameter is
295 * left here because export.php calls
296 * $this->exportStructure() also for other
297 * export types which use this parameter
298 * @param bool $do_mime whether to include mime comments
299 * @param bool $show_dates whether to include creation/update/check dates
300 * @param bool $add_semicolon whether to add semicolon and end-of-line
301 * at the end
302 * @param bool $view whether we're handling a view
304 * @return string resulting schema
306 function getTableDef(
307 $db,
308 $table,
309 $crlf,
310 $error_url,
311 $do_relation,
312 $do_comments,
313 $do_mime,
314 $show_dates = false,
315 $add_semicolon = true,
316 $view = false
318 global $cfgRelation;
320 $text_output = '';
323 * Get the unique keys in the table
325 $unique_keys = array();
326 $keys = PMA_DBI_get_table_indexes($db, $table);
327 foreach ($keys as $key) {
328 if ($key['Non_unique'] == 0) {
329 $unique_keys[] = $key['Column_name'];
334 * Gets fields properties
336 PMA_DBI_select_db($db);
338 // Check if we can use Relations
339 if ($do_relation && ! empty($cfgRelation['relation'])) {
340 // Find which tables are related with the current one and write it in
341 // an array
342 $res_rel = PMA_getForeigners($db, $table);
344 if ($res_rel && count($res_rel) > 0) {
345 $have_rel = true;
346 } else {
347 $have_rel = false;
349 } else {
350 $have_rel = false;
351 } // end if
354 * Displays the table structure
357 $columns_cnt = 4;
358 if ($do_relation && $have_rel) {
359 $columns_cnt++;
361 if ($do_comments && $cfgRelation['commwork']) {
362 $columns_cnt++;
364 if ($do_mime && $cfgRelation['mimework']) {
365 $columns_cnt++;
368 $text_output .= "|------\n";
369 $text_output .= '|' . __('Column');
370 $text_output .= '|' . __('Type');
371 $text_output .= '|' . __('Null');
372 $text_output .= '|' . __('Default');
373 if ($do_relation && $have_rel) {
374 $text_output .= '|' . __('Links to');
376 if ($do_comments) {
377 $text_output .= '|' . __('Comments');
378 $comments = PMA_getComments($db, $table);
380 if ($do_mime && $cfgRelation['mimework']) {
381 $text_output .= '|' . htmlspecialchars('MIME');
382 $mime_map = PMA_getMIME($db, $table, true);
384 $text_output .= "\n|------\n";
386 $columns = PMA_DBI_get_columns($db, $table);
387 foreach ($columns as $column) {
388 $text_output .= $this->formatOneColumnDefinition($column, $unique_keys);
389 $field_name = $column['Field'];
391 if ($do_relation && $have_rel) {
392 $text_output .= '|'
393 . (isset($res_rel[$field_name])
394 ? htmlspecialchars(
395 $res_rel[$field_name]['foreign_table']
396 . ' (' . $res_rel[$field_name]['foreign_field'] . ')'
398 : '');
400 if ($do_comments && $cfgRelation['commwork']) {
401 $text_output .= '|'
402 . (isset($comments[$field_name])
403 ? htmlspecialchars($comments[$field_name])
404 : '');
406 if ($do_mime && $cfgRelation['mimework']) {
407 $text_output .= '|'
408 . (isset($mime_map[$field_name])
409 ? htmlspecialchars(
410 str_replace('_', '/', $mime_map[$field_name]['mimetype'])
412 : '');
415 $text_output .= "\n";
416 } // end foreach
418 return $text_output;
419 } // end of the '$this->getTableDef()' function
422 * Outputs triggers
424 * @param string $db database name
425 * @param string $table table name
427 * @return string Formatted triggers list
429 function getTriggers($db, $table)
431 $text_output .= "|------\n";
432 $text_output .= '|' . __('Column');
433 $dump = "|------\n";
434 $dump .= '|' . __('Name');
435 $dump .= '|' . __('Time');
436 $dump .= '|' . __('Event');
437 $dump .= '|' . __('Definition');
438 $dump .= "\n|------\n";
440 $triggers = PMA_DBI_get_triggers($db, $table);
442 foreach ($triggers as $trigger) {
443 $dump .= '|' . $trigger['name'];
444 $dump .= '|' . $trigger['action_timing'];
445 $dump .= '|' . $trigger['event_manipulation'];
446 $dump .= '|' .
447 str_replace(
448 '|',
449 '&#124;',
450 htmlspecialchars($trigger['definition'])
452 $dump .= "\n";
455 return $dump;
459 * Outputs table's structure
461 * @param string $db database name
462 * @param string $table table name
463 * @param string $crlf the end of line sequence
464 * @param string $error_url the url to go back in case of error
465 * @param string $export_mode 'create_table', 'triggers', 'create_view',
466 * 'stand_in'
467 * @param string $export_type 'server', 'database', 'table'
468 * @param bool $do_relation whether to include relation comments
469 * @param bool $do_comments whether to include the pmadb-style column
470 * comments as comments in the structure;
471 * this is deprecated but the parameter is
472 * left here because export.php calls
473 * $this->exportStructure() also for other
474 * export types which use this parameter
475 * @param bool $do_mime whether to include mime comments
476 * @param bool $dates whether to include creation/update/check dates
478 * @return bool Whether it succeeded
480 function exportStructure(
481 $db,
482 $table,
483 $crlf,
484 $error_url,
485 $export_mode,
486 $export_type,
487 $do_relation = false,
488 $do_comments = false,
489 $do_mime = false,
490 $dates = false
492 $dump = '';
494 switch($export_mode) {
495 case 'create_table':
496 $dump .= '== ' . __('Table structure for table') . ' ' .$table . "\n\n";
497 $dump .= $this->getTableDef(
498 $db, $table, $crlf, $error_url, $do_relation, $do_comments,
499 $do_mime, $dates
501 break;
502 case 'triggers':
503 $dump = '';
504 $triggers = PMA_DBI_get_triggers($db, $table);
505 if ($triggers) {
506 $dump .= '== ' . __('Triggers') . ' ' .$table . "\n\n";
507 $dump .= $this->getTriggers($db, $table);
509 break;
510 case 'create_view':
511 $dump .= '== ' . __('Structure for view') . ' ' .$table . "\n\n";
512 $dump .= $this->getTableDef(
513 $db, $table, $crlf, $error_url, $do_relation, $do_comments,
514 $do_mime, $dates, true, true
516 break;
517 case 'stand_in':
518 $dump .= '== ' . __('Stand-in structure for view')
519 . ' ' .$table . "\n\n";
520 // export a stand-in definition to resolve view dependencies
521 $dump .= $this->getTableDefStandIn($db, $table, $crlf);
522 } // end switch
524 return PMA_exportOutputHandler($dump);
528 * Formats the definition for one column
530 * @param array $column info about this column
531 * @param array $unique_keys unique keys for this table
533 * @return string Formatted column definition
535 function formatOneColumnDefinition(
536 $column, $unique_keys
538 $extracted_columnspec
539 = PMA_Util::extractColumnSpec($column['Type']);
540 $type = $extracted_columnspec['print_type'];
541 if (empty($type)) {
542 $type = '&nbsp;';
545 if (! isset($column['Default'])) {
546 if ($column['Null'] != 'NO') {
547 $column['Default'] = 'NULL';
551 $fmt_pre = '';
552 $fmt_post = '';
553 if (in_array($column['Field'], $unique_keys)) {
554 $fmt_pre = '**' . $fmt_pre;
555 $fmt_post = $fmt_post . '**';
557 if ($column['Key']=='PRI') {
558 $fmt_pre = '//' . $fmt_pre;
559 $fmt_post = $fmt_post . '//';
561 $definition = '|'
562 . $fmt_pre . htmlspecialchars($column['Field']) . $fmt_post;
563 $definition .= '|' . htmlspecialchars($type);
564 $definition .= '|'
565 . (($column['Null'] == '' || $column['Null'] == 'NO')
566 ? __('No') : __('Yes'));
567 $definition .= '|'
568 . htmlspecialchars(
569 isset($column['Default']) ? $column['Default'] : ''
571 return $definition;