update-db-1.8.8-1.9.0.inc.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Chamilo LMS
  5. *
  6. * Update the Chamilo database from an older Chamilo version
  7. * Notice : This script has to be included by index.php
  8. * or update_courses.php (deprecated).
  9. *
  10. * @package chamilo.install
  11. * @todo
  12. * - conditional changing of tables. Currently we execute for example
  13. * ALTER TABLE $dbNameForm.cours
  14. * instructions without checking wether this is necessary.
  15. * - reorganise code into functions
  16. * @todo use database library
  17. */
  18. Log::notice('Entering file');
  19. $old_file_version = '1.8.8';
  20. $new_file_version = '1.9.0';
  21. // Check if we come from index.php or update_courses.php - otherwise display error msg
  22. if (defined('SYSTEM_INSTALLATION')) {
  23. // Check if the current Chamilo install is eligible for update
  24. if (!file_exists('../inc/conf/configuration.php')) {
  25. echo '<strong>'.get_lang('Error').' !</strong> Chamilo '.implode('|', $updateFromVersion).' '.get_lang('HasNotBeenFound').'.<br /><br />
  26. '.get_lang('PleasGoBackToStep1').'.
  27. <p><button type="submit" class="back" name="step1" value="&lt; '.get_lang('Back').'">'.get_lang('Back').'</button></p>
  28. </td></tr></table></form></body></html>';
  29. exit ();
  30. }
  31. $_configuration['db_glue'] = get_config_param('dbGlu');
  32. if ($singleDbForm) {
  33. $_configuration['table_prefix'] = get_config_param('courseTablePrefix');
  34. $_configuration['main_database'] = get_config_param('mainDbName');
  35. $_configuration['db_prefix'] = get_config_param('dbNamePrefix');
  36. }
  37. $dbScormForm = preg_replace('/[^a-zA-Z0-9_\-]/', '', $dbScormForm);
  38. if (!empty($dbPrefixForm) && strpos($dbScormForm, $dbPrefixForm) !== 0) {
  39. $dbScormForm = $dbPrefixForm.$dbScormForm;
  40. }
  41. if (empty($dbScormForm) || $dbScormForm == 'mysql' || $dbScormForm == $dbPrefixForm) {
  42. $dbScormForm = $dbPrefixForm.'scorm';
  43. }
  44. /* Normal upgrade procedure: start by updating main, statistic, user databases */
  45. // If this script has been included by index.php, not update_courses.php, so
  46. // that we want to change the main databases as well...
  47. $only_test = false;
  48. if (defined('SYSTEM_INSTALLATION')) {
  49. if ($singleDbForm) {
  50. $dbStatsForm = $dbNameForm;
  51. $dbScormForm = $dbNameForm;
  52. $dbUserForm = $dbNameForm;
  53. }
  54. /**
  55. * Update the databases "pre" migration
  56. */
  57. include_once '../lang/english/trad4all.inc.php';
  58. if ($languageForm != 'english') {
  59. // languageForm has been escaped in index.php
  60. include_once '../lang/'.$languageForm.'/trad4all.inc.php';
  61. }
  62. // Get the main queries list (m_q_list)
  63. $m_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'main');
  64. if (count($m_q_list) > 0) {
  65. // Now use the $m_q_list
  66. /**
  67. * We connect to the right DB first to make sure we can use the queries
  68. * without a database name
  69. */
  70. if (strlen($dbNameForm) > 40) {
  71. Log::error('Database name '.$dbNameForm.' is too long, skipping');
  72. } elseif (!in_array($dbNameForm, $dblist)) {
  73. Log::error('Database '.$dbNameForm.' was not found, skipping');
  74. } else {
  75. iDatabase::select_db($dbNameForm);
  76. foreach ($m_q_list as $query){
  77. if ($only_test) {
  78. Log::notice("iDatabase::query($dbNameForm,$query)");
  79. } else {
  80. $res = iDatabase::query($query);
  81. if ($res === false) {
  82. Log::error('Error in '.$query.': '.iDatabase::error());
  83. }
  84. }
  85. }
  86. }
  87. }
  88. if (INSTALL_TYPE_UPDATE == 'update') {
  89. //Migrate classes to the new classes (usergroups)
  90. $sql = "SELECT selected_value FROM $dbNameForm.settings_current WHERE variable='use_session_mode' ";
  91. $result = iDatabase::query($sql);
  92. $result = Database::fetch_array($result);
  93. $session_mode = $result['selected_value'];
  94. if ($session_mode == 'true') {
  95. $sql = "UPDATE $dbNameForm.settings_current SET selected_value = 'true' WHERE variable='use_session_mode' ";
  96. $result = iDatabase::query($sql);
  97. $sql = "SELECT * FROM $dbNameForm.class";
  98. $result = iDatabase::query($sql);
  99. $count = 0;
  100. $new_table = "$dbNameForm.usergroup";
  101. $classes_added = 0;
  102. $mapping_classes = array();
  103. if (Database::num_rows($result)) {
  104. while($row = iDatabase::fetch_array($result, 'ASSOC')) {
  105. $old_id = $row['id'];
  106. unset($row['id']);
  107. unset($row['code']);
  108. $new_user_group_id = Database::insert($new_table, $row);
  109. $mapping_classes[$old_id] = $new_user_group_id;
  110. if (is_numeric($id)) {
  111. $classes_added ++;
  112. }
  113. }
  114. }
  115. $sql = "SELECT * FROM $dbNameForm.class_user";
  116. $result = iDatabase::query($sql);
  117. $new_table = "$dbNameForm.usergroup_rel_user";
  118. if (Database::num_rows($result)) {
  119. while ($row = iDatabase::fetch_array($result, 'ASSOC')) {
  120. $values = array('usergroup_id' => $mapping_classes[$row['class_id']],
  121. 'user_id' => $row['user_id']);
  122. iDatabase::insert($new_table, $values);
  123. }
  124. }
  125. $sql = "SELECT * FROM $dbNameForm.course_rel_class";
  126. $result = iDatabase::query($sql);
  127. $new_table = "$dbNameForm.usergroup_rel_course";
  128. if (Database::num_rows($result)) {
  129. while ($row = iDatabase::fetch_array($result, 'ASSOC')) {
  130. $course_code = $row['course_code'];
  131. $course_code = addslashes($course_code);
  132. $sql_course = "SELECT id from $dbNameForm.course WHERE code = '$course_code'";
  133. $result_course = iDatabase::query($sql_course);
  134. $result_course = Database::fetch_array($result_course);
  135. $course_id = $result_course['id'];
  136. $values = array('usergroup_id' => $mapping_classes[$row['class_id']],
  137. 'course_id' => $course_id);
  138. iDatabase::insert($new_table, $values);
  139. }
  140. }
  141. Log::notice("#classes added $classes_added");
  142. }
  143. }
  144. // Get the stats queries list (s_q_list)
  145. $s_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'stats');
  146. if (count($s_q_list) > 0) {
  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. Log::error('Database name '.$dbStatsForm.' is too long, skipping');
  154. } elseif (!in_array($dbStatsForm, $dblist)){
  155. Log::error('Database '.$dbStatsForm.' was not found, skipping');
  156. } else {
  157. iDatabase::select_db($dbStatsForm);
  158. foreach ($s_q_list as $query) {
  159. if ($only_test) {
  160. Log::notice("iDatabase::query($dbStatsForm,$query)");
  161. } else {
  162. $res = iDatabase::query($query);
  163. if ($res === false) {
  164. Log::error('Error in '.$query.': '.iDatabase::error());
  165. }
  166. }
  167. }
  168. }
  169. }
  170. //Moving Stats DB to the main DB
  171. $stats_table = array(
  172. "track_c_browsers",
  173. "track_c_countries",
  174. "track_c_os",
  175. "track_c_providers",
  176. "track_c_referers",
  177. "track_e_access",
  178. "track_e_attempt",
  179. "track_e_attempt_recording",
  180. "track_e_course_access",
  181. "track_e_default",
  182. "track_e_downloads",
  183. "track_e_exercices",
  184. "track_e_hotpotatoes",
  185. "track_e_hotspot",
  186. "track_e_item_property",
  187. "track_e_lastaccess",
  188. "track_e_links",
  189. "track_e_login",
  190. "track_e_online",
  191. "track_e_open",
  192. "track_e_uploads",
  193. "track_stored_values",
  194. "track_stored_values_stack",
  195. );
  196. if ($dbNameForm != $dbStatsForm) {
  197. iDatabase::select_db($dbStatsForm);
  198. foreach ($stats_table as $stat_table) {
  199. $sql = "ALTER TABLE $dbStatsForm.$stat_table RENAME $dbNameForm.$stat_table";
  200. iDatabase::query($sql);
  201. }
  202. iDatabase::select_db($dbNameForm);
  203. }
  204. // Get the user queries list (u_q_list)
  205. $u_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'user');
  206. if (count($u_q_list) > 0) {
  207. // Now use the $u_q_list
  208. /**
  209. * We connect to the right DB first to make sure we can use the queries
  210. * without a database name
  211. */
  212. if (strlen($dbUserForm) > 40) {
  213. Log::error('Database name '.$dbUserForm.' is too long, skipping');
  214. } elseif (!in_array($dbUserForm,$dblist)) {
  215. Log::error('Database '.$dbUserForm.' was not found, skipping');
  216. } else {
  217. iDatabase::select_db($dbUserForm);
  218. foreach ($u_q_list as $query) {
  219. if ($only_test) {
  220. Log::notice("iDatabase::query($dbUserForm,$query)");
  221. Log::notice("In $dbUserForm, executed: $query");
  222. } else {
  223. $res = iDatabase::query($query);
  224. if ($res === false) {
  225. Log::error('Error in '.$query.': '.iDatabase::error());
  226. }
  227. }
  228. }
  229. }
  230. }
  231. //Moving user database to the main database
  232. $users_tables = array(
  233. "personal_agenda",
  234. "personal_agenda_repeat",
  235. "personal_agenda_repeat_not",
  236. "user_course_category"
  237. );
  238. if ($dbNameForm != $dbUserForm) {
  239. iDatabase::select_db($dbUserForm);
  240. foreach ($users_tables as $table) {
  241. $sql = "ALTER TABLE $dbUserForm.$table RENAME $dbNameForm.$table";
  242. iDatabase::query($sql);
  243. }
  244. iDatabase::select_db($dbNameForm);
  245. }
  246. }
  247. //Adding admin user in the access_url_rel_user table
  248. $sql = "SELECT user_id FROM admin WHERE user_id = 1";
  249. $result = iDatabase::query($sql);
  250. $has_user_id = Database::num_rows($result) > 0;
  251. $sql = "SELECT * FROM access_url_rel_user WHERE user_id = 1 AND access_url_id = 1";
  252. $result = iDatabase::query($sql);
  253. $has_entry = Database::num_rows($result) > 0;
  254. if ($has_user_id && !$has_entry) {
  255. $sql = "INSERT INTO access_url_rel_user VALUES(1, 1)";
  256. iDatabase::query($sql);
  257. }
  258. global $_configuration;
  259. $prefix = '';
  260. if ($singleDbForm) {
  261. $prefix = get_config_param('table_prefix');
  262. }
  263. Log::notice("Database prefix: '$prefix'");
  264. // Get the courses databases queries list (c_q_list)
  265. $c_q_list = get_sql_file_contents('migrate-db-'.$old_file_version.'-'.$new_file_version.'-pre.sql', 'course');
  266. Log::notice('Starting migration: '.$old_file_version.' - '.$new_file_version);
  267. if (count($c_q_list) > 0) {
  268. // Get the courses list
  269. if (strlen($dbNameForm) > 40) {
  270. Log::error('Database name '.$dbNameForm.' is too long, skipping');
  271. } elseif(!in_array($dbNameForm, $dblist)) {
  272. Log::error('Database '.$dbNameForm.' was not found, skipping');
  273. } else {
  274. iDatabase::select_db($dbNameForm);
  275. $res = iDatabase::query("SELECT id, code, db_name, directory, course_language, id as real_id FROM course WHERE target_course_code IS NULL ORDER BY code");
  276. if ($res === false) { die('Error while querying the courses list in update_db-1.8.8-1.9.0.inc.php'); }
  277. $errors = array();
  278. if (iDatabase::num_rows($res) > 0) {
  279. $i = 0;
  280. $list = array();
  281. while ($row = iDatabase::fetch_array($res)) {
  282. $list[] = $row;
  283. $i++;
  284. }
  285. foreach ($list as $row_course) {
  286. if (!$singleDbForm) { // otherwise just use the main one
  287. iDatabase::select_db($row_course['db_name']);
  288. }
  289. Log::notice('Course db ' . $row_course['db_name']);
  290. // Now use the $c_q_list
  291. foreach ($c_q_list as $query) {
  292. if ($singleDbForm) {
  293. $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);
  294. }
  295. if ($only_test) {
  296. Log::notice("iDatabase::query(".$row_course['db_name'].",$query)");
  297. } else {
  298. $res = iDatabase::query($query);
  299. if ($res === false) {
  300. Log::error('Error in '.$query.': '.iDatabase::error());
  301. }
  302. }
  303. }
  304. $work_table = $row_course['db_name'].".student_publication";
  305. $item_table = $row_course['db_name'].".item_property";
  306. if ($singleDbForm) {
  307. $work_table = "$prefix{$row_course['db_name']}_student_publication";
  308. $item_table = $row_course['db_name'].".item_property";
  309. }
  310. if (!$singleDbForm) {
  311. // otherwise just use the main one
  312. iDatabase::select_db($row_course['db_name']);
  313. } else {
  314. iDatabase::select_db($dbNameForm);
  315. }
  316. /* Start work fix */
  317. /* Fixes the work subfolder and work with no parent issues */
  318. //1. Searching for works with no parents
  319. $sql = "SELECT * FROM $work_table WHERE parent_id = 0 AND filetype ='file'";
  320. $result = Database::query($sql);
  321. $work_list = array();
  322. if (Database::num_rows($result)) {
  323. while ($row = Database::fetch_array($result, 'ASSOC')) {
  324. $work_list[] = $row;
  325. }
  326. }
  327. $today = api_get_utc_datetime();
  328. $user_id = 1;
  329. require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
  330. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  331. $course_dir = $sys_course_path . $row_course['directory'];
  332. $base_work_dir = $course_dir . '/work';
  333. //2. Looping if there are works with no parents
  334. if (!empty($work_list)) {
  335. $work_dir_created = array();
  336. foreach ($work_list as $work) {
  337. $session_id = intval($work['session_id']);
  338. $group_id = intval($work['post_group_id']);
  339. $work_key = $session_id.$group_id;
  340. //Only create the folder once
  341. if (!isset($work_dir_created[$work_key])) {
  342. $dir_name = "default_tasks_".$group_id."_".$session_id;
  343. //2.1 Creating a new work folder
  344. $sql = "INSERT INTO $work_table SET
  345. url = 'work/".$dir_name."',
  346. title = 'Tasks',
  347. description = '',
  348. author = '',
  349. active = '1',
  350. accepted = '1',
  351. filetype = 'folder',
  352. post_group_id = '$group_id',
  353. sent_date = '".$today."',
  354. parent_id = '0',
  355. qualificator_id = '',
  356. user_id = '".$user_id."'";
  357. iDatabase::query($sql);
  358. $id = Database::insert_id();
  359. //2.2 Adding the folder in item property
  360. if ($id) {
  361. //api_item_property_update($row_course, 'work', $id, 'DirectoryCreated', $user_id, $group_id, null, 0, 0 , $session_id);
  362. $sql = "INSERT INTO $item_table (tool, ref, insert_date, insert_user_id, lastedit_date, lastedit_type, lastedit_user_id, to_group_id, visibility, id_session)
  363. VALUES ('work','$id','$today', '$user_id', '$today', 'DirectoryCreated','$user_id', '$group_id', '1', '$session_id')";
  364. iDatabase::query($sql);
  365. $work_dir_created[$work_key] = $id;
  366. create_unexisting_work_directory($base_work_dir, $dir_name);
  367. $final_dir = $base_work_dir.'/'.$dir_name;
  368. }
  369. } else {
  370. $final_dir = $base_work_dir.'/'.$dir_name;
  371. }
  372. //2.3 Updating the url
  373. if (!empty($work_dir_created[$work_key])) {
  374. $parent_id = $work_dir_created[$work_key];
  375. $new_url = "work/".$dir_name.'/'.basename($work['url']);
  376. $new_url = Database::escape_string($new_url);$sql = "UPDATE $work_table SET url = '$new_url', parent_id = $parent_id, contains_file = '1' WHERE id = {$work['id']}";
  377. iDatabase::query($sql);
  378. if (is_dir($final_dir)) {
  379. rename($course_dir.'/'.$work['url'], $course_dir.'/'.$new_url);
  380. }
  381. }
  382. }
  383. }
  384. //3.0 Moving subfolders to the root
  385. $sql = "SELECT * FROM $work_table WHERE parent_id <> 0 AND filetype ='folder'";
  386. $result = Database::query($sql);
  387. $work_list = array();
  388. if (Database::num_rows($result)) {
  389. while ($row = Database::fetch_array($result, 'ASSOC')) {
  390. $work_list[] = $row;
  391. }
  392. if (!empty($work_list)) {
  393. foreach ($work_list as $work_folder) {
  394. $folder_id = $work_folder['id'];
  395. check_work($folder_id, $work_folder['url'], $work_table, $base_work_dir);
  396. }
  397. }
  398. }
  399. /* End of work fix */
  400. //Course tables to be migrated
  401. $table_list = array(
  402. 'announcement',
  403. 'announcement_attachment',
  404. 'attendance',
  405. 'attendance_calendar',
  406. 'attendance_result',
  407. 'attendance_sheet',
  408. 'attendance_sheet_log',
  409. 'blog',
  410. 'blog_attachment',
  411. 'blog_comment',
  412. 'blog_post',
  413. 'blog_rating',
  414. 'blog_rel_user',
  415. 'blog_task',
  416. 'blog_task_rel_user',
  417. 'calendar_event',
  418. 'calendar_event_attachment',
  419. 'calendar_event_repeat',
  420. 'calendar_event_repeat_not',
  421. 'chat_connected',
  422. 'course_description',
  423. 'course_setting',
  424. 'document',
  425. 'dropbox_category',
  426. 'dropbox_feedback',
  427. 'dropbox_file',
  428. 'dropbox_person',
  429. 'dropbox_post',
  430. 'forum_attachment',
  431. 'forum_category',
  432. 'forum_forum',
  433. 'forum_mailcue',
  434. 'forum_notification',
  435. 'forum_post',
  436. 'forum_thread',
  437. 'forum_thread_qualify',
  438. 'forum_thread_qualify_log',
  439. 'glossary',
  440. 'group_category',
  441. 'group_info',
  442. 'group_rel_tutor',
  443. 'group_rel_user',
  444. 'item_property',
  445. 'link',
  446. 'link_category',
  447. 'lp',
  448. 'lp_item',
  449. 'lp_item_view',
  450. 'lp_iv_interaction',
  451. 'lp_iv_objective',
  452. 'lp_view',
  453. 'notebook',
  454. 'metadata',
  455. 'online_connected',
  456. 'online_link',
  457. 'permission_group',
  458. 'permission_task',
  459. 'permission_user',
  460. 'quiz',
  461. 'quiz_answer',
  462. 'quiz_question',
  463. 'quiz_question_option',
  464. 'quiz_rel_question',
  465. 'resource',
  466. 'role',
  467. 'role_group',
  468. 'role_permissions',
  469. 'role_user',
  470. 'student_publication',
  471. 'student_publication_assignment',
  472. 'survey',
  473. 'survey_answer',
  474. 'survey_group',
  475. 'survey_invitation',
  476. 'survey_question',
  477. 'survey_question_option',
  478. 'thematic',
  479. 'thematic_advance',
  480. 'thematic_plan',
  481. 'tool',
  482. 'tool_intro',
  483. 'userinfo_content',
  484. 'userinfo_def',
  485. 'wiki',
  486. 'wiki_conf',
  487. 'wiki_discuss',
  488. 'wiki_mailcue'
  489. );
  490. Log::notice('<<<------- Loading DB course '.$row_course['db_name'].' -------->>');
  491. //Install::message('<<<------- Loading DB course '.$row_course['db_name'].' -------->>');
  492. $count = $old_count = 0;
  493. foreach ($table_list as $table) {
  494. $just_table_name = $table;
  495. $old_table = $row_course['db_name'].".".$table;
  496. if ($singleDbForm) {
  497. $old_table = "$prefix{$row_course['db_name']}_".$table;
  498. $just_table_name = "$prefix{$row_course['db_name']}_".$table;
  499. }
  500. $course_id = $row_course['id'];
  501. $new_table = DB_COURSE_PREFIX.$table;
  502. //Use the old database (if this is the case)
  503. if (!$singleDbForm) {
  504. // otherwise just use the main one
  505. iDatabase::select_db($row_course['db_name']);
  506. } else {
  507. iDatabase::select_db($dbNameForm);
  508. }
  509. //Count of rows
  510. $sql = "SHOW TABLES LIKE '$just_table_name'";
  511. $result = iDatabase::query($sql);
  512. if (Database::num_rows($result)) {
  513. $sql = "SELECT count(*) FROM $old_table";
  514. $result = iDatabase::query($sql);
  515. $old_count = 0;
  516. if ($result) {
  517. $row = iDatabase::fetch_row($result);
  518. $old_count = $row[0];
  519. } else {
  520. Log::error("Count(*) in table $old_table failed");
  521. }
  522. Log::notice("# rows in $old_table: $old_count");
  523. $sql = "SELECT * FROM $old_table";
  524. $result = iDatabase::query($sql);
  525. $count = 0;
  526. /* Loads the main database */
  527. iDatabase::select_db($dbNameForm);
  528. while($row = iDatabase::fetch_array($result, 'ASSOC')) {
  529. $row['c_id'] = $course_id;
  530. $id = iDatabase::insert($new_table, $row);
  531. if (is_numeric($id)) {
  532. $count++;
  533. } else {
  534. $errors[$old_table][] = $row;
  535. }
  536. }
  537. Log::notice("#rows inserted in $new_table: $count");
  538. if ($old_count != $count) {
  539. Log::error("ERROR count of new and old table doesn't match: $old_count - $new_table");
  540. Log::error("Check the results: ");
  541. Log::error(print_r($errors, 1));
  542. error_log(print_r($errors, 1));
  543. }
  544. } else {
  545. Log::error("Seems that the table $old_table doesn't exists ");
  546. }
  547. }
  548. Log::notice('<<<------- end -------->>');
  549. }
  550. }
  551. }
  552. }
  553. } else {
  554. echo 'You are not allowed here !' . __FILE__;
  555. }
  556. function check_work($folder_id, $work_url, $work_table, $base_work_dir) {
  557. $uniq_id = uniqid();
  558. //Looking for subfolders
  559. $sql = "SELECT * FROM $work_table WHERE parent_id = $folder_id AND filetype ='folder'";
  560. $result = Database::query($sql);
  561. if (Database::num_rows($result)) {
  562. while ($row = Database::fetch_array($result, 'ASSOC')) {
  563. check_work($row['id'], $row['url'], $work_table, $base_work_dir);
  564. }
  565. }
  566. //Moving the subfolder in the root
  567. $new_url = '/'.basename($work_url).'_mv_'.$uniq_id;
  568. $new_url = Database::escape_string($new_url);
  569. $sql = "UPDATE $work_table SET url = '$new_url', parent_id = 0 WHERE id = $folder_id";
  570. iDatabase::query($sql);
  571. if (is_dir($base_work_dir.$work_url)) {
  572. rename($base_work_dir.$work_url, $base_work_dir.$new_url);
  573. //Rename all files inside the folder
  574. $sql = "SELECT * FROM $work_table WHERE parent_id = $folder_id AND filetype ='file'";
  575. $result = Database::query($sql);
  576. if (Database::num_rows($result)) {
  577. while ($row = Database::fetch_array($result, 'ASSOC')) {
  578. $new_url = "work".$new_url.'/'.basename($row['url']);
  579. $new_url = Database::escape_string($new_url);
  580. $sql = "UPDATE $work_table SET url = '$new_url', parent_id = $folder_id, contains_file = '1' WHERE id = {$row['id']}";
  581. iDatabase::query($sql);
  582. }
  583. }
  584. }
  585. }