PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/plugins/export/ExportMediawiki.class.php

http://github.com/phpmyadmin/phpmyadmin
PHP | 355 lines | 186 code | 38 blank | 131 comment | 15 complexity | 1965eb8d721bb884c87a0f9d52a6d977 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-3.0
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Set of functions used to build MediaWiki dumps of tables
  5. *
  6. * @package PhpMyAdmin-Export
  7. * @subpackage MediaWiki
  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 MediaWiki class
  16. *
  17. * @package PhpMyAdmin-Export
  18. */
  19. class ExportMediawiki extends ExportPlugin
  20. {
  21. /**
  22. * Constructor
  23. */
  24. public function __construct()
  25. {
  26. $this->setProperties();
  27. }
  28. /**
  29. * Sets the export MediaWiki properties
  30. *
  31. * @return void
  32. */
  33. protected function setProperties()
  34. {
  35. $this->properties = array(
  36. 'text' => __('MediaWiki Table'),
  37. 'extension' => 'mediawiki',
  38. 'mime_type' => 'text/plain',
  39. 'options' => array(),
  40. 'options_text' => __('Options')
  41. );
  42. // general options
  43. $this->properties['options'][] = array(
  44. 'type' => 'begin_group',
  45. 'name' => 'general_opts'
  46. );
  47. // what to dump (structure/data/both)
  48. $this->properties['options'][] = array(
  49. 'type' => 'begin_subgroup',
  50. 'subgroup_header' => array(
  51. 'type' => 'message_only',
  52. 'text' => __('Dump table')
  53. )
  54. );
  55. $this->properties['options'][] = array(
  56. 'type' => 'radio',
  57. 'name' => 'structure_or_data',
  58. 'values' => array(
  59. 'structure' => __('structure'),
  60. 'data' => __('data'),
  61. 'structure_and_data' => __('structure and data')
  62. )
  63. );
  64. $this->properties['options'][] = array(
  65. 'type' => 'end_subgroup'
  66. );
  67. // export table name
  68. $this->properties['options'][] = array(
  69. 'type' => 'bool',
  70. 'name' => 'caption',
  71. 'text' => __('Export table names')
  72. );
  73. // export table headers
  74. $this->properties['options'][] = array(
  75. 'type' => 'bool',
  76. 'name' => 'headers',
  77. 'text' => __('Export table headers')
  78. );
  79. // end general options
  80. $this->properties['options'][] = array(
  81. 'type' => 'end_group'
  82. );
  83. }
  84. /**
  85. * This method is called when any PluginManager to which the observer
  86. * is attached calls PluginManager::notify()
  87. *
  88. * @param SplSubject $subject The PluginManager notifying the observer
  89. * of an update.
  90. *
  91. * @return void
  92. */
  93. public function update (SplSubject $subject)
  94. {
  95. }
  96. /**
  97. * Outputs export header
  98. *
  99. * @return bool Whether it succeeded
  100. */
  101. public function exportHeader ()
  102. {
  103. return true;
  104. }
  105. /**
  106. * Outputs export footer
  107. *
  108. * @return bool Whether it succeeded
  109. */
  110. public function exportFooter ()
  111. {
  112. return true;
  113. }
  114. /**
  115. * Outputs database header
  116. *
  117. * @param string $db Database name
  118. *
  119. * @return bool Whether it succeeded
  120. */
  121. public function exportDBHeader ($db)
  122. {
  123. return true;
  124. }
  125. /**
  126. * Outputs database footer
  127. *
  128. * @param string $db Database name
  129. *
  130. * @return bool Whether it succeeded
  131. */
  132. public function exportDBFooter ($db)
  133. {
  134. return true;
  135. }
  136. /**
  137. * Outputs CREATE DATABASE statement
  138. *
  139. * @param string $db Database name
  140. *
  141. * @return bool Whether it succeeded
  142. */
  143. public function exportDBCreate($db)
  144. {
  145. return true;
  146. }
  147. /**
  148. * Outputs table's structure
  149. *
  150. * @param string $db database name
  151. * @param string $table table name
  152. * @param string $crlf the end of line sequence
  153. * @param string $error_url the url to go back in case of error
  154. * @param string $export_mode 'create_table','triggers','create_view',
  155. * 'stand_in'
  156. * @param string $export_type 'server', 'database', 'table'
  157. * @param bool $do_relation whether to include relation comments
  158. * @param bool $do_comments whether to include the pmadb-style column
  159. * comments as comments in the structure; this is
  160. * deprecated but the parameter is left here
  161. * because export.php calls exportStructure()
  162. * also for other export types which use this
  163. * parameter
  164. * @param bool $do_mime whether to include mime comments
  165. * @param bool $dates whether to include creation/update/check dates
  166. *
  167. * @return bool Whether it succeeded
  168. */
  169. public function exportStructure(
  170. $db,
  171. $table,
  172. $crlf,
  173. $error_url,
  174. $export_mode,
  175. $export_type,
  176. $do_relation = false,
  177. $do_comments = false,
  178. $do_mime = false,
  179. $dates = false
  180. ) {
  181. switch($export_mode) {
  182. case 'create_table':
  183. $columns = PMA_DBI_get_columns($db, $table);
  184. $columns = array_values($columns);
  185. $row_cnt = count($columns);
  186. // Print structure comment
  187. $output = $this->_exportComment(
  188. "Table structure for "
  189. . PMA_backquote($table)
  190. );
  191. // Begin the table construction
  192. $output .= "{| class=\"wikitable\" style=\"text-align:center;\""
  193. . $this->_exportCRLF();
  194. // Add the table name
  195. if ($GLOBALS['mediawiki_caption']) {
  196. $output .= "|+'''" . $table . "'''" . $this->_exportCRLF();
  197. }
  198. // Add the table headers
  199. if ($GLOBALS['mediawiki_headers']) {
  200. $output .= "|- style=\"background:#ffdead;\"" . $this->_exportCRLF();
  201. $output .= "! style=\"background:#ffffff\" | "
  202. . $this->_exportCRLF();
  203. for ($i = 0; $i < $row_cnt; ++$i) {
  204. $output .= " | " . $columns[$i]['Field']. $this->_exportCRLF();
  205. }
  206. }
  207. // Add the table structure
  208. $output .= "|-" . $this->_exportCRLF();
  209. $output .= "! Type" . $this->_exportCRLF();
  210. for ($i = 0; $i < $row_cnt; ++$i) {
  211. $output .= " | " . $columns[$i]['Type'] . $this->_exportCRLF();
  212. }
  213. $output .= "|-" . $this->_exportCRLF();
  214. $output .= "! Null" . $this->_exportCRLF();
  215. for ($i = 0; $i < $row_cnt; ++$i) {
  216. $output .= " | " . $columns[$i]['Null'] . $this->_exportCRLF();
  217. }
  218. $output .= "|-" . $this->_exportCRLF();
  219. $output .= "! Default" . $this->_exportCRLF();
  220. for ($i = 0; $i < $row_cnt; ++$i) {
  221. $output .= " | " . $columns[$i]['Default'] . $this->_exportCRLF();
  222. }
  223. $output .= "|-" . $this->_exportCRLF();
  224. $output .= "! Extra" . $this->_exportCRLF();
  225. for ($i = 0; $i < $row_cnt; ++$i) {
  226. $output .= " | " . $columns[$i]['Extra'] . $this->_exportCRLF();
  227. }
  228. $output .= "|}" . str_repeat($this->_exportCRLF(), 2);
  229. break;
  230. } // end switch
  231. return PMA_exportOutputHandler($output);
  232. }
  233. /**
  234. * Outputs the content of a table in MediaWiki format
  235. *
  236. * @param string $db database name
  237. * @param string $table table name
  238. * @param string $crlf the end of line sequence
  239. * @param string $error_url the url to go back in case of error
  240. * @param string $sql_query SQL query for obtaining data
  241. *
  242. * @return bool Whether it succeeded
  243. */
  244. public function exportData(
  245. $db,
  246. $table,
  247. $crlf,
  248. $error_url,
  249. $sql_query
  250. ) {
  251. // Print data comment
  252. $output = $this->_exportComment("Table data for ". PMA_backquote($table));
  253. // Begin the table construction
  254. // Use the "wikitable" class for style
  255. // Use the "sortable" class for allowing tables to be sorted by column
  256. $output .= "{| class=\"wikitable sortable\" style=\"text-align:center;\""
  257. . $this->_exportCRLF();
  258. // Add the table name
  259. if ($GLOBALS['mediawiki_caption']) {
  260. $output .= "|+'''" . $table . "'''" . $this->_exportCRLF();
  261. }
  262. // Add the table headers
  263. if ($GLOBALS['mediawiki_headers']) {
  264. // Get column names
  265. $column_names = PMA_DBI_get_column_names($db, $table);
  266. // Add column names as table headers
  267. if ( ! is_null($column_names) ) {
  268. // Use '|-' for separating rows
  269. $output .= "|-" . $this->_exportCRLF();
  270. // Use '!' for separating table headers
  271. foreach ($column_names as $column) {
  272. $output .= " ! " . $column . "" . $this->_exportCRLF();
  273. }
  274. }
  275. }
  276. // Get the table data from the database
  277. $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
  278. $fields_cnt = PMA_DBI_num_fields($result);
  279. while ($row = PMA_DBI_fetch_row($result)) {
  280. $output .= "|-" . $this->_exportCRLF();
  281. // Use '|' for separating table columns
  282. for ($i = 0; $i < $fields_cnt; ++ $i) {
  283. $output .= " | " . $row[$i] . "" . $this->_exportCRLF();
  284. }
  285. }
  286. // End table construction
  287. $output .= "|}" . str_repeat($this->_exportCRLF(), 2);
  288. return PMA_exportOutputHandler($output);
  289. }
  290. /**
  291. * Outputs comments containing info about the exported tables
  292. *
  293. * @param string $text Text of comment
  294. *
  295. * @return string The formatted comment
  296. */
  297. private function _exportComment($text = '')
  298. {
  299. // see http://www.mediawiki.org/wiki/Help:Formatting
  300. $comment = $this->_exportCRLF();
  301. $comment .= '<!--' . $this->_exportCRLF();
  302. $comment .= $text . $this->_exportCRLF();
  303. $comment .= '-->' . str_repeat($this->_exportCRLF(), 2);
  304. return $comment;
  305. }
  306. /**
  307. * Outputs CRLF
  308. *
  309. * @return string CRLF
  310. */
  311. private function _exportCRLF()
  312. {
  313. // The CRLF expected by the mediawiki format is "\n"
  314. return "\n";
  315. }
  316. }
  317. ?>