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

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