ExportMediawiki.php 12 KB

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