update-db-1.8.6.2-1.8.7.inc.php 13 KB

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