ExportPlugin.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. /**
  3. * Abstract class for the export plugins
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins;
  7. use PhpMyAdmin\Export;
  8. use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
  9. use PhpMyAdmin\Relation;
  10. use PhpMyAdmin\Transformations;
  11. use function stripos;
  12. /**
  13. * Provides a common interface that will have to be implemented by all of the
  14. * export plugins. Some of the plugins will also implement other public
  15. * methods, but those are not declared here, because they are not implemented
  16. * by all export plugins.
  17. */
  18. abstract class ExportPlugin
  19. {
  20. /**
  21. * PhpMyAdmin\Properties\Plugins\ExportPluginProperties object containing
  22. * the specific export plugin type properties
  23. *
  24. * @var ExportPluginProperties
  25. */
  26. protected $properties;
  27. /** @var Relation */
  28. public $relation;
  29. /** @var Export */
  30. protected $export;
  31. /** @var Transformations */
  32. protected $transformations;
  33. public function __construct()
  34. {
  35. global $dbi;
  36. $this->relation = new Relation($dbi);
  37. $this->export = new Export($dbi);
  38. $this->transformations = new Transformations();
  39. }
  40. /**
  41. * Outputs export header
  42. *
  43. * @return bool Whether it succeeded
  44. */
  45. abstract public function exportHeader();
  46. /**
  47. * Outputs export footer
  48. *
  49. * @return bool Whether it succeeded
  50. */
  51. abstract public function exportFooter();
  52. /**
  53. * Outputs database header
  54. *
  55. * @param string $db Database name
  56. * @param string $db_alias Aliases of db
  57. *
  58. * @return bool Whether it succeeded
  59. */
  60. abstract public function exportDBHeader($db, $db_alias = '');
  61. /**
  62. * Outputs database footer
  63. *
  64. * @param string $db Database name
  65. *
  66. * @return bool Whether it succeeded
  67. */
  68. abstract public function exportDBFooter($db);
  69. /**
  70. * Outputs CREATE DATABASE statement
  71. *
  72. * @param string $db Database name
  73. * @param string $export_type 'server', 'database', 'table'
  74. * @param string $db_alias Aliases of db
  75. *
  76. * @return bool Whether it succeeded
  77. */
  78. abstract public function exportDBCreate($db, $export_type, $db_alias = '');
  79. /**
  80. * Outputs the content of a table
  81. *
  82. * @param string $db database name
  83. * @param string $table table name
  84. * @param string $crlf the end of line sequence
  85. * @param string $error_url the url to go back in case of error
  86. * @param string $sql_query SQL query for obtaining data
  87. * @param array $aliases Aliases of db/table/columns
  88. *
  89. * @return bool Whether it succeeded
  90. */
  91. abstract public function exportData(
  92. $db,
  93. $table,
  94. $crlf,
  95. $error_url,
  96. $sql_query,
  97. array $aliases = []
  98. );
  99. /**
  100. * The following methods are used in /export or in /database/operations,
  101. * but they are not implemented by all export plugins
  102. */
  103. /**
  104. * Exports routines (procedures and functions)
  105. *
  106. * @param string $db Database
  107. * @param array $aliases Aliases of db/table/columns
  108. *
  109. * @return bool Whether it succeeded
  110. */
  111. public function exportRoutines($db, array $aliases = [])
  112. {
  113. return true;
  114. }
  115. /**
  116. * Exports events
  117. *
  118. * @param string $db Database
  119. *
  120. * @return bool Whether it succeeded
  121. */
  122. public function exportEvents($db)
  123. {
  124. return true;
  125. }
  126. /**
  127. * Outputs for raw query
  128. *
  129. * @param string $err_url the url to go back in case of error
  130. * @param string $sql_query the rawquery to output
  131. * @param string $crlf the seperator for a file
  132. *
  133. * @return bool if succeeded
  134. */
  135. public function exportRawQuery(
  136. string $err_url,
  137. string $sql_query,
  138. string $crlf
  139. ): bool {
  140. return false;
  141. }
  142. /**
  143. * Outputs table's structure
  144. *
  145. * @param string $db database name
  146. * @param string $table table name
  147. * @param string $crlf the end of line sequence
  148. * @param string $error_url the url to go back in case of error
  149. * @param string $export_mode 'create_table','triggers','create_view',
  150. * 'stand_in'
  151. * @param string $export_type 'server', 'database', 'table'
  152. * @param bool $relation whether to include relation comments
  153. * @param bool $comments whether to include the pmadb-style column comments
  154. * as comments in the structure; this is deprecated
  155. * but the parameter is left here because /export
  156. * calls exportStructure() also for other export
  157. * types which use this parameter
  158. * @param bool $mime whether to include mime comments
  159. * @param bool $dates whether to include creation/update/check dates
  160. * @param array $aliases Aliases of db/table/columns
  161. *
  162. * @return bool Whether it succeeded
  163. */
  164. public function exportStructure(
  165. $db,
  166. $table,
  167. $crlf,
  168. $error_url,
  169. $export_mode,
  170. $export_type,
  171. $relation = false,
  172. $comments = false,
  173. $mime = false,
  174. $dates = false,
  175. array $aliases = []
  176. ) {
  177. return true;
  178. }
  179. /**
  180. * Exports metadata from Configuration Storage
  181. *
  182. * @param string $db database being exported
  183. * @param string|array $tables table(s) being exported
  184. * @param array $metadataTypes types of metadata to export
  185. *
  186. * @return bool Whether it succeeded
  187. */
  188. public function exportMetadata(
  189. $db,
  190. $tables,
  191. array $metadataTypes
  192. ) {
  193. return true;
  194. }
  195. /**
  196. * Returns a stand-in CREATE definition to resolve view dependencies
  197. *
  198. * @param string $db the database name
  199. * @param string $view the view name
  200. * @param string $crlf the end of line sequence
  201. * @param array $aliases Aliases of db/table/columns
  202. *
  203. * @return string resulting definition
  204. */
  205. public function getTableDefStandIn($db, $view, $crlf, $aliases = [])
  206. {
  207. return '';
  208. }
  209. /**
  210. * Outputs triggers
  211. *
  212. * @param string $db database name
  213. * @param string $table table name
  214. *
  215. * @return string Formatted triggers list
  216. */
  217. protected function getTriggers($db, $table)
  218. {
  219. return '';
  220. }
  221. /**
  222. * Initialize the specific variables for each export plugin
  223. *
  224. * @return void
  225. */
  226. protected function initSpecificVariables()
  227. {
  228. }
  229. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  230. /**
  231. * Gets the export specific format plugin properties
  232. *
  233. * @return ExportPluginProperties
  234. */
  235. public function getProperties()
  236. {
  237. return $this->properties;
  238. }
  239. /**
  240. * Sets the export plugins properties and is implemented by each export
  241. * plugin
  242. *
  243. * @return void
  244. */
  245. abstract protected function setProperties();
  246. /**
  247. * The following methods are implemented here so that they
  248. * can be used by all export plugin without overriding it.
  249. * Note: If you are creating a export plugin then don't include
  250. * below methods unless you want to override them.
  251. */
  252. /**
  253. * Initialize aliases
  254. *
  255. * @param array $aliases Alias information for db/table/column
  256. * @param string $db the database
  257. * @param string $table the table
  258. *
  259. * @return void
  260. */
  261. public function initAlias($aliases, &$db, &$table = null)
  262. {
  263. if (! empty($aliases[$db]['tables'][$table]['alias'])) {
  264. $table = $aliases[$db]['tables'][$table]['alias'];
  265. }
  266. if (empty($aliases[$db]['alias'])) {
  267. return;
  268. }
  269. $db = $aliases[$db]['alias'];
  270. }
  271. /**
  272. * Search for alias of a identifier.
  273. *
  274. * @param array $aliases Alias information for db/table/column
  275. * @param string $id the identifier to be searched
  276. * @param string $type db/tbl/col or any combination of them
  277. * representing what to be searched
  278. * @param string $db the database in which search is to be done
  279. * @param string $tbl the table in which search is to be done
  280. *
  281. * @return string alias of the identifier if found or ''
  282. */
  283. public function getAlias(array $aliases, $id, $type = 'dbtblcol', $db = '', $tbl = '')
  284. {
  285. if (! empty($db) && isset($aliases[$db])) {
  286. $aliases = [
  287. $db => $aliases[$db],
  288. ];
  289. }
  290. // search each database
  291. foreach ($aliases as $db_key => $db) {
  292. // check if id is database and has alias
  293. if (stripos($type, 'db') !== false
  294. && $db_key === $id
  295. && ! empty($db['alias'])
  296. ) {
  297. return $db['alias'];
  298. }
  299. if (empty($db['tables'])) {
  300. continue;
  301. }
  302. if (! empty($tbl) && isset($db['tables'][$tbl])) {
  303. $db['tables'] = [
  304. $tbl => $db['tables'][$tbl],
  305. ];
  306. }
  307. // search each of its tables
  308. foreach ($db['tables'] as $table_key => $table) {
  309. // check if id is table and has alias
  310. if (stripos($type, 'tbl') !== false
  311. && $table_key === $id
  312. && ! empty($table['alias'])
  313. ) {
  314. return $table['alias'];
  315. }
  316. if (empty($table['columns'])) {
  317. continue;
  318. }
  319. // search each of its columns
  320. foreach ($table['columns'] as $col_key => $col) {
  321. // check if id is column
  322. if (stripos($type, 'col') !== false
  323. && $col_key === $id
  324. && ! empty($col)
  325. ) {
  326. return $col;
  327. }
  328. }
  329. }
  330. }
  331. return '';
  332. }
  333. /**
  334. * Gives the relation string and
  335. * also substitutes with alias if required
  336. * in this format:
  337. * [Foreign Table] ([Foreign Field])
  338. *
  339. * @param array $res_rel the foreigners array
  340. * @param string $field_name the field name
  341. * @param string $db the field name
  342. * @param array $aliases Alias information for db/table/column
  343. *
  344. * @return string the Relation string
  345. */
  346. public function getRelationString(
  347. array $res_rel,
  348. $field_name,
  349. $db,
  350. array $aliases = []
  351. ) {
  352. $relation = '';
  353. $foreigner = $this->relation->searchColumnInForeigners($res_rel, $field_name);
  354. if ($foreigner) {
  355. $ftable = $foreigner['foreign_table'];
  356. $ffield = $foreigner['foreign_field'];
  357. if (! empty($aliases[$db]['tables'][$ftable]['columns'][$ffield])) {
  358. $ffield = $aliases[$db]['tables'][$ftable]['columns'][$ffield];
  359. }
  360. if (! empty($aliases[$db]['tables'][$ftable]['alias'])) {
  361. $ftable = $aliases[$db]['tables'][$ftable]['alias'];
  362. }
  363. $relation = $ftable . ' (' . $ffield . ')';
  364. }
  365. return $relation;
  366. }
  367. }