update-db-1.8.8-1.9.0.inc.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Chamilo LMS
  5. *
  6. * Update the Chamilo database from an older Chamilo 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. $old_file_version = '1.8.8';
  19. $new_file_version = '1.9.0';
  20. // Check if we come from index.php or update_courses.php - otherwise display error msg
  21. if (defined('SYSTEM_INSTALLATION')) {
  22. // Check if the current Chamilo install is eligible for update
  23. if (!file_exists('../inc/conf/configuration.php')) {
  24. echo '<strong>'.get_lang('Error').' !</strong> Chamilo '.implode('|', $updateFromVersion).' '.get_lang('HasNotBeenFound').'.<br /><br />
  25. '.get_lang('PleasGoBackToStep1').'.
  26. <p><button type="submit" class="back" name="step1" value="&lt; '.get_lang('Back').'">'.get_lang('Back').'</button></p>
  27. </td></tr></table></form></body></html>';
  28. exit ();
  29. }
  30. $_configuration['db_glue'] = get_config_param('dbGlu');
  31. if ($singleDbForm) {
  32. $_configuration['table_prefix'] = get_config_param('courseTablePrefix');
  33. $_configuration['main_database'] = get_config_param('mainDbName');
  34. $_configuration['db_prefix'] = get_config_param('dbNamePrefix');
  35. }
  36. $dbScormForm = preg_replace('/[^a-zA-Z0-9_\-]/', '', $dbScormForm);
  37. if (!empty($dbPrefixForm) && strpos($dbScormForm, $dbPrefixForm) !== 0) {
  38. $dbScormForm = $dbPrefixForm.$dbScormForm;
  39. }
  40. if (empty($dbScormForm) || $dbScormForm == 'mysql' || $dbScormForm == $dbPrefixForm) {
  41. $dbScormForm = $dbPrefixForm.'scorm';
  42. }
  43. /* Normal upgrade procedure: start by updating main, statistic, user databases */
  44. // If this script has been included by index.php, not update_courses.php, so
  45. // that we want to change the main databases as well...
  46. $only_test = false;
  47. $log = 0;
  48. if (defined('SYSTEM_INSTALLATION')) {
  49. if ($singleDbForm) {
  50. $dbStatsForm = $dbNameForm;
  51. $dbScormForm = $dbNameForm;
  52. $dbUserForm = $dbNameForm;
  53. }
  54. /**
  55. * Update the databases "pre" migration
  56. */
  57. include '../lang/english/create_course.inc.php';
  58. if ($languageForm != 'english') {
  59. // languageForm has been escaped in index.php
  60. include '../lang/'.$languageForm.'/create_course.inc.php';
  61. }
  62. // Get the main queries list (m_q_list)
  63. $m_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'main');
  64. if (count($m_q_list) > 0) {
  65. // Now use the $m_q_list
  66. /**
  67. * We connect to the right DB first to make sure we can use the queries
  68. * without a database name
  69. */
  70. if (strlen($dbNameForm) > 40) {
  71. error_log('Database name '.$dbNameForm.' is too long, skipping', 0);
  72. } elseif (!in_array($dbNameForm, $dblist)) {
  73. error_log('Database '.$dbNameForm.' was not found, skipping', 0);
  74. } else {
  75. Database::select_db($dbNameForm);
  76. foreach ($m_q_list as $query){
  77. if ($only_test) {
  78. error_log("Database::query($dbNameForm,$query)", 0);
  79. } else {
  80. $res = Database::query($query);
  81. if ($log) {
  82. error_log("In $dbNameForm, executed: $query", 0);
  83. }
  84. if ($res === false) {
  85. error_log('Error in '.$query.': '.Database::error());
  86. }
  87. }
  88. }
  89. }
  90. }
  91. // Get the stats queries list (s_q_list)
  92. $s_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'stats');
  93. if (count($s_q_list) > 0) {
  94. // Now use the $s_q_list
  95. /**
  96. * We connect to the right DB first to make sure we can use the queries
  97. * without a database name
  98. */
  99. if (strlen($dbStatsForm) > 40) {
  100. error_log('Database name '.$dbStatsForm.' is too long, skipping', 0);
  101. } elseif (!in_array($dbStatsForm, $dblist)){
  102. error_log('Database '.$dbStatsForm.' was not found, skipping', 0);
  103. } else {
  104. Database::select_db($dbStatsForm);
  105. foreach ($s_q_list as $query) {
  106. if ($only_test) {
  107. error_log("Database::query($dbStatsForm,$query)", 0);
  108. } else {
  109. $res = Database::query($query);
  110. if ($log) {
  111. error_log("In $dbStatsForm, executed: $query", 0);
  112. }
  113. if ($res === false) {
  114. error_log('Error in '.$query.': '.Database::error());
  115. }
  116. }
  117. }
  118. }
  119. }
  120. //Moving Stats DB to the main database
  121. $stats_table = array(
  122. "track_c_browsers",
  123. "track_c_countries",
  124. "track_c_os",
  125. "track_c_providers",
  126. "track_c_referers",
  127. "track_e_access",
  128. "track_e_attempt",
  129. "track_e_attempt_recording",
  130. "track_e_course_access",
  131. "track_e_default",
  132. "track_e_downloads",
  133. "track_e_exercices",
  134. "track_e_hotpotatoes",
  135. "track_e_hotspot",
  136. "track_e_item_property",
  137. "track_e_lastaccess",
  138. "track_e_links",
  139. "track_e_login",
  140. "track_e_online",
  141. "track_e_open",
  142. "track_e_uploads",
  143. "report_keys", //@todo add the "track_" prefix see #3967
  144. "report_values",
  145. "report_keys",
  146. "stored_values",
  147. "stored_values_stack",
  148. );
  149. if ($dbNameForm != $dbStatsForm) {
  150. Database::select_db($dbStatsForm);
  151. foreach($stats_table as $stat_table) {
  152. $sql = "ALTER TABLE $dbStatsForm.$stat_table RENAME $dbNameForm.$stat_table";
  153. Database::query($sql);
  154. }
  155. Database::select_db($dbNameForm);
  156. }
  157. // Get the user queries list (u_q_list)
  158. $u_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'user');
  159. if (count($u_q_list) > 0) {
  160. // Now use the $u_q_list
  161. /**
  162. * We connect to the right DB first to make sure we can use the queries
  163. * without a database name
  164. */
  165. if (strlen($dbUserForm) > 40) {
  166. error_log('Database name '.$dbUserForm.' is too long, skipping', 0);
  167. } elseif (!in_array($dbUserForm,$dblist)) {
  168. error_log('Database '.$dbUserForm.' was not found, skipping', 0);
  169. } else {
  170. Database::select_db($dbUserForm);
  171. foreach ($u_q_list as $query) {
  172. if ($only_test) {
  173. error_log("Database::query($dbUserForm,$query)", 0);
  174. error_log("In $dbUserForm, executed: $query", 0);
  175. } else {
  176. $res = Database::query($query);
  177. if ($res === false) {
  178. error_log('Error in '.$query.': '.Database::error());
  179. }
  180. }
  181. }
  182. }
  183. }
  184. //Moving User DB to the main database
  185. $users_table = array(
  186. "personal_agenda",
  187. "personal_agenda_repeat",
  188. "personal_agenda_repeat_not",
  189. "user_course_category"
  190. );
  191. if ($dbNameForm != $dbUserForm) {
  192. Database::select_db($dbUserForm);
  193. foreach($users_table as $table) {
  194. $sql = "ALTER TABLE $dbUserForm.$table RENAME $dbNameForm.$table";
  195. Database::query($sql);
  196. }
  197. Database::select_db($dbNameForm);
  198. }
  199. }
  200. //Adds the c_XXX courses tables see #3910
  201. require_once api_get_path(LIBRARY_PATH).'add_course.lib.inc.php';
  202. global $_configuration;
  203. update_Db_course();
  204. $prefix = '';
  205. if ($singleDbForm) {
  206. $prefix = get_config_param('table_prefix');
  207. }
  208. // Get the courses databases queries list (c_q_list)
  209. $c_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'course');
  210. error_log('Starting migration: '.$old_file_version.' - '.$new_file_version);
  211. if (count($c_q_list) > 0) {
  212. // Get the courses list
  213. if (strlen($dbNameForm) > 40) {
  214. error_log('Database name '.$dbNameForm.' is too long, skipping', 0);
  215. } elseif(!in_array($dbNameForm, $dblist)) {
  216. error_log('Database '.$dbNameForm.' was not found, skipping', 0);
  217. } else {
  218. Database::select_db($dbNameForm);
  219. $res = Database::query("SELECT id, code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL ORDER BY code");
  220. if ($res === false) { die('Error while querying the courses list in update_db-1.8.6.2-1.8.7.inc.php'); }
  221. $errors = array();
  222. //$filename = api_get_path(SYS_ARCHIVE_PATH).'migration_report.log';
  223. //$time = date(); file_put_contents($filename, "File creation at $time ");
  224. if (Database::num_rows($res) > 0) {
  225. $i = 0;
  226. $list = array();
  227. while ($row = Database::fetch_array($res)) {
  228. $list[] = $row;
  229. $i++;
  230. }
  231. $query_id = '';
  232. foreach ($list as $row_course) {
  233. // Now use the $c_q_list
  234. if (!$singleDbForm) { // otherwise just use the main one
  235. Database::select_db($row_course['db_name']);
  236. }
  237. foreach ($c_q_list as $query) {
  238. if ($singleDbForm) {
  239. $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);
  240. }
  241. if ($only_test) {
  242. error_log("Database::query(".$row_course['db_name'].",$query)", 0);
  243. } else {
  244. $res = Database::query($query);
  245. if ($log) {
  246. error_log("In ".$row_course['db_name'].", executed: $query", 0);
  247. }
  248. if ($res === false) {
  249. error_log('Error in '.$query.': '.Database::error());
  250. }
  251. }
  252. }
  253. //Course tables to be migrated
  254. $table_list = array(
  255. 'announcement',
  256. 'announcement_attachment',
  257. 'attendance',
  258. 'attendance_calendar',
  259. 'attendance_result',
  260. 'attendance_sheet',
  261. 'attendance_sheet_log',
  262. 'blog',
  263. 'blog_attachment',
  264. 'blog_comment',
  265. 'blog_post',
  266. 'blog_rating',
  267. 'blog_rel_user',
  268. 'blog_task',
  269. 'blog_task_rel_user',
  270. 'calendar_event',
  271. 'calendar_event_attachment',
  272. 'calendar_event_repeat',
  273. 'calendar_event_repeat_not',
  274. 'chat_connected',
  275. 'course_description',
  276. 'course_setting',
  277. 'document',
  278. 'dropbox_category',
  279. 'dropbox_feedback',
  280. 'dropbox_file',
  281. 'dropbox_person',
  282. 'dropbox_post',
  283. 'forum_attachment',
  284. 'forum_category',
  285. 'forum_forum',
  286. 'forum_mailcue',
  287. 'forum_notification',
  288. 'forum_post',
  289. 'forum_thread',
  290. 'forum_thread_qualify',
  291. 'forum_thread_qualify_log',
  292. 'glossary',
  293. 'group_category',
  294. 'group_info',
  295. 'group_rel_tutor',
  296. 'group_rel_user',
  297. 'item_property',
  298. 'link',
  299. 'link_category',
  300. 'lp',
  301. 'lp_item',
  302. 'lp_item_view',
  303. 'lp_iv_interaction',
  304. 'lp_iv_objective',
  305. 'lp_view',
  306. 'notebook',
  307. 'metadata',
  308. 'online_connected',
  309. 'online_link',
  310. 'permission_group',
  311. 'permission_task',
  312. 'permission_user',
  313. 'quiz',
  314. 'quiz_answer',
  315. 'quiz_question',
  316. 'quiz_question_option',
  317. 'quiz_rel_question',
  318. 'resource',
  319. 'role',
  320. 'role_group',
  321. 'role_permissions',
  322. 'role_user',
  323. 'student_publication',
  324. 'student_publication_assignment',
  325. 'survey',
  326. 'survey_answer',
  327. 'survey_group',
  328. 'survey_invitation',
  329. 'survey_question',
  330. 'survey_question_option',
  331. 'thematic',
  332. 'thematic_advance',
  333. 'thematic_plan',
  334. 'tool',
  335. 'tool_intro',
  336. 'userinfo_content',
  337. 'userinfo_def',
  338. 'wiki',
  339. 'wiki_conf',
  340. 'wiki_discuss',
  341. 'wiki_mailcue'
  342. );
  343. error_log('<<<------- Loading DB course '.$row_course['db_name'].' -------->>');
  344. $count = $old_count = 0;
  345. foreach($table_list as $table) {
  346. $old_table = $row_course['db_name'].".".$table;
  347. if ($singleDbForm) {
  348. $old_table = "$prefix{$row_course['db_name']}_".$table;
  349. }
  350. $course_id = $row_course['id'];
  351. $new_table = DB_COURSE_PREFIX.$table;
  352. if (!$singleDbForm) {
  353. // otherwise just use the main one
  354. Database::select_db($row_course['db_name']);
  355. } else {
  356. Database::select_db($dbNameForm);
  357. }
  358. //Count of rows
  359. $sql = "SELECT count(*) FROM $old_table";
  360. $result = Database::query($sql);
  361. $old_count = 0;
  362. if ($result) {
  363. $row = Database::fetch_row($result);
  364. $old_count = $row[0];
  365. } else {
  366. error_log("Seems that the table $old_table doesn't exists ");
  367. }
  368. error_log("#rows in $old_table: $old_count");
  369. $sql = "SELECT * FROM $old_table";
  370. $result = Database::query($sql);
  371. $count = 0;
  372. while($row = Database::fetch_array($result, 'ASSOC')) {
  373. $row['c_id'] = $course_id;
  374. Database::select_db($dbNameForm);
  375. $id = Database::insert($new_table, $row);
  376. if (is_numeric($id)) {
  377. $count++;
  378. } else {
  379. $errors[$old_table][] = $row;
  380. }
  381. }
  382. error_log("# rows inserted in $new_table: $count");
  383. if ($old_count != $count) {
  384. error_log("ERROR count of new and old table doesn't match: $old_count - $new_table");
  385. error_log("Check the results: ");
  386. error_log(print_r($errors, 1));
  387. }
  388. }
  389. error_log('<<<------- end -------->>');
  390. //error
  391. /*
  392. //Adding all_day to the calendar event table
  393. $calendar_event_table = $row_course['db_name'].".calendar_event";
  394. if ($singleDbForm) {
  395. $calendar_event_table = "$prefix{$row_course['db_name']}_calendar_event";
  396. }
  397. $query = "ALTER TABLE `".$calendar_event_table."` ADD COLUMN all_day INTEGER NOT NULL DEFAULT 0;";
  398. $res = Database::query($query);
  399. if ($res === false) {
  400. error_log('Error in '.$query.': '.Database::error());
  401. } */
  402. }
  403. }
  404. }
  405. }
  406. } else {
  407. echo 'You are not allowed here !';
  408. }