update-db-1.8.3-1.8.4.inc.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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.3-1.8.4-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.3-1.8.4-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.3-1.8.4-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.3-1.8.4-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) { die('Error while querying the courses list in update_db-1.8.3-1.8.4.inc.php'); }
  154. if (Database::num_rows($res) > 0) {
  155. $i = 0;
  156. $list = array();
  157. while ($row = Database::fetch_array($res)) {
  158. $list[] = $row;
  159. $i++;
  160. }
  161. foreach ($list as $row_course) {
  162. // Now use the $c_q_list
  163. /**
  164. * We connect to the right DB first to make sure we can use the queries
  165. * without a database name
  166. */
  167. if (!$singleDbForm) { //otherwise just use the main one
  168. Database::select_db($row_course['db_name']);
  169. }
  170. foreach ($c_q_list as $query) {
  171. if ($singleDbForm) {
  172. $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);
  173. }
  174. if ($only_test) {
  175. error_log("Database::query(".$row_course['db_name'].",$query)", 0);
  176. } else {
  177. $res = Database::query($query);
  178. if ($log) {
  179. error_log("In ".$row_course['db_name'].", executed: $query", 0);
  180. }
  181. }
  182. }
  183. // Ensure each learnpath is present in the item_property table
  184. $prefix_course = '';
  185. if ($singleDbForm) {
  186. $prefix_course = $prefix.$row_course['db_name']."_";
  187. }
  188. $sql_ip = "SELECT * FROM ".$prefix_course."item_property WHERE tool='learnpath'";
  189. $res_ip = Database::query($sql_ip);
  190. $paths = array();
  191. while ($row_ip = Database::fetch_array($res_ip)) {
  192. $paths[] = $row_ip['ref'];
  193. }
  194. $sql_lp = "SELECT * FROM ".$prefix_course."lp";
  195. $res_lp = Database::query($sql_lp);
  196. $tbl_tool = $prefix_course."tool";
  197. while ($row_lp = Database::fetch_array($res_lp)) {
  198. $time = date("Y-m-d H:i:s", time());
  199. $vis = 'v';
  200. $input = stripslashes($row_lp['name']);
  201. $input = str_replace("'", "''", $input);
  202. $input = str_replace('"', "''", $input);
  203. $mylink = 'newscorm/lp_controller.php?action=view&lp_id='.$row_lp['id'];
  204. $sql2 = "SELECT * FROM $tbl_tool where (name='$input' and image='scormbuilder.gif' and link LIKE '$mylink%')";
  205. if (in_array($row_lp['id'], $paths)) {
  206. // The path is already in item_property, check the visibility is the
  207. // same as the homepage tool's
  208. $res2 = Database::query($sql2);
  209. if (Database::num_rows($res2) > 0) {
  210. $row2 = Database::fetch_array($res2);
  211. $vis = $row2['visibility'];
  212. }
  213. $visi = array('v' => 1, 'i' => 0);
  214. if ($visi[$vis] != $row_ip['visibility']) {
  215. $sql_upd = "UPDATE ".$prefix_course."item_propery SET visibility=".$visi[$vis]." WHERE tool='learnpath' AND ref='".$row_lp['id']."'";
  216. $res_upd = Database::query($sql_upd);
  217. }
  218. } else {
  219. // The path is not in item_property, insert it
  220. $res2 = Database::query($sql2);
  221. if (Database::num_rows($res2) > 0) {
  222. $row2 = Database::fetch_array($res2);
  223. $vis = $row2['visibility'];
  224. }
  225. $visi = array('v' => 1, 'i' => 0);
  226. $sql_ins = "INSERT INTO ".$prefix_course."item_property " .
  227. "(tool,ref,insert_date,last_edit_date,insert_user_id,lastedit_type,lastedit_user_id,visibility)" .
  228. "VALUES" .
  229. "('learnpath',".$row_lp['id'].",'$time','$time',1,'learnpathAdded',1,".$visi[$vis].")";
  230. $res_ins = Database::query($sql_ins);
  231. }
  232. }
  233. }
  234. }
  235. }
  236. }
  237. } else {
  238. echo 'You are not allowed here !' . __FILE__;
  239. }