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

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