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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php // $Id: $
  2. /* See license terms in /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 chamilo.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. 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('SYSTEM_INSTALLATION') || defined('DOKEOS_COURSE_UPDATE')) {
  35. //check if the current Dokeos install is elligible for update
  36. if (!file_exists('../inc/conf/configuration.php')) {
  37. echo '<strong>'.get_lang('Error').' !</strong> Dokeos '.implode('|', $updateFromVersion).' '.get_lang('HasNotBeenFound').'.<br /><br />
  38. '.get_lang('PleasGoBackToStep1').'.
  39. <p><button type="submit" class="back" name="step1" value="&lt; '.get_lang('Back').'">'.get_lang('Back').'</button></p>
  40. </td></tr></table></form></body></html>';
  41. exit ();
  42. }
  43. //get_config_param() comes from install_functions.inc.php and
  44. //actually gets the param from
  45. $_configuration['db_glue'] = get_config_param('dbGlu');
  46. if ($singleDbForm) {
  47. $_configuration['table_prefix'] = get_config_param('courseTablePrefix');
  48. $_configuration['main_database'] = get_config_param('mainDbName');
  49. $_configuration['db_prefix'] = get_config_param('dbNamePrefix');
  50. }
  51. $dbScormForm = preg_replace('/[^a-zA-Z0-9_\-]/', '', $dbScormForm);
  52. if (!empty($dbPrefixForm) && strpos($dbScormForm, $dbPrefixForm) !== 0) {
  53. $dbScormForm = $dbPrefixForm.$dbScormForm;
  54. }
  55. if (empty($dbScormForm) || $dbScormForm == 'mysql' || $dbScormForm == $dbPrefixForm) {
  56. $dbScormForm = $dbPrefixForm.'scorm';
  57. }
  58. $res = @Database::connect(array('server' => $dbHostForm, 'username' => $dbUsernameForm, 'password' => $dbPassForm));
  59. //if error on connection to the database, show error and exit
  60. if ($res === false) {
  61. //$no = Database::errno();
  62. //$msg = Database::error();
  63. //echo '<hr />['.$no.'] - '.$msg.'<hr />';
  64. echo get_lang('DBServerDoesntWorkOrLoginPassIsWrong').'.<br /><br />' .
  65. ' '.get_lang('PleaseCheckTheseValues').' :<br /><br />
  66. <strong>'.get_lang('DBHost').'</strong> : '.$dbHostForm.'<br />
  67. <strong>'.get_lang('DBLogin').'</strong> : '.$dbUsernameForm.'<br />
  68. <strong>'.get_lang('DBPassword').'</strong> : '.$dbPassForm.'<br /><br />
  69. '.get_lang('PleaseGoBackToStep').' '. (defined('SYSTEM_INSTALLATION') ? '3' : '1').'.
  70. <p><button type="submit" class="back" name="step'. (defined('SYSTEM_INSTALLATION') ? '3' : '1').'" value="&lt; '.get_lang('Back').'">'.get_lang('Back').'</button></p>
  71. </td></tr></table></form></body></html>';
  72. exit ();
  73. }
  74. @Database::query("set session sql_mode='';"); // Disabling special SQL modes (MySQL 5)
  75. $dblist = Database::get_databases();
  76. /*
  77. -----------------------------------------------------------
  78. Normal upgrade procedure:
  79. start by updating main, statistic, user databases
  80. -----------------------------------------------------------
  81. */
  82. //if this script has been included by index.php, not update_courses.php, so
  83. // that we want to change the main databases as well...
  84. $only_test = false;
  85. $log = 0;
  86. if (defined('SYSTEM_INSTALLATION')) {
  87. if ($singleDbForm) {
  88. $dbStatsForm = $dbNameForm;
  89. $dbScormForm = $dbNameForm;
  90. $dbUserForm = $dbNameForm;
  91. }
  92. /**
  93. * Update the databases "pre" migration
  94. */
  95. include '../lang/english/create_course.inc.php';
  96. if ($languageForm != 'english') {
  97. //languageForm has been escaped in index.php
  98. include '../lang/'.$languageForm.'/create_course.inc.php';
  99. }
  100. //get the main queries list (m_q_list)
  101. $m_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'main');
  102. if (count($m_q_list) > 0) {
  103. //now use the $m_q_list
  104. /**
  105. * We connect to the right DB first to make sure we can use the queries
  106. * without a database name
  107. */
  108. if (strlen($dbNameForm) > 40) {
  109. error_log('Database name '.$dbNameForm.' is too long, skipping', 0);
  110. } elseif (!in_array($dbNameForm, $dblist)) {
  111. error_log('Database '.$dbNameForm.' was not found, skipping', 0);
  112. } else {
  113. Database::select_db($dbNameForm);
  114. foreach ($m_q_list as $query){
  115. if ($only_test) {
  116. error_log("Database::query($dbNameForm,$query)", 0);
  117. } else {
  118. $res = Database::query($query);
  119. if ($log) {
  120. error_log("In $dbNameForm, executed: $query", 0);
  121. }
  122. if ($res === false) {
  123. error_log('Error in '.$query.': '.Database::error());
  124. }
  125. }
  126. }
  127. }
  128. }
  129. //
  130. // now clean the deprecated id_coach field from the session_rel_course table
  131. /*
  132. $m_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-post.sql', 'main');
  133. if (count($m_q_list) > 0) {
  134. //now use the $m_q_list
  135. // We connect to the right DB first to make sure we can use the queries
  136. // without a database name
  137. if (strlen($dbNameForm) > 40) {
  138. error_log('Database name '.$dbNameForm.' is too long, skipping', 0);
  139. } elseif (!in_array($dbNameForm, $dblist)) {
  140. error_log('Database '.$dbNameForm.' was not found, skipping', 0);
  141. } else {
  142. Database::select_db($dbNameForm);
  143. foreach ($m_q_list as $query) {
  144. if ($only_test) {
  145. error_log("Database::query($dbNameForm,$query)", 0);
  146. } else {
  147. $res = Database::query($query);
  148. if ($log) {
  149. error_log("In $dbNameForm, executed: $query", 0);
  150. }
  151. }
  152. }
  153. }
  154. }
  155. */
  156. //get the stats queries list (s_q_list)
  157. $s_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'stats');
  158. if (count($s_q_list) > 0) {
  159. //now use the $s_q_list
  160. /**
  161. * We connect to the right DB first to make sure we can use the queries
  162. * without a database name
  163. */
  164. if (strlen($dbStatsForm) > 40) {
  165. error_log('Database name '.$dbStatsForm.' is too long, skipping', 0);
  166. } elseif (!in_array($dbStatsForm, $dblist)){
  167. error_log('Database '.$dbStatsForm.' was not found, skipping', 0);
  168. } else {
  169. Database::select_db($dbStatsForm);
  170. foreach ($s_q_list as $query) {
  171. if ($only_test) {
  172. error_log("Database::query($dbStatsForm,$query)", 0);
  173. } else {
  174. $res = Database::query($query);
  175. if ($log) {
  176. error_log("In $dbStatsForm, executed: $query", 0);
  177. }
  178. if ($res === false) {
  179. error_log('Error in '.$query.': '.Database::error());
  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. //now use the $u_q_list
  189. /**
  190. * We connect to the right DB first to make sure we can use the queries
  191. * without a database name
  192. */
  193. if (strlen($dbUserForm) > 40) {
  194. error_log('Database name '.$dbUserForm.' is too long, skipping', 0);
  195. } elseif (!in_array($dbUserForm,$dblist)) {
  196. error_log('Database '.$dbUserForm.' was not found, skipping', 0);
  197. } else {
  198. Database::select_db($dbUserForm);
  199. foreach ($u_q_list as $query) {
  200. if ($only_test) {
  201. error_log("Database::query($dbUserForm,$query)", 0);
  202. error_log("In $dbUserForm, executed: $query", 0);
  203. } else {
  204. $res = Database::query($query);
  205. if ($res === false) {
  206. error_log('Error in '.$query.': '.Database::error());
  207. }
  208. }
  209. }
  210. }
  211. }
  212. //the SCORM database doesn't need a change in the pre-migrate part - ignore
  213. }
  214. /*
  215. -----------------------------------------------------------
  216. Update the Dokeos course databases
  217. this part can be accessed in two ways:
  218. - from the normal upgrade process
  219. - from the script update_courses.php,
  220. which is used to upgrade more than MAX_COURSE_TRANSFER courses
  221. Every time this script is accessed, only
  222. MAX_COURSE_TRANSFER courses are upgraded.
  223. -----------------------------------------------------------
  224. */
  225. $prefix = '';
  226. if ($singleDbForm) {
  227. $prefix = get_config_param ('table_prefix');
  228. }
  229. //get the courses databases queries list (c_q_list)
  230. $c_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'course');
  231. if (count($c_q_list) > 0) {
  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. Database::select_db($dbNameForm);
  239. $res = Database::query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL ORDER BY code");
  240. if ($res === false) { die('Error while querying the courses list in update_db-1.8.6.2-1.8.7.inc.php'); }
  241. if (Database::num_rows($res) > 0) {
  242. $i = 0;
  243. $list = array();
  244. //while (($i < MAX_COURSE_TRANSFER) && ($row = Database::fetch_array($res)))
  245. while ($row = Database::fetch_array($res)) {
  246. $list[] = $row;
  247. $i++;
  248. }
  249. foreach ($list as $row_course) {
  250. //now use the $c_q_list
  251. /**
  252. * We connect to the right DB first to make sure we can use the queries
  253. * without a database name
  254. */
  255. if (!$singleDbForm) { //otherwise just use the main one
  256. Database::select_db($row_course['db_name']);
  257. }
  258. foreach ($c_q_list as $query) {
  259. if ($singleDbForm) { //otherwise just use the main one
  260. $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);
  261. }
  262. if ($only_test) {
  263. error_log("Database::query(".$row_course['db_name'].",$query)", 0);
  264. } else {
  265. $res = Database::query($query);
  266. if ($log) {
  267. error_log("In ".$row_course['db_name'].", executed: $query", 0);
  268. }
  269. if ($res === false) {
  270. error_log('Error in '.$query.': '.Database::error());
  271. }
  272. }
  273. }
  274. }
  275. }
  276. }
  277. }
  278. } else {
  279. echo 'You are not allowed here !';
  280. }