Path: blob/master/src/infrastructure/management/PhabricatorManagementWorkflow.php
12249 views
<?php12abstract class PhabricatorManagementWorkflow extends PhutilArgumentWorkflow {34public function isExecutable() {5return true;6}78public function getViewer() {9// Some day, we might provide a more general viewer mechanism to scripts.10// For now, workflows can call this method for convenience and future11// flexibility.12return PhabricatorUser::getOmnipotentUser();13}1415protected function parseTimeArgument($time) {16if (!strlen($time)) {17return null;18}1920$epoch = strtotime($time);21if ($epoch <= 0) {22throw new PhutilArgumentUsageException(23pht('Unable to parse time "%s".', $time));24}25return $epoch;26}2728protected function newContentSource() {29return PhabricatorContentSource::newForSource(30PhabricatorConsoleContentSource::SOURCECONST);31}3233protected function logInfo($label, $message) {34$this->logRaw(35tsprintf(36"**<bg:blue> %s </bg>** %s\n",37$label,38$message));39}4041protected function logOkay($label, $message) {42$this->logRaw(43tsprintf(44"**<bg:green> %s </bg>** %s\n",45$label,46$message));47}4849protected function logWarn($label, $message) {50$this->logRaw(51tsprintf(52"**<bg:yellow> %s </bg>** %s\n",53$label,54$message));55}5657protected function logFail($label, $message) {58$this->logRaw(59tsprintf(60"**<bg:red> %s </bg>** %s\n",61$label,62$message));63}6465private function logRaw($message) {66fprintf(STDERR, '%s', $message);67}6869final protected function loadUsersFromArguments(array $identifiers) {70if (!$identifiers) {71return array();72}7374$ids = array();75$phids = array();76$usernames = array();7778$user_type = PhabricatorPeopleUserPHIDType::TYPECONST;7980foreach ($identifiers as $identifier) {81// If the value is a user PHID, treat as a PHID.82if (phid_get_type($identifier) === $user_type) {83$phids[$identifier] = $identifier;84continue;85}8687// If the value is "@..." and then some text, treat it as a username.88if ((strlen($identifier) > 1) && ($identifier[0] == '@')) {89$usernames[$identifier] = substr($identifier, 1);90continue;91}9293// If the value is digits, treat it as both an ID and a username.94// Entirely numeric usernames, like "1234", are valid.95if (ctype_digit($identifier)) {96$ids[$identifier] = $identifier;97$usernames[$identifier] = $identifier;98continue;99}100101// Otherwise, treat it as an unescaped username.102$usernames[$identifier] = $identifier;103}104105$viewer = $this->getViewer();106$results = array();107108if ($phids) {109$users = id(new PhabricatorPeopleQuery())110->setViewer($viewer)111->withPHIDs($phids)112->execute();113foreach ($users as $user) {114$phid = $user->getPHID();115$results[$phid][] = $user;116}117}118119if ($usernames) {120$users = id(new PhabricatorPeopleQuery())121->setViewer($viewer)122->withUsernames($usernames)123->execute();124125$reverse_map = array();126foreach ($usernames as $identifier => $username) {127$username = phutil_utf8_strtolower($username);128$reverse_map[$username][] = $identifier;129}130131foreach ($users as $user) {132$username = $user->getUsername();133$username = phutil_utf8_strtolower($username);134135$reverse_identifiers = idx($reverse_map, $username, array());136137if (count($reverse_identifiers) > 1) {138throw new PhutilArgumentUsageException(139pht(140'Multiple user identifiers (%s) correspond to the same user. '.141'Identify each user exactly once.',142implode(', ', $reverse_identifiers)));143}144145foreach ($reverse_identifiers as $reverse_identifier) {146$results[$reverse_identifier][] = $user;147}148}149}150151if ($ids) {152$users = id(new PhabricatorPeopleQuery())153->setViewer($viewer)154->withIDs($ids)155->execute();156157foreach ($users as $user) {158$id = $user->getID();159$results[$id][] = $user;160}161}162163$list = array();164foreach ($identifiers as $identifier) {165$users = idx($results, $identifier, array());166if (!$users) {167throw new PhutilArgumentUsageException(168pht(169'No user "%s" exists. Specify users by username, ID, or PHID.',170$identifier));171}172173if (count($users) > 1) {174// This can happen if you have a user "@25", a user with ID 25, and175// specify "--user 25". You can disambiguate this by specifying176// "--user @25".177throw new PhutilArgumentUsageException(178pht(179'Identifier "%s" matches multiple users. Specify each user '.180'unambiguously with "@username" or by using user PHIDs.',181$identifier));182}183184$list[] = head($users);185}186187return $list;188}189190}191192193