ExportPlugin.php 11 KB

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