update-db-1.8.6-1.8.6.1.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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';
  20. $new_file_version = '1.8.6.1';
  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. }
  140. }
  141. }
  142. }
  143. //get the stats queries list (s_q_list)
  144. $s_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql','stats');
  145. if(count($s_q_list)>0)
  146. {
  147. //now use the $s_q_list
  148. /**
  149. * We connect to the right DB first to make sure we can use the queries
  150. * without a database name
  151. */
  152. if(strlen($dbStatsForm)>40){
  153. error_log('Database name '.$dbStatsForm.' is too long, skipping',0);
  154. }elseif(!in_array($dbStatsForm,$dblist)){
  155. error_log('Database '.$dbStatsForm.' was not found, skipping',0);
  156. }else{
  157. mysql_select_db($dbStatsForm);
  158. foreach($s_q_list as $query){
  159. if($only_test){
  160. error_log("mysql_query($dbStatsForm,$query)",0);
  161. }else{
  162. $res = mysql_query($query);
  163. if($log)
  164. {
  165. error_log("In $dbStatsForm, executed: $query",0);
  166. }
  167. }
  168. }
  169. }
  170. }
  171. //get the user queries list (u_q_list)
  172. $u_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql','user');
  173. if(count($u_q_list)>0)
  174. {
  175. //now use the $u_q_list
  176. /**
  177. * We connect to the right DB first to make sure we can use the queries
  178. * without a database name
  179. */
  180. if(strlen($dbUserForm)>40){
  181. error_log('Database name '.$dbUserForm.' is too long, skipping',0);
  182. }elseif(!in_array($dbUserForm,$dblist)){
  183. error_log('Database '.$dbUserForm.' was not found, skipping',0);
  184. }else{
  185. mysql_select_db($dbUserForm);
  186. foreach($u_q_list as $query){
  187. if($only_test){
  188. error_log("mysql_query($dbUserForm,$query)",0);
  189. error_log("In $dbUserForm, executed: $query",0);
  190. }else{
  191. $res = mysql_query($query);
  192. }
  193. }
  194. }
  195. }
  196. //the SCORM database doesn't need a change in the pre-migrate part - ignore
  197. }
  198. /*
  199. -----------------------------------------------------------
  200. Update the Dokeos course databases
  201. this part can be accessed in two ways:
  202. - from the normal upgrade process
  203. - from the script update_courses.php,
  204. which is used to upgrade more than MAX_COURSE_TRANSFER courses
  205. Every time this script is accessed, only
  206. MAX_COURSE_TRANSFER courses are upgraded.
  207. -----------------------------------------------------------
  208. */
  209. $prefix = '';
  210. if ($singleDbForm)
  211. {
  212. $prefix = get_config_param ('table_prefix');
  213. }
  214. //get the courses databases queries list (c_q_list)
  215. $c_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql','course');
  216. if(count($c_q_list)>0)
  217. {
  218. //get the courses list
  219. if(strlen($dbNameForm)>40)
  220. {
  221. error_log('Database name '.$dbNameForm.' is too long, skipping',0);
  222. }
  223. elseif(!in_array($dbNameForm,$dblist))
  224. {
  225. error_log('Database '.$dbNameForm.' was not found, skipping',0);
  226. }
  227. else
  228. {
  229. mysql_select_db($dbNameForm);
  230. $res = mysql_query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL ORDER BY code");
  231. if($res===false){die('Error while querying the courses list in update_db.inc.php');}
  232. if(mysql_num_rows($res)>0)
  233. {
  234. $i=0;
  235. $list = array();
  236. //while( ($i < MAX_COURSE_TRANSFER) && ($row = mysql_fetch_array($res)))
  237. while($row = mysql_fetch_array($res))
  238. {
  239. $list[] = $row;
  240. $i++;
  241. }
  242. foreach($list as $row_course)
  243. {
  244. //now use the $c_q_list
  245. /**
  246. * We connect to the right DB first to make sure we can use the queries
  247. * without a database name
  248. */
  249. if (!$singleDbForm) //otherwise just use the main one
  250. {
  251. mysql_select_db($row_course['db_name']);
  252. }
  253. foreach($c_q_list as $query)
  254. {
  255. if ($singleDbForm) //otherwise just use the main one
  256. {
  257. $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);
  258. }
  259. if($only_test)
  260. {
  261. error_log("mysql_query(".$row_course['db_name'].",$query)",0);
  262. }
  263. else
  264. {
  265. $res = mysql_query($query);
  266. if($log)
  267. {
  268. error_log("In ".$row_course['db_name'].", executed: $query",0);
  269. }
  270. }
  271. }
  272. $t_wiki = $row_course['db_name'].".wiki";
  273. $t_wiki_conf = $row_course['db_name'].".wiki_conf";
  274. if($singleDbForm)
  275. {
  276. $t_wiki = "$prefix{$row_course['db_name']}_wiki";
  277. $t_wiki_conf = "$prefix{$row_course['db_name']}_wiki_conf";
  278. }
  279. //update correct page_id to wiki table, actually only store 0
  280. $query = "SELECT id, reflink FROM $t_wiki";
  281. $res_page = mysql_query($query);
  282. $wiki_id = $reflink = array();
  283. if (mysql_num_rows($res_page) > 0 ) {
  284. while ($row_page = mysql_fetch_row($res_page)) {
  285. $wiki_id[] = $row_page[0];
  286. $reflink[] = $row_page[1];
  287. }
  288. }
  289. $reflink_unique = array_unique($reflink);
  290. $reflink_flip = array_flip($reflink_unique);
  291. if (is_array($wiki_id)) {
  292. foreach ($wiki_id as $key=>$wiki_page) {
  293. $pag_id = $reflink_flip[$reflink[$key]];
  294. $sql= "UPDATE $t_wiki SET page_id='".($pag_id + 1)."' WHERE id = '$wiki_page'";
  295. $res_update = mysql_query($sql);
  296. }
  297. }
  298. //insert page_id into wiki_conf table, actually this table is empty
  299. $query = "SELECT DISTINCT page_id FROM $t_wiki ORDER BY page_id";
  300. $myres_wiki = mysql_query($query);
  301. if (mysql_num_rows($myres_wiki) > 0 ) {
  302. while ($row_wiki = mysql_fetch_row($myres_wiki)) {
  303. $page_id = $row_wiki[0];
  304. $query="INSERT INTO ".$t_wiki_conf." (page_id, task, feedback1, feedback2, feedback3, fprogress1, fprogress2, fprogress3) VALUES ('".$page_id."','','','','','','','')";
  305. $myres_wiki_conf = mysql_query($query);
  306. }
  307. }
  308. }
  309. }
  310. }
  311. }
  312. }
  313. else
  314. {
  315. echo 'You are not allowed here !';
  316. }
  317. ?>