Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / phame / storage / PhameBlog.php
blob71a518622538e6c516f1961adcb2dac0f5006750
1 <?php
3 final class PhameBlog extends PhameDAO
4 implements
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';
18 protected $name;
19 protected $subtitle;
20 protected $description;
21 protected $domain;
22 protected $domainFullURI;
23 protected $parentSite;
24 protected $parentDomain;
25 protected $configData;
26 protected $creatorPHID;
27 protected $viewPolicy;
28 protected $editPolicy;
29 protected $status;
30 protected $mailKey;
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() {
41 return array(
42 self::CONFIG_AUX_PHID => true,
43 self::CONFIG_SERIALIZATION => array(
44 'configData' => self::SERIALIZATION_JSON,
46 self::CONFIG_COLUMN_SCHEMA => array(
47 'name' => 'text64',
48 'subtitle' => 'text64',
49 'description' => 'text',
50 'domain' => 'text128?',
51 'domainFullURI' => 'text128?',
52 'parentSite' => 'text128?',
53 'parentDomain' => 'text128?',
54 'status' => 'text32',
55 'mailKey' => 'bytes20',
56 'profileImagePHID' => 'phid?',
57 'headerImagePHID' => 'phid?',
59 // T6203/NULLABILITY
60 // These policies should always be non-null.
61 'editPolicy' => 'policy?',
62 'viewPolicy' => 'policy?',
64 self::CONFIG_KEY_SCHEMA => array(
65 'key_phid' => null,
66 'phid' => array(
67 'columns' => array('phid'),
68 'unique' => true,
70 'domain' => array(
71 'columns' => array('domain'),
72 'unique' => true,
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);
96 return $blog;
99 public function isArchived() {
100 return ($this->getStatus() == self::STATUS_ARCHIVED);
103 public static function getStatusNameMap() {
104 return array(
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.
116 * @return 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)) {
131 return pht(
132 'The custom domain should include a valid protocol in the URI '.
133 '(for example, "%s"). Valid protocols are "http" or "https".',
134 $example_domain);
137 if (strlen($path) && $path != '/') {
138 return pht(
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").',
142 $example_domain);
145 if (strpos($domain, '.') === false) {
146 return pht(
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".',
150 $example_domain);
153 if (!PhabricatorEnv::getEnvConfig('policy.allow-public')) {
154 $href = PhabricatorEnv::getProductionURI(
155 '/config/edit/policy.allow-public/');
156 return pht(
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 '.
161 'changed.',
162 phutil_tag(
163 'a',
164 array('href' => $href),
165 pht('here')));
168 return null;
171 public function getLiveURI() {
172 if (strlen($this->getDomain())) {
173 return $this->getExternalLiveURI();
174 } else {
175 return $this->getInternalLiveURI();
179 public function getExternalLiveURI() {
180 $uri = new PhutilURI($this->getDomainFullURI());
181 PhabricatorEnv::requireValidRemoteURIForLink($uri);
182 return (string)$uri;
185 public function getExternalParentURI() {
186 $uri = $this->getParentDomain();
187 PhabricatorEnv::requireValidRemoteURIForLink($uri);
188 return (string)$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;
209 return $this;
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;
222 return $this;
225 public function getHeaderImageFile() {
226 return $this->assertAttached($this->headerImageFile);
230 /* -( PhabricatorPolicyInterface Implementation )-------------------------- */
233 public function getCapabilities() {
234 return array(
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)) {
257 return true;
259 break;
262 return false;
266 public function describeAutomaticCapability($capability) {
267 switch ($capability) {
268 case PhabricatorPolicyCapability::CAN_VIEW:
269 return pht(
270 'Users who can edit a blog can always view it.');
273 return null;
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(
297 $field,
298 $output,
299 PhutilMarkupEngine $engine) {
300 return $output;
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()))
317 ->execute();
318 foreach ($posts as $post) {
319 $engine->destroyObject($post);
321 $this->delete();
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) {
343 return false;
347 /* -( PhabricatorConduitResultInterface )---------------------------------- */
350 public function getFieldSpecificationsForConduit() {
351 return array(
352 id(new PhabricatorConduitSearchFieldSpecification())
353 ->setKey('name')
354 ->setType('string')
355 ->setDescription(pht('The name of the blog.')),
356 id(new PhabricatorConduitSearchFieldSpecification())
357 ->setKey('description')
358 ->setType('string')
359 ->setDescription(pht('Blog description.')),
360 id(new PhabricatorConduitSearchFieldSpecification())
361 ->setKey('status')
362 ->setType('string')
363 ->setDescription(pht('Archived or active status.')),
367 public function getFieldValuesForConduit() {
368 return array(
369 'name' => $this->getName(),
370 'description' => $this->getDescription(),
371 'status' => $this->getStatus(),
375 public function getConduitSearchAttachments() {
376 return array();
380 /* -( PhabricatorFulltextInterface )--------------------------------------- */
382 public function newFulltextEngine() {
383 return new PhameBlogFulltextEngine();
387 /* -( PhabricatorFerretInterface )----------------------------------------- */
390 public function newFerretEngine() {
391 return new PhameBlogFerretEngine();