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

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