Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / settings / query / PhabricatorUserPreferencesQuery.php
blob1a6133724db9c7402bd84873df2e09b7e1b47acf
1 <?php
3 final class PhabricatorUserPreferencesQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery {
6 private $ids;
7 private $phids;
8 private $userPHIDs;
9 private $builtinKeys;
10 private $hasUserPHID;
11 private $users = array();
12 private $synthetic;
14 public function withIDs(array $ids) {
15 $this->ids = $ids;
16 return $this;
19 public function withPHIDs(array $phids) {
20 $this->phids = $phids;
21 return $this;
24 public function withHasUserPHID($is_user) {
25 $this->hasUserPHID = $is_user;
26 return $this;
29 public function withUserPHIDs(array $phids) {
30 $this->userPHIDs = $phids;
31 return $this;
34 public function withUsers(array $users) {
35 assert_instances_of($users, 'PhabricatorUser');
36 $this->users = mpull($users, null, 'getPHID');
37 $this->withUserPHIDs(array_keys($this->users));
38 return $this;
41 public function withBuiltinKeys(array $keys) {
42 $this->builtinKeys = $keys;
43 return $this;
46 /**
47 * Always return preferences for every queried user.
49 * If no settings exist for a user, a new empty settings object with
50 * appropriate defaults is returned.
52 * @param bool True to generate synthetic preferences for missing users.
54 public function needSyntheticPreferences($synthetic) {
55 $this->synthetic = $synthetic;
56 return $this;
59 public function newResultObject() {
60 return new PhabricatorUserPreferences();
63 protected function loadPage() {
64 $preferences = $this->loadStandardPage($this->newResultObject());
66 if ($this->synthetic) {
67 $user_map = mpull($preferences, null, 'getUserPHID');
68 foreach ($this->userPHIDs as $user_phid) {
69 if (isset($user_map[$user_phid])) {
70 continue;
72 $preferences[] = $this->newResultObject()
73 ->setUserPHID($user_phid);
77 return $preferences;
80 protected function willFilterPage(array $prefs) {
81 $user_phids = mpull($prefs, 'getUserPHID');
82 $user_phids = array_filter($user_phids);
84 // If some of the preferences are attached to users, try to use any objects
85 // we were handed first. If we're missing some, load them.
87 if ($user_phids) {
88 $users = $this->users;
90 $user_phids = array_fuse($user_phids);
91 $load_phids = array_diff_key($user_phids, $users);
92 $load_phids = array_keys($load_phids);
94 if ($load_phids) {
95 $load_users = id(new PhabricatorPeopleQuery())
96 ->setViewer($this->getViewer())
97 ->withPHIDs($load_phids)
98 ->execute();
99 $load_users = mpull($load_users, null, 'getPHID');
100 $users += $load_users;
102 } else {
103 $users = array();
106 $need_global = array();
107 foreach ($prefs as $key => $pref) {
108 $user_phid = $pref->getUserPHID();
109 if (!$user_phid) {
110 $pref->attachUser(null);
111 continue;
114 $need_global[] = $pref;
116 $user = idx($users, $user_phid);
117 if (!$user) {
118 $this->didRejectResult($pref);
119 unset($prefs[$key]);
120 continue;
123 $pref->attachUser($user);
126 // If we loaded any user preferences, load the global defaults and attach
127 // them if they exist.
128 if ($need_global) {
129 $global = id(new self())
130 ->setViewer($this->getViewer())
131 ->withBuiltinKeys(
132 array(
133 PhabricatorUserPreferences::BUILTIN_GLOBAL_DEFAULT,
135 ->executeOne();
136 if ($global) {
137 foreach ($need_global as $pref) {
138 $pref->attachDefaultSettings($global);
143 return $prefs;
146 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
147 $where = parent::buildWhereClauseParts($conn);
149 if ($this->ids !== null) {
150 $where[] = qsprintf(
151 $conn,
152 'id IN (%Ld)',
153 $this->ids);
156 if ($this->phids !== null) {
157 $where[] = qsprintf(
158 $conn,
159 'phid IN (%Ls)',
160 $this->phids);
163 if ($this->userPHIDs !== null) {
164 $where[] = qsprintf(
165 $conn,
166 'userPHID IN (%Ls)',
167 $this->userPHIDs);
170 if ($this->builtinKeys !== null) {
171 $where[] = qsprintf(
172 $conn,
173 'builtinKey IN (%Ls)',
174 $this->builtinKeys);
177 if ($this->hasUserPHID !== null) {
178 if ($this->hasUserPHID) {
179 $where[] = qsprintf(
180 $conn,
181 'userPHID IS NOT NULL');
182 } else {
183 $where[] = qsprintf(
184 $conn,
185 'userPHID IS NULL');
189 return $where;
192 public function getQueryApplicationClass() {
193 return 'PhabricatorSettingsApplication';