multi_url_conversion.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. // For licensing terms, see /license.txt
  3. /**
  4. * This script takes a single-URL portal and changes the database so the
  5. * original portal becomes a secondary URL (not the main admin URL).
  6. * This means creating a new URL of ID 1 and moving all previous records
  7. * referencing ID 1 to ID 2.
  8. */
  9. die();
  10. require __DIR__.'/../../main/inc/global.inc.php';
  11. $tableAccessUrl = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
  12. $tableUserRelCourseVote = Database::get_main_table(TABLE_MAIN_USER_REL_COURSE_VOTE);
  13. $tableTrackOnline = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
  14. $tableAnnouncement = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS);
  15. $tableSkill = Database::get_main_table(TABLE_MAIN_SKILL);
  16. $tableAccessUrlRelCourse = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  17. $tableAccessUrlRelCourseCategory = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
  18. $tableAccessUrlRelSession = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  19. $tableAccessUrlRelUser = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  20. $tableAccessUrlRelUserGroup = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP);
  21. // Usage
  22. echo "This script converts a single-URL portal to a multi-URL portal by making".PHP_EOL;
  23. echo "the current URL into the secondary URL and creating a new URL as admin URL.".PHP_EOL;
  24. echo "Please take a database backup before launching this script.".PHP_EOL;PHP_EOL;
  25. $adminUrl = '';
  26. if (!empty($argv[1])) {
  27. $adminUrl = Database::escape_string($argv[1]);
  28. } else {
  29. echo "New admin URL was not defined. Please try again, passing it as argument to this script".PHP_EOL;
  30. echo "Usage: php multi_url_conversion.php URL [admin-username]".PHP_EOL;
  31. echo " URL The URL (with http(s):// and trailing slash of the (new) admin portal".PHP_EOL;
  32. echo " username The username of the global admin (to be set as admin of new portal)".PHP_EOL.PHP_EOL;
  33. exit();
  34. }
  35. $userId = 1;
  36. if (!empty($argv[2])) {
  37. $username = $argv[2];
  38. $user = api_get_user_info_from_username($username);
  39. $userId = $user['id'];
  40. } else {
  41. echo "No admin username provided. Using admin of ID 1".PHP_EOL;
  42. }
  43. $mainUrl = api_get_path(WEB_PATH);
  44. echo "Converting portal to multi-URL. Processing...".PHP_EOL;
  45. $sql = "SELECT id FROM $tableAccessUrl";
  46. $result = Database::query($sql);
  47. if (Database::num_rows($result) > 1) {
  48. echo "There are already more than one URL on this portal. Process cancelled".PHP_EOL;
  49. exit();
  50. }
  51. echo "Updating default URL to ".$mainUrl.PHP_EOL;
  52. while ($row = Database::fetch_assoc($result)) {
  53. $sqlU = "UPDATE $tableAccessUrl SET url = '$adminUrl', description = 'The main admin URL' WHERE id = ".$row['id'];
  54. $resU = Database::query($sqlU);
  55. if ($resU === false) {
  56. echo "Found issue executing the following query. Process cancelled: $sqlU".PHP_EOL;
  57. exit();
  58. } else {
  59. $adminUrlId = $row['id'];
  60. echo "...done!".PHP_EOL;
  61. break;
  62. }
  63. }
  64. $date = api_get_utc_datetime();
  65. $oldUrlId = $adminUrlId + 1;
  66. echo "Creating new URL and converting previous URL (".$adminUrlId.") to secondary. Processing...".PHP_EOL;
  67. $sqlI = "INSERT INTO $tableAccessUrl (id, url, description, active, created_by, tms, url_type)".
  68. " VALUES ($oldUrlId, '$mainUrl', '', 1, 1, '$date', null)";
  69. $resI = Database::query($sqlI);
  70. echo "Updating all relevant tables to define the secondary URL".PHP_EOL;
  71. $sqlU = "UPDATE $tableUserRelCourseVote SET url_id = $oldUrlId WHERE url_id = $adminUrlId";
  72. $resU = Database::query($sqlU);
  73. echo "Table $tableUserRelCourseVote updated".PHP_EOL;
  74. $sqlU = "UPDATE $tableTrackOnline SET access_url_id = $oldUrlId WHERE access_url_id = $adminUrlId";
  75. $resU = Database::query($sqlU);
  76. echo "Table $tableTrackOnline updated".PHP_EOL;
  77. $sqlU = "UPDATE $tableAnnouncement SET access_url_id = $oldUrlId WHERE access_url_id = $adminUrlId";
  78. $resU = Database::query($sqlU);
  79. echo "Table $tableAnnouncement updated".PHP_EOL;
  80. $sqlU = "UPDATE $tableSkill SET access_url_id = $oldUrlId WHERE access_url_id = $adminUrlId";
  81. $resU = Database::query($sqlU);
  82. echo "Table $tableSkill updated".PHP_EOL;
  83. //$sqlU = "UPDATE settings_current SET access_url = $oldUrlId WHERE access_url = $adminUrlId";
  84. $sqlU = "UPDATE $tableAccessUrlRelCourse SET access_url_id = $oldUrlId WHERE access_url_id = $adminUrlId";
  85. $resU = Database::query($sqlU);
  86. echo "Table $tableAccessUrlRelCourse updated".PHP_EOL;
  87. $sqlU = "UPDATE $tableAccessUrlRelCourseCategory SET access_url_id = $oldUrlId WHERE access_url_id = $adminUrlId";
  88. $resU = Database::query($sqlU);
  89. echo "Table $tableAccessUrlRelCourseCategory updated".PHP_EOL;
  90. $sqlU = "UPDATE $tableAccessUrlRelSession SET access_url_id = $oldUrlId WHERE access_url_id = $adminUrlId";
  91. $resU = Database::query($sqlU);
  92. echo "Table $tableAccessUrlRelSession updated".PHP_EOL;
  93. $sqlU = "UPDATE $tableAccessUrlRelUser SET access_url_id = $oldUrlId WHERE access_url_id = $adminUrlId";
  94. $resU = Database::query($sqlU);
  95. $sqlI = "INSERT INTO $tableAccessUrlRelUser (access_url_id, user_id) VALUES ($adminUrlId, $userId)";
  96. $resI = Database::query($sqlI);
  97. echo "Table $tableAccessUrlRelUser updated".PHP_EOL;
  98. $sqlU = "UPDATE $tableAccessUrlRelUserGroup SET access_url_id = $oldUrlId WHERE access_url_id = $adminUrlId";
  99. $resU = Database::query($sqlU);
  100. echo "Table $tableAccessUrlRelUserGroup updated".PHP_EOL;
  101. echo "Database updated.".PHP_EOL;
  102. echo "Please set \$_configuration['multiple_access_urls'] to true in the app/config/configuration.php file".PHP_EOL;