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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php // $Id: update-db-1.8.4-1.8.5.inc.php 14753 2008-04-03 22:20:25Z juliomontoya $
  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><input type="submit" name="step1" value="&lt; '.get_lang('Back').'"></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><input type="submit" name="step'. (defined('DOKEOS_INSTALL') ? '3' : '1').'" value="&lt; '.get_lang('Back').'"></p>
  77. </td></tr></table></form></body></html>';
  78. exit ();
  79. }
  80. $dblistres = mysql_list_dbs();
  81. $dblist = array();
  82. while ($row = mysql_fetch_object($dblistres)) {
  83. $dblist[] = $row->Database;
  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. $log = 0;
  95. if (defined('DOKEOS_INSTALL'))
  96. {
  97. if ($singleDbForm)
  98. {
  99. $dbStatsForm = $dbNameForm;
  100. $dbScormForm = $dbNameForm;
  101. $dbUserForm = $dbNameForm;
  102. }
  103. /**
  104. * Update the databases "pre" migration
  105. */
  106. include ("../lang/english/create_course.inc.php");
  107. if ($languageForm != 'english')
  108. {
  109. //languageForm has been escaped in index.php
  110. include ("../lang/$languageForm/create_course.inc.php");
  111. }
  112. //get the main queries list (m_q_list)
  113. $m_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql','main');
  114. if(count($m_q_list)>0)
  115. {
  116. //now use the $m_q_list
  117. /**
  118. * We connect to the right DB first to make sure we can use the queries
  119. * without a database name
  120. */
  121. if(strlen($dbNameForm)>40){
  122. error_log('Database name '.$dbNameForm.' is too long, skipping',0);
  123. }elseif(!in_array($dbNameForm,$dblist)){
  124. error_log('Database '.$dbNameForm.' was not found, skipping',0);
  125. }else{
  126. mysql_select_db($dbNameForm);
  127. foreach($m_q_list as $query){
  128. if($only_test){
  129. error_log("mysql_query($dbNameForm,$query)",0);
  130. }else{
  131. $res = mysql_query($query);
  132. if($log)
  133. {
  134. error_log("In $dbNameForm, executed: $query",0);
  135. }
  136. }
  137. }
  138. }
  139. }
  140. //get the stats queries list (s_q_list)
  141. $s_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql','stats');
  142. if(count($s_q_list)>0)
  143. {
  144. //now use the $s_q_list
  145. /**
  146. * We connect to the right DB first to make sure we can use the queries
  147. * without a database name
  148. */
  149. if(strlen($dbStatsForm)>40){
  150. error_log('Database name '.$dbStatsForm.' is too long, skipping',0);
  151. }elseif(!in_array($dbStatsForm,$dblist)){
  152. error_log('Database '.$dbStatsForm.' was not found, skipping',0);
  153. }else{
  154. mysql_select_db($dbStatsForm);
  155. foreach($s_q_list as $query){
  156. if($only_test){
  157. error_log("mysql_query($dbStatsForm,$query)",0);
  158. }else{
  159. $res = mysql_query($query);
  160. if($log)
  161. {
  162. error_log("In $dbStatsForm, executed: $query",0);
  163. }
  164. }
  165. }
  166. }
  167. }
  168. //get the user queries list (u_q_list)
  169. $u_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql','user');
  170. if(count($u_q_list)>0)
  171. {
  172. //now use the $u_q_list
  173. /**
  174. * We connect to the right DB first to make sure we can use the queries
  175. * without a database name
  176. */
  177. if(strlen($dbUserForm)>40){
  178. error_log('Database name '.$dbUserForm.' is too long, skipping',0);
  179. }elseif(!in_array($dbUserForm,$dblist)){
  180. error_log('Database '.$dbUserForm.' was not found, skipping',0);
  181. }else{
  182. mysql_select_db($dbUserForm);
  183. foreach($u_q_list as $query){
  184. if($only_test){
  185. error_log("mysql_query($dbUserForm,$query)",0);
  186. error_log("In $dbUserForm, executed: $query",0);
  187. }else{
  188. $res = mysql_query($query);
  189. }
  190. }
  191. }
  192. }
  193. //the SCORM database doesn't need a change in the pre-migrate part - ignore
  194. }
  195. /*
  196. -----------------------------------------------------------
  197. Update the Dokeos course databases
  198. this part can be accessed in two ways:
  199. - from the normal upgrade process
  200. - from the script update_courses.php,
  201. which is used to upgrade more than MAX_COURSE_TRANSFER courses
  202. Every time this script is accessed, only
  203. MAX_COURSE_TRANSFER courses are upgraded.
  204. -----------------------------------------------------------
  205. */
  206. $prefix = '';
  207. if ($singleDbForm)
  208. {
  209. $prefix = get_config_param ('table_prefix');
  210. }
  211. //get the courses databases queries list (c_q_list)
  212. $c_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql','course');
  213. if(count($c_q_list)>0)
  214. {
  215. //get the courses list
  216. if(strlen($dbNameForm)>40)
  217. {
  218. error_log('Database name '.$dbNameForm.' is too long, skipping',0);
  219. }
  220. elseif(!in_array($dbNameForm,$dblist))
  221. {
  222. error_log('Database '.$dbNameForm.' was not found, skipping',0);
  223. }
  224. else
  225. {
  226. mysql_select_db($dbNameForm);
  227. $res = mysql_query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
  228. if($res===false){die('Error while querying the courses list in update_db.inc.php');}
  229. if(mysql_num_rows($res)>0)
  230. {
  231. $i=0;
  232. //while( ($i < MAX_COURSE_TRANSFER) && ($row = mysql_fetch_array($res)))
  233. while($row = mysql_fetch_array($res))
  234. {
  235. $list[] = $row;
  236. $i++;
  237. }
  238. foreach($list as $row_course)
  239. {
  240. //now use the $c_q_list
  241. /**
  242. * We connect to the right DB first to make sure we can use the queries
  243. * without a database name
  244. */
  245. if (!$singleDbForm) //otherwise just use the main one
  246. {
  247. mysql_select_db($row_course['db_name']);
  248. }
  249. foreach($c_q_list as $query)
  250. {
  251. if ($singleDbForm) //otherwise just use the main one
  252. {
  253. $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);
  254. }
  255. if($only_test)
  256. {
  257. error_log("mysql_query(".$row_course['db_name'].",$query)",0);
  258. }
  259. else
  260. {
  261. $res = mysql_query($query);
  262. if($log)
  263. {
  264. error_log("In ".$row_course['db_name'].", executed: $query",0);
  265. }
  266. }
  267. }
  268. $mytable = $row_course['db_name'].".lp_item";
  269. if($singleDbForm)
  270. {
  271. $mytable = "$prefix{$row_course['db_name']}_lp_item";
  272. }
  273. $mysql = "SELECT * FROM $mytable WHERE min_score != 0 AND prerequisite != ''";
  274. $myres = mysql_query($query);
  275. if($myres!==false && mysql_num_rows($myres)>0)
  276. {
  277. while($myrow = mysql_fetch_array($myres))
  278. {
  279. if(is_numeric($myrow['prerequisite']))
  280. {
  281. $mysql2 = "UPDATE $mytable SET mastery_score = '".$myrow['min_score']."' WHERE id = '".$myrow['prerequisite']."'";
  282. $myres2 = mysql_query($mysql2);
  283. //echo $mysql2."<br />";
  284. if($myres2 !== false)
  285. {
  286. $mysql3 = "UPDATE $mytable SET min_score = 0 WHERE id = '".$myrow['id']."'";
  287. $myres3 = mysql_query($mysql3);
  288. //echo $mysql3."<br />";
  289. }
  290. }
  291. }
  292. }
  293. // Work Tool Folder Update
  294. // we search into DB all the folders in the work tool
  295. if($singleDbForm)
  296. {
  297. $my_course_table = "$prefix{$row_course['db_name']}_student_publication";
  298. }
  299. else
  300. {
  301. $my_course_table = $row_course['db_name'].".student_publication";
  302. }
  303. $sys_course_path = $_configuration['root_sys'].$_configuration['course_folder'];
  304. $course_dir=$sys_course_path.$row_course['directory'].'/work';
  305. $dir_to_array =directory_to_array($course_dir,true);
  306. $only_dir=array();
  307. $sql_select= "SELECT filetype FROM " . $my_course_table . " WHERE filetype = 'folder'";
  308. $result = mysql_query($sql_select);
  309. $num_row=mysql_num_rows($result);
  310. // check if there are already folder registered
  311. if ($num_row == 0)
  312. {
  313. for($i=0;$i<count($dir_to_array);$i++)
  314. {
  315. $only_dir[]=substr($dir_to_array[$i],strlen($course_dir), strlen($dir_to_array[$i]));
  316. }
  317. for($i=0;$i<count($only_dir);$i++)
  318. {
  319. $sql_insert_all= "INSERT INTO " . $my_course_table . " SET url = '" . $only_dir[$i] . "', " .
  320. "title = '',
  321. description = '',
  322. author = '',
  323. active = '0',
  324. accepted = '1',
  325. filetype = 'folder',
  326. post_group_id = '0',
  327. sent_date = '0000-00-00 00:00:00' ";
  328. mysql_query($sql_insert_all);
  329. }
  330. }
  331. }
  332. }
  333. }
  334. }
  335. }
  336. else
  337. {
  338. echo 'You are not allowed here !';
  339. }
  340. ?>