update-db-1.6.x-1.8.0.inc.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 (empty($updateFromConfigFile) || !file_exists($_POST['updatePath'] . $updateFromConfigFile) || !in_array(get_config_param('clarolineVersion'), $update_from_version_6)) {
  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. if (empty($dbStatsForm))
  49. $dbStatsForm = $dbNameForm;
  50. if (empty($dbScormForm))
  51. $dbScormForm = $dbNameForm;
  52. if (empty($dbUserForm))
  53. $dbUserForm = $dbNameForm;
  54. }
  55. /**
  56. * Update the databases "pre" migration
  57. */
  58. include '../lang/english/create_course.inc.php';
  59. if ($languageForm != 'english') {
  60. // languageForm has been escaped in index.php
  61. include '../lang/' . $languageForm . '/create_course.inc.php';
  62. }
  63. //get the main queries list (m_q_list)
  64. $m_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-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. error_log('Database name ' . $dbNameForm . ' is too long, skipping', 0);
  73. } elseif (!in_array($dbNameForm, $dblist)) {
  74. error_log('Database ' . $dbNameForm . ' was not found, skipping', 0);
  75. } else {
  76. Database::select_db($dbNameForm);
  77. foreach ($m_q_list as $query) {
  78. if ($only_test) {
  79. error_log("Database::query($dbNameForm,$query)", 0);
  80. } else {
  81. $res = Database::query($query);
  82. if ($log) {
  83. error_log("In $dbNameForm, executed: $query", 0);
  84. }
  85. }
  86. }
  87. }
  88. }
  89. // Get the stats queries list (s_q_list)
  90. $s_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-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. error_log('Database name ' . $dbStatsForm . ' is too long, skipping', 0);
  99. } elseif (!in_array($dbStatsForm, $dblist)) {
  100. error_log('Database ' . $dbStatsForm . ' was not found, skipping', 0);
  101. } else {
  102. Database::select_db($dbStatsForm);
  103. foreach ($s_q_list as $query) {
  104. if ($only_test) {
  105. error_log("Database::query($dbStatsForm,$query)", 0);
  106. } else {
  107. $res = Database::query($query);
  108. if ($log) {
  109. error_log("In $dbStatsForm, executed: $query", 0);
  110. }
  111. }
  112. }
  113. }
  114. }
  115. // Get the user queries list (u_q_list)
  116. $u_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-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. error_log('Database name ' . $dbUserForm . ' is too long, skipping', 0);
  125. } elseif (!in_array($dbUserForm, $dblist)) {
  126. error_log('Database ' . $dbUserForm . ' was not found, skipping', 0);
  127. } else {
  128. Database::select_db($dbUserForm);
  129. foreach ($u_q_list as $query) {
  130. if ($only_test) {
  131. error_log("Database::query($dbUserForm,$query)", 0);
  132. error_log("In $dbUserForm, executed: $query", 0);
  133. } else {
  134. $res = Database::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 = $_configuration['table_prefix'];
  144. }
  145. // Get the courses databases queries list (c_q_list)
  146. $c_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql', 'course');
  147. if (count($c_q_list) > 0) {
  148. // Get the courses list
  149. if (strlen($dbNameForm) > 40) {
  150. error_log('Database name ' . $dbNameForm . ' is too long, skipping', 0);
  151. } elseif (!in_array($dbNameForm, $dblist)) {
  152. error_log('Database ' . $dbNameForm . ' was not found, skipping', 0);
  153. } else {
  154. Database::select_db($dbNameForm);
  155. $res = Database::query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
  156. if ($res === false) {
  157. die('Error while querying the courses list in update_db-1.6.x-1.8.0.inc.php');
  158. }
  159. if (Database::num_rows($res) > 0) {
  160. $i = 0;
  161. $list = array();
  162. while ($row = Database::fetch_array($res)) {
  163. $list[] = $row;
  164. $i++;
  165. }
  166. foreach ($list as $row_course) {
  167. // Now use the $c_q_list
  168. /**
  169. * We connect to the right DB first to make sure we can use the queries
  170. * without a database name
  171. */
  172. if (!$singleDbForm) { // otherwise just use the main one
  173. Database::select_db($row_course['db_name']);
  174. }
  175. foreach ($c_q_list as $query) {
  176. if ($singleDbForm) {
  177. $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);
  178. }
  179. if ($only_test) {
  180. error_log("Database::query(" . $row_course['db_name'] . ",$query)", 0);
  181. } else {
  182. $res = Database::query($query);
  183. if ($log) {
  184. error_log("In " . $row_course['db_name'] . ", executed: $query", 0);
  185. }
  186. }
  187. }
  188. // Prepare reusable users list to avoid repetition of the SQL query, but only select
  189. // users from the current course to avoid blowing the memory limit
  190. $users_list = array();
  191. $sql_uc = "SELECT u.user_id as ui, u.firstname as fn, u.lastname as ln " .
  192. " FROM $dbNameForm.user u, $dbNameForm.course_rel_user cu " .
  193. " WHERE cu.course_code = '" . $row_course['code'] . "' " .
  194. " AND u.user_id = cu.user_id";
  195. $res_uc = Database::query($sql_uc);
  196. while ($user_row = Database::fetch_array($res_uc)) {
  197. $users_list[$user_row['fn'] . ' ' . $user_row['ln']] = $user_row['ui'];
  198. }
  199. // Update course manually
  200. // Update group_category.forum_state ?
  201. // Update group_info.tutor_id (put it in group_tutor table?) ?
  202. // Update group_info.forum_state, forum_id ?
  203. // Update forum tables (migrate from bb_ tables to forum_ tables)
  204. // Migrate categories
  205. $prefix_course = $prefix;
  206. if ($singleDbForm) {
  207. $prefix_course = $prefix . $row_course['db_name'] . "_";
  208. }
  209. $sql_orig = "SELECT * FROM " . $prefix_course . "bb_categories";
  210. $res_orig = Database::query($sql_orig);
  211. $order = 1;
  212. while ($row = Database::fetch_array($res_orig)) {
  213. $myorder = (empty($row['cat_order']) ? $order : $row['cat_order']);
  214. $sql = "INSERT INTO " . $prefix_course . "forum_category " .
  215. "(cat_id,cat_title,cat_comment,cat_order,locked) VALUES " .
  216. "('" . $row['cat_id'] . "','" . Database::escape_string($row['cat_title']) . "','','" . $myorder . "',0)";
  217. $res = Database::query($sql);
  218. $lastcatid = Database::insert_id();
  219. //error_log($sql,0);
  220. $order++;
  221. // Add item_property - forum categories were not put into item_properties before
  222. $sql = "INSERT INTO " . $prefix_course . "item_property (tool,insert_user_id,ref,lastedit_type,lastedit_user_id,visibility) " .
  223. "VALUES ('forum_category','1','$lastcatid','ForumCategoryAdded','1','1')";
  224. $res = Database::query($sql);
  225. //error_log($sql,0);
  226. }
  227. $sql_orig = "SELECT * FROM " . $prefix_course . "bb_forums ORDER BY forum_last_post_id desc";
  228. $res_orig = Database::query($sql_orig);
  229. $order = 1;
  230. while ($row = Database::fetch_array($res_orig)) {
  231. $sql = "INSERT INTO " . $prefix_course . "forum_forum " .
  232. "(forum_id,forum_category,allow_edit,forum_comment," .
  233. "forum_title," .
  234. "forum_last_post, forum_threads," .
  235. "locked, forum_posts, " .
  236. "allow_new_threads, forum_order) VALUES " .
  237. "('" . $row['forum_id'] . "','" . $row['cat_id'] . "',1,'" . Database::escape_string($row['forum_desc']) . "'," .
  238. "'" . Database::escape_string($row['forum_name']) . "'," .
  239. "'" . $row['forum_last_post_id'] . "','" . $row['forum_topics'] . "'," .
  240. "0,'" . $row['forum_posts'] . "'," .
  241. "1,$order)";
  242. //error_log($sql,0);
  243. $res = Database::query($sql);
  244. $lastforumid = Database::insert_id();
  245. $order++;
  246. // Add item_property - forums were not put into item_properties before
  247. $sql = "INSERT INTO " . $prefix_course . "item_property (tool,insert_user_id,ref,lastedit_type,lastedit_user_id,visibility) " .
  248. "VALUES ('forum','1','$lastforumid','ForumAdded','1','1')";
  249. $res = Database::query($sql);
  250. //error_log($sql,0);
  251. }
  252. $sql_orig = "SELECT * FROM " . $prefix_course . "bb_topics";
  253. $res_orig = Database::query($sql_orig);
  254. while ($row = Database::fetch_array($res_orig)) {
  255. $name = $row['prenom'] . ' ' . $row['nom'];
  256. // Check whether user id is reusable
  257. if ($row['topic_poster'] <= 1) {
  258. if (isset($users_list[$name])) {
  259. $poster_id = $users_list[$name];
  260. } else {
  261. $poster_id = $row['topic_poster'];
  262. }
  263. }
  264. // Convert time from varchar to datetime
  265. $time = $row['topic_time'];
  266. $name = Database::escape_string($name);
  267. $sql = "INSERT INTO " . $prefix_course . "forum_thread " .
  268. "(thread_id,forum_id,thread_poster_id," .
  269. "locked,thread_replies,thread_sticky,thread_title," .
  270. "thread_poster_name, thread_date, thread_last_post," .
  271. "thread_views) VALUES " .
  272. "('" . $row['topic_id'] . "','" . $row['forum_id'] . "','" . $poster_id . "'," .
  273. "0,'" . $row['topic_replies'] . "',0,'" . Database::escape_string($row['topic_title']) . "'," .
  274. "'$name','$time','" . $row['topic_last_post_id'] . "'," .
  275. "'" . $row['topic_views'] . "')";
  276. //error_log($sql,0);
  277. $res = Database::query($sql);
  278. $lastthreadid = Database::insert_id();
  279. // Add item_property - forum threads were not put into item_properties before
  280. $sql = "INSERT INTO " . $prefix_course . "item_property (tool,insert_user_id,ref,lastedit_type,lastedit_user_id,visibility) " .
  281. "VALUES ('forum_thread','1','$lastthreadid','ForumThreadAdded','1','1')";
  282. $res = Database::query($sql);
  283. //error_log($sql,0);
  284. }
  285. $sql_orig = "SELECT * FROM " . $prefix_course . "bb_posts bp, " . $prefix_course . "bb_posts_text bpt WHERE bp.post_id = bpt.post_id";
  286. $res_orig = Database::query($sql_orig);
  287. while ($row = Database::fetch_array($res_orig)) {
  288. $name = $row['prenom'] . ' ' . $row['nom'];
  289. // Check whether user id is reusable
  290. if ($row['poster_id'] <= 0) {
  291. if (isset($users_list[$name])) {
  292. $poster_id = $users_list[$name];
  293. } else {
  294. $poster_id = $row['poster_id'];
  295. }
  296. }
  297. // Convert time from varchar to datetime
  298. $time = $row['post_time'];
  299. $name = Database::escape_string($name);
  300. $sql = "INSERT INTO " . $prefix_course . "forum_post " .
  301. "(post_id,forum_id,thread_id," .
  302. "poster_id,post_parent_id,visible, " .
  303. "post_title,poster_name, post_text, " .
  304. "post_date, post_notification) VALUES " .
  305. "('" . $row['post_id'] . "','" . $row['forum_id'] . "','" . $row['topic_id'] . "'," .
  306. "'" . $poster_id . "','" . $row['parent_id'] . "',1," .
  307. "'" . Database::escape_string($row['post_title']) . "','$name', '" . Database::escape_string($row['post_text']) . "'," .
  308. "'$time',0)";
  309. //error_log($sql,0);
  310. $res = Database::query($sql);
  311. $lastpostid = Database::insert_id();
  312. // Add item_property - forum threads were not put into item_properties before
  313. $sql = "INSERT INTO " . $prefix_course . "item_property(tool,insert_user_id,ref,lastedit_type,lastedit_user_id,visibility) " .
  314. "VALUES ('forum_post','1','$lastpostid','ForumPostAdded','1','1')";
  315. $res = Database::query($sql);
  316. //error_log($sql,0);
  317. }
  318. unset($users_list);
  319. $sql_orig = "SELECT id, tutor_id FROM " . $prefix_course . "group_info";
  320. $res_orig = Database::query($sql_orig);
  321. $order = 1;
  322. while ($row = Database::fetch_array($res_orig)) {
  323. $sql = "INSERT INTO " . $prefix_course . "group_rel_tutor " .
  324. "(user_id,group_id) VALUES " .
  325. "('" . $row['tutor_id'] . "','" . $row['id'] . "')";
  326. $res = Database::query($sql);
  327. }
  328. }
  329. }
  330. }
  331. }
  332. // Load the old-scorm to new-scorm migration script
  333. if (!$only_test) {
  334. include('update-db-scorm-1.6.x-1.8.0.inc.php');
  335. }
  336. if (defined('SYSTEM_INSTALLATION')) {
  337. if ($singleDbForm) {
  338. if (empty($dbStatsForm))
  339. $dbStatsForm = $dbNameForm;
  340. if (empty($dbScormForm))
  341. $dbScormForm = $dbNameForm;
  342. if (empty($dbUserForm))
  343. $dbUserForm = $dbNameForm;
  344. }
  345. // Deal with migrate-db-1.6.x-1.8.0-post.sql
  346. // Get the main queries list (m_q_list)
  347. $m_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-post.sql', 'main');
  348. if (count($m_q_list) > 0) {
  349. // Now use the $m_q_list
  350. /**
  351. * We connect to the right DB first to make sure we can use the queries
  352. * without a database name
  353. */
  354. if (strlen($dbNameForm) > 40) {
  355. error_log('Database name ' . $dbNameForm . ' is too long, skipping', 0);
  356. } elseif (!in_array($dbNameForm, $dblist)) {
  357. error_log('Database ' . $dbNameForm . ' was not found, skipping', 0);
  358. } else {
  359. Database::select_db($dbNameForm);
  360. foreach ($m_q_list as $query) {
  361. if ($only_test) {
  362. error_log("Database::query($dbNameForm,$query)", 0);
  363. } else {
  364. $res = Database::query($query);
  365. if ($log) {
  366. error_log("In $dbNameForm, executed: $query", 0);
  367. }
  368. }
  369. }
  370. }
  371. }
  372. // Get the stats queries list (s_q_list)
  373. $s_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-post.sql', 'stats');
  374. if (count($s_q_list) > 0) {
  375. // Now use the $s_q_list
  376. /**
  377. * We connect to the right DB first to make sure we can use the queries
  378. * without a database name
  379. */
  380. if (strlen($dbStatsForm) > 40) {
  381. error_log('Database name ' . $dbStatsForm . ' is too long, skipping', 0);
  382. } elseif (!in_array($dbNameForm, $dblist)) {
  383. error_log('Database ' . $dbNameForm . ' was not found, skipping', 0);
  384. } else {
  385. Database::select_db($dbStatsForm);
  386. foreach ($s_q_list as $query) {
  387. if ($only_test) {
  388. error_log("Database::query($dbStatsForm,$query)", 0);
  389. } else {
  390. $res = Database::query($query);
  391. if ($log) {
  392. error_log("In $dbStatsForm, executed: $query", 0);
  393. }
  394. }
  395. }
  396. }
  397. }
  398. // Get the user queries list (u_q_list)
  399. $u_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-post.sql', 'user');
  400. if (count($u_q_list) > 0) {
  401. //now use the $u_q_list
  402. /**
  403. * We connect to the right DB first to make sure we can use the queries
  404. * without a database name
  405. */
  406. if (strlen($dbUserForm) > 40) {
  407. error_log('Database name ' . $dbUserForm . ' is too long, skipping', 0);
  408. } elseif (!in_array($dbUserForm, $dblist)) {
  409. error_log('Database ' . $dbUserForm . ' was not found, skipping', 0);
  410. } else {
  411. Database::select_db($dbUserForm);
  412. foreach ($u_q_list as $query) {
  413. if ($only_test) {
  414. error_log("Database::query($dbUserForm,$query)", 0);
  415. } else {
  416. $res = Database::query($query);
  417. if ($log) {
  418. error_log("In $dbUserForm, executed: $query", 0);
  419. }
  420. }
  421. }
  422. }
  423. }
  424. // The SCORM database should need a drop in the post-migrate part. However, we will keep these tables a bit more, just in case...
  425. }
  426. // Get the courses databases queries list (c_q_list)
  427. $c_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-post.sql', 'course');
  428. if (count($c_q_list) > 0) {
  429. // Get the courses list
  430. if (strlen($dbNameForm) > 40) {
  431. error_log('Database name ' . $dbNameForm . ' is too long, skipping', 0);
  432. } elseif (!in_array($dbNameForm, $dblist)) {
  433. error_log('Database ' . $dbNameForm . ' was not found, skipping', 0);
  434. } else {
  435. Database::select_db($dbNameForm);
  436. $res = Database::query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
  437. if ($res === false) {
  438. die('Error while querying the courses list in update_db-1.6.x-1.8.0.inc.php');
  439. }
  440. if (Database::num_rows($res) > 0) {
  441. $i = 0;
  442. while ($row = Database::fetch_array($res)) {
  443. $list[] = $row;
  444. $i++;
  445. }
  446. foreach ($list as $row) {
  447. // Now use the $c_q_list
  448. /**
  449. * We connect to the right DB first to make sure we can use the queries
  450. * without a database name
  451. */
  452. $prefix_course = $prefix;
  453. if ($singleDbForm) {
  454. $prefix_course = $prefix . $row['db_name'] . "_";
  455. } else {
  456. Database::select_db($row['db_name']);
  457. }
  458. foreach ($c_q_list as $query) {
  459. if ($singleDbForm) { //otherwise just use the main one
  460. $query = preg_replace('/^(UPDATE|ALTER TABLE|CREATE TABLE|DROP TABLE|INSERT INTO|DELETE FROM)\s+(\w*)(.*)$/', "$1 $prefix$2$3", $query);
  461. }
  462. if ($only_test) {
  463. error_log("Database::query(" . $row['db_name'] . ",$query)", 0);
  464. } else {
  465. $res = Database::query($query);
  466. if ($log) {
  467. error_log("In " . $row['db_name'] . ", executed: $query", 0);
  468. }
  469. }
  470. }
  471. }
  472. }
  473. }
  474. }
  475. // Upgrade user categories sort
  476. $table_user_categories = $dbUserForm . '.user_course_category';
  477. $sql = 'SELECT * FROM ' . $table_user_categories . ' ORDER BY user_id, title';
  478. $rs = Database::query($sql);
  479. $sort = 0;
  480. $old_user = 0;
  481. while ($cat = Database::fetch_array($rs)) {
  482. if ($old_user != $cat['user_id']) {
  483. $old_user = $cat['user_id'];
  484. $sort = 0;
  485. }
  486. $sort++;
  487. $sql = 'UPDATE ' . $table_user_categories . ' SET
  488. sort = ' . intval($sort) . '
  489. WHERE id=' . intval($cat['id']);
  490. Database::query($sql);
  491. }
  492. } else {
  493. echo 'You are not allowed here !' . __FILE__;
  494. }