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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. $old_file_version = '1.8.4';
  36. $new_file_version = '1.8.5';
  37. //remove memory and time limits as much as possible as this might be a long process...
  38. if(function_exists('ini_set'))
  39. {
  40. ini_set('memory_limit',-1);
  41. ini_set('max_execution_time',0);
  42. }else{
  43. error_log('Update-db script: could not change memory and time limits',0);
  44. }
  45. /*
  46. ==============================================================================
  47. MAIN CODE
  48. ==============================================================================
  49. */
  50. //check if we come from index.php or update_courses.php - otherwise display error msg
  51. if (defined('DOKEOS_INSTALL') || defined('DOKEOS_COURSE_UPDATE'))
  52. {
  53. //check if the current Dokeos install is elligible for update
  54. if (!file_exists('../inc/conf/configuration.php'))
  55. {
  56. echo '<b>'.get_lang('Error').' !</b> Dokeos '.implode('|', $updateFromVersion).' '.get_lang('HasNotBeenFound').'.<br><br>
  57. '.get_lang('PleasGoBackToStep1').'.
  58. <p><input type="submit" name="step1" value="&lt; '.get_lang('Back').'"></p>
  59. </td></tr></table></form></body></html>';
  60. exit ();
  61. }
  62. //get_config_param() comes from install_functions.inc.php and
  63. //actually gets the param from
  64. $_configuration['db_glue'] = get_config_param('dbGlu');
  65. if ($singleDbForm)
  66. {
  67. $_configuration['table_prefix'] = get_config_param('courseTablePrefix');
  68. $_configuration['main_database'] = get_config_param('mainDbName');
  69. $_configuration['db_prefix'] = get_config_param('dbNamePrefix');
  70. }
  71. $dbScormForm = eregi_replace('[^a-z0-9_-]', '', $dbScormForm);
  72. if (!empty ($dbPrefixForm) && !ereg('^'.$dbPrefixForm, $dbScormForm))
  73. {
  74. $dbScormForm = $dbPrefixForm.$dbScormForm;
  75. }
  76. if (empty ($dbScormForm) || $dbScormForm == 'mysql' || $dbScormForm == $dbPrefixForm)
  77. {
  78. $dbScormForm = $dbPrefixForm.'scorm';
  79. }
  80. $res = @mysql_connect($dbHostForm, $dbUsernameForm, $dbPassForm);
  81. //if error on connection to the database, show error and exit
  82. if ($res === false)
  83. {
  84. //$no = mysql_errno();
  85. //$msg = mysql_error();
  86. //echo '<hr>['.$no.'] - '.$msg.'<hr>';
  87. echo get_lang('DBServerDoesntWorkOrLoginPassIsWrong').'.<br /><br />' .
  88. ' '.get_lang('PleaseCheckTheseValues').' :<br /><br />
  89. <b>'.get_lang('DBHost').'</b> : '.$dbHostForm.'<br />
  90. <b>'.get_lang('DBLogin').'</b> : '.$dbUsernameForm.'<br />
  91. <b>'.get_lang('DBPassword').'</b> : '.$dbPassForm.'<br /><br />
  92. '.get_lang('PleaseGoBackToStep').' '. (defined('DOKEOS_INSTALL') ? '3' : '1').'.
  93. <p><input type="submit" name="step'. (defined('DOKEOS_INSTALL') ? '3' : '1').'" value="&lt; '.get_lang('Back').'"></p>
  94. </td></tr></table></form></body></html>';
  95. exit ();
  96. }
  97. $dblistres = mysql_list_dbs();
  98. $dblist = array();
  99. while ($row = mysql_fetch_object($dblistres)) {
  100. $dblist[] = $row->Database;
  101. }
  102. /*
  103. -----------------------------------------------------------
  104. Normal upgrade procedure:
  105. start by updating main, statistic, user databases
  106. -----------------------------------------------------------
  107. */
  108. //if this script has been included by index.php, not update_courses.php, so
  109. // that we want to change the main databases as well...
  110. $only_test = false;
  111. $log = 0;
  112. if (defined('DOKEOS_INSTALL'))
  113. {
  114. if ($singleDbForm)
  115. {
  116. $dbStatsForm = $dbNameForm;
  117. $dbScormForm = $dbNameForm;
  118. $dbUserForm = $dbNameForm;
  119. }
  120. /**
  121. * Update the databases "pre" migration
  122. */
  123. include ("../lang/english/create_course.inc.php");
  124. if ($languageForm != 'english')
  125. {
  126. //languageForm has been escaped in index.php
  127. include ("../lang/$languageForm/create_course.inc.php");
  128. }
  129. //get the main queries list (m_q_list)
  130. $m_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql','main');
  131. if(count($m_q_list)>0)
  132. {
  133. //now use the $m_q_list
  134. /**
  135. * We connect to the right DB first to make sure we can use the queries
  136. * without a database name
  137. */
  138. if(strlen($dbNameForm)>40){
  139. error_log('Database name '.$dbNameForm.' is too long, skipping',0);
  140. }elseif(!in_array($dbNameForm,$dblist)){
  141. error_log('Database '.$dbNameForm.' was not found, skipping',0);
  142. }else{
  143. mysql_select_db($dbNameForm);
  144. foreach($m_q_list as $query){
  145. if($only_test){
  146. error_log("mysql_query($dbNameForm,$query)",0);
  147. }else{
  148. $res = mysql_query($query);
  149. if($log)
  150. {
  151. error_log("In $dbNameForm, executed: $query",0);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. //get the stats queries list (s_q_list)
  158. $s_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql','stats');
  159. if(count($s_q_list)>0)
  160. {
  161. //now use the $s_q_list
  162. /**
  163. * We connect to the right DB first to make sure we can use the queries
  164. * without a database name
  165. */
  166. if(strlen($dbStatsForm)>40){
  167. error_log('Database name '.$dbStatsForm.' is too long, skipping',0);
  168. }elseif(!in_array($dbStatsForm,$dblist)){
  169. error_log('Database '.$dbStatsForm.' was not found, skipping',0);
  170. }else{
  171. mysql_select_db($dbStatsForm);
  172. foreach($s_q_list as $query){
  173. if($only_test){
  174. error_log("mysql_query($dbStatsForm,$query)",0);
  175. }else{
  176. $res = mysql_query($query);
  177. if($log)
  178. {
  179. error_log("In $dbStatsForm, executed: $query",0);
  180. }
  181. }
  182. }
  183. }
  184. }
  185. //get the user queries list (u_q_list)
  186. $u_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql','user');
  187. if(count($u_q_list)>0)
  188. {
  189. //now use the $u_q_list
  190. /**
  191. * We connect to the right DB first to make sure we can use the queries
  192. * without a database name
  193. */
  194. if(strlen($dbUserForm)>40){
  195. error_log('Database name '.$dbUserForm.' is too long, skipping',0);
  196. }elseif(!in_array($dbUserForm,$dblist)){
  197. error_log('Database '.$dbUserForm.' was not found, skipping',0);
  198. }else{
  199. mysql_select_db($dbUserForm);
  200. foreach($u_q_list as $query){
  201. if($only_test){
  202. error_log("mysql_query($dbUserForm,$query)",0);
  203. error_log("In $dbUserForm, executed: $query",0);
  204. }else{
  205. $res = mysql_query($query);
  206. }
  207. }
  208. }
  209. }
  210. //the SCORM database doesn't need a change in the pre-migrate part - ignore
  211. }
  212. /*
  213. -----------------------------------------------------------
  214. Update the Dokeos course databases
  215. this part can be accessed in two ways:
  216. - from the normal upgrade process
  217. - from the script update_courses.php,
  218. which is used to upgrade more than MAX_COURSE_TRANSFER courses
  219. Every time this script is accessed, only
  220. MAX_COURSE_TRANSFER courses are upgraded.
  221. -----------------------------------------------------------
  222. */
  223. $prefix = '';
  224. if ($singleDbForm)
  225. {
  226. $prefix = $_configuration['table_prefix'];
  227. }
  228. //get the courses databases queries list (c_q_list)
  229. $c_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql','course');
  230. if(count($c_q_list)>0)
  231. {
  232. //get the courses list
  233. if(strlen($dbNameForm)>40){
  234. error_log('Database name '.$dbNameForm.' is too long, skipping',0);
  235. }elseif(!in_array($dbNameForm,$dblist)){
  236. error_log('Database '.$dbNameForm.' was not found, skipping',0);
  237. }else{
  238. mysql_select_db($dbNameForm);
  239. $res = mysql_query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
  240. if($res===false){die('Error while querying the courses list in update_db.inc.php');}
  241. if(mysql_num_rows($res)>0)
  242. {
  243. $i=0;
  244. //while( ($i < MAX_COURSE_TRANSFER) && ($row = mysql_fetch_array($res)))
  245. while($row = mysql_fetch_array($res))
  246. {
  247. $list[] = $row;
  248. $i++;
  249. }
  250. foreach($list as $row_course)
  251. {
  252. //now use the $c_q_list
  253. /**
  254. * We connect to the right DB first to make sure we can use the queries
  255. * without a database name
  256. */
  257. if (!$singleDbForm) //otherwise just use the main one
  258. {
  259. mysql_select_db($row_course['db_name']);
  260. }
  261. foreach($c_q_list as $query)
  262. {
  263. if ($singleDbForm) //otherwise just use the main one
  264. {
  265. $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);
  266. }
  267. if($only_test)
  268. {
  269. error_log("mysql_query(".$row_course['db_name'].",$query)",0);
  270. }else{
  271. $res = mysql_query($query);
  272. if($log)
  273. {
  274. error_log("In ".$row_course['db_name'].", executed: $query",0);
  275. }
  276. }
  277. }
  278. $mytable = $row_course['db_name'].".lp_item";
  279. if($singleDbForm)
  280. {
  281. $mytable = "$prefix{$row_course['db_name']}_lp_item";
  282. }
  283. $mysql = "SELECT * FROM $mytable WHERE min_score != 0 AND prerequisite != ''";
  284. $myres = mysql_query($query);
  285. if($myres!==false && mysql_num_rows($myres)>0)
  286. {
  287. while($myrow = mysql_fetch_array($myres))
  288. {
  289. if(is_numeric($myrow['prerequisite']))
  290. {
  291. $mysql2 = "UPDATE $mytable SET mastery_score = '".$myrow['min_score']."' WHERE id = '".$myrow['prerequisite']."'";
  292. $myres2 = mysql_query($mysql2);
  293. //echo $mysql2."<br />";
  294. if($myres2 !== false)
  295. {
  296. $mysql3 = "UPDATE $mytable SET min_score = 0 WHERE id = '".$myrow['id']."'";
  297. $myres3 = mysql_query($mysql3);
  298. //echo $mysql3."<br />";
  299. }
  300. }
  301. }
  302. }
  303. $sql = "UPDATE $mydb.lp_item SET ";
  304. }
  305. }
  306. }
  307. }
  308. }
  309. else
  310. {
  311. echo 'You are not allowed here !';
  312. }
  313. ?>