/**
  * Edit a transaction's comment. This method effects the required create,
  * update or delete to set the transaction's comment to the provided comment.
  */
 public function applyEdit(PhabricatorApplicationTransaction $xaction, PhabricatorApplicationTransactionComment $comment)
 {
     $this->validateEdit($xaction, $comment);
     $actor = $this->requireActor();
     $comment->setContentSource($this->getContentSource());
     $comment->setAuthorPHID($this->getActingAsPHID());
     // TODO: This needs to be more sophisticated once we have meta-policies.
     $comment->setViewPolicy(PhabricatorPolicies::POLICY_PUBLIC);
     $comment->setEditPolicy($this->getActingAsPHID());
     $file_phids = PhabricatorMarkupEngine::extractFilePHIDsFromEmbeddedFiles($actor, array($comment->getContent()));
     $xaction->openTransaction();
     $xaction->beginReadLocking();
     if ($xaction->getID()) {
         $xaction->reload();
     }
     $new_version = $xaction->getCommentVersion() + 1;
     $comment->setCommentVersion($new_version);
     $comment->setTransactionPHID($xaction->getPHID());
     $comment->save();
     $old_comment = $xaction->getComment();
     $comment->attachOldComment($old_comment);
     $xaction->setCommentVersion($new_version);
     $xaction->setCommentPHID($comment->getPHID());
     $xaction->setViewPolicy($comment->getViewPolicy());
     $xaction->setEditPolicy($comment->getEditPolicy());
     $xaction->save();
     $xaction->attachComment($comment);
     // For comment edits, we need to make sure there are no automagical
     // transactions like adding mentions or projects.
     if ($new_version > 1) {
         $object = id(new PhabricatorObjectQuery())->withPHIDs(array($xaction->getObjectPHID()))->setViewer($this->getActor())->executeOne();
         if ($object && $object instanceof PhabricatorApplicationTransactionInterface) {
             $editor = $object->getApplicationTransactionEditor();
             $editor->setActor($this->getActor());
             $support_xactions = $editor->getExpandedSupportTransactions($object, $xaction);
             if ($support_xactions) {
                 $editor->setContentSource($this->getContentSource())->setContinueOnNoEffect(true)->applyTransactions($object, $support_xactions);
             }
         }
     }
     $xaction->endReadLocking();
     $xaction->saveTransaction();
     // Add links to any files newly referenced by the edit.
     if ($file_phids) {
         $editor = new PhabricatorEdgeEditor();
         foreach ($file_phids as $file_phid) {
             $editor->addEdge($xaction->getObjectPHID(), PhabricatorObjectHasFileEdgeType::EDGECONST, $file_phid);
         }
         $editor->save();
     }
     return $this;
 }
 public function generateTransactionsFromText(PhabricatorUser $viewer, ConpherenceThread $conpherence, $text)
 {
     $files = array();
     $file_phids = PhabricatorMarkupEngine::extractFilePHIDsFromEmbeddedFiles($viewer, array($text));
     // Since these are extracted from text, we might be re-including the
     // same file -- e.g. a mock under discussion. Filter files we
     // already have.
     $existing_file_phids = $conpherence->getFilePHIDs();
     $file_phids = array_diff($file_phids, $existing_file_phids);
     if ($file_phids) {
         $files = id(new PhabricatorFileQuery())->setViewer($this->getActor())->withPHIDs($file_phids)->execute();
     }
     $xactions = array();
     if ($files) {
         $xactions[] = id(new ConpherenceTransaction())->setTransactionType(ConpherenceTransaction::TYPE_FILES)->setNewValue(array('+' => mpull($files, 'getPHID')));
     }
     $xactions[] = id(new ConpherenceTransaction())->setTransactionType(PhabricatorTransactions::TYPE_COMMENT)->attachComment(id(new ConpherenceTransactionComment())->setContent($text)->setConpherencePHID($conpherence->getPHID()));
     return $xactions;
 }
 /**
  * Edit a transaction's comment. This method effects the required create,
  * update or delete to set the transaction's comment to the provided comment.
  */
 public function applyEdit(PhabricatorApplicationTransaction $xaction, PhabricatorApplicationTransactionComment $comment)
 {
     $this->validateEdit($xaction, $comment);
     $actor = $this->requireActor();
     $comment->setContentSource($this->getContentSource());
     $comment->setAuthorPHID($this->getActingAsPHID());
     // TODO: This needs to be more sophisticated once we have meta-policies.
     $comment->setViewPolicy(PhabricatorPolicies::POLICY_PUBLIC);
     $comment->setEditPolicy($this->getActingAsPHID());
     $file_phids = PhabricatorMarkupEngine::extractFilePHIDsFromEmbeddedFiles($actor, array($comment->getContent()));
     $xaction->openTransaction();
     $xaction->beginReadLocking();
     if ($xaction->getID()) {
         $xaction->reload();
     }
     $new_version = $xaction->getCommentVersion() + 1;
     $comment->setCommentVersion($new_version);
     $comment->setTransactionPHID($xaction->getPHID());
     $comment->save();
     $xaction->setCommentVersion($new_version);
     $xaction->setCommentPHID($comment->getPHID());
     $xaction->setViewPolicy($comment->getViewPolicy());
     $xaction->setEditPolicy($comment->getEditPolicy());
     $xaction->save();
     $xaction->endReadLocking();
     $xaction->saveTransaction();
     // Add links to any files newly referenced by the edit.
     if ($file_phids) {
         $editor = new PhabricatorEdgeEditor();
         foreach ($file_phids as $file_phid) {
             $editor->addEdge($xaction->getObjectPHID(), PhabricatorEdgeConfig::TYPE_OBJECT_HAS_FILE, $file_phid);
         }
         $editor->save();
     }
     $xaction->attachComment($comment);
     return $this;
 }
 /**
  * Extract the PHIDs of any files which these transactions attach.
  *
  * @task files
  */
 private function extractFilePHIDs(PhabricatorLiskDAO $object, array $xactions)
 {
     $blocks = array();
     foreach ($xactions as $xaction) {
         $blocks[] = $this->getRemarkupBlocksFromTransaction($xaction);
     }
     $blocks = array_mergev($blocks);
     $phids = array();
     if ($blocks) {
         $phids[] = PhabricatorMarkupEngine::extractFilePHIDsFromEmbeddedFiles($this->getActor(), $blocks);
     }
     foreach ($xactions as $xaction) {
         $phids[] = $this->extractFilePHIDsFromCustomTransaction($object, $xaction);
     }
     $phids = array_unique(array_filter(array_mergev($phids)));
     if (!$phids) {
         return array();
     }
     // Only let a user attach files they can actually see, since this would
     // otherwise let you access any file by attaching it to an object you have
     // view permission on.
     $files = id(new PhabricatorFileQuery())->setViewer($this->getActor())->withPHIDs($phids)->execute();
     return mpull($files, 'getPHID');
 }