ExportPlugin.php 11 KB

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