ExportMediawiki.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. declare(strict_types=1);
  10. namespace PhpMyAdmin\Plugins\Export;
  11. use PhpMyAdmin\DatabaseInterface;
  12. use PhpMyAdmin\Export;
  13. use PhpMyAdmin\Plugins\ExportPlugin;
  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\Properties\Plugins\ExportPluginProperties;
  20. use PhpMyAdmin\Util;
  21. /**
  22. * Handles the export for the MediaWiki class
  23. *
  24. * @package PhpMyAdmin-Export
  25. * @subpackage MediaWiki
  26. */
  27. class ExportMediawiki extends ExportPlugin
  28. {
  29. /**
  30. * Constructor
  31. */
  32. public function __construct()
  33. {
  34. parent::__construct();
  35. $this->setProperties();
  36. }
  37. /**
  38. * Sets the export MediaWiki properties
  39. *
  40. * @return void
  41. */
  42. protected function setProperties()
  43. {
  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. "Format Specific Options"
  54. );
  55. // general options main group
  56. $generalOptions = new OptionsPropertyMainGroup(
  57. "general_opts",
  58. __('Dump table')
  59. );
  60. // what to dump (structure/data/both)
  61. $subgroup = new OptionsPropertySubgroup(
  62. "dump_table",
  63. __("Dump table")
  64. );
  65. $leaf = new RadioPropertyItem('structure_or_data');
  66. $leaf->setValues(
  67. [
  68. 'structure' => __('structure'),
  69. 'data' => __('data'),
  70. 'structure_and_data' => __('structure and data'),
  71. ]
  72. );
  73. $subgroup->setSubgroupHeader($leaf);
  74. $generalOptions->addProperty($subgroup);
  75. // export table name
  76. $leaf = new BoolPropertyItem(
  77. "caption",
  78. __('Export table names')
  79. );
  80. $generalOptions->addProperty($leaf);
  81. // export table headers
  82. $leaf = new BoolPropertyItem(
  83. "headers",
  84. __('Export table headers')
  85. );
  86. $generalOptions->addProperty($leaf);
  87. //add the main group to the root group
  88. $exportSpecificOptions->addProperty($generalOptions);
  89. // set the options for the export plugin property item
  90. $exportPluginProperties->setOptions($exportSpecificOptions);
  91. $this->properties = $exportPluginProperties;
  92. }
  93. /**
  94. * Outputs export header
  95. *
  96. * @return bool Whether it succeeded
  97. */
  98. public function exportHeader()
  99. {
  100. return true;
  101. }
  102. /**
  103. * Outputs export footer
  104. *
  105. * @return bool Whether it succeeded
  106. */
  107. public function exportFooter()
  108. {
  109. return true;
  110. }
  111. /**
  112. * Outputs database header
  113. *
  114. * @param string $db Database name
  115. * @param string $db_alias Alias of db
  116. *
  117. * @return bool Whether it succeeded
  118. */
  119. public function exportDBHeader($db, $db_alias = '')
  120. {
  121. return true;
  122. }
  123. /**
  124. * Outputs database footer
  125. *
  126. * @param string $db Database name
  127. *
  128. * @return bool Whether it succeeded
  129. */
  130. public function exportDBFooter($db)
  131. {
  132. return true;
  133. }
  134. /**
  135. * Outputs CREATE DATABASE statement
  136. *
  137. * @param string $db Database name
  138. * @param string $export_type 'server', 'database', 'table'
  139. * @param string $db_alias Aliases of db
  140. *
  141. * @return bool Whether it succeeded
  142. */
  143. public function exportDBCreate($db, $export_type, $db_alias = '')
  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. * @param array $aliases Aliases of db/table/columns
  167. *
  168. * @return bool Whether it succeeded
  169. */
  170. public function exportStructure(
  171. $db,
  172. $table,
  173. $crlf,
  174. $error_url,
  175. $export_mode,
  176. $export_type,
  177. $do_relation = false,
  178. $do_comments = false,
  179. $do_mime = false,
  180. $dates = false,
  181. array $aliases = []
  182. ) {
  183. $db_alias = $db;
  184. $table_alias = $table;
  185. $this->initAlias($aliases, $db_alias, $table_alias);
  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. . Util::backquote($table_alias)
  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_alias . "'''" . $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. $col_as = $columns[$i]['Field'];
  211. if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])
  212. ) {
  213. $col_as
  214. = $aliases[$db]['tables'][$table]['columns'][$col_as];
  215. }
  216. $output .= " | " . $col_as . $this->_exportCRLF();
  217. }
  218. }
  219. // Add the table structure
  220. $output .= "|-" . $this->_exportCRLF();
  221. $output .= "! Type" . $this->_exportCRLF();
  222. for ($i = 0; $i < $row_cnt; ++$i) {
  223. $output .= " | " . $columns[$i]['Type'] . $this->_exportCRLF();
  224. }
  225. $output .= "|-" . $this->_exportCRLF();
  226. $output .= "! Null" . $this->_exportCRLF();
  227. for ($i = 0; $i < $row_cnt; ++$i) {
  228. $output .= " | " . $columns[$i]['Null'] . $this->_exportCRLF();
  229. }
  230. $output .= "|-" . $this->_exportCRLF();
  231. $output .= "! Default" . $this->_exportCRLF();
  232. for ($i = 0; $i < $row_cnt; ++$i) {
  233. $output .= " | " . $columns[$i]['Default'] . $this->_exportCRLF();
  234. }
  235. $output .= "|-" . $this->_exportCRLF();
  236. $output .= "! Extra" . $this->_exportCRLF();
  237. for ($i = 0; $i < $row_cnt; ++$i) {
  238. $output .= " | " . $columns[$i]['Extra'] . $this->_exportCRLF();
  239. }
  240. $output .= "|}" . str_repeat($this->_exportCRLF(), 2);
  241. break;
  242. } // end switch
  243. return $this->export->outputHandler($output);
  244. }
  245. /**
  246. * Outputs the content of a table in MediaWiki format
  247. *
  248. * @param string $db database name
  249. * @param string $table table name
  250. * @param string $crlf the end of line sequence
  251. * @param string $error_url the url to go back in case of error
  252. * @param string $sql_query SQL query for obtaining data
  253. * @param array $aliases Aliases of db/table/columns
  254. *
  255. * @return bool Whether it succeeded
  256. */
  257. public function exportData(
  258. $db,
  259. $table,
  260. $crlf,
  261. $error_url,
  262. $sql_query,
  263. array $aliases = []
  264. ) {
  265. $db_alias = $db;
  266. $table_alias = $table;
  267. $this->initAlias($aliases, $db_alias, $table_alias);
  268. // Print data comment
  269. $output = $this->_exportComment(
  270. "Table data for " . Util::backquote($table_alias)
  271. );
  272. // Begin the table construction
  273. // Use the "wikitable" class for style
  274. // Use the "sortable" class for allowing tables to be sorted by column
  275. $output .= "{| class=\"wikitable sortable\" style=\"text-align:center;\""
  276. . $this->_exportCRLF();
  277. // Add the table name
  278. if (isset($GLOBALS['mediawiki_caption'])) {
  279. $output .= "|+'''" . $table_alias . "'''" . $this->_exportCRLF();
  280. }
  281. // Add the table headers
  282. if (isset($GLOBALS['mediawiki_headers'])) {
  283. // Get column names
  284. $column_names = $GLOBALS['dbi']->getColumnNames($db, $table);
  285. // Add column names as table headers
  286. if ($column_names !== null) {
  287. // Use '|-' for separating rows
  288. $output .= "|-" . $this->_exportCRLF();
  289. // Use '!' for separating table headers
  290. foreach ($column_names as $column) {
  291. if (! empty($aliases[$db]['tables'][$table]['columns'][$column])
  292. ) {
  293. $column
  294. = $aliases[$db]['tables'][$table]['columns'][$column];
  295. }
  296. $output .= " ! " . $column . "" . $this->_exportCRLF();
  297. }
  298. }
  299. }
  300. // Get the table data from the database
  301. $result = $GLOBALS['dbi']->query(
  302. $sql_query,
  303. DatabaseInterface::CONNECT_USER,
  304. DatabaseInterface::QUERY_UNBUFFERED
  305. );
  306. $fields_cnt = $GLOBALS['dbi']->numFields($result);
  307. while ($row = $GLOBALS['dbi']->fetchRow($result)) {
  308. $output .= "|-" . $this->_exportCRLF();
  309. // Use '|' for separating table columns
  310. for ($i = 0; $i < $fields_cnt; ++$i) {
  311. $output .= " | " . $row[$i] . "" . $this->_exportCRLF();
  312. }
  313. }
  314. // End table construction
  315. $output .= "|}" . str_repeat($this->_exportCRLF(), 2);
  316. return $this->export->outputHandler($output);
  317. }
  318. /**
  319. * Outputs comments containing info about the exported tables
  320. *
  321. * @param string $text Text of comment
  322. *
  323. * @return string The formatted comment
  324. */
  325. private function _exportComment($text = '')
  326. {
  327. // see https://www.mediawiki.org/wiki/Help:Formatting
  328. $comment = $this->_exportCRLF();
  329. $comment .= '<!--' . $this->_exportCRLF();
  330. $comment .= htmlspecialchars($text) . $this->_exportCRLF();
  331. $comment .= '-->' . str_repeat($this->_exportCRLF(), 2);
  332. return $comment;
  333. }
  334. /**
  335. * Outputs CRLF
  336. *
  337. * @return string CRLF
  338. */
  339. private function _exportCRLF()
  340. {
  341. // The CRLF expected by the mediawiki format is "\n"
  342. return "\n";
  343. }
  344. }