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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2007 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) various contributors
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  17. Mail: info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. ==============================================================================
  22. * Update the Dokeos database from an older version
  23. * Notice : This script has to be included by index.php or update_courses.php
  24. *
  25. * @package dokeos.install
  26. * @todo
  27. * - conditional changing of tables. Currently we execute for example
  28. * ALTER TABLE `$dbNameForm`.`cours` instructions without checking wether this is necessary.
  29. * - reorganise code into functions
  30. * @todo use database library
  31. ==============================================================================
  32. */
  33. //load helper functions
  34. require_once("install_upgrade.lib.php");
  35. /*
  36. ==============================================================================
  37. MAIN CODE
  38. ==============================================================================
  39. */
  40. //check if we come from index.php or update_courses.php - otherwise display error msg
  41. if (defined('DOKEOS_INSTALL') || defined('DOKEOS_COURSE_UPDATE'))
  42. {
  43. //check if the current Dokeos install is elligible for update
  44. if (empty ($updateFromConfigFile) || !file_exists($_POST['updatePath'].$updateFromConfigFile) || !in_array(get_config_param('clarolineVersion'), $update_from_version))
  45. {
  46. echo '<b>'.get_lang('Error').' !</b> Dokeos '.implode('|', $updateFromVersion).' '.get_lang('HasNotBeenFound').'.<br><br>
  47. '.get_lang('PleasGoBackToStep1').'.
  48. <p><input type="submit" name="step1" value="&lt; '.get_lang('Back').'"></p>
  49. </td></tr></table></form></body></html>';
  50. exit ();
  51. }
  52. //get_config_param() comes from install_functions.inc.php and
  53. //actually gets the param from
  54. $_configuration['db_glue'] = get_config_param('dbGlu');
  55. if ($singleDbForm)
  56. {
  57. $_configuration['table_prefix'] = get_config_param('courseTablePrefix');
  58. }
  59. $dbScormForm = eregi_replace('[^a-z0-9_-]', '', $dbScormForm);
  60. if (!empty ($dbPrefixForm) && !ereg('^'.$dbPrefixForm, $dbScormForm))
  61. {
  62. $dbScormForm = $dbPrefixForm.$dbScormForm;
  63. }
  64. if (empty ($dbScormForm) || $dbScormForm == 'mysql' || $dbScormForm == $dbPrefixForm)
  65. {
  66. $dbScormForm = $dbPrefixForm.'scorm';
  67. }
  68. @mysql_connect($dbHostForm, $dbUsernameForm, $dbPassForm);
  69. //if error on connection to the database, show error and exit
  70. if (mysql_errno() > 0)
  71. {
  72. $no = mysql_errno();
  73. $msg = mysql_error();
  74. echo '<hr>['.$no.'] - '.$msg.'<hr>
  75. '.get_lang('DBServerDoesntWorkOrLoginPassIsWrong').'.<br><br>
  76. '.get_lang('PleaseCheckTheseValues').' :<br><br>
  77. <b>'.get_lang('DBHost').'</b> : '.$dbHostForm.'<br>
  78. <b>'.get_lang('DBLogin').'</b> : '.$dbUsernameForm.'<br>
  79. <b>'.get_lang('DBPassword').'</b> : '.$dbPassForm.'<br><br>
  80. '.get_lang('PleaseGoBackToStep').' '. (defined('DOKEOS_INSTALL') ? '3' : '1').'.
  81. <p><input type="submit" name="step'. (defined('DOKEOS_INSTALL') ? '3' : '1').'" value="&lt; '.get_lang('Back').'"></p>
  82. </td></tr></table></form></body></html>';
  83. exit ();
  84. }
  85. /*
  86. -----------------------------------------------------------
  87. Normal upgrade procedure:
  88. start by updating main, statistic, user databases
  89. -----------------------------------------------------------
  90. */
  91. //if this script has been included by index.php, not update_courses.php, so
  92. // that we want to change the main databases as well...
  93. $only_test = false;
  94. if (defined('DOKEOS_INSTALL'))
  95. {
  96. if ($singleDbForm)
  97. {
  98. $dbStatsForm = $dbNameForm;
  99. $dbScormForm = $dbNameForm;
  100. $dbUserForm = $dbNameForm;
  101. }
  102. /**
  103. * Update the databases "pre" migration
  104. */
  105. include ("../lang/english/create_course.inc.php");
  106. if ($languageForm != 'english')
  107. {
  108. //languageForm has been escaped in index.php
  109. include ("../lang/$languageForm/create_course.inc.php");
  110. }
  111. //get the main queries list (m_q_list)
  112. $m_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','main');
  113. if(count($m_q_list)>0)
  114. {
  115. //now use the $m_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. mysql_select_db($dbNameForm);
  121. foreach($m_q_list as $query){
  122. if($only_test){
  123. error_log("mysql_query($dbNameForm,$query)",0);
  124. }else{
  125. $res = mysql_query($query);
  126. error_log("In $dbNameForm, executed: $query",0);
  127. }
  128. }
  129. }
  130. //get the stats queries list (s_q_list)
  131. $s_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','stats');
  132. if(count($s_q_list)>0)
  133. {
  134. //now use the $s_q_list
  135. /**
  136. * We connect to the right DB first to make sure we can use the queries
  137. * without a database name
  138. */
  139. mysql_select_db($dbStatsForm);
  140. foreach($s_q_list as $query){
  141. if($only_test){
  142. error_log("mysql_query($dbStatsForm,$query)",0);
  143. }else{
  144. $res = mysql_query($query);
  145. error_log("In $dbStatsForm, executed: $query",0);
  146. }
  147. }
  148. }
  149. //get the user queries list (u_q_list)
  150. $u_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','user');
  151. if(count($u_q_list)>0)
  152. {
  153. //now use the $u_q_list
  154. /**
  155. * We connect to the right DB first to make sure we can use the queries
  156. * without a database name
  157. */
  158. mysql_select_db($dbUserForm);
  159. foreach($u_q_list as $query){
  160. if($only_test){
  161. error_log("mysql_query($dbUserForm,$query)",0);
  162. error_log("In $dbUserForm, executed: $query",0);
  163. }else{
  164. $res = mysql_query($query);
  165. }
  166. }
  167. }
  168. //the SCORM database doesn't need a change in the pre-migrate part - ignore
  169. }
  170. /*
  171. -----------------------------------------------------------
  172. Update the Dokeos course databases
  173. this part can be accessed in two ways:
  174. - from the normal upgrade process
  175. - from the script update_courses.php,
  176. which is used to upgrade more than MAX_COURSE_TRANSFER courses
  177. Every time this script is accessed, only
  178. MAX_COURSE_TRANSFER courses are upgraded.
  179. -----------------------------------------------------------
  180. */
  181. $prefix = '';
  182. if ($singleDbForm)
  183. {
  184. $prefix = $_configuration['table_prefix'];
  185. }
  186. //get the courses databases queries list (c_q_list)
  187. $c_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-pre.sql','course');
  188. if(count($c_q_list)>0)
  189. {
  190. //get the courses list
  191. mysql_select_db($dbNameForm);
  192. $res = mysql_query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
  193. if($res===false){die('Error while querying the courses list in update_db.inc.php');}
  194. if(mysql_num_rows($res)>0)
  195. {
  196. $i=0;
  197. //while( ($i < MAX_COURSE_TRANSFER) && ($row = mysql_fetch_array($res)))
  198. while($row = mysql_fetch_array($res))
  199. {
  200. $list[] = $row;
  201. $i++;
  202. }
  203. foreach($list as $row)
  204. {
  205. //now use the $c_q_list
  206. /**
  207. * We connect to the right DB first to make sure we can use the queries
  208. * without a database name
  209. */
  210. if (!$singleDbForm) //otherwise just use the main one
  211. {
  212. mysql_select_db($row['db_name']);
  213. }
  214. foreach($c_q_list as $query)
  215. {
  216. if ($singleDbForm) //otherwise just use the main one
  217. {
  218. $query = preg_replace('/^(UPDATE|ALTER TABLE|CREATE TABLE|DROP TABLE|INSERT INTO|DELETE FROM)\s+(\w*)(.*)$/',"$1 $prefix$2$3",$query);
  219. }
  220. if($only_test)
  221. {
  222. error_log("mysql_query(".$row['db_name'].",$query)",0);
  223. }else{
  224. $res = mysql_query($query);
  225. error_log("In ".$row['db_name'].", executed: $query",0);
  226. }
  227. }
  228. //update course manually
  229. //update group_category.forum_state ?
  230. //update group_info.tutor_id (put it in group_tutor table?) ?
  231. //update group_info.forum_state, forum_id ?
  232. //update forum tables (migrate from bb_ tables to forum_ tables)
  233. //migrate categories
  234. $sql_orig = "SELECT * FROM ".$prefix."bb_categories";
  235. $res_orig = mysql_query($sql_orig);
  236. $order = 1;
  237. while($row = mysql_fetch_array($res_orig)){
  238. $myorder = (empty($row['cat_order'])?$order:$row['cat_order']);
  239. $sql = "INSERT INTO ".$prefix."forum_category " .
  240. "(cat_id,cat_title,cat_comment,cat_order,locked) VALUES " .
  241. "('".$row['cat_id']."','".mysql_real_escape_string($row['cat_title'])."','','".$myorder."',0)";
  242. $res = mysql_query($sql);
  243. $lastcatid = mysql_insert_id();
  244. error_log($sql,0);
  245. $order ++;
  246. //add item_property - forum categories were not put into item_properties before
  247. $sql = "INSERT INTO ".$prefix."item_property (tool,insert_user_id,ref,lastedit_type,lastedit_user_id,visibility) " .
  248. "VALUES ('forum_category','1','$lastcatid','ForumCategoryAdded','1','1')";
  249. $res = mysql_query($sql);
  250. error_log($sql,0);
  251. }
  252. $sql_orig = "SELECT * FROM ".$prefix."bb_forums ORDER BY forum_last_post_id desc";
  253. $res_orig = mysql_query($sql_orig);
  254. $order = 1;
  255. while($row = mysql_fetch_array($res_orig)){
  256. $sql = "INSERT INTO ".$prefix."forum_forum " .
  257. "(forum_id,forum_category,allow_edit,forum_comment," .
  258. "forum_title," .
  259. "forum_last_post, forum_threads," .
  260. "locked, forum_posts, " .
  261. "allow_new_threads, forum_order) VALUES " .
  262. "('".$row['forum_id']."','".$row['cat_id']."',1,'".mysql_real_escape_string($row['forum_desc'])."'," .
  263. "'".mysql_real_escape_string($row['forum_name'])."'," .
  264. "'".$row['forum_last_post_id']."','".$row['forum_topics']."'," .
  265. "0,'".$row['forum_posts']."'," .
  266. "1,$order)";
  267. error_log($sql,0);
  268. $res = mysql_query($sql);
  269. $lastforumid = mysql_insert_id();
  270. $order++;
  271. //add item_property - forums were not put into item_properties before
  272. $sql = "INSERT INTO ".$prefix."item_property (tool,insert_user_id,ref,lastedit_type,lastedit_user_id,visibility) " .
  273. "VALUES ('forum','1','$lastforumid','ForumAdded','1','1')";
  274. $res = mysql_query($sql);
  275. error_log($sql,0);
  276. }
  277. $sql_orig = "SELECT * FROM ".$prefix."bb_topics";
  278. $res_orig = mysql_query($sql_orig);
  279. while($row = mysql_fetch_array($res_orig)){
  280. //convert time from varchar to datetime
  281. $time = $row['topic_time'];
  282. $poster_id = ($row['topic_poster']==-1?1:$row['topic_poster']);
  283. $name = mysql_real_escape_string($row['prenom']." ".$row['nom']);
  284. $sql = "INSERT INTO ".$prefix."forum_thread " .
  285. "(thread_id,forum_id,thread_poster_id," .
  286. "locked,thread_replies,thread_sticky,thread_title," .
  287. "thread_poster_name, thread_date, thread_last_post," .
  288. "thread_views) VALUES " .
  289. "('".$row['topic_id']."','".$row['forum_id']."','".$poster_id."'," .
  290. "0,'".$row['topic_replies']."',0,'".mysql_real_escape_string($row['topic_title'])."'," .
  291. "'$name','$time','".$row['topic_last_post_id']."'," .
  292. "'".$row['topic_views']."')";
  293. error_log($sql,0);
  294. $res = mysql_query($sql);
  295. $lastthreadid = mysql_insert_id();
  296. //add item_property - forum threads were not put into item_properties before
  297. $sql = "INSERT INTO ".$prefix."item_property (tool,insert_user_id,ref,lastedit_type,lastedit_user_id,visibility) " .
  298. "VALUES ('forum_thread','1','$lastthreadid','ForumThreadAdded','1','1')";
  299. $res = mysql_query($sql);
  300. error_log($sql,0);
  301. }
  302. $sql_orig = "SELECT * FROM ".$prefix."bb_posts bp, ".$prefix."bb_posts_text bpt WHERE bp.post_id = bpt.post_id";
  303. $res_orig = mysql_query($sql_orig);
  304. while($row = mysql_fetch_array($res_orig)){
  305. //convert time from varchar to datetime
  306. $time = $row['post_time'];
  307. $poster_id = ($row['topic_poster']==-1?1:$row['topic_poster']);
  308. $name = mysql_real_escape_string($row['prenom']." ".$row['nom']);
  309. $sql = "INSERT INTO ".$prefix."forum_post " .
  310. "(post_id,forum_id,thread_id," .
  311. "poster_id,post_parent_id,visible, " .
  312. "post_title,poster_name, post_text, " .
  313. "post_date, post_notification) VALUES " .
  314. "('".$row['post_id']."','".$row['forum_id']."','".$row['topic_id']."'," .
  315. "'".$poster_id."','".$row['parent_id']."',1," .
  316. "'".mysql_real_escape_string($row['post_title'])."','$name', '".mysql_real_escape_string($row['post_text'])."'," .
  317. "'$time',0)";
  318. error_log($sql,0);
  319. $res = mysql_query($sql);
  320. $lastpostid = mysql_insert_id();
  321. //add item_property - forum threads were not put into item_properties before
  322. $sql = "INSERT INTO ".$prefix."item_property(tool,insert_user_id,ref,lastedit_type,lastedit_user_id,visibility) " .
  323. "VALUES ('forum_post','1','$lastpostid','ForumPostAdded','1','1')";
  324. $res = mysql_query($sql);
  325. error_log($sql,0);
  326. }
  327. }
  328. }
  329. }
  330. //load the old-scorm to new-scorm migration script
  331. //TODO: deal with the fact that this should only act on MAX_COURSE_TRANSFER courses
  332. if(!$only_test){
  333. include('update-db-scorm-1.6.x-1.8.0.inc.php');
  334. }
  335. if (defined('DOKEOS_INSTALL'))
  336. {
  337. if ($singleDbForm)
  338. {
  339. $dbStatsForm = $dbNameForm;
  340. $dbScormForm = $dbNameForm;
  341. $dbUserForm = $dbNameForm;
  342. }
  343. //deal with migrate-db-1.6.x-1.8.0-post.sql
  344. //get the main queries list (m_q_list)
  345. $m_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-post.sql','main');
  346. if(count($m_q_list)>0)
  347. {
  348. //now use the $m_q_list
  349. /**
  350. * We connect to the right DB first to make sure we can use the queries
  351. * without a database name
  352. */
  353. mysql_select_db($dbNameForm);
  354. foreach($m_q_list as $query){
  355. if($only_test){
  356. error_log("mysql_query($dbNameForm,$query)",0);
  357. }else{
  358. $res = mysql_query($query);
  359. error_log("In $dbNameForm, executed: $query",0);
  360. }
  361. }
  362. }
  363. //get the stats queries list (s_q_list)
  364. $s_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-post.sql','stats');
  365. if(count($s_q_list)>0)
  366. {
  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. mysql_select_db($dbStatsForm);
  373. foreach($s_q_list as $query){
  374. if($only_test){
  375. error_log("mysql_query($dbStatsForm,$query)",0);
  376. }else{
  377. $res = mysql_query($query);
  378. error_log("In $dbStatsForm, executed: $query",0);
  379. }
  380. }
  381. }
  382. //get the user queries list (u_q_list)
  383. $u_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-post.sql','user');
  384. if(count($u_q_list)>0)
  385. {
  386. //now use the $u_q_list
  387. /**
  388. * We connect to the right DB first to make sure we can use the queries
  389. * without a database name
  390. */
  391. mysql_select_db($dbUserForm);
  392. foreach($u_q_list as $query){
  393. if($only_test){
  394. error_log("mysql_query($dbUserForm,$query)",0);
  395. }else{
  396. $res = mysql_query($query);
  397. error_log("In $dbUserForm, executed: $query",0);
  398. }
  399. }
  400. }
  401. //the SCORM database should need a drop in the post-migrate part. However, we will keep these tables a bit more, just in case...
  402. }
  403. //get the courses databases queries list (c_q_list)
  404. $c_q_list = get_sql_file_contents('migrate-db-1.6.x-1.8.0-post.sql','course');
  405. if(count($c_q_list)>0)
  406. {
  407. //get the courses list
  408. mysql_select_db($dbNameForm);
  409. $res = mysql_query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
  410. if($res===false){die('Error while querying the courses list in update_db.inc.php');}
  411. if(mysql_num_rows($res)>0)
  412. {
  413. $i=0;
  414. //while( ($i < MAX_COURSE_TRANSFER) && ($row = mysql_fetch_array($res)))
  415. while($row = mysql_fetch_array($res))
  416. {
  417. $list[] = $row;
  418. $i++;
  419. }
  420. foreach($list as $row)
  421. {
  422. //now use the $c_q_list
  423. /**
  424. * We connect to the right DB first to make sure we can use the queries
  425. * without a database name
  426. */
  427. mysql_select_db($row['db_name']);
  428. foreach($c_q_list as $query)
  429. {
  430. if ($singleDbForm) //otherwise just use the main one
  431. {
  432. $query = preg_replace('/^(UPDATE|ALTER TABLE|CREATE TABLE|DROP TABLE|INSERT INTO|DELETE FROM)\s+(\w*)(.*)$/',"$1 $prefix$2$3",$query);
  433. }
  434. if($only_test)
  435. {
  436. error_log("mysql_query(".$row['db_name'].",$query)",0);
  437. }else{
  438. $res = mysql_query($query);
  439. error_log("In ".$row['db_name'].", executed: $query",0);
  440. }
  441. }
  442. }
  443. }
  444. }
  445. }
  446. else
  447. {
  448. echo 'You are not allowed here !';
  449. }
  450. ?>