ExportMediawiki.class.php 11 KB

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