PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/classes/Plugins/ExportPlugin.php

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