3 final class PhameBlog
extends PhameDAO
5 PhabricatorPolicyInterface
,
6 PhabricatorMarkupInterface
,
7 PhabricatorSubscribableInterface
,
8 PhabricatorFlaggableInterface
,
9 PhabricatorProjectInterface
,
10 PhabricatorDestructibleInterface
,
11 PhabricatorApplicationTransactionInterface
,
12 PhabricatorConduitResultInterface
,
13 PhabricatorFulltextInterface
,
14 PhabricatorFerretInterface
{
18 protected $description;
20 protected $domainFullURI;
21 protected $parentSite;
22 protected $parentDomain;
23 protected $configData;
24 protected $creatorPHID;
25 protected $viewPolicy;
26 protected $editPolicy;
27 protected $interactPolicy;
30 protected $profileImagePHID;
31 protected $headerImagePHID;
33 private $profileImageFile = self
::ATTACHABLE
;
34 private $headerImageFile = self
::ATTACHABLE
;
36 const STATUS_ACTIVE
= 'active';
37 const STATUS_ARCHIVED
= 'archived';
39 protected function getConfiguration() {
41 self
::CONFIG_AUX_PHID
=> true,
42 self
::CONFIG_SERIALIZATION
=> array(
43 'configData' => self
::SERIALIZATION_JSON
,
45 self
::CONFIG_COLUMN_SCHEMA
=> array(
47 'subtitle' => 'text64',
48 'description' => 'text',
49 'domain' => 'text128?',
50 'domainFullURI' => 'text128?',
51 'parentSite' => 'text128?',
52 'parentDomain' => 'text128?',
54 'mailKey' => 'bytes20',
55 'profileImagePHID' => 'phid?',
56 'headerImagePHID' => 'phid?',
58 'editPolicy' => 'policy',
59 'viewPolicy' => 'policy',
60 'interactPolicy' => 'policy',
62 self
::CONFIG_KEY_SCHEMA
=> array(
65 'columns' => array('phid'),
69 'columns' => array('domain'),
73 ) + parent
::getConfiguration();
76 public function save() {
77 if (!$this->getMailKey()) {
78 $this->setMailKey(Filesystem
::readRandomCharacters(20));
80 return parent
::save();
83 public function generatePHID() {
84 return PhabricatorPHID
::generateNewPHID(
85 PhabricatorPhameBlogPHIDType
::TYPECONST
);
88 public static function initializeNewBlog(PhabricatorUser
$actor) {
89 $blog = id(new PhameBlog())
90 ->setCreatorPHID($actor->getPHID())
91 ->setStatus(self
::STATUS_ACTIVE
)
92 ->setViewPolicy(PhabricatorPolicies
::getMostOpenPolicy())
93 ->setEditPolicy(PhabricatorPolicies
::POLICY_USER
)
94 ->setInteractPolicy(PhabricatorPolicies
::POLICY_USER
);
99 public function isArchived() {
100 return ($this->getStatus() == self
::STATUS_ARCHIVED
);
103 public static function getStatusNameMap() {
105 self
::STATUS_ACTIVE
=> pht('Active'),
106 self
::STATUS_ARCHIVED
=> pht('Archived'),
111 * Makes sure a given custom blog uri is properly configured in DNS
112 * to point at this Phabricator instance. If there is an error in
113 * the configuration, return a string describing the error and how
114 * to fix it. If there is no error, return an empty string.
118 public function validateCustomDomain($domain_full_uri) {
119 $example_domain = 'http://blog.example.com/';
120 $label = pht('Invalid');
122 // note this "uri" should be pretty busted given the desired input
123 // so just use it to test if there's a protocol specified
124 $uri = new PhutilURI($domain_full_uri);
125 $domain = $uri->getDomain();
126 $protocol = $uri->getProtocol();
127 $path = $uri->getPath();
128 $supported_protocols = array('http', 'https');
130 if (!in_array($protocol, $supported_protocols)) {
132 'The custom domain should include a valid protocol in the URI '.
133 '(for example, "%s"). Valid protocols are "http" or "https".',
137 if (strlen($path) && $path != '/') {
139 'The custom domain should not specify a path (hosting a Phame '.
140 'blog at a path is currently not supported). Instead, just provide '.
141 'the bare domain name (for example, "%s").',
145 if (strpos($domain, '.') === false) {
147 'The custom domain should contain at least one dot (.) because '.
148 'some browsers fail to set cookies on domains without a dot. '.
149 'Instead, use a normal looking domain name like "%s".',
153 if (!PhabricatorEnv
::getEnvConfig('policy.allow-public')) {
154 $href = PhabricatorEnv
::getProductionURI(
155 '/config/edit/policy.allow-public/');
157 'For custom domains to work, this Phabricator instance must be '.
158 'configured to allow the public access policy. Configure this '.
159 'setting %s, or ask an administrator to configure this setting. '.
160 'The domain can be specified later once this setting has been '.
164 array('href' => $href),
171 public function getLiveURI() {
172 if (strlen($this->getDomain())) {
173 return $this->getExternalLiveURI();
175 return $this->getInternalLiveURI();
179 public function getExternalLiveURI() {
180 $uri = new PhutilURI($this->getDomainFullURI());
181 PhabricatorEnv
::requireValidRemoteURIForLink($uri);
185 public function getExternalParentURI() {
186 $uri = $this->getParentDomain();
187 PhabricatorEnv
::requireValidRemoteURIForLink($uri);
191 public function getInternalLiveURI() {
192 return '/phame/live/'.$this->getID().'/';
195 public function getViewURI() {
196 return '/phame/blog/view/'.$this->getID().'/';
199 public function getManageURI() {
200 return '/phame/blog/manage/'.$this->getID().'/';
203 public function getProfileImageURI() {
204 return $this->getProfileImageFile()->getBestURI();
207 public function attachProfileImageFile(PhabricatorFile
$file) {
208 $this->profileImageFile
= $file;
212 public function getProfileImageFile() {
213 return $this->assertAttached($this->profileImageFile
);
216 public function getHeaderImageURI() {
217 return $this->getHeaderImageFile()->getBestURI();
220 public function attachHeaderImageFile(PhabricatorFile
$file) {
221 $this->headerImageFile
= $file;
225 public function getHeaderImageFile() {
226 return $this->assertAttached($this->headerImageFile
);
230 /* -( PhabricatorPolicyInterface Implementation )-------------------------- */
233 public function getCapabilities() {
235 PhabricatorPolicyCapability
::CAN_VIEW
,
236 PhabricatorPolicyCapability
::CAN_INTERACT
,
237 PhabricatorPolicyCapability
::CAN_EDIT
,
242 public function getPolicy($capability) {
243 switch ($capability) {
244 case PhabricatorPolicyCapability
::CAN_VIEW
:
245 return $this->getViewPolicy();
246 case PhabricatorPolicyCapability
::CAN_INTERACT
:
247 return $this->getInteractPolicy();
248 case PhabricatorPolicyCapability
::CAN_EDIT
:
249 return $this->getEditPolicy();
253 public function hasAutomaticCapability($capability, PhabricatorUser
$user) {
254 $can_edit = PhabricatorPolicyCapability
::CAN_EDIT
;
256 switch ($capability) {
257 case PhabricatorPolicyCapability
::CAN_VIEW
:
258 // Users who can edit or post to a blog can always view it.
259 if (PhabricatorPolicyFilter
::hasCapability($user, $this, $can_edit)) {
269 public function describeAutomaticCapability($capability) {
270 switch ($capability) {
271 case PhabricatorPolicyCapability
::CAN_VIEW
:
273 'Users who can edit a blog can always view it.');
280 /* -( PhabricatorMarkupInterface Implementation )-------------------------- */
283 public function getMarkupFieldKey($field) {
284 $content = $this->getMarkupText($field);
285 return PhabricatorMarkupEngine
::digestRemarkupContent($this, $content);
289 public function newMarkupEngine($field) {
290 return PhabricatorMarkupEngine
::newPhameMarkupEngine();
294 public function getMarkupText($field) {
295 return $this->getDescription();
299 public function didMarkupText(
302 PhutilMarkupEngine
$engine) {
306 public function shouldUseMarkupCache($field) {
307 return (bool)$this->getPHID();
310 /* -( PhabricatorDestructibleInterface )----------------------------------- */
312 public function destroyObjectPermanently(
313 PhabricatorDestructionEngine
$engine) {
315 $this->openTransaction();
317 $posts = id(new PhamePostQuery())
318 ->setViewer($engine->getViewer())
319 ->withBlogPHIDs(array($this->getPHID()))
321 foreach ($posts as $post) {
322 $engine->destroyObject($post);
326 $this->saveTransaction();
330 /* -( PhabricatorApplicationTransactionInterface )------------------------- */
333 public function getApplicationTransactionEditor() {
334 return new PhameBlogEditor();
337 public function getApplicationTransactionTemplate() {
338 return new PhameBlogTransaction();
342 /* -( PhabricatorSubscribableInterface Implementation )-------------------- */
345 public function isAutomaticallySubscribed($phid) {
350 /* -( PhabricatorConduitResultInterface )---------------------------------- */
353 public function getFieldSpecificationsForConduit() {
355 id(new PhabricatorConduitSearchFieldSpecification())
358 ->setDescription(pht('The name of the blog.')),
359 id(new PhabricatorConduitSearchFieldSpecification())
360 ->setKey('description')
362 ->setDescription(pht('Blog description.')),
363 id(new PhabricatorConduitSearchFieldSpecification())
366 ->setDescription(pht('Archived or active status.')),
370 public function getFieldValuesForConduit() {
372 'name' => $this->getName(),
373 'description' => $this->getDescription(),
374 'status' => $this->getStatus(),
378 public function getConduitSearchAttachments() {
383 /* -( PhabricatorFulltextInterface )--------------------------------------- */
385 public function newFulltextEngine() {
386 return new PhameBlogFulltextEngine();
390 /* -( PhabricatorFerretInterface )----------------------------------------- */
393 public function newFerretEngine() {
394 return new PhameBlogFerretEngine();