forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathPhabricatorConduitMethodQuery.php
158 lines (134 loc) · 3.99 KB
/
PhabricatorConduitMethodQuery.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
<?php
final class PhabricatorConduitMethodQuery
extends PhabricatorCursorPagedPolicyAwareQuery {
private $isDeprecated;
private $isStable;
private $isUnstable;
private $applicationNames;
private $nameContains;
private $methods;
private $isInternal;
public function withMethods(array $methods) {
$this->methods = $methods;
return $this;
}
public function withNameContains($name_contains) {
$this->nameContains = $name_contains;
return $this;
}
public function withIsStable($is_stable) {
$this->isStable = $is_stable;
return $this;
}
public function withIsUnstable($is_unstable) {
$this->isUnstable = $is_unstable;
return $this;
}
public function withIsDeprecated($is_deprecated) {
$this->isDeprecated = $is_deprecated;
return $this;
}
public function withIsInternal($is_internal) {
$this->isInternal = $is_internal;
return $this;
}
protected function loadPage() {
$methods = $this->getAllMethods();
$methods = $this->filterMethods($methods);
return $methods;
}
private function getAllMethods() {
return id(new PhutilClassMapQuery())
->setAncestorClass('ConduitAPIMethod')
->setSortMethod('getSortOrder')
->execute();
}
private function filterMethods(array $methods) {
foreach ($methods as $key => $method) {
$application = $method->getApplication();
if (!$application) {
continue;
}
if (!$application->isInstalled()) {
unset($methods[$key]);
}
}
$status = array(
ConduitAPIMethod::METHOD_STATUS_STABLE => $this->isStable,
ConduitAPIMethod::METHOD_STATUS_FROZEN => $this->isStable,
ConduitAPIMethod::METHOD_STATUS_DEPRECATED => $this->isDeprecated,
ConduitAPIMethod::METHOD_STATUS_UNSTABLE => $this->isUnstable,
);
// Only apply status filters if any of them are set.
if (array_filter($status)) {
foreach ($methods as $key => $method) {
$keep = idx($status, $method->getMethodStatus());
if (!$keep) {
unset($methods[$key]);
}
}
}
if ($this->nameContains) {
$needle = phutil_utf8_strtolower($this->nameContains);
foreach ($methods as $key => $method) {
$haystack = $method->getAPIMethodName();
$haystack = phutil_utf8_strtolower($haystack);
if (strpos($haystack, $needle) === false) {
unset($methods[$key]);
}
}
}
if ($this->methods) {
$map = array_fuse($this->methods);
foreach ($methods as $key => $method) {
$needle = $method->getAPIMethodName();
if (empty($map[$needle])) {
unset($methods[$key]);
}
}
}
if ($this->isInternal !== null) {
foreach ($methods as $key => $method) {
if ($method->isInternalAPI() !== $this->isInternal) {
unset($methods[$key]);
}
}
}
return $methods;
}
protected function willFilterPage(array $methods) {
$application_phids = array();
foreach ($methods as $method) {
$application = $method->getApplication();
if ($application === null) {
continue;
}
$application_phids[] = $application->getPHID();
}
if ($application_phids) {
$applications = id(new PhabricatorApplicationQuery())
->setParentQuery($this)
->setViewer($this->getViewer())
->withPHIDs($application_phids)
->execute();
$applications = mpull($applications, null, 'getPHID');
} else {
$applications = array();
}
// Remove methods which belong to an application the viewer can not see.
foreach ($methods as $key => $method) {
$application = $method->getApplication();
if ($application === null) {
continue;
}
if (empty($applications[$application->getPHID()])) {
$this->didRejectResult($method);
unset($methods[$key]);
}
}
return $methods;
}
public function getQueryApplicationClass() {
return 'PhabricatorConduitApplication';
}
}