public static function run($userId, $mode = 'test')
 {
     $testMode = $mode != 'run';
     $message = '';
     $userlist = new UserListModel();
     $userlist->read();
     $badAvatarLinks = 0;
     foreach ($userlist->entries as $userParams) {
         // foreach existing user
         $userId = $userParams['id'];
         $user = new UserProfileModel($userId);
         if (strpos($user->avatar_ref, '/') !== FALSE or strpos($user->avatar_ref, '\\') !== FALSE) {
             if ($user->avatar_color != '' && $user->avatar_shape != '') {
                 $newRef = $user->avatar_color . '-' . $user->avatar_shape . '-128x128.png';
             } else {
                 $newRef = 'anonymoose.png';
             }
             $message .= "Changed user {$userId} 's avatar from " . $user->avatar_ref . " to {$newRef}\n";
             $user->avatar_ref = $newRef;
             $badAvatarLinks++;
             if (!$testMode) {
                 $user->write();
             }
         }
     }
     if ($badAvatarLinks > 0) {
         $message .= "\n\nFixed {$badAvatarLinks} bad avatar URLs\n\n";
     } else {
         $message .= "\n\nNo bad avatar URLs were found\n\n";
     }
     return $message;
 }
 public function testUserList_HadOnlyUsers()
 {
     $userList = new UserListModel();
     $userList->read();
     foreach ($userList->entries as $entry) {
         $this->assertArrayHasKey('username', $entry, "Key 'username' not found " . print_r($entry, true));
     }
 }
 public function run($userId, $mode = 'test')
 {
     $testMode = $mode != 'run';
     $message = "Fix site roles integrity\n\n";
     // loop over every project
     $projectlist = new ProjectListModel();
     $projectlist->read();
     $fixCount = array();
     $userNoRoleCount = 0;
     foreach ($projectlist->entries as $projectParams) {
         // foreach existing project
         $projectId = $projectParams['id'];
         $project = new ProjectModel($projectId);
         $hostname = $project->siteName;
         $website = Website::get($hostname);
         $fixCount[$hostname] = 0;
         $projectUserRefs = array_keys($project->users->getArrayCopy());
         //$message .= "-------------  " . $project->projectName . "\n";
         foreach ($projectUserRefs as $userId) {
             // foreach user that is a member of this project
             $user = new UserModel($userId);
             if (!array_key_exists($hostname, $user->siteRole) && $user->username != '') {
                 $message .= "Fixed user '" . $user->username . "' who did not have a site role on " . $hostname . "\n";
                 $fixCount[$hostname]++;
                 $user->siteRole[$hostname] = $website->userDefaultSiteRole;
                 if (!$testMode) {
                     $user->write();
                 }
             }
         }
     }
     // loop over users who do not belong to any projects
     $userlist = new UserListModel();
     $userlist->read();
     foreach ($userlist->entries as $userParams) {
         // foreach existing user
         $userId = $userParams['id'];
         $user = new UserModel($userId);
         if (count($user->projects->refs) == 0 && count(array_keys($user->siteRole->getArrayCopy())) == 0) {
             $userNoRoleCount++;
             //$message .= "Warning: user '" . $user->username . "' has no projects and no siteRoles on any site!\n";
         }
     }
     foreach ($fixCount as $site => $count) {
         if ($count > 0) {
             $message .= "\n\n{$site} : Fixed {$count} non-existent site roles \n\n";
         } else {
             $message .= "\n\n{$site} : Nothing to do \n\n";
         }
     }
     if ($userNoRoleCount > 0) {
         $message .= "Warning: {$userNoRoleCount} useless users had no projects and no siteRoles on any site!\n";
     }
     return $message;
 }
 public function run($mode = 'test')
 {
     $testMode = $mode == 'test';
     $message = "";
     $userlist = new UserListModel();
     $userlist->read();
     $badUserRoles = 0;
     foreach ($userlist->entries as $userParams) {
         // foreach existing user
         $userId = $userParams['id'];
         $user = new UserModel($userId);
         if (!$user->role) {
             $user->role = SystemRoles::USER;
             if (!$testMode) {
                 $user->write();
             }
             $badUserRoles++;
             $message .= "Fixed role of user {$userId}\n";
         }
     }
     if ($badUserRoles > 0) {
         $message .= "\n\nFixed {$badUserRoles} non-existent user roles from the users collection\n\n";
     } else {
         $message .= "\n\nNo non-existent user roles found in the users collection\n\n";
     }
     $projectlist = new ProjectListModel();
     $projectlist->read();
     $badProjectUserRoles = 0;
     foreach ($projectlist->entries as $projectParams) {
         // foreach existing project
         $projectId = $projectParams['id'];
         $project = new ProjectModel($projectId);
         $projectUserRefs = array_keys($project->users);
         foreach ($projectUserRefs as $ref) {
             // foreach user that is a member of this project
             if (!isset($project->users[$ref]->role)) {
                 $project->users[$ref]->role = ProjectRoles::CONTRIBUTOR;
                 $badProjectUserRoles++;
                 $message .= "Fixed role of user {$ref} for project {$projectId}\n";
             }
         }
         if (!$testMode) {
             $project->write();
         }
     }
     if ($badProjectUserRoles > 0) {
         $message .= "\n\nFixed {$badProjectUserRoles} non-existent user roles from the projects collection\n\n";
     } else {
         $message .= "\n\nNo non-existent user roles found in the projects collection\n\n";
     }
     return $message;
 }
 public function run($mode = 'test')
 {
     $testMode = $mode == 'test';
     $message = "";
     $userlist = new UserListModel();
     $userlist->read();
     $userIds = array_map(function ($e) {
         return $e['id'];
     }, $userlist->entries);
     $projectlist = new ProjectListModel();
     $projectlist->read();
     $projectIds = array_map(function ($e) {
         return $e['id'];
     }, $projectlist->entries);
     $deadProjectLinks = 0;
     foreach ($userlist->entries as $userParams) {
         // foreach existing user
         $userId = $userParams['id'];
         $user = new UserModel($userId);
         $userProjectRefs = $user->projects->refs;
         foreach ($userProjectRefs as $ref) {
             // foreach project the user belongs to
             if (!in_array($ref, $projectIds)) {
                 $user->removeProject($ref);
                 // remove dead project link
                 $deadProjectLinks++;
                 $message .= "Removed dead project link {$ref} from user {$userId}\n";
             }
         }
         if (!$testMode) {
             $user->write();
         }
     }
     if ($deadProjectLinks > 0) {
         $message .= "\n\nRemoved {$deadProjectLinks} dead project links from the users collection\n\n";
     } else {
         $message .= "\n\nNo dead project links were found\n\n";
     }
     $deadUserLinks = 0;
     foreach ($projectlist->entries as $projectParams) {
         // foreach existing project
         $projectId = $projectParams['id'];
         $project = new ProjectModel($projectId);
         $projectUserRefs = array_keys($project->users);
         foreach ($projectUserRefs as $ref) {
             // foreach user that is a member of this project
             if (!in_array($ref, $userIds)) {
                 $project->removeUser($ref);
                 // remove dead user link
                 $deadUserLinks++;
                 $message .= "Removed dead user link {$ref} for project {$projectId}\n";
             }
         }
         if (!$testMode) {
             $project->write();
         }
     }
     if ($deadUserLinks > 0) {
         $message .= "\n\nRemoved {$deadUserLinks} dead user links from the projects collection\n\n";
     } else {
         $message .= "\n\nNo dead user links were found\n\n";
     }
     return $message;
 }
 public function run($mode = 'test')
 {
     $testMode = $mode == 'test';
     $message = "";
     $userList = new UserListModel();
     $userList->read();
     $userIds = array_map(function ($e) {
         return $e['id'];
     }, $userList->entries);
     $projectList = new ProjectListModel();
     $projectList->read();
     $projectIds = array_map(function ($e) {
         return $e['id'];
     }, $projectList->entries);
     $deadCommentUserRefs = 0;
     $deadAnswerUserRefs = 0;
     foreach ($projectIds as $projectId) {
         $project = new ProjectModel($projectId);
         $textList = new TextListModel($project);
         $textList->read();
         $textIds = array_map(function ($e) {
             return $e['id'];
         }, $textList->entries);
         foreach ($textIds as $textId) {
             $questionList = new QuestionListModel($project, $textId);
             $questionList->read();
             $questionIds = array_map(function ($e) {
                 return $e['id'];
             }, $questionList->entries);
             foreach ($questionIds as $questionId) {
                 $question = new QuestionModel($project, $questionId);
                 foreach ($question->answers as $answerId => $answer) {
                     foreach ($answer->comments as $commentId => $comment) {
                         /** @var IdReference $ref */
                         $ref = $comment->userRef;
                         if (!empty($ref->id) && !in_array($ref->asString(), $userIds)) {
                             $comment->userRef->id = '';
                             if (!$testMode) {
                                 $question->writeComment($project->databaseName(), $questionId, $answerId, $comment);
                             }
                             $deadCommentUserRefs++;
                             $message .= "Removed dead user-comment ref {$ref} from question {$questionId}, answer {$answerId}, comment {$commentId}\n";
                         }
                     }
                     $ref = $answer->userRef;
                     if (!empty($ref->id) && !in_array($ref->asString(), $userIds)) {
                         $answer->userRef->id = '';
                         if (!$testMode) {
                             $question->writeAnswer($answer);
                         }
                         $deadAnswerUserRefs++;
                         $message .= "Removed dead user-answer ref {$ref} from question {$questionId}, answer {$answerId}\n";
                     }
                 }
             }
         }
     }
     if ($deadAnswerUserRefs > 0) {
         $message .= "\n\nRemoved dead user references from {$deadAnswerUserRefs} answers\n\n";
     } else {
         $message .= "\n\nNo dead user references were found in answers\n\n";
     }
     if ($deadCommentUserRefs > 0) {
         $message .= "\n\nRemoved dead user references from {$deadCommentUserRefs} comments\n\n";
     } else {
         $message .= "\n\nNo dead user references were found in comments\n\n";
     }
     return $message;
 }
 /**
  * @return UserListModel
  */
 public static function listUsers()
 {
     $list = new UserListModel();
     $list->read();
     $projectListModel = new ProjectListModel();
     $projectListModel->read();
     $projectList = array();
     foreach ($projectListModel->entries as $p) {
         $projectList[$p['id']] = $p;
     }
     foreach ($list->entries as $key => $item) {
         if (array_key_exists('projects', $item)) {
             $projectIds = $item['projects'];
             $list->entries[$key]['projects'] = array();
             foreach ($projectIds as $id) {
                 $list->entries[$key]['projects'][] = $projectList[(string) $id];
             }
         }
     }
     // Default sort on username (currently needed to sort on Site Admin because MongoDB doesn't do case insensitive sorts)
     usort($list->entries, function ($a, $b) {
         $sortOn = 'username';
         if (array_key_exists($sortOn, $a) && array_key_exists($sortOn, $b)) {
             return strtolower($a[$sortOn]) > strtolower($b[$sortOn]) ? 1 : -1;
         } else {
             return 0;
         }
     });
     return $list;
 }
 /**
  * UpdateDBSiteName: for dev or local site, migrate the mongodb sitename according to the mapping
  * @param bool $runForReal
  */
 public function UpdateDBSiteName($runForReal = false)
 {
     $siteNameMap = array();
     if ($this->environment == "dev") {
         print "Site names being converted for DEVELOPMENT SERVER khrap\n";
         $siteNameMap['scriptureforge.org'] = 'dev.scriptureforge.org';
         $siteNameMap['jamaicanpsalms.scriptureforge.org'] = 'jamaicanpsalms.dev.scriptureforge.org';
         $siteNameMap['languageforge.org'] = 'dev.languageforge.org';
     } else {
         if ($this->environment == "local") {
             print "Site names being converted for LOCAL MACHINE khrap\n";
             $siteNameMap['scriptureforge.org'] = 'scriptureforge.local';
             $siteNameMap['jamaicanpsalms.scriptureforge.org'] = 'jamaicanpsalms.scriptureforge.local';
             $siteNameMap['languageforge.org'] = 'languageforge.local';
         }
     }
     $siteNameCount = array();
     $userCount = array();
     foreach ($siteNameMap as $from => $to) {
         $siteNameCount[$from] = 0;
         $userCount[$from] = 0;
     }
     // loop over every project
     $projectList = new ProjectListModel();
     $projectList->read();
     foreach ($projectList->entries as $projectParams) {
         $project = new ProjectModel($projectParams['id']);
         $siteName = $project->siteName;
         if (array_key_exists($siteName, $siteNameMap)) {
             $project->siteName = $siteNameMap[$siteName];
             $siteNameCount[$siteName]++;
             if ($runForReal) {
                 $project->write();
             }
         }
     }
     // loop over every user
     $userList = new UserListModel();
     $userList->read();
     foreach ($userList->entries as $userParams) {
         $user = new UserModel($userParams['id']);
         $newSiteRole = array();
         //$message .= $user->username . "\n";
         foreach ($user->siteRole as $siteName => $role) {
             //$message .= "$siteName : $role\n";
             if (array_key_exists($siteName, $siteNameMap)) {
                 //$message .= "MATCH: $siteName\n";
                 $newSiteRole[$siteNameMap[$siteName]] = $role;
                 $userCount[$siteName]++;
             }
         }
         $user->siteRole->exchangeArray($newSiteRole);
         if ($runForReal) {
             $user->write();
         }
     }
     // report changes
     foreach ($siteNameMap as $from => $to) {
         $count = $siteNameCount[$from];
         if ($count > 0) {
             print "{$count} {$from} projects changed site to {$to}\n";
         } else {
             print "No {$from} projects encountered\n";
         }
         $count = $userCount[$from];
         if ($count > 0) {
             print "{$count} {$from} users changed site to {$to}\n";
         } else {
             print "No {$from} users encountered\n";
         }
     }
 }