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

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