ExportMediawiki.php 13 KB

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