ExportMediawiki.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. $output = '';
  187. switch($export_mode) {
  188. case 'create_table':
  189. $columns = $GLOBALS['dbi']->getColumns($db, $table);
  190. $columns = array_values($columns);
  191. $row_cnt = count($columns);
  192. // Print structure comment
  193. $output = $this->_exportComment(
  194. "Table structure for "
  195. . PMA_Util::backquote($table)
  196. );
  197. // Begin the table construction
  198. $output .= "{| class=\"wikitable\" style=\"text-align:center;\""
  199. . $this->_exportCRLF();
  200. // Add the table name
  201. if (isset($GLOBALS['mediawiki_caption'])) {
  202. $output .= "|+'''" . $table . "'''" . $this->_exportCRLF();
  203. }
  204. // Add the table headers
  205. if (isset($GLOBALS['mediawiki_headers'])) {
  206. $output .= "|- style=\"background:#ffdead;\"" . $this->_exportCRLF();
  207. $output .= "! style=\"background:#ffffff\" | "
  208. . $this->_exportCRLF();
  209. for ($i = 0; $i < $row_cnt; ++$i) {
  210. $output .= " | " . $columns[$i]['Field'] . $this->_exportCRLF();
  211. }
  212. }
  213. // Add the table structure
  214. $output .= "|-" . $this->_exportCRLF();
  215. $output .= "! Type" . $this->_exportCRLF();
  216. for ($i = 0; $i < $row_cnt; ++$i) {
  217. $output .= " | " . $columns[$i]['Type'] . $this->_exportCRLF();
  218. }
  219. $output .= "|-" . $this->_exportCRLF();
  220. $output .= "! Null" . $this->_exportCRLF();
  221. for ($i = 0; $i < $row_cnt; ++$i) {
  222. $output .= " | " . $columns[$i]['Null'] . $this->_exportCRLF();
  223. }
  224. $output .= "|-" . $this->_exportCRLF();
  225. $output .= "! Default" . $this->_exportCRLF();
  226. for ($i = 0; $i < $row_cnt; ++$i) {
  227. $output .= " | " . $columns[$i]['Default'] . $this->_exportCRLF();
  228. }
  229. $output .= "|-" . $this->_exportCRLF();
  230. $output .= "! Extra" . $this->_exportCRLF();
  231. for ($i = 0; $i < $row_cnt; ++$i) {
  232. $output .= " | " . $columns[$i]['Extra'] . $this->_exportCRLF();
  233. }
  234. $output .= "|}" . str_repeat($this->_exportCRLF(), 2);
  235. break;
  236. } // end switch
  237. return PMA_exportOutputHandler($output);
  238. }
  239. /**
  240. * Outputs the content of a table in MediaWiki format
  241. *
  242. * @param string $db database name
  243. * @param string $table table name
  244. * @param string $crlf the end of line sequence
  245. * @param string $error_url the url to go back in case of error
  246. * @param string $sql_query SQL query for obtaining data
  247. *
  248. * @return bool Whether it succeeded
  249. */
  250. public function exportData(
  251. $db,
  252. $table,
  253. $crlf,
  254. $error_url,
  255. $sql_query
  256. ) {
  257. // Print data comment
  258. $output = $this->_exportComment(
  259. "Table data for " . PMA_Util::backquote($table)
  260. );
  261. // Begin the table construction
  262. // Use the "wikitable" class for style
  263. // Use the "sortable" class for allowing tables to be sorted by column
  264. $output .= "{| class=\"wikitable sortable\" style=\"text-align:center;\""
  265. . $this->_exportCRLF();
  266. // Add the table name
  267. if (isset($GLOBALS['mediawiki_caption'])) {
  268. $output .= "|+'''" . $table . "'''" . $this->_exportCRLF();
  269. }
  270. // Add the table headers
  271. if (isset($GLOBALS['mediawiki_headers'])) {
  272. // Get column names
  273. $column_names = $GLOBALS['dbi']->getColumnNames($db, $table);
  274. // Add column names as table headers
  275. if ( ! is_null($column_names) ) {
  276. // Use '|-' for separating rows
  277. $output .= "|-" . $this->_exportCRLF();
  278. // Use '!' for separating table headers
  279. foreach ($column_names as $column) {
  280. $output .= " ! " . $column . "" . $this->_exportCRLF();
  281. }
  282. }
  283. }
  284. // Get the table data from the database
  285. $result = $GLOBALS['dbi']->query(
  286. $sql_query, null, PMA_DatabaseInterface::QUERY_UNBUFFERED
  287. );
  288. $fields_cnt = $GLOBALS['dbi']->numFields($result);
  289. while ($row = $GLOBALS['dbi']->fetchRow($result)) {
  290. $output .= "|-" . $this->_exportCRLF();
  291. // Use '|' for separating table columns
  292. for ($i = 0; $i < $fields_cnt; ++ $i) {
  293. $output .= " | " . $row[$i] . "" . $this->_exportCRLF();
  294. }
  295. }
  296. // End table construction
  297. $output .= "|}" . str_repeat($this->_exportCRLF(), 2);
  298. return PMA_exportOutputHandler($output);
  299. }
  300. /**
  301. * Outputs comments containing info about the exported tables
  302. *
  303. * @param string $text Text of comment
  304. *
  305. * @return string The formatted comment
  306. */
  307. private function _exportComment($text = '')
  308. {
  309. // see http://www.mediawiki.org/wiki/Help:Formatting
  310. $comment = $this->_exportCRLF();
  311. $comment .= '<!--' . $this->_exportCRLF();
  312. $comment .= $text . $this->_exportCRLF();
  313. $comment .= '-->' . str_repeat($this->_exportCRLF(), 2);
  314. return $comment;
  315. }
  316. /**
  317. * Outputs CRLF
  318. *
  319. * @return string CRLF
  320. */
  321. private function _exportCRLF()
  322. {
  323. // The CRLF expected by the mediawiki format is "\n"
  324. return "\n";
  325. }
  326. }
  327. ?>