Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / diffusion / query / DiffusionResolveUserQuery.php
blob8ad13f660e56f609d7e3134335212a2688c1f9e8
1 <?php
3 /**
4 * Resolve an author or committer name, like
5 * `"Abraham Lincoln <alincoln@logcab.in>"`, into a valid Phabricator user
6 * account, like `@alincoln`.
7 */
8 final class DiffusionResolveUserQuery extends Phobject {
10 private $name;
12 public function withName($name) {
13 $this->name = $name;
14 return $this;
17 public function execute() {
18 return $this->findUserPHID($this->name);
21 private function findUserPHID($user_name) {
22 if (!strlen($user_name)) {
23 return null;
26 $phid = $this->findUserByUserName($user_name);
27 if ($phid) {
28 return $phid;
31 $phid = $this->findUserByEmailAddress($user_name);
32 if ($phid) {
33 return $phid;
36 $phid = $this->findUserByRealName($user_name);
37 if ($phid) {
38 return $phid;
41 // No hits yet, try to parse it as an email address.
43 $email = new PhutilEmailAddress($user_name);
45 $phid = $this->findUserByEmailAddress($email->getAddress());
46 if ($phid) {
47 return $phid;
50 $display_name = $email->getDisplayName();
51 if ($display_name) {
52 $phid = $this->findUserByUserName($display_name);
53 if ($phid) {
54 return $phid;
57 $phid = $this->findUserByRealName($display_name);
58 if ($phid) {
59 return $phid;
63 return null;
67 private function findUserByUserName($user_name) {
68 $by_username = id(new PhabricatorUser())->loadOneWhere(
69 'userName = %s',
70 $user_name);
72 if ($by_username) {
73 return $by_username->getPHID();
76 return null;
80 private function findUserByRealName($real_name) {
81 // Note, real names are not guaranteed unique, which is why we do it this
82 // way.
83 $by_realname = id(new PhabricatorUser())->loadAllWhere(
84 'realName = %s',
85 $real_name);
87 if (count($by_realname) == 1) {
88 return head($by_realname)->getPHID();
91 return null;
95 private function findUserByEmailAddress($email_address) {
96 $by_email = PhabricatorUser::loadOneWithEmailAddress($email_address);
98 if ($by_email) {
99 return $by_email->getPHID();
102 return null;