2012-04-12 22:09:04 +02:00
|
|
|
<?php
|
|
|
|
|
2012-10-15 23:51:04 +02:00
|
|
|
final class PhamePost extends PhameDAO
|
2013-05-09 23:40:43 +02:00
|
|
|
implements
|
|
|
|
PhabricatorPolicyInterface,
|
|
|
|
PhabricatorMarkupInterface,
|
|
|
|
PhabricatorTokenReceiverInterface {
|
2012-10-15 23:51:04 +02:00
|
|
|
|
|
|
|
const MARKUP_FIELD_BODY = 'markup:body';
|
2012-10-17 17:36:33 +02:00
|
|
|
const MARKUP_FIELD_SUMMARY = 'markup:summary';
|
2012-04-12 22:09:04 +02:00
|
|
|
|
|
|
|
const VISIBILITY_DRAFT = 0;
|
|
|
|
const VISIBILITY_PUBLISHED = 1;
|
|
|
|
|
|
|
|
protected $bloggerPHID;
|
|
|
|
protected $title;
|
|
|
|
protected $phameTitle;
|
|
|
|
protected $body;
|
|
|
|
protected $visibility;
|
|
|
|
protected $configData;
|
|
|
|
protected $datePublished;
|
2012-10-15 23:50:04 +02:00
|
|
|
protected $blogPHID;
|
|
|
|
|
|
|
|
private $blog;
|
|
|
|
|
2014-03-11 23:51:53 +01:00
|
|
|
public static function initializePost(
|
|
|
|
PhabricatorUser $blogger,
|
|
|
|
PhameBlog $blog) {
|
|
|
|
|
|
|
|
$post = id(new PhamePost())
|
|
|
|
->setBloggerPHID($blogger->getPHID())
|
|
|
|
->setBlogPHID($blog->getPHID())
|
|
|
|
->setBlog($blog)
|
|
|
|
->setDatePublished(0)
|
|
|
|
->setVisibility(self::VISIBILITY_DRAFT);
|
|
|
|
return $post;
|
|
|
|
}
|
|
|
|
|
2012-10-15 23:50:04 +02:00
|
|
|
public function setBlog(PhameBlog $blog) {
|
|
|
|
$this->blog = $blog;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBlog() {
|
|
|
|
return $this->blog;
|
|
|
|
}
|
2012-04-12 22:09:04 +02:00
|
|
|
|
2013-01-09 04:53:34 +01:00
|
|
|
public function getViewURI() {
|
2012-04-12 22:09:04 +02:00
|
|
|
// go for the pretty uri if we can
|
2013-01-09 04:53:34 +01:00
|
|
|
$domain = ($this->blog ? $this->blog->getDomain() : '');
|
|
|
|
if ($domain) {
|
2012-04-12 22:09:04 +02:00
|
|
|
$phame_title = PhabricatorSlug::normalize($this->getPhameTitle());
|
2013-01-09 04:53:34 +01:00
|
|
|
return 'http://'.$domain.'/post/'.$phame_title;
|
2012-04-12 22:09:04 +02:00
|
|
|
}
|
2013-01-09 04:53:34 +01:00
|
|
|
$uri = '/phame/post/view/'.$this->getID().'/';
|
|
|
|
return PhabricatorEnv::getProductionURI($uri);
|
2012-04-12 22:09:04 +02:00
|
|
|
}
|
|
|
|
|
2014-04-30 22:19:14 +02:00
|
|
|
public function getEditURI() {
|
|
|
|
return '/phame/post/edit/'.$this->getID().'/';
|
|
|
|
}
|
|
|
|
|
2012-04-12 22:09:04 +02:00
|
|
|
public function isDraft() {
|
|
|
|
return $this->getVisibility() == self::VISIBILITY_DRAFT;
|
|
|
|
}
|
|
|
|
|
2012-07-19 18:03:10 +02:00
|
|
|
public function getHumanName() {
|
|
|
|
if ($this->isDraft()) {
|
|
|
|
$name = 'draft';
|
|
|
|
} else {
|
|
|
|
$name = 'post';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
2012-04-12 22:09:04 +02:00
|
|
|
public function getCommentsWidget() {
|
|
|
|
$config_data = $this->getConfigData();
|
|
|
|
if (empty($config_data)) {
|
|
|
|
return 'none';
|
|
|
|
}
|
|
|
|
return idx($config_data, 'comments_widget', 'none');
|
|
|
|
}
|
2012-10-15 23:50:04 +02:00
|
|
|
|
2012-04-12 22:09:04 +02:00
|
|
|
public function getConfiguration() {
|
|
|
|
return array(
|
|
|
|
self::CONFIG_AUX_PHID => true,
|
|
|
|
self::CONFIG_SERIALIZATION => array(
|
|
|
|
'configData' => self::SERIALIZATION_JSON,
|
|
|
|
),
|
2014-09-24 22:50:57 +02:00
|
|
|
self::CONFIG_COLUMN_SCHEMA => array(
|
|
|
|
'title' => 'text255',
|
2014-10-01 17:18:53 +02:00
|
|
|
'phameTitle' => 'sort64',
|
2014-09-24 22:50:57 +02:00
|
|
|
'visibility' => 'uint32',
|
2014-10-01 16:59:44 +02:00
|
|
|
|
|
|
|
// T6203/NULLABILITY
|
|
|
|
// These seem like they should always be non-null?
|
|
|
|
'blogPHID' => 'phid?',
|
|
|
|
'body' => 'text?',
|
|
|
|
'configData' => 'text?',
|
|
|
|
|
|
|
|
// T6203/NULLABILITY
|
|
|
|
// This one probably should be nullable?
|
|
|
|
'datePublished' => 'epoch',
|
2014-09-24 22:50:57 +02:00
|
|
|
),
|
|
|
|
self::CONFIG_KEY_SCHEMA => array(
|
|
|
|
'key_phid' => null,
|
|
|
|
'phid' => array(
|
|
|
|
'columns' => array('phid'),
|
|
|
|
'unique' => true,
|
|
|
|
),
|
|
|
|
'phameTitle' => array(
|
|
|
|
'columns' => array('bloggerPHID', 'phameTitle'),
|
|
|
|
'unique' => true,
|
|
|
|
),
|
|
|
|
'bloggerPosts' => array(
|
2014-10-01 16:55:09 +02:00
|
|
|
'columns' => array(
|
|
|
|
'bloggerPHID',
|
|
|
|
'visibility',
|
|
|
|
'datePublished',
|
|
|
|
'id',
|
|
|
|
),
|
2014-09-24 22:50:57 +02:00
|
|
|
),
|
|
|
|
),
|
2012-04-12 22:09:04 +02:00
|
|
|
) + parent::getConfiguration();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function generatePHID() {
|
|
|
|
return PhabricatorPHID::generateNewPHID(
|
2014-07-24 00:05:46 +02:00
|
|
|
PhabricatorPhamePostPHIDType::TYPECONST);
|
2012-04-12 22:09:04 +02:00
|
|
|
}
|
|
|
|
|
2014-03-11 23:51:53 +01:00
|
|
|
public function toDictionary() {
|
|
|
|
return array(
|
|
|
|
'id' => $this->getID(),
|
|
|
|
'phid' => $this->getPHID(),
|
|
|
|
'blogPHID' => $this->getBlogPHID(),
|
|
|
|
'bloggerPHID' => $this->getBloggerPHID(),
|
|
|
|
'viewURI' => $this->getViewURI(),
|
|
|
|
'title' => $this->getTitle(),
|
|
|
|
'phameTitle' => $this->getPhameTitle(),
|
|
|
|
'body' => $this->getBody(),
|
|
|
|
'summary' => PhabricatorMarkupEngine::summarize($this->getBody()),
|
|
|
|
'datePublished' => $this->getDatePublished(),
|
|
|
|
'published' => !$this->isDraft(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-04-12 22:09:04 +02:00
|
|
|
public static function getVisibilityOptionsForSelect() {
|
|
|
|
return array(
|
|
|
|
self::VISIBILITY_DRAFT => 'Draft: visible only to me.',
|
|
|
|
self::VISIBILITY_PUBLISHED => 'Published: visible to the whole world.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCommentsWidgetOptionsForSelect() {
|
|
|
|
$current = $this->getCommentsWidget();
|
|
|
|
$options = array();
|
|
|
|
|
|
|
|
if ($current == 'facebook' ||
|
2014-07-22 13:18:15 +02:00
|
|
|
PhabricatorFacebookAuthProvider::getFacebookApplicationID()) {
|
2012-04-12 22:09:04 +02:00
|
|
|
$options['facebook'] = 'Facebook';
|
|
|
|
}
|
|
|
|
if ($current == 'disqus' ||
|
|
|
|
PhabricatorEnv::getEnvConfig('disqus.shortname')) {
|
|
|
|
$options['disqus'] = 'Disqus';
|
|
|
|
}
|
|
|
|
$options['none'] = 'None';
|
|
|
|
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
2012-10-15 23:50:04 +02:00
|
|
|
|
|
|
|
/* -( PhabricatorPolicyInterface Implementation )-------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
public function getCapabilities() {
|
|
|
|
return array(
|
|
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
|
|
|
PhabricatorPolicyCapability::CAN_EDIT,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getPolicy($capability) {
|
|
|
|
// Draft posts are visible only to the author. Published posts are visible
|
|
|
|
// to whoever the blog is visible to.
|
|
|
|
|
|
|
|
switch ($capability) {
|
|
|
|
case PhabricatorPolicyCapability::CAN_VIEW:
|
|
|
|
if (!$this->isDraft() && $this->getBlog()) {
|
|
|
|
return $this->getBlog()->getViewPolicy();
|
|
|
|
} else {
|
|
|
|
return PhabricatorPolicies::POLICY_NOONE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PhabricatorPolicyCapability::CAN_EDIT:
|
|
|
|
return PhabricatorPolicies::POLICY_NOONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function hasAutomaticCapability($capability, PhabricatorUser $user) {
|
|
|
|
// A blog post's author can always view it, and is the only user allowed
|
|
|
|
// to edit it.
|
|
|
|
|
|
|
|
switch ($capability) {
|
|
|
|
case PhabricatorPolicyCapability::CAN_VIEW:
|
|
|
|
case PhabricatorPolicyCapability::CAN_EDIT:
|
|
|
|
return ($user->getPHID() == $this->getBloggerPHID());
|
|
|
|
}
|
2013-09-27 17:43:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function describeAutomaticCapability($capability) {
|
|
|
|
return pht(
|
|
|
|
'The author of a blog post can always view and edit it.');
|
2012-10-15 23:50:04 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 23:51:04 +02:00
|
|
|
|
|
|
|
/* -( PhabricatorMarkupInterface Implementation )-------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
public function getMarkupFieldKey($field) {
|
2012-10-15 23:51:30 +02:00
|
|
|
$hash = PhabricatorHash::digest($this->getMarkupText($field));
|
|
|
|
return $this->getPHID().':'.$field.':'.$hash;
|
2012-10-15 23:51:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function newMarkupEngine($field) {
|
|
|
|
return PhabricatorMarkupEngine::newPhameMarkupEngine();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getMarkupText($field) {
|
2012-10-17 17:36:33 +02:00
|
|
|
switch ($field) {
|
|
|
|
case self::MARKUP_FIELD_BODY:
|
|
|
|
return $this->getBody();
|
|
|
|
case self::MARKUP_FIELD_SUMMARY:
|
|
|
|
return PhabricatorMarkupEngine::summarize($this->getBody());
|
|
|
|
}
|
2012-10-15 23:51:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function didMarkupText(
|
|
|
|
$field,
|
|
|
|
$output,
|
|
|
|
PhutilMarkupEngine $engine) {
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function shouldUseMarkupCache($field) {
|
|
|
|
return (bool)$this->getPHID();
|
|
|
|
}
|
|
|
|
|
2013-05-09 23:40:43 +02:00
|
|
|
/* -( PhabricatorTokenReceiverInterface )---------------------------------- */
|
|
|
|
|
|
|
|
public function getUsersToNotifyOfTokenGiven() {
|
|
|
|
return array(
|
|
|
|
$this->getBloggerPHID(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-04-12 22:09:04 +02:00
|
|
|
}
|