Changeset View
Changeset View
Standalone View
Standalone View
src/applications/audit/storage/PhabricatorAuditComment.php
<?php | <?php | ||||
final class PhabricatorAuditComment extends PhabricatorAuditDAO | final class PhabricatorAuditComment | ||||
implements PhabricatorMarkupInterface { | implements PhabricatorMarkupInterface { | ||||
const METADATA_ADDED_AUDITORS = 'added-auditors'; | const METADATA_ADDED_AUDITORS = 'added-auditors'; | ||||
const METADATA_ADDED_CCS = 'added-ccs'; | const METADATA_ADDED_CCS = 'added-ccs'; | ||||
const MARKUP_FIELD_BODY = 'markup:body'; | const MARKUP_FIELD_BODY = 'markup:body'; | ||||
protected $phid; | |||||
protected $actorPHID; | |||||
protected $targetPHID; | |||||
protected $action; | |||||
protected $content = ''; | |||||
protected $metadata = array(); | |||||
private $proxyComment; | private $proxyComment; | ||||
private $proxy; | |||||
public function __construct() { | |||||
$this->proxy = new PhabricatorAuditTransaction(); | |||||
} | |||||
public function __clone() { | |||||
$this->proxy = clone $this->proxy; | |||||
if ($this->proxyComment) { | |||||
$this->proxyComment = clone $this->proxyComment; | |||||
} | |||||
} | |||||
public static function newFromModernTransaction( | |||||
PhabricatorAuditTransaction $xaction) { | |||||
$obj = new PhabricatorAuditComment(); | |||||
$obj->proxy = $xaction; | |||||
if ($xaction->hasComment()) { | |||||
$obj->proxyComment = $xaction->getComment(); | |||||
} | |||||
return $obj; | |||||
} | |||||
public static function loadComments( | public static function loadComments( | ||||
PhabricatorUser $viewer, | PhabricatorUser $viewer, | ||||
$commit_phid) { | $commit_phid) { | ||||
$comments = id(new PhabricatorAuditComment())->loadAllWhere( | $xactions = id(new PhabricatorAuditTransactionQuery()) | ||||
'targetPHID = %s', | ->setViewer($viewer) | ||||
$commit_phid); | ->withObjectPHIDs(array($commit_phid)) | ||||
->needComments(true) | |||||
if ($comments) { | ->execute(); | ||||
$table = new PhabricatorAuditTransactionComment(); | |||||
$conn_r = $table->establishConnection('r'); | $comments = array(); | ||||
foreach ($xactions as $xaction) { | |||||
$data = queryfx_all( | $comments[] = self::newFromModernTransaction($xaction); | ||||
$conn_r, | |||||
'SELECT * FROM %T WHERE legacyCommentID IN (%Ld) AND pathID IS NULL', | |||||
$table->getTableName(), | |||||
mpull($comments, 'getID')); | |||||
$texts = $table->loadAllFromArray($data); | |||||
$texts = mpull($texts, null, 'getLegacyCommentID'); | |||||
foreach ($comments as $comment) { | |||||
$text = idx($texts, $comment->getID()); | |||||
if ($text) { | |||||
$comment->setProxyComment($text); | |||||
} | } | ||||
return $comments; | |||||
} | } | ||||
public function getPHID() { | |||||
return $this->proxy->getPHID(); | |||||
} | } | ||||
return $comments; | public function getActorPHID() { | ||||
return $this->proxy->getAuthorPHID(); | |||||
} | } | ||||
public function getConfiguration() { | public function setActorPHID($actor_phid) { | ||||
return array( | $this->proxy->setAuthorPHID($actor_phid); | ||||
joshuaspence: Do you need to do `$this->proxyComment->setAuthorPHID($actor_phid)` as well? | |||||
Not Done Inline ActionsThe save() workflow and CommentEditor do this for us, although it's probably slightly more correct/predictable to do it here too. epriestley: The save() workflow and CommentEditor do this for us, although it's probably slightly more… | |||||
self::CONFIG_SERIALIZATION => array( | return $this; | ||||
'metadata' => self::SERIALIZATION_JSON, | |||||
), | |||||
self::CONFIG_AUX_PHID => true, | |||||
) + parent::getConfiguration(); | |||||
} | } | ||||
public function generatePHID() { | public function setTargetPHID($target_phid) { | ||||
return PhabricatorPHID::generateNewPHID('ACMT'); | $this->getProxyComment()->setCommitPHID($target_phid); | ||||
$this->proxy->setObjectPHID($target_phid); | |||||
return $this; | |||||
} | } | ||||
public function getTargetPHID() { | |||||
return $this->proxy->getObjectPHID(); | |||||
} | |||||
public function getContent() { | public function getContent() { | ||||
return $this->getProxyComment()->getContent(); | return $this->getProxyComment()->getContent(); | ||||
} | } | ||||
public function setContent($content) { | public function setContent($content) { | ||||
// NOTE: We no longer read this field, but there's no cost to continuing | |||||
// to write it in case something goes horribly wrong, since it makes it | |||||
// far easier to back out of this. | |||||
$this->content = $content; | |||||
$this->getProxyComment()->setContent($content); | $this->getProxyComment()->setContent($content); | ||||
return $this; | return $this; | ||||
} | } | ||||
public function setContentSource($content_source) { | |||||
$this->proxy->setContentSource($content_source); | |||||
$this->proxyComment->setContentSource($content_source); | |||||
return $this; | |||||
} | |||||
public function getContentSource() { | |||||
return $this->proxy->getContentSource(); | |||||
} | |||||
private function getProxyComment() { | private function getProxyComment() { | ||||
if (!$this->proxyComment) { | if (!$this->proxyComment) { | ||||
$this->proxyComment = new PhabricatorAuditTransactionComment(); | $this->proxyComment = new PhabricatorAuditTransactionComment(); | ||||
} | } | ||||
return $this->proxyComment; | return $this->proxyComment; | ||||
} | } | ||||
public function setProxyComment(PhabricatorAuditTransactionComment $proxy) { | public function setProxyComment(PhabricatorAuditTransactionComment $proxy) { | ||||
if ($this->proxyComment) { | if ($this->proxyComment) { | ||||
throw new Exception(pht('You can not overwrite a proxy comment.')); | throw new Exception(pht('You can not overwrite a proxy comment.')); | ||||
} | } | ||||
$this->proxyComment = $proxy; | $this->proxyComment = $proxy; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function setTargetPHID($target_phid) { | public function setAction($action) { | ||||
$this->getProxyComment()->setCommitPHID($target_phid); | switch ($action) { | ||||
return parent::setTargetPHID($target_phid); | case PhabricatorAuditActionConstants::INLINE: | ||||
case PhabricatorAuditActionConstants::ADD_CCS: | |||||
case PhabricatorAuditActionConstants::ADD_AUDITORS: | |||||
$this->proxy->setTransactionType($action); | |||||
break; | |||||
case PhabricatorAuditActionConstants::COMMENT: | |||||
$this->proxy->setTransactionType(PhabricatorTransactions::TYPE_COMMENT); | |||||
break; | |||||
default: | |||||
$this->proxy | |||||
->setTransactionType(PhabricatorAuditActionConstants::ACTION) | |||||
->setNewValue($action); | |||||
break; | |||||
} | |||||
return $this; | |||||
} | |||||
public function getAction() { | |||||
$type = $this->proxy->getTransactionType(); | |||||
switch ($type) { | |||||
case PhabricatorTransactions::TYPE_COMMENT: | |||||
return PhabricatorAuditActionConstants::COMMENT; | |||||
case PhabricatorAuditActionConstants::INLINE: | |||||
case PhabricatorAuditActionConstants::ADD_CCS: | |||||
case PhabricatorAuditActionConstants::ADD_AUDITORS: | |||||
return $type; | |||||
default: | |||||
return $this->proxy->getNewValue(); | |||||
} | |||||
} | |||||
public function setMetadata(array $metadata) { | |||||
if (!$this->proxy->getTransactionType()) { | |||||
throw new Exception(pht('Call setAction() before getMetadata()!')); | |||||
} | |||||
$type = $this->proxy->getTransactionType(); | |||||
switch ($type) { | |||||
case PhabricatorAuditActionConstants::ADD_CCS: | |||||
$raw_phids = idx($metadata, self::METADATA_ADDED_CCS, array()); | |||||
break; | |||||
case PhabricatorAuditActionConstants::ADD_AUDITORS: | |||||
$raw_phids = idx($metadata, self::METADATA_ADDED_AUDITORS, array()); | |||||
break; | |||||
default: | |||||
throw new Exception(pht('No metadata expected!')); | |||||
} | |||||
$this->proxy->setOldValue(array()); | |||||
$this->proxy->setNewValue(array_fuse($raw_phids)); | |||||
return $this; | |||||
} | |||||
public function getMetadata() { | |||||
if (!$this->proxy->getTransactionType()) { | |||||
throw new Exception(pht('Call setAction() before getMetadata()!')); | |||||
} | |||||
$type = $this->proxy->getTransactionType(); | |||||
$new_value = $this->proxy->getNewValue(); | |||||
switch ($type) { | |||||
case PhabricatorAuditActionConstants::ADD_CCS: | |||||
return array( | |||||
self::METADATA_ADDED_CCS => array_keys($new_value), | |||||
Not Done Inline ActionsI'm not sure why you array_fuse the data on the way in and then array_keys on the way out? joshuaspence: I'm not sure why you `array_fuse` the data on the way in and then `array_keys` on the way out? | |||||
); | |||||
case PhabricatorAuditActionConstants::ADD_AUDITORS: | |||||
return array( | |||||
self::METADATA_ADDED_AUDITORS => array_keys($new_value), | |||||
Not Done Inline ActionsAs above. joshuaspence: As above. | |||||
); | |||||
} | |||||
return array(); | |||||
} | } | ||||
public function save() { | public function save() { | ||||
$this->openTransaction(); | $this->proxy->openTransaction(); | ||||
$result = parent::save(); | $this->proxy | ||||
->setViewPolicy('public') | |||||
->setEditPolicy($this->getActorPHID()) | |||||
->save(); | |||||
if (strlen($this->getContent())) { | if (strlen($this->getContent())) { | ||||
$content_source = PhabricatorContentSource::newForSource( | $this->getProxyComment() | ||||
PhabricatorContentSource::SOURCE_LEGACY, | |||||
array()); | |||||
$xaction_phid = PhabricatorPHID::generateNewPHID( | |||||
PhabricatorApplicationTransactionTransactionPHIDType::TYPECONST, | |||||
PhabricatorRepositoryCommitPHIDType::TYPECONST); | |||||
$proxy = $this->getProxyComment(); | |||||
$proxy | |||||
->setAuthorPHID($this->getActorPHID()) | ->setAuthorPHID($this->getActorPHID()) | ||||
Not Done Inline ActionsSpecifically, this forces the comment's author PHID to the correct value. epriestley: Specifically, this forces the comment's author PHID to the correct value. | |||||
->setViewPolicy('public') | ->setViewPolicy('public') | ||||
->setEditPolicy($this->getActorPHID()) | ->setEditPolicy($this->getActorPHID()) | ||||
->setContentSource($content_source) | |||||
->setCommentVersion(1) | ->setCommentVersion(1) | ||||
->setLegacyCommentID($this->getID()) | ->setTransactionPHID($this->proxy->getPHID()) | ||||
->setTransactionPHID($xaction_phid) | ->save(); | ||||
$this->proxy | |||||
->setCommentVersion(1) | |||||
->setCommentPHID($this->getProxyComment()->getPHID()) | |||||
->save(); | ->save(); | ||||
} | } | ||||
$this->proxy->saveTransaction(); | |||||
$this->saveTransaction(); | return $this; | ||||
} | |||||
public function getDateCreated() { | |||||
return $this->proxy->getDateCreated(); | |||||
} | |||||
return $result; | public function getDateModified() { | ||||
return $this->proxy->getDateModified(); | |||||
} | } | ||||
/* -( PhabricatorMarkupInterface Implementation )-------------------------- */ | /* -( PhabricatorMarkupInterface Implementation )-------------------------- */ | ||||
public function getMarkupFieldKey($field) { | public function getMarkupFieldKey($field) { | ||||
return 'AC:'.$this->getID(); | return 'AC:'.$this->getPHID(); | ||||
} | } | ||||
public function newMarkupEngine($field) { | public function newMarkupEngine($field) { | ||||
return PhabricatorMarkupEngine::newDiffusionMarkupEngine(); | return PhabricatorMarkupEngine::newDiffusionMarkupEngine(); | ||||
} | } | ||||
public function getMarkupText($field) { | public function getMarkupText($field) { | ||||
return $this->getContent(); | return $this->getContent(); | ||||
} | } | ||||
public function didMarkupText($field, $output, PhutilMarkupEngine $engine) { | public function didMarkupText($field, $output, PhutilMarkupEngine $engine) { | ||||
return $output; | return $output; | ||||
} | } | ||||
public function shouldUseMarkupCache($field) { | public function shouldUseMarkupCache($field) { | ||||
return (bool)$this->getID(); | return (bool)$this->getPHID(); | ||||
} | } | ||||
} | } |
Do you need to do $this->proxyComment->setAuthorPHID($actor_phid) as well?