|
| 1 | +#!/usr/bin/php -q |
| 2 | + |
| 3 | +<?php |
| 4 | + |
| 5 | +require_once('../scriptsConfig.php'); |
| 6 | + |
| 7 | +use Api\Model\Shared\ProjectListModel; |
| 8 | +use Api\Model\Shared\ProjectModel; |
| 9 | +use Api\Model\Shared\UserListModel; |
| 10 | +use Api\Model\Shared\UserModel; |
| 11 | + |
| 12 | +(php_sapi_name() == 'cli') or die('this script must be run on the command-line'); |
| 13 | + |
| 14 | +// When we change the site name, don't update the date modified timestamps |
| 15 | +define('MAPPERMODEL_NO_TIMESTAMP_UPDATE', true); |
| 16 | + |
| 17 | +class ChangeSiteName |
| 18 | +{ |
| 19 | + public static function getNewSiteName($target, $siteName) { |
| 20 | + if (strpos($siteName, "languageforge") !== false) { |
| 21 | + if ($target == "qa") { |
| 22 | + return "qa.languageforge.org"; |
| 23 | + } |
| 24 | + return "languageforge.localhost"; |
| 25 | + } |
| 26 | + if (strpos($siteName, "scriptureforge") !== false) { |
| 27 | + if ($target == "qa") { |
| 28 | + return "qa.scriptureforge.org"; |
| 29 | + } |
| 30 | + return "scriptureforge.localhost"; |
| 31 | + } |
| 32 | + return ''; |
| 33 | + } |
| 34 | + |
| 35 | + public static function run($mode, $target) |
| 36 | + { |
| 37 | + $testMode = ($mode != 'run'); |
| 38 | + $siteNameCount = array(); |
| 39 | + |
| 40 | + // loop over every project |
| 41 | + $projectList = new ProjectListModel(); |
| 42 | + $projectList->read(); |
| 43 | + |
| 44 | + print "Parsing " . $projectList->count . " projects.\n"; |
| 45 | + foreach ($projectList->entries as $projectParams) { // foreach existing project |
| 46 | + $projectId = $projectParams['id']; |
| 47 | + $project = new ProjectModel($projectId); |
| 48 | + $siteName = $project->siteName; |
| 49 | + $newSiteName = self::getNewSiteName($target, $siteName); |
| 50 | + if ($newSiteName) { |
| 51 | + $project->siteName = $newSiteName; |
| 52 | + if (!$testMode) { |
| 53 | + $project->write(); |
| 54 | + } |
| 55 | + if (array_key_exists($siteName, $siteNameCount)) { |
| 56 | + $siteNameCount[$siteName]++; |
| 57 | + } else { |
| 58 | + $siteNameCount[$siteName] = 1; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + foreach(array_keys($siteNameCount) as $from) { |
| 63 | + $count = $siteNameCount[$from]; |
| 64 | + print "$count $from projects changed site to " . self::getNewSiteName($target, $from) . "\n"; |
| 65 | + } |
| 66 | + print "\n"; |
| 67 | + |
| 68 | + $siteNameCount = array(); |
| 69 | + |
| 70 | + // loop over every user |
| 71 | + $userList = new UserListModel(); |
| 72 | + $userList->read(); |
| 73 | + $userChangeCount = 0; |
| 74 | + print "Parsing " . $userList->count . " users.\n"; |
| 75 | + foreach ($userList->entries as $userParams) { |
| 76 | + $siteNamesToRemove = array(); |
| 77 | + $userId = $userParams['id']; |
| 78 | + $user = new UserModel($userId); |
| 79 | + foreach ($user->siteRole->getArrayCopy() as $siteName => $role) { |
| 80 | + $newSiteName = self::getNewSiteName($target, $siteName); |
| 81 | + if ($newSiteName) { |
| 82 | + $user->siteRole[$newSiteName] = $role; |
| 83 | + $siteNamesToRemove[] = $siteName; |
| 84 | + if (array_key_exists($siteName, $siteNameCount)) { |
| 85 | + $siteNameCount[$siteName]++; |
| 86 | + } else { |
| 87 | + $siteNameCount[$siteName] = 1; |
| 88 | + } |
| 89 | + $userChangeCount++; |
| 90 | + } |
| 91 | + } |
| 92 | + foreach ($siteNamesToRemove as $siteName) { |
| 93 | + unset($user->siteRole[$siteName]); |
| 94 | + } |
| 95 | + if (!$testMode) { |
| 96 | + $user->write(); |
| 97 | + } |
| 98 | + } |
| 99 | + print "$userChangeCount users changed\n\n"; |
| 100 | + foreach(array_keys($siteNameCount) as $from) { |
| 101 | + $count = $siteNameCount[$from]; |
| 102 | + print "$count users of $from projects changed site to " . self::getNewSiteName($target, $from) . "\n"; |
| 103 | + } |
| 104 | + print "\n"; |
| 105 | + } |
| 106 | +} |
| 107 | +if (count($argv) != 3) { |
| 108 | + print "Usage:\n" . "php changeSiteName.php [run|test] [qa|local]\n\n" . |
| 109 | + "examples:\n\n" . "php changeSiteName.php test local\n - test changes but do not make actual changes. " . |
| 110 | + "Change site names to the localhost site available on developer machines\n\n" . |
| 111 | + "php changeSiteName.php run qa\n - change the database. change site names to the QA site\n"; |
| 112 | + exit; |
| 113 | +} |
| 114 | + |
| 115 | +$mode = $argv[1]; |
| 116 | +if ($mode != 'test' && $mode != 'run') { |
| 117 | + print "Error: first argument must be either 'test' or 'run' which determines script run mode\n"; |
| 118 | + exit; |
| 119 | +} |
| 120 | + |
| 121 | +$target = $argv[2]; |
| 122 | +if ($target != 'qa' && $target != 'local') { |
| 123 | + print "Error: second argument must be either 'qa' or 'local' which determines the target site\n"; |
| 124 | + exit; |
| 125 | +} |
| 126 | + |
| 127 | +ChangeSiteName::run($mode, $target); |
0 commit comments