/plugins/system/jsntplframework/libraries/joomlashine/database/backup.php

https://bitbucket.org/pastor399/newcastleunifc · PHP · 123 lines · 53 code · 18 blank · 52 comment · 5 complexity · 14057e5f6b866f8ea0f8ac41f7a2f4e1 MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package JSNExtension
  5. * @subpackage JSNTPLFramework
  6. * @author JoomlaShine Team <support@joomlashine.com>
  7. * @copyright Copyright (C) 2012 JoomlaShine.com. All Rights Reserved.
  8. * @license GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
  9. *
  10. * Websites: http://www.joomlashine.com
  11. * Technical Support: Feedback - http://www.joomlashine.com/contact-us/get-support.html
  12. */
  13. // No direct access to this file
  14. defined('_JEXEC') or die('Restricted access');
  15. /**
  16. * Class use to backup existing database
  17. * to a file
  18. *
  19. * @package JSNTPLFramework
  20. * @subpackage Database
  21. * @since 1.0.0
  22. */
  23. abstract class JSNTplDatabaseBackup
  24. {
  25. /**
  26. * This method use to dump data of all tables in
  27. * the database
  28. *
  29. * @param array $tables List of tables will be backup
  30. * @param string $file File to save exported data
  31. *
  32. * @return string
  33. */
  34. public static function dump ($tables, $file)
  35. {
  36. $dbo = JFactory::getDBO();
  37. $existedTables = $dbo->getTableList();
  38. $fileHandle = fopen($file, 'w+');
  39. foreach ($tables as $table)
  40. {
  41. if (in_array($table, $existedTables))
  42. self::dumpTable($table, $fileHandle);
  43. }
  44. }
  45. /**
  46. * Dump a table data to a file or string
  47. *
  48. * @param string $table Name of the table
  49. * @param mixed $output Output can be file handler resource, path to file
  50. *
  51. * @return mixed
  52. */
  53. public static function dumpTable ($table, $output)
  54. {
  55. $dbo = JFactory::getDBO();
  56. $createQuery = $dbo->getTableCreate($table);
  57. $dumpBuffer = array();
  58. $columns = array();
  59. $fileHandle = is_resource($output) ? $output : fopen($output, 'w+');
  60. // Append create query to file
  61. fwrite($fileHandle, "DROP TABLE IF EXISTS `{$table}`;\r\n");
  62. fwrite($fileHandle, current($createQuery) . ";\r\n\r\n");
  63. // Query all rows in table
  64. $dbo->setQuery("SELECT * FROM {$table}");
  65. foreach ($dbo->loadAssocList() as $row)
  66. {
  67. $dumpBuffer[] = $row;
  68. if (empty($columns))
  69. $columns = array_keys($row);
  70. if (count($dumpBuffer) == 10) {
  71. self::_writeBuffer($fileHandle, $table, $columns, $dumpBuffer);
  72. $dumpBuffer = array();
  73. }
  74. }
  75. if (!empty($dumpBuffer))
  76. self::_writeBuffer($fileHandle, $table, $columns, $dumpBuffer);
  77. }
  78. /**
  79. * Save buffered content to file
  80. *
  81. * @param resource $fileHandle Resource that pointed to opened file handler
  82. * @param string $table Name of the table
  83. * @param array $columns Columns of the table
  84. * @param array $rows Buffered data rows
  85. *
  86. * @return void
  87. */
  88. private static function _writeBuffer ($fileHandle, $table, $columns, $rows)
  89. {
  90. $dbo = JFactory::getDBO();
  91. // Write insert query
  92. fwrite($fileHandle, "INSERT INTO `{$table}` (`" . implode('`, `', $columns) . "`) VALUES\r\n");
  93. // Buffer parsed content
  94. $content = array();
  95. // Loop to each row in buffer and write it to file
  96. foreach ($rows as $row)
  97. {
  98. $values = array();
  99. foreach ($row as $value)
  100. $values[] = sprintf('\'%s\'', $dbo->escape($value));
  101. $content[] = sprintf("\t(%s)", implode(', ', $values));
  102. }
  103. fwrite($fileHandle, implode(",\r\n", $content) . ";\r\n\r\n");
  104. }
  105. }