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

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