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
{
16 const MARKUP_FIELD_DESCRIPTION
= 'markup:description';
20 protected $description;
22 protected $domainFullURI;
23 protected $parentSite;
24 protected $parentDomain;
25 protected $configData;
26 protected $creatorPHID;
27 protected $viewPolicy;
28 protected $editPolicy;
31 protected $profileImagePHID;
32 protected $headerImagePHID;
34 private $profileImageFile = self
::ATTACHABLE
;
35 private $headerImageFile = self
::ATTACHABLE
;
37 const STATUS_ACTIVE
= 'active';
38 const STATUS_ARCHIVED
= 'archived';
40 protected function getConfiguration() {
42 self
::CONFIG_AUX_PHID
=> true,
43 self
::CONFIG_SERIALIZATION
=> array(
44 'configData' => self
::SERIALIZATION_JSON
,
46 self
::CONFIG_COLUMN_SCHEMA
=> array(
48 'subtitle' => 'text64',
49 'description' => 'text',
50 'domain' => 'text128?',
51 'domainFullURI' => 'text128?',
52 'parentSite' => 'text128?',
53 'parentDomain' => 'text128?',
55 'mailKey' => 'bytes20',
56 'profileImagePHID' => 'phid?',
57 'headerImagePHID' => 'phid?',
60 // These policies should always be non-null.
61 'editPolicy' => 'policy?',
62 'viewPolicy' => 'policy?',
64 self
::CONFIG_KEY_SCHEMA
=> array(
67 'columns' => array('phid'),
71 'columns' => array('domain'),
75 ) + parent
::getConfiguration();
78 public function save() {
79 if (!$this->getMailKey()) {
80 $this->setMailKey(Filesystem
::readRandomCharacters(20));
82 return parent
::save();
85 public function generatePHID() {
86 return PhabricatorPHID
::generateNewPHID(
87 PhabricatorPhameBlogPHIDType
::TYPECONST
);
90 public static function initializeNewBlog(PhabricatorUser
$actor) {
91 $blog = id(new PhameBlog())
92 ->setCreatorPHID($actor->getPHID())
93 ->setStatus(self
::STATUS_ACTIVE
)
94 ->setViewPolicy(PhabricatorPolicies
::getMostOpenPolicy())
95 ->setEditPolicy(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_EDIT
,
241 public function getPolicy($capability) {
242 switch ($capability) {
243 case PhabricatorPolicyCapability
::CAN_VIEW
:
244 return $this->getViewPolicy();
245 case PhabricatorPolicyCapability
::CAN_EDIT
:
246 return $this->getEditPolicy();
250 public function hasAutomaticCapability($capability, PhabricatorUser
$user) {
251 $can_edit = PhabricatorPolicyCapability
::CAN_EDIT
;
253 switch ($capability) {
254 case PhabricatorPolicyCapability
::CAN_VIEW
:
255 // Users who can edit or post to a blog can always view it.
256 if (PhabricatorPolicyFilter
::hasCapability($user, $this, $can_edit)) {
266 public function describeAutomaticCapability($capability) {
267 switch ($capability) {
268 case PhabricatorPolicyCapability
::CAN_VIEW
:
270 'Users who can edit a blog can always view it.');
277 /* -( PhabricatorMarkupInterface Implementation )-------------------------- */
280 public function getMarkupFieldKey($field) {
281 $content = $this->getMarkupText($field);
282 return PhabricatorMarkupEngine
::digestRemarkupContent($this, $content);
286 public function newMarkupEngine($field) {
287 return PhabricatorMarkupEngine
::newPhameMarkupEngine();
291 public function getMarkupText($field) {
292 return $this->getDescription();
296 public function didMarkupText(
299 PhutilMarkupEngine
$engine) {
303 public function shouldUseMarkupCache($field) {
304 return (bool)$this->getPHID();
307 /* -( PhabricatorDestructibleInterface )----------------------------------- */
309 public function destroyObjectPermanently(
310 PhabricatorDestructionEngine
$engine) {
312 $this->openTransaction();
314 $posts = id(new PhamePostQuery())
315 ->setViewer($engine->getViewer())
316 ->withBlogPHIDs(array($this->getPHID()))
318 foreach ($posts as $post) {
319 $engine->destroyObject($post);
323 $this->saveTransaction();
327 /* -( PhabricatorApplicationTransactionInterface )------------------------- */
330 public function getApplicationTransactionEditor() {
331 return new PhameBlogEditor();
334 public function getApplicationTransactionTemplate() {
335 return new PhameBlogTransaction();
339 /* -( PhabricatorSubscribableInterface Implementation )-------------------- */
342 public function isAutomaticallySubscribed($phid) {
347 /* -( PhabricatorConduitResultInterface )---------------------------------- */
350 public function getFieldSpecificationsForConduit() {
352 id(new PhabricatorConduitSearchFieldSpecification())
355 ->setDescription(pht('The name of the blog.')),
356 id(new PhabricatorConduitSearchFieldSpecification())
357 ->setKey('description')
359 ->setDescription(pht('Blog description.')),
360 id(new PhabricatorConduitSearchFieldSpecification())
363 ->setDescription(pht('Archived or active status.')),
367 public function getFieldValuesForConduit() {
369 'name' => $this->getName(),
370 'description' => $this->getDescription(),
371 'status' => $this->getStatus(),
375 public function getConduitSearchAttachments() {
380 /* -( PhabricatorFulltextInterface )--------------------------------------- */
382 public function newFulltextEngine() {
383 return new PhameBlogFulltextEngine();
387 /* -( PhabricatorFerretInterface )----------------------------------------- */
390 public function newFerretEngine() {
391 return new PhameBlogFerretEngine();