update-db-1.8.2-1.8.3.inc.php 6.8 KB

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