forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhabricatorObjectListQuery.php
171 lines (142 loc) · 4.75 KB
/
PhabricatorObjectListQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
final class PhabricatorObjectListQuery {
private $viewer;
private $objectList;
private $allowedTypes = array();
private $allowPartialResults;
public function setAllowPartialResults($allow_partial_results) {
$this->allowPartialResults = $allow_partial_results;
return $this;
}
public function getAllowPartialResults() {
return $this->allowPartialResults;
}
public function setAllowedTypes(array $allowed_types) {
$this->allowedTypes = $allowed_types;
return $this;
}
public function getAllowedTypes() {
return $this->allowedTypes;
}
public function setViewer(PhabricatorUser $viewer) {
$this->viewer = $viewer;
return $this;
}
public function getViewer() {
return $this->viewer;
}
public function setObjectList($object_list) {
$this->objectList = $object_list;
return $this;
}
public function getObjectList() {
return $this->objectList;
}
public function execute() {
$names = $this->getObjectList();
$names = array_unique(array_filter(preg_split('/[\s,]+/', $names)));
$objects = $this->loadObjects($names);
$types = array();
foreach ($objects as $name => $object) {
$types[phid_get_type($object->getPHID())][] = $name;
}
$invalid = array();
if ($this->getAllowedTypes()) {
$allowed = array_fuse($this->getAllowedTypes());
foreach ($types as $type => $names_of_type) {
if (empty($allowed[$type])) {
$invalid[] = $names_of_type;
}
}
}
$invalid = array_mergev($invalid);
$missing = array();
foreach ($names as $name) {
if (empty($objects[$name])) {
$missing[] = $name;
}
}
// NOTE: We could couple this less tightly with Differential, but it is
// currently the only thing that uses it, and we'd have to add a lot of
// extra API to loosen this. It's not clear that this will be useful
// elsewhere any time soon, so let's cross that bridge when we come to it.
if (!$this->getAllowPartialResults()) {
if ($invalid && $missing) {
throw new DifferentialFieldParseException(
pht(
'The objects you have listed include objects of the wrong '.
'type (%s) and objects which do not exist (%s).',
implode(', ', $invalid),
implode(', ', $missing)));
} else if ($invalid) {
throw new DifferentialFieldParseException(
pht(
'The objects you have listed include objects of the wrong '.
'type (%s).',
implode(', ', $invalid)));
} else if ($missing) {
throw new DifferentialFieldParseException(
pht(
'The objects you have listed include objects which do not '.
'exist (%s).',
implode(', ', $missing)));
}
}
return array_values(array_unique(mpull($objects, 'getPHID')));
}
private function loadObjects($names) {
// First, try to load visible objects using monograms. This covers most
// object types, but does not cover users or user email addresses.
$query = id(new PhabricatorObjectQuery())
->setViewer($this->getViewer())
->withNames($names);
$query->execute();
$objects = $query->getNamedResults();
$results = array();
foreach ($names as $key => $name) {
if (isset($objects[$name])) {
$results[$name] = $objects[$name];
unset($names[$key]);
}
}
if ($names) {
// We still have some symbols we haven't been able to resolve, so try to
// load users. Try by username first...
$users = id(new PhabricatorPeopleQuery())
->setViewer($this->getViewer())
->withUsernames($names)
->execute();
$user_map = array();
foreach ($users as $user) {
$user_map[phutil_utf8_strtolower($user->getUsername())] = $user;
}
foreach ($names as $key => $name) {
$normal_name = phutil_utf8_strtolower($name);
if (isset($user_map[$normal_name])) {
$results[$name] = $user_map[$normal_name];
unset($names[$key]);
}
}
}
$mailing_list_app = PhabricatorApplication::getByClass(
'PhabricatorApplicationMailingLists');
if ($mailing_list_app->isInstalled()) {
if ($names) {
// We still haven't been able to resolve everything; try mailing lists
// by name as a last resort.
$lists = id(new PhabricatorMailingListQuery())
->setViewer($this->getViewer())
->withNames($names)
->execute();
$lists = mpull($lists, null, 'getName');
foreach ($names as $key => $name) {
if (isset($lists[$name])) {
$results[$name] = $lists[$name];
unset($names[$key]);
}
}
}
}
return $results;
}
}