PageRenderTime 64ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/public/phpmyadmin/libraries/plugins/ExportPlugin.class.php

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