Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/packages/storage/PhabricatorPackagesPublisher.php
13458 views
1
<?php
2
3
final class PhabricatorPackagesPublisher
4
extends PhabricatorPackagesDAO
5
implements
6
PhabricatorPolicyInterface,
7
PhabricatorApplicationTransactionInterface,
8
PhabricatorDestructibleInterface,
9
PhabricatorSubscribableInterface,
10
PhabricatorProjectInterface,
11
PhabricatorConduitResultInterface,
12
PhabricatorNgramsInterface {
13
14
protected $name;
15
protected $publisherKey;
16
protected $editPolicy;
17
18
public static function initializeNewPublisher(PhabricatorUser $actor) {
19
$packages_application = id(new PhabricatorApplicationQuery())
20
->setViewer($actor)
21
->withClasses(array('PhabricatorPackagesApplication'))
22
->executeOne();
23
24
$edit_policy = $packages_application->getPolicy(
25
PhabricatorPackagesPublisherDefaultEditCapability::CAPABILITY);
26
27
return id(new self())
28
->setEditPolicy($edit_policy);
29
}
30
31
protected function getConfiguration() {
32
return array(
33
self::CONFIG_AUX_PHID => true,
34
self::CONFIG_COLUMN_SCHEMA => array(
35
'name' => 'sort64',
36
'publisherKey' => 'sort64',
37
),
38
self::CONFIG_KEY_SCHEMA => array(
39
'key_publisher' => array(
40
'columns' => array('publisherKey'),
41
'unique' => true,
42
),
43
),
44
) + parent::getConfiguration();
45
}
46
47
public function generatePHID() {
48
return PhabricatorPHID::generateNewPHID(
49
PhabricatorPackagesPublisherPHIDType::TYPECONST);
50
}
51
52
public function getURI() {
53
$publisher_key = $this->getPublisherKey();
54
return "/package/{$publisher_key}/";
55
}
56
57
public static function assertValidPublisherName($value) {
58
$length = phutil_utf8_strlen($value);
59
if (!$length) {
60
throw new Exception(
61
pht(
62
'Publisher name "%s" is not valid: publisher names are required.',
63
$value));
64
}
65
66
$max_length = 64;
67
if ($length > $max_length) {
68
throw new Exception(
69
pht(
70
'Publisher name "%s" is not valid: publisher names must not be '.
71
'more than %s characters long.',
72
$value,
73
new PhutilNumber($max_length)));
74
}
75
}
76
77
public static function assertValidPublisherKey($value) {
78
$length = phutil_utf8_strlen($value);
79
if (!$length) {
80
throw new Exception(
81
pht(
82
'Publisher key "%s" is not valid: publisher keys are required.',
83
$value));
84
}
85
86
$max_length = 64;
87
if ($length > $max_length) {
88
throw new Exception(
89
pht(
90
'Publisher key "%s" is not valid: publisher keys must not be '.
91
'more than %s characters long.',
92
$value,
93
new PhutilNumber($max_length)));
94
}
95
96
if (!preg_match('/^[a-z]+\z/', $value)) {
97
throw new Exception(
98
pht(
99
'Publisher key "%s" is not valid: publisher keys may only contain '.
100
'lowercase latin letters.',
101
$value));
102
}
103
}
104
105
106
/* -( PhabricatorSubscribableInterface )----------------------------------- */
107
108
109
public function isAutomaticallySubscribed($phid) {
110
return false;
111
}
112
113
114
/* -( Policy Interface )--------------------------------------------------- */
115
116
117
public function getCapabilities() {
118
return array(
119
PhabricatorPolicyCapability::CAN_VIEW,
120
PhabricatorPolicyCapability::CAN_EDIT,
121
);
122
}
123
124
public function getPolicy($capability) {
125
switch ($capability) {
126
case PhabricatorPolicyCapability::CAN_VIEW:
127
return PhabricatorPolicies::getMostOpenPolicy();
128
case PhabricatorPolicyCapability::CAN_EDIT:
129
return $this->getEditPolicy();
130
}
131
}
132
133
public function hasAutomaticCapability($capability, PhabricatorUser $user) {
134
return false;
135
}
136
137
138
/* -( PhabricatorDestructibleInterface )----------------------------------- */
139
140
141
public function destroyObjectPermanently(
142
PhabricatorDestructionEngine $engine) {
143
$viewer = $engine->getViewer();
144
145
$this->openTransaction();
146
147
$packages = id(new PhabricatorPackagesPackageQuery())
148
->setViewer($viewer)
149
->withPublisherPHIDs(array($this->getPHID()))
150
->execute();
151
foreach ($packages as $package) {
152
$engine->destroyObject($package);
153
}
154
155
$this->delete();
156
157
$this->saveTransaction();
158
}
159
160
161
/* -( PhabricatorApplicationTransactionInterface )------------------------- */
162
163
164
public function getApplicationTransactionEditor() {
165
return new PhabricatorPackagesPublisherEditor();
166
}
167
168
public function getApplicationTransactionTemplate() {
169
return new PhabricatorPackagesPublisherTransaction();
170
}
171
172
173
/* -( PhabricatorNgramsInterface )----------------------------------------- */
174
175
176
public function newNgrams() {
177
return array(
178
id(new PhabricatorPackagesPublisherNameNgrams())
179
->setValue($this->getName()),
180
);
181
}
182
183
184
/* -( PhabricatorConduitResultInterface )---------------------------------- */
185
186
187
public function getFieldSpecificationsForConduit() {
188
return array(
189
id(new PhabricatorConduitSearchFieldSpecification())
190
->setKey('name')
191
->setType('string')
192
->setDescription(pht('The name of the publisher.')),
193
id(new PhabricatorConduitSearchFieldSpecification())
194
->setKey('publisherKey')
195
->setType('string')
196
->setDescription(pht('The unique key of the publisher.')),
197
);
198
}
199
200
public function getFieldValuesForConduit() {
201
return array(
202
'name' => $this->getName(),
203
'publisherKey' => $this->getPublisherKey(),
204
);
205
}
206
207
public function getConduitSearchAttachments() {
208
return array();
209
}
210
211
212
}
213
214