update-db-1.8.6-1.8.6.1.inc.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. $old_file_version = '1.8.6';
  19. $new_file_version = '1.8.6.1';
  20. //check if we come from index.php or update_courses.php - otherwise display error msg
  21. if (defined('SYSTEM_INSTALLATION')) {
  22. // Check if the current Dokeos install is eligible for update
  23. if (!file_exists('../inc/conf/configuration.php')) {
  24. echo '<strong>'.get_lang('Error').' !</strong> Dokeos '.implode('|', $updateFromVersion).' '.get_lang('HasNotBeenFound').'.<br /><br />
  25. '.get_lang('PleasGoBackToStep1').'.
  26. <p><button type="submit" class="back" name="step1" value="&lt; '.get_lang('Back').'">'.get_lang('Back').'</button></p>
  27. </td></tr></table></form></body></html>';
  28. exit ();
  29. }
  30. $_configuration['db_glue'] = get_config_param('dbGlu');
  31. if ($singleDbForm) {
  32. $_configuration['table_prefix'] = get_config_param('courseTablePrefix');
  33. $_configuration['main_database'] = get_config_param('mainDbName');
  34. $_configuration['db_prefix'] = get_config_param('dbNamePrefix');
  35. }
  36. $dbScormForm = preg_replace('/[^a-zA-Z0-9_\-]/', '', $dbScormForm);
  37. if (!empty($dbPrefixForm) && strpos($dbScormForm, $dbPrefixForm) !== 0) {
  38. $dbScormForm = $dbPrefixForm.$dbScormForm;
  39. }
  40. if (empty($dbScormForm) || $dbScormForm == 'mysql' || $dbScormForm == $dbPrefixForm) {
  41. $dbScormForm = $dbPrefixForm.'scorm';
  42. }
  43. /* Normal upgrade procedure: start by updating main, statistic, user databases */
  44. // If this script has been included by index.php, not update_courses.php, so
  45. // that we want to change the main databases as well...
  46. $only_test = false;
  47. $log = 0;
  48. if (defined('SYSTEM_INSTALLATION')) {
  49. if ($singleDbForm) {
  50. $dbStatsForm = $dbNameForm;
  51. $dbScormForm = $dbNameForm;
  52. $dbUserForm = $dbNameForm;
  53. }
  54. /**
  55. * Update the databases "pre" migration
  56. */
  57. include '../lang/english/create_course.inc.php';
  58. if ($languageForm != 'english') {
  59. //languageForm has been escaped in index.php
  60. include '../lang/'.$languageForm.'/create_course.inc.php';
  61. }
  62. //get the main queries list (m_q_list)
  63. $m_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'main');
  64. if (count($m_q_list) > 0) {
  65. // Now use the $m_q_list
  66. /**
  67. * We connect to the right DB first to make sure we can use the queries
  68. * without a database name
  69. */
  70. if (strlen($dbNameForm) > 40) {
  71. error_log('Database name '.$dbNameForm.' is too long, skipping', 0);
  72. } elseif (!in_array($dbNameForm,$dblist)) {
  73. error_log('Database '.$dbNameForm.' was not found, skipping', 0);
  74. } else {
  75. Database::select_db($dbNameForm);
  76. foreach($m_q_list as $query) {
  77. if ($only_test){
  78. error_log("Database::query($dbNameForm,$query)", 0);
  79. } else {
  80. $res = Database::query($query);
  81. if ($log) {
  82. error_log("In $dbNameForm, executed: $query", 0);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. // Get the stats queries list (s_q_list)
  89. $s_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'stats');
  90. if (count($s_q_list) > 0) {
  91. // Now use the $s_q_list
  92. /**
  93. * We connect to the right DB first to make sure we can use the queries
  94. * without a database name
  95. */
  96. if (strlen($dbStatsForm) > 40) {
  97. error_log('Database name '.$dbStatsForm.' is too long, skipping', 0);
  98. } elseif (!in_array($dbStatsForm,$dblist)) {
  99. error_log('Database '.$dbStatsForm.' was not found, skipping', 0);
  100. } else {
  101. Database::select_db($dbStatsForm);
  102. foreach ($s_q_list as $query) {
  103. if ($only_test) {
  104. error_log("Database::query($dbStatsForm,$query)", 0);
  105. } else {
  106. $res = Database::query($query);
  107. if ($log) {
  108. error_log("In $dbStatsForm, executed: $query", 0);
  109. }
  110. }
  111. }
  112. }
  113. }
  114. // Get the user queries list (u_q_list)
  115. $u_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'user');
  116. if (count($u_q_list) > 0) {
  117. // Now use the $u_q_list
  118. /**
  119. * We connect to the right DB first to make sure we can use the queries
  120. * without a database name
  121. */
  122. if (strlen($dbUserForm) > 40) {
  123. error_log('Database name '.$dbUserForm.' is too long, skipping', 0);
  124. } elseif (!in_array($dbUserForm, $dblist)) {
  125. error_log('Database '.$dbUserForm.' was not found, skipping', 0);
  126. } else {
  127. Database::select_db($dbUserForm);
  128. foreach ($u_q_list as $query) {
  129. if ($only_test) {
  130. error_log("Database::query($dbUserForm,$query)", 0);
  131. error_log("In $dbUserForm, executed: $query", 0);
  132. } else {
  133. $res = Database::query($query);
  134. }
  135. }
  136. }
  137. }
  138. // The SCORM database doesn't need a change in the pre-migrate part - ignore
  139. }
  140. $prefix = '';
  141. if ($singleDbForm) {
  142. $prefix = get_config_param ('table_prefix');
  143. }
  144. // Get the courses databases queries list (c_q_list)
  145. $c_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'course');
  146. if (count($c_q_list) > 0) {
  147. // Get the courses list
  148. if (strlen($dbNameForm) > 40) {
  149. error_log('Database name '.$dbNameForm.' is too long, skipping', 0);
  150. } elseif (!in_array($dbNameForm, $dblist)) {
  151. error_log('Database '.$dbNameForm.' was not found, skipping', 0);
  152. } else {
  153. Database::select_db($dbNameForm);
  154. $res = Database::query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL ORDER BY code");
  155. if ($res === false) { die('Error while querying the courses list in update_db-1.8.6-1.8.6.1.inc.php'); }
  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. foreach ($c_q_list as $query) {
  173. if ($singleDbForm) {
  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. $t_wiki = $row_course['db_name'].".wiki";
  186. $t_wiki_conf = $row_course['db_name'].".wiki_conf";
  187. if ($singleDbForm) {
  188. $t_wiki = "$prefix{$row_course['db_name']}_wiki";
  189. $t_wiki_conf = "$prefix{$row_course['db_name']}_wiki_conf";
  190. }
  191. // Update correct page_id to wiki table, actually only store 0
  192. $query = "SELECT id, reflink FROM $t_wiki";
  193. $res_page = Database::query($query);
  194. $wiki_id = $reflink = array();
  195. if (Database::num_rows($res_page) > 0) {
  196. while ($row_page = Database::fetch_row($res_page)) {
  197. $wiki_id[] = $row_page[0];
  198. $reflink[] = $row_page[1];
  199. }
  200. }
  201. $reflink_unique = array_unique($reflink);
  202. $reflink_flip = array_flip($reflink_unique);
  203. if (is_array($wiki_id)) {
  204. foreach ($wiki_id as $key => $wiki_page) {
  205. $pag_id = $reflink_flip[$reflink[$key]];
  206. $sql= "UPDATE $t_wiki SET page_id='".($pag_id + 1)."' WHERE id = '$wiki_page'";
  207. $res_update = Database::query($sql);
  208. }
  209. }
  210. // Insert page_id into wiki_conf table, actually this table is empty
  211. $query = "SELECT DISTINCT page_id FROM $t_wiki ORDER BY page_id";
  212. $myres_wiki = Database::query($query);
  213. if (Database::num_rows($myres_wiki) > 0) {
  214. while ($row_wiki = Database::fetch_row($myres_wiki)) {
  215. $page_id = $row_wiki[0];
  216. $query = "INSERT INTO ".$t_wiki_conf." (page_id, task, feedback1, feedback2, feedback3, fprogress1, fprogress2, fprogress3) VALUES ('".$page_id."','','','','','','','')";
  217. $myres_wiki_conf = Database::query($query);
  218. }
  219. }
  220. }
  221. }
  222. }
  223. }
  224. } else {
  225. echo 'You are not allowed here !';
  226. }