update-db-1.8.0-1.8.2.inc.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Chamilo LMS
  5. *
  6. * Update the Chamilo database from an older Dokeos version
  7. * Notice : This script has to be included by index.php
  8. * or update_courses.php (deprecated).
  9. *
  10. * @package chamilo.install
  11. * @todo
  12. * - conditional changing of tables. Currently we execute for example
  13. * ALTER TABLE `$dbNameForm`.`cours`
  14. * instructions without checking wether this is necessary.
  15. * - reorganise code into functions
  16. * @todo use database library
  17. */
  18. // Check if we come from index.php or update_courses.php - otherwise display error msg
  19. if (defined('SYSTEM_INSTALLATION')) {
  20. // Check if the current Dokeos install is eligible for update
  21. if (!file_exists('../inc/conf/configuration.php')) {
  22. echo '<strong>' . get_lang('Error') . ' !</strong> Dokeos ' . implode('|', $updateFromVersion) . ' ' . get_lang('HasNotBeenFound') . '.<br /><br />
  23. ' . get_lang('PleasGoBackToStep1') . '.
  24. <p><button type="submit" class="back" name="step1" value="&lt; ' . get_lang('Back') . '">' . get_lang('Back') . '</button></p>
  25. </td></tr></table></form></body></html>';
  26. exit();
  27. }
  28. $_configuration['db_glue'] = get_config_param('dbGlu');
  29. if ($singleDbForm) {
  30. $_configuration['table_prefix'] = get_config_param('courseTablePrefix');
  31. $_configuration['main_database'] = get_config_param('mainDbName');
  32. $_configuration['db_prefix'] = get_config_param('dbNamePrefix');
  33. }
  34. $dbScormForm = preg_replace('/[^a-zA-Z0-9_\-]/', '', $dbScormForm);
  35. if (!empty($dbPrefixForm) && strpos($dbScormForm, $dbPrefixForm) !== 0) {
  36. $dbScormForm = $dbPrefixForm . $dbScormForm;
  37. }
  38. if (empty($dbScormForm) || $dbScormForm == 'mysql' || $dbScormForm == $dbPrefixForm) {
  39. $dbScormForm = $dbPrefixForm . 'scorm';
  40. }
  41. /* Normal upgrade procedure: start by updating main, statistic, user databases */
  42. // If this script has been included by index.php, not update_courses.php, so
  43. // that we want to change the main databases as well...
  44. $only_test = false;
  45. $log = 0;
  46. if (defined('SYSTEM_INSTALLATION')) {
  47. if ($singleDbForm) {
  48. $dbStatsForm = $dbNameForm;
  49. $dbScormForm = $dbNameForm;
  50. $dbUserForm = $dbNameForm;
  51. }
  52. /**
  53. * Update the databases "pre" migration
  54. */
  55. include '../lang/english/create_course.inc.php';
  56. if ($languageForm != 'english') {
  57. //languageForm has been escaped in index.php
  58. include '../lang/' . $languageForm . '/create_course.inc.php';
  59. }
  60. // Get the main queries list (m_q_list)
  61. $m_q_list = get_sql_file_contents('migrate-db-1.8.0-1.8.2-pre.sql', 'main');
  62. if (count($m_q_list) > 0) {
  63. //now use the $m_q_list
  64. /**
  65. * We connect to the right DB first to make sure we can use the queries
  66. * without a database name
  67. */
  68. if (strlen($dbNameForm) > 40) {
  69. error_log('Database name ' . $dbNameForm . ' is too long, skipping', 0);
  70. } elseif (!in_array($dbNameForm, $dblist)) {
  71. error_log('Database ' . $dbNameForm . ' was not found, skipping', 0);
  72. } else {
  73. Database::select_db($dbNameForm);
  74. foreach ($m_q_list as $query) {
  75. if ($only_test) {
  76. error_log("Database::query($dbNameForm,$query)", 0);
  77. } else {
  78. $res = Database::query($query);
  79. if ($log) {
  80. error_log("In $dbNameForm, executed: $query", 0);
  81. }
  82. }
  83. }
  84. }
  85. }
  86. // Get the stats queries list (s_q_list)
  87. $s_q_list = get_sql_file_contents('migrate-db-1.8.0-1.8.2-pre.sql', 'stats');
  88. if (count($s_q_list) > 0) {
  89. // Now use the $s_q_list
  90. /**
  91. * We connect to the right DB first to make sure we can use the queries
  92. * without a database name
  93. */
  94. if (strlen($dbStatsForm) > 40) {
  95. error_log('Database name ' . $dbStatsForm . ' is too long, skipping', 0);
  96. } elseif (!in_array($dbStatsForm, $dblist)) {
  97. error_log('Database ' . $dbStatsForm . ' was not found, skipping', 0);
  98. } else {
  99. Database::select_db($dbStatsForm);
  100. foreach ($s_q_list as $query) {
  101. if ($only_test) {
  102. error_log("Database::query($dbStatsForm,$query)", 0);
  103. } else {
  104. $res = Database::query($query);
  105. if ($log) {
  106. error_log("In $dbStatsForm, executed: $query", 0);
  107. }
  108. }
  109. }
  110. }
  111. }
  112. // Get the user queries list (u_q_list)
  113. $u_q_list = get_sql_file_contents('migrate-db-1.8.0-1.8.2-pre.sql', 'user');
  114. if (count($u_q_list) > 0) {
  115. //now use the $u_q_list
  116. /**
  117. * We connect to the right DB first to make sure we can use the queries
  118. * without a database name
  119. */
  120. if (strlen($dbUserForm) > 40) {
  121. error_log('Database name ' . $dbUserForm . ' is too long, skipping', 0);
  122. } elseif (!in_array($dbUserForm, $dblist)) {
  123. error_log('Database ' . $dbUserForm . ' was not found, skipping', 0);
  124. } else {
  125. Database::select_db($dbUserForm);
  126. foreach ($u_q_list as $query) {
  127. if ($only_test) {
  128. error_log("Database::query($dbUserForm,$query)", 0);
  129. error_log("In $dbUserForm, executed: $query", 0);
  130. } else {
  131. $res = Database::query($query);
  132. }
  133. }
  134. }
  135. }
  136. // The SCORM database doesn't need a change in the pre-migrate part - ignore
  137. }
  138. $prefix = '';
  139. if ($singleDbForm) {
  140. $prefix = $_configuration['table_prefix'];
  141. }
  142. // Get the courses databases queries list (c_q_list)
  143. $c_q_list = get_sql_file_contents('migrate-db-1.8.0-1.8.2-pre.sql', 'course');
  144. if (count($c_q_list) > 0) {
  145. // Get the courses list
  146. if (strlen($dbNameForm) > 40) {
  147. error_log('Database name ' . $dbNameForm . ' is too long, skipping', 0);
  148. } elseif (!in_array($dbNameForm, $dblist)) {
  149. error_log('Database ' . $dbNameForm . ' was not found, skipping', 0);
  150. } else {
  151. Database::select_db($dbNameForm);
  152. $res = Database::query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
  153. if ($res === false) {
  154. die('Error while querying the courses list in update_db-1.8.0-1.8.2.inc.php');
  155. }
  156. if (Database::num_rows($res) > 0) {
  157. $i = 0;
  158. $list = array();
  159. while ($row = Database::fetch_array($res)) {
  160. $list[] = $row;
  161. $i++;
  162. }
  163. foreach ($list as $row_course) {
  164. // Now use the $c_q_list
  165. /**
  166. * We connect to the right DB first to make sure we can use the queries
  167. * without a database name
  168. */
  169. if (!$singleDbForm) { // otherwise just use the main one
  170. Database::select_db($row_course['db_name']);
  171. }
  172. $app['monolog']->addInfo('Course db ' . $row_course['db_name']);
  173. foreach ($c_q_list as $query) {
  174. if ($singleDbForm) {
  175. $query = preg_replace('/^(UPDATE|ALTER TABLE|CREATE TABLE|DROP TABLE|INSERT INTO|DELETE FROM)\s+(\w*)(.*)$/', "$1 $prefix{$row_course['db_name']}_$2$3", $query);
  176. }
  177. if ($only_test) {
  178. error_log("Database::query(" . $row_course['db_name'] . ",$query)", 0);
  179. } else {
  180. $res = Database::query($query);
  181. if ($log) {
  182. error_log("In " . $row_course['db_name'] . ", executed: $query", 0);
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. } else {
  191. echo 'You are not allowed here !' . __FILE__;
  192. }