2012-07-19 18:03:10 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2012 Facebook, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group phame
|
|
|
|
*/
|
2012-10-15 23:51:04 +02:00
|
|
|
final class PhameBlog extends PhameDAO
|
|
|
|
implements PhabricatorPolicyInterface, PhabricatorMarkupInterface {
|
|
|
|
|
|
|
|
const MARKUP_FIELD_DESCRIPTION = 'markup:description';
|
|
|
|
|
2012-10-13 01:01:33 +02:00
|
|
|
const SKIN_DEFAULT = 'PhabricatorBlogSkin';
|
|
|
|
|
2012-07-19 18:03:10 +02:00
|
|
|
protected $id;
|
|
|
|
protected $phid;
|
|
|
|
protected $name;
|
|
|
|
protected $description;
|
2012-10-01 02:10:27 +02:00
|
|
|
protected $domain;
|
2012-07-19 18:03:10 +02:00
|
|
|
protected $configData;
|
|
|
|
protected $creatorPHID;
|
2012-10-15 23:49:52 +02:00
|
|
|
protected $viewPolicy;
|
|
|
|
protected $editPolicy;
|
|
|
|
protected $joinPolicy;
|
2012-10-13 01:01:33 +02:00
|
|
|
|
2012-07-19 18:03:10 +02:00
|
|
|
private $bloggerPHIDs;
|
|
|
|
private $bloggers;
|
|
|
|
|
2012-10-13 01:01:33 +02:00
|
|
|
static private $requestBlog;
|
|
|
|
|
2012-07-19 18:03:10 +02:00
|
|
|
public function getConfiguration() {
|
|
|
|
return array(
|
|
|
|
self::CONFIG_AUX_PHID => true,
|
|
|
|
self::CONFIG_SERIALIZATION => array(
|
|
|
|
'configData' => self::SERIALIZATION_JSON,
|
|
|
|
),
|
|
|
|
) + parent::getConfiguration();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function generatePHID() {
|
|
|
|
return PhabricatorPHID::generateNewPHID(
|
|
|
|
PhabricatorPHIDConstants::PHID_TYPE_BLOG);
|
|
|
|
}
|
|
|
|
|
2012-10-13 01:01:33 +02:00
|
|
|
public function getSkinRenderer() {
|
2012-10-13 03:05:55 +02:00
|
|
|
try {
|
|
|
|
$skin = newv($this->getSkin(), array());
|
|
|
|
} catch (PhutilMissingSymbolException $ex) {
|
|
|
|
// If this blog has a skin but it's no longer available (for example,
|
|
|
|
// it was uninstalled) just return the default skin.
|
|
|
|
$skin = newv(self::SKIN_DEFAULT, array());
|
|
|
|
}
|
2012-10-13 01:01:33 +02:00
|
|
|
|
2012-10-13 03:05:55 +02:00
|
|
|
return $skin;
|
2012-10-13 01:01:33 +02:00
|
|
|
}
|
|
|
|
|
2012-10-01 02:10:27 +02:00
|
|
|
/**
|
|
|
|
* Makes sure a given custom blog uri is properly configured in DNS
|
|
|
|
* to point at this Phabricator instance. If there is an error in
|
|
|
|
* the configuration, return a string describing the error and how
|
|
|
|
* to fix it. If there is no error, return an empty string.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function validateCustomDomain($custom_domain) {
|
|
|
|
$example_domain = '(e.g. blog.example.com)';
|
|
|
|
$valid = '';
|
|
|
|
|
|
|
|
// note this "uri" should be pretty busted given the desired input
|
|
|
|
// so just use it to test if there's a protocol specified
|
|
|
|
$uri = new PhutilURI($custom_domain);
|
|
|
|
if ($uri->getProtocol()) {
|
|
|
|
return 'Do not specify a protocol, just the domain. '.$example_domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($custom_domain, '/') !== false) {
|
|
|
|
return 'Do not specify a path, just the domain. '.$example_domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($custom_domain, '.') === false) {
|
|
|
|
return 'Custom domain must contain at least one dot (.) because '.
|
|
|
|
'some browsers fail to set cookies on domains such as '.
|
|
|
|
'http://example. '.$example_domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $valid;
|
|
|
|
}
|
|
|
|
|
2012-07-19 18:03:10 +02:00
|
|
|
public function loadBloggerPHIDs() {
|
|
|
|
if (!$this->getPHID()) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->bloggerPHIDs) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->bloggerPHIDs = PhabricatorEdgeQuery::loadDestinationPHIDs(
|
|
|
|
$this->getPHID(),
|
|
|
|
PhabricatorEdgeConfig::TYPE_BLOG_HAS_BLOGGER
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBloggerPHIDs() {
|
|
|
|
if ($this->bloggerPHIDs === null) {
|
|
|
|
throw new Exception(
|
|
|
|
'You must loadBloggerPHIDs before you can getBloggerPHIDs!'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->bloggerPHIDs;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function loadBloggers() {
|
|
|
|
if ($this->bloggers) {
|
|
|
|
return $this->bloggers;
|
|
|
|
}
|
|
|
|
|
|
|
|
$blogger_phids = $this->loadBloggerPHIDs()->getBloggerPHIDs();
|
|
|
|
|
|
|
|
if (empty($blogger_phids)) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$bloggers = id(new PhabricatorObjectHandleData($blogger_phids))
|
|
|
|
->loadHandles();
|
|
|
|
|
|
|
|
$this->attachBloggers($bloggers);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function attachBloggers(array $bloggers) {
|
|
|
|
assert_instances_of($bloggers, 'PhabricatorObjectHandle');
|
|
|
|
|
|
|
|
$this->bloggers = $bloggers;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBloggers() {
|
|
|
|
if ($this->bloggers === null) {
|
|
|
|
throw new Exception(
|
|
|
|
'You must loadBloggers or attachBloggers before you can getBloggers!'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->bloggers;
|
|
|
|
}
|
|
|
|
|
2012-10-13 01:01:33 +02:00
|
|
|
public function getSkin() {
|
|
|
|
$config = coalesce($this->getConfigData(), array());
|
|
|
|
return idx($config, 'skin', self::SKIN_DEFAULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setSkin($skin) {
|
|
|
|
$config = coalesce($this->getConfigData(), array());
|
|
|
|
$config['skin'] = $skin;
|
|
|
|
return $this->setConfigData($config);
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function getSkinOptionsForSelect() {
|
|
|
|
$classes = id(new PhutilSymbolLoader())
|
|
|
|
->setAncestorClass('PhameBlogSkin')
|
|
|
|
->setType('class')
|
|
|
|
->setConcreteOnly(true)
|
|
|
|
->selectSymbolsWithoutLoading();
|
|
|
|
|
|
|
|
return ipull($classes, 'name', 'name');
|
|
|
|
}
|
|
|
|
|
2012-07-19 18:03:10 +02:00
|
|
|
public function getPostListURI() {
|
|
|
|
return $this->getActionURI('posts');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEditURI() {
|
|
|
|
return $this->getActionURI('edit');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEditFilter() {
|
|
|
|
return 'blog/edit/'.$this->getPHID();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDeleteURI() {
|
|
|
|
return $this->getActionURI('delete');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getActionURI($action) {
|
|
|
|
return '/phame/blog/'.$action.'/'.$this->getPHID().'/';
|
|
|
|
}
|
2012-10-13 01:01:33 +02:00
|
|
|
|
|
|
|
public static function setRequestBlog(PhameBlog $blog) {
|
|
|
|
self::$requestBlog = $blog;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getRequestBlog() {
|
|
|
|
return self::$requestBlog;
|
|
|
|
}
|
2012-10-15 23:49:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* -( PhabricatorPolicyInterface Implementation )-------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
public function getCapabilities() {
|
|
|
|
return array(
|
|
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
|
|
|
PhabricatorPolicyCapability::CAN_EDIT,
|
|
|
|
PhabricatorPolicyCapability::CAN_JOIN,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getPolicy($capability) {
|
|
|
|
switch ($capability) {
|
|
|
|
case PhabricatorPolicyCapability::CAN_VIEW:
|
|
|
|
return $this->getViewPolicy();
|
|
|
|
case PhabricatorPolicyCapability::CAN_EDIT:
|
|
|
|
return $this->getEditPolicy();
|
|
|
|
case PhabricatorPolicyCapability::CAN_JOIN:
|
|
|
|
return $this->getJoinPolicy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasAutomaticCapability($capability, PhabricatorUser $user) {
|
|
|
|
$can_edit = PhabricatorPolicyCapability::CAN_EDIT;
|
|
|
|
$can_join = PhabricatorPolicyCapability::CAN_JOIN;
|
|
|
|
|
|
|
|
switch ($capability) {
|
|
|
|
case PhabricatorPolicyCapability::CAN_VIEW:
|
|
|
|
// Users who can edit or post to a blog can always view it.
|
|
|
|
if (PhabricatorPolicyFilter::hasCapability($user, $this, $can_edit)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (PhabricatorPolicyFilter::hasCapability($user, $this, $can_join)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PhabricatorPolicyCapability::CAN_JOIN:
|
|
|
|
// Users who can edit a blog can always post to it.
|
|
|
|
if (PhabricatorPolicyFilter::hasCapability($user, $this, $can_edit)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-15 23:51:04 +02:00
|
|
|
|
|
|
|
/* -( PhabricatorMarkupInterface Implementation )-------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
public function getMarkupFieldKey($field) {
|
|
|
|
$hash = PhabricatorHash::digest($this->getMarkupText($field));
|
|
|
|
return $this->getPHID().':'.$field.':'.$hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function newMarkupEngine($field) {
|
|
|
|
return PhabricatorMarkupEngine::newPhameMarkupEngine();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getMarkupText($field) {
|
|
|
|
return $this->getDescription();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function didMarkupText(
|
|
|
|
$field,
|
|
|
|
$output,
|
|
|
|
PhutilMarkupEngine $engine) {
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function shouldUseMarkupCache($field) {
|
|
|
|
return (bool)$this->getPHID();
|
|
|
|
}
|
|
|
|
|
2012-07-19 18:03:10 +02:00
|
|
|
}
|