update-db-1.8.4-1.8.5.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php // $Id: update-db-1.8.4-1.8.5.inc.php 19471 2009-03-31 23:28:53Z cvargas1 $
  2. /* See license terms in /dokeos_license.txt */
  3. /**
  4. ==============================================================================
  5. * Update the Dokeos database from an older version
  6. * Notice : This script has to be included by index.php or update_courses.php
  7. *
  8. * @package dokeos.install
  9. * @todo
  10. * - conditional changing of tables. Currently we execute for example
  11. * ALTER TABLE `$dbNameForm`.`cours` instructions without checking wether this is necessary.
  12. * - reorganise code into functions
  13. * @todo use database library
  14. ==============================================================================
  15. */
  16. //load helper functions
  17. require_once("install_upgrade.lib.php");
  18. $old_file_version = '1.8.4';
  19. $new_file_version = '1.8.5';
  20. //remove memory and time limits as much as possible as this might be a long process...
  21. if(function_exists('ini_set'))
  22. {
  23. ini_set('memory_limit',-1);
  24. ini_set('max_execution_time',0);
  25. }else{
  26. error_log('Update-db script: could not change memory and time limits',0);
  27. }
  28. /*
  29. ==============================================================================
  30. MAIN CODE
  31. ==============================================================================
  32. */
  33. //check if we come from index.php or update_courses.php - otherwise display error msg
  34. if (defined('DOKEOS_INSTALL') || defined('DOKEOS_COURSE_UPDATE'))
  35. {
  36. //check if the current Dokeos install is elligible for update
  37. if (!file_exists('../inc/conf/configuration.php'))
  38. {
  39. echo '<b>'.get_lang('Error').' !</b> Dokeos '.implode('|', $updateFromVersion).' '.get_lang('HasNotBeenFound').'.<br><br>
  40. '.get_lang('PleasGoBackToStep1').'.
  41. <p><button type="submit" class="back" name="step1" value="&lt; '.get_lang('Back').'">'.get_lang('Back').'</button></p>
  42. </td></tr></table></form></body></html>';
  43. exit ();
  44. }
  45. //get_config_param() comes from install_functions.inc.php and
  46. //actually gets the param from
  47. $_configuration['db_glue'] = get_config_param('dbGlu');
  48. if ($singleDbForm)
  49. {
  50. $_configuration['table_prefix'] = get_config_param('courseTablePrefix');
  51. $_configuration['main_database'] = get_config_param('mainDbName');
  52. $_configuration['db_prefix'] = get_config_param('dbNamePrefix');
  53. }
  54. $dbScormForm = eregi_replace('[^a-z0-9_-]', '', $dbScormForm);
  55. if (!empty ($dbPrefixForm) && !ereg('^'.$dbPrefixForm, $dbScormForm))
  56. {
  57. $dbScormForm = $dbPrefixForm.$dbScormForm;
  58. }
  59. if (empty ($dbScormForm) || $dbScormForm == 'mysql' || $dbScormForm == $dbPrefixForm)
  60. {
  61. $dbScormForm = $dbPrefixForm.'scorm';
  62. }
  63. $res = @mysql_connect($dbHostForm, $dbUsernameForm, $dbPassForm);
  64. //if error on connection to the database, show error and exit
  65. if ($res === false)
  66. {
  67. //$no = mysql_errno();
  68. //$msg = mysql_error();
  69. //echo '<hr>['.$no.'] - '.$msg.'<hr>';
  70. echo get_lang('DBServerDoesntWorkOrLoginPassIsWrong').'.<br /><br />' .
  71. ' '.get_lang('PleaseCheckTheseValues').' :<br /><br />
  72. <b>'.get_lang('DBHost').'</b> : '.$dbHostForm.'<br />
  73. <b>'.get_lang('DBLogin').'</b> : '.$dbUsernameForm.'<br />
  74. <b>'.get_lang('DBPassword').'</b> : '.$dbPassForm.'<br /><br />
  75. '.get_lang('PleaseGoBackToStep').' '. (defined('DOKEOS_INSTALL') ? '3' : '1').'.
  76. <p><button type="submit" class="back" name="step'. (defined('DOKEOS_INSTALL') ? '3' : '1').'" value="&lt; '.get_lang('Back').'">'.get_lang('Back').'</button></p>
  77. </td></tr></table></form></body></html>';
  78. exit ();
  79. }
  80. // The Dokeos system has not been designed to use special SQL modes that were introduced since MySQL 5
  81. @mysql_query("set session sql_mode='';");
  82. $dblistres = mysql_list_dbs();
  83. $dblist = array();
  84. while ($row = mysql_fetch_object($dblistres)) {
  85. $dblist[] = $row->Database;
  86. }
  87. /*
  88. -----------------------------------------------------------
  89. Normal upgrade procedure:
  90. start by updating main, statistic, user databases
  91. -----------------------------------------------------------
  92. */
  93. //if this script has been included by index.php, not update_courses.php, so
  94. // that we want to change the main databases as well...
  95. $only_test = false;
  96. $log = 0;
  97. if (defined('DOKEOS_INSTALL'))
  98. {
  99. if ($singleDbForm)
  100. {
  101. $dbStatsForm = $dbNameForm;
  102. $dbScormForm = $dbNameForm;
  103. $dbUserForm = $dbNameForm;
  104. }
  105. /**
  106. * Update the databases "pre" migration
  107. */
  108. include ("../lang/english/create_course.inc.php");
  109. if ($languageForm != 'english')
  110. {
  111. //languageForm has been escaped in index.php
  112. include ("../lang/$languageForm/create_course.inc.php");
  113. }
  114. //get the main queries list (m_q_list)
  115. $m_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql','main');
  116. if(count($m_q_list)>0)
  117. {
  118. //now use the $m_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($dbNameForm)>40){
  124. error_log('Database name '.$dbNameForm.' is too long, skipping',0);
  125. }elseif(!in_array($dbNameForm,$dblist)){
  126. error_log('Database '.$dbNameForm.' was not found, skipping',0);
  127. }else{
  128. mysql_select_db($dbNameForm);
  129. foreach($m_q_list as $query){
  130. if($only_test){
  131. error_log("mysql_query($dbNameForm,$query)",0);
  132. }else{
  133. $res = mysql_query($query);
  134. if($log)
  135. {
  136. error_log("In $dbNameForm, executed: $query",0);
  137. }
  138. }
  139. }
  140. }
  141. }
  142. //get the stats queries list (s_q_list)
  143. $s_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql','stats');
  144. if(count($s_q_list)>0)
  145. {
  146. //now use the $s_q_list
  147. /**
  148. * We connect to the right DB first to make sure we can use the queries
  149. * without a database name
  150. */
  151. if(strlen($dbStatsForm)>40){
  152. error_log('Database name '.$dbStatsForm.' is too long, skipping',0);
  153. }elseif(!in_array($dbStatsForm,$dblist)){
  154. error_log('Database '.$dbStatsForm.' was not found, skipping',0);
  155. }else{
  156. mysql_select_db($dbStatsForm);
  157. foreach($s_q_list as $query){
  158. if($only_test){
  159. error_log("mysql_query($dbStatsForm,$query)",0);
  160. }else{
  161. $res = mysql_query($query);
  162. if($log)
  163. {
  164. error_log("In $dbStatsForm, executed: $query",0);
  165. }
  166. }
  167. }
  168. }
  169. }
  170. //get the user queries list (u_q_list)
  171. $u_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql','user');
  172. if(count($u_q_list)>0)
  173. {
  174. //now use the $u_q_list
  175. /**
  176. * We connect to the right DB first to make sure we can use the queries
  177. * without a database name
  178. */
  179. if(strlen($dbUserForm)>40){
  180. error_log('Database name '.$dbUserForm.' is too long, skipping',0);
  181. }elseif(!in_array($dbUserForm,$dblist)){
  182. error_log('Database '.$dbUserForm.' was not found, skipping',0);
  183. }else{
  184. mysql_select_db($dbUserForm);
  185. foreach($u_q_list as $query){
  186. if($only_test){
  187. error_log("mysql_query($dbUserForm,$query)",0);
  188. error_log("In $dbUserForm, executed: $query",0);
  189. }else{
  190. $res = mysql_query($query);
  191. }
  192. }
  193. }
  194. }
  195. //the SCORM database doesn't need a change in the pre-migrate part - ignore
  196. }
  197. /*
  198. -----------------------------------------------------------
  199. Update the Dokeos course databases
  200. this part can be accessed in two ways:
  201. - from the normal upgrade process
  202. - from the script update_courses.php,
  203. which is used to upgrade more than MAX_COURSE_TRANSFER courses
  204. Every time this script is accessed, only
  205. MAX_COURSE_TRANSFER courses are upgraded.
  206. -----------------------------------------------------------
  207. */
  208. $prefix = '';
  209. if ($singleDbForm)
  210. {
  211. $prefix = get_config_param ('table_prefix');
  212. }
  213. //get the courses databases queries list (c_q_list)
  214. $c_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql','course');
  215. if(count($c_q_list)>0)
  216. {
  217. //get the courses list
  218. if(strlen($dbNameForm)>40)
  219. {
  220. error_log('Database name '.$dbNameForm.' is too long, skipping',0);
  221. }
  222. elseif(!in_array($dbNameForm,$dblist))
  223. {
  224. error_log('Database '.$dbNameForm.' was not found, skipping',0);
  225. }
  226. else
  227. {
  228. mysql_select_db($dbNameForm);
  229. $res = mysql_query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL ORDER BY code");
  230. if($res===false){die('Error while querying the courses list in update_db.inc.php');}
  231. if(mysql_num_rows($res)>0)
  232. {
  233. $i=0;
  234. $list = array();
  235. //while( ($i < MAX_COURSE_TRANSFER) && ($row = mysql_fetch_array($res)))
  236. while($row = mysql_fetch_array($res))
  237. {
  238. $list[] = $row;
  239. $i++;
  240. }
  241. foreach($list as $row_course)
  242. {
  243. //now use the $c_q_list
  244. /**
  245. * We connect to the right DB first to make sure we can use the queries
  246. * without a database name
  247. */
  248. if (!$singleDbForm) //otherwise just use the main one
  249. {
  250. mysql_select_db($row_course['db_name']);
  251. }
  252. foreach($c_q_list as $query)
  253. {
  254. if ($singleDbForm) //otherwise just use the main one
  255. {
  256. $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);
  257. }
  258. if($only_test)
  259. {
  260. error_log("mysql_query(".$row_course['db_name'].",$query)",0);
  261. }
  262. else
  263. {
  264. $res = mysql_query($query);
  265. if($log)
  266. {
  267. error_log("In ".$row_course['db_name'].", executed: $query",0);
  268. }
  269. }
  270. }
  271. $mytable = $row_course['db_name'].".lp_item";
  272. if($singleDbForm)
  273. {
  274. $mytable = "$prefix{$row_course['db_name']}_lp_item";
  275. }
  276. $mysql = "SELECT * FROM $mytable WHERE min_score != 0 AND prerequisite != ''";
  277. $myres = mysql_query($query);
  278. if($myres!==false && mysql_num_rows($myres)>0)
  279. {
  280. while($myrow = mysql_fetch_array($myres))
  281. {
  282. if(is_numeric($myrow['prerequisite']))
  283. {
  284. $mysql2 = "UPDATE $mytable SET mastery_score = '".$myrow['min_score']."' WHERE id = '".$myrow['prerequisite']."'";
  285. $myres2 = mysql_query($mysql2);
  286. //echo $mysql2."<br />";
  287. if($myres2 !== false)
  288. {
  289. $mysql3 = "UPDATE $mytable SET min_score = 0 WHERE id = '".$myrow['id']."'";
  290. $myres3 = mysql_query($mysql3);
  291. //echo $mysql3."<br />";
  292. }
  293. }
  294. }
  295. }
  296. // Work Tool Folder Update
  297. // we search into DB all the folders in the work tool
  298. if($singleDbForm)
  299. {
  300. $my_course_table = "$prefix{$row_course['db_name']}_student_publication";
  301. }
  302. else
  303. {
  304. $my_course_table = $row_course['db_name'].".student_publication";
  305. }
  306. $sys_course_path = $_configuration['root_sys'].$_configuration['course_folder'];
  307. $course_dir=$sys_course_path.$row_course['directory'].'/work';
  308. $dir_to_array = my_directory_to_array($course_dir,true);
  309. $only_dir=array();
  310. $sql_select= "SELECT filetype FROM " . $my_course_table . " WHERE filetype = 'folder'";
  311. $result = mysql_query($sql_select);
  312. $num_row=mysql_num_rows($result);
  313. // check if there are already folder registered
  314. if ($num_row == 0)
  315. {
  316. for($i=0;$i<count($dir_to_array);$i++)
  317. {
  318. $only_dir[]=substr($dir_to_array[$i],strlen($course_dir), strlen($dir_to_array[$i]));
  319. }
  320. for($i=0;$i<count($only_dir);$i++)
  321. {
  322. $sql_insert_all= "INSERT INTO " . $my_course_table . " SET url = '" . $only_dir[$i] . "', " .
  323. "title = '',
  324. description = '',
  325. author = '',
  326. active = '0',
  327. accepted = '1',
  328. filetype = 'folder',
  329. post_group_id = '0',
  330. sent_date = '0000-00-00 00:00:00' ";
  331. mysql_query($sql_insert_all);
  332. }
  333. }
  334. }
  335. }
  336. }
  337. }
  338. }
  339. else
  340. {
  341. echo 'You are not allowed here !';
  342. }
  343. ?>