PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/classes/InstallQuery.php

https://bitbucket.org/mstetson/obiblio/
PHP | 141 lines | 115 code | 14 blank | 12 comment | 24 complexity | e507b5c00d26c66d789ac89463e9a29b MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /* This file is part of a copyrighted work; it is distributed with NO WARRANTY.
  3. * See the file COPYRIGHT.html for more details.
  4. */
  5. require_once("../shared/global_constants.php");
  6. require_once("../classes/Query.php");
  7. class InstallQuery extends Query {
  8. /* Override constructor so the installer can test the database connection */
  9. function InstallQuery() {
  10. ;
  11. }
  12. function dropTable($tableName) {
  13. $sql = $this->mkSQL("drop table if exists %I ", $tableName);
  14. $this->exec($sql);
  15. }
  16. function renameTables($fromTablePrfx, $toTablePrfx = DB_TABLENAME_PREFIX) {
  17. $fromTableNames = $this->getTableNames($fromTablePrfx.'%');
  18. foreach($fromTableNames as $fromTableName) {
  19. $toTableName = str_replace($fromTablePrfx, $toTablePrfx, $fromTableName);
  20. $this->renameTable($fromTableName, $toTableName);
  21. }
  22. }
  23. function renameTable($fromTableName, $toTableName) {
  24. $this->dropTable($toTableName);
  25. $sql = "rename table ".$fromTableName." to ".$toTableName;
  26. $this->exec($sql);
  27. }
  28. function getTableNames($pattern = "") {
  29. if($pattern == "") {
  30. $pattern = DB_TABLENAME_PREFIX.'%';
  31. }
  32. $sql = "show tables like '".$pattern."'";
  33. $rows = $this->exec($sql, OBIB_NUM);
  34. $tablenames = array();
  35. foreach ($rows as $row) {
  36. $tablenames[] = $row[0];
  37. }
  38. return $tablenames;
  39. }
  40. function _getSettings($tablePrfx) {
  41. $sql = $this->mkSQL('SHOW TABLES LIKE %Q ', $tablePrfx.'settings');
  42. $row = $this->select01($sql);
  43. if (!$row) {
  44. return false;
  45. }
  46. $sql = $this->mkSQL('SELECT * FROM %I ', $tablePrfx.'settings');
  47. return $this->select1($sql);
  48. }
  49. function getCurrentLocale($tablePrfx = DB_TABLENAME_PREFIX) {
  50. $array = $this->_getSettings($tablePrfx);
  51. if($array == false ||
  52. !isset($array["locale"])) {
  53. return 'en'; //Earlier versions of Openbiblio only supported English
  54. }
  55. else {
  56. return $array["locale"];
  57. }
  58. }
  59. function getCurrentDatabaseVersion($tablePrfx = DB_TABLENAME_PREFIX) {
  60. $array = $this->_getSettings($tablePrfx);
  61. if($array == false) {
  62. return false;
  63. }
  64. else {
  65. return $array["version"];
  66. }
  67. }
  68. function freshInstall($locale, $sampleDataRequired = false,
  69. $version=OBIB_LATEST_DB_VERSION,
  70. $tablePrfx = DB_TABLENAME_PREFIX) {
  71. $rootDir = '../install/' . $version . '/sql';
  72. $localeDir = '../locale/' . $locale . '/sql/' . $version;
  73. $this->executeSqlFilesInDir($rootDir, $tablePrfx);
  74. $this->executeSqlFilesInDir($localeDir . '/domain/', $tablePrfx);
  75. if($sampleDataRequired) {
  76. $this->executeSqlFilesInDir($localeDir . '/sample/', $tablePrfx);
  77. }
  78. }
  79. function executeSqlFilesInDir($dir, $tablePrfx = "") {
  80. if (is_dir($dir)) {
  81. if ($dh = opendir($dir)) {
  82. while (($filename = readdir($dh)) !== false) {
  83. if(preg_match('/\\.sql$/', $filename)) {
  84. $this->executeSqlFile($dir.'/'.$filename, $tablePrfx);
  85. }
  86. }
  87. closedir($dh);
  88. }
  89. }
  90. }
  91. /**********************************************************************************
  92. * Function to read through an sql file executing SQL only when ";" is encountered
  93. **********************************************************************************/
  94. function executeSqlFile($filename, $tablePrfx = DB_TABLENAME_PREFIX) {
  95. $fp = fopen($filename, "r");
  96. # show error if file could not be opened
  97. if ($fp == false) {
  98. Fatal::error("Error reading file: ".H($filename));
  99. } else {
  100. $sqlStmt = "";
  101. while (!feof ($fp)) {
  102. $char = fgetc($fp);
  103. if ($char == ";") {
  104. //replace table prefix, we're doing it here as the install script may
  105. //want to override the required prefix (eg. during upgrade / conversion
  106. //process)
  107. $sql = str_replace("%prfx%",$tablePrfx,$sqlStmt);
  108. //replace ENGINE with TYPE for old MySQL versions
  109. $MySQLn = explode('.', implode('', explode('-', mysql_get_server_info())));
  110. if ($MySQLn[0] < '5') {
  111. $sql = str_replace("ENGINE=","TYPE=",$sql);
  112. $sql = str_replace("engine=","type=",$sql);
  113. } else {
  114. $sql = str_replace("TYPE=","ENGINE=",$sql);
  115. $sql = str_replace("type=","engine=",$sql);
  116. }
  117. $this->exec($sql);
  118. $sqlStmt = "";
  119. } else {
  120. $sqlStmt .= $char;
  121. }
  122. }
  123. fclose($fp);
  124. }
  125. }
  126. }
  127. ?>