Differential D19909 Diff 47596 src/applications/paste/xaction/PhabricatorPasteContentTransaction.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/paste/xaction/PhabricatorPasteContentTransaction.php
<?php | <?php | ||||
final class PhabricatorPasteContentTransaction | final class PhabricatorPasteContentTransaction | ||||
extends PhabricatorPasteTransactionType { | extends PhabricatorPasteTransactionType { | ||||
const TRANSACTIONTYPE = 'paste.create'; | const TRANSACTIONTYPE = 'paste.create'; | ||||
private $fileName; | |||||
public function generateOldValue($object) { | public function generateOldValue($object) { | ||||
return $object->getFilePHID(); | return $object->getFilePHID(); | ||||
} | } | ||||
public function applyInternalEffects($object, $value) { | public function applyInternalEffects($object, $value) { | ||||
$object->setFilePHID($value); | $object->setFilePHID($value); | ||||
} | } | ||||
Show All 9 Lines | public function validateTransactions($object, array $xactions) { | ||||
$error = $this->newError( | $error = $this->newError( | ||||
pht('Required'), | pht('Required'), | ||||
pht('You must provide content to create a paste.')); | pht('You must provide content to create a paste.')); | ||||
$error->setIsMissingFieldError(true); | $error->setIsMissingFieldError(true); | ||||
return array($error); | return array($error); | ||||
} | } | ||||
public function willApplyTransactions($object, array $xactions) { | |||||
// Find the most user-friendly filename we can by examining the title of | |||||
// the paste and the pending transactions. We'll use this if we create a | |||||
// new file to store raw content later. | |||||
$name = $object->getTitle(); | |||||
if (!strlen($name)) { | |||||
$name = 'paste.raw'; | |||||
} | |||||
$type_title = PhabricatorPasteTitleTransaction::TRANSACTIONTYPE; | |||||
foreach ($xactions as $xaction) { | |||||
if ($xaction->getTransactionType() == $type_title) { | |||||
$name = $xaction->getNewValue(); | |||||
} | |||||
} | |||||
$this->fileName = $name; | |||||
} | |||||
public function generateNewValue($object, $value) { | public function generateNewValue($object, $value) { | ||||
// If this transaction does not really change the paste content, return | // If this transaction does not really change the paste content, return | ||||
// the current file PHID so this transaction no-ops. | // the current file PHID so this transaction no-ops. | ||||
$old_content = $object->getRawContent(); | $old_content = $object->getRawContent(); | ||||
if ($value === $old_content) { | if ($value === $old_content) { | ||||
$file_phid = $object->getFilePHID(); | $file_phid = $object->getFilePHID(); | ||||
if ($file_phid) { | if ($file_phid) { | ||||
return $file_phid; | return $file_phid; | ||||
} | } | ||||
} | } | ||||
$editor = $this->getEditor(); | $editor = $this->getEditor(); | ||||
$actor = $editor->getActor(); | $actor = $editor->getActor(); | ||||
$file = $this->newFileForPaste($actor, $this->fileName, $value); | $file = $this->newFileForPaste($actor, $value); | ||||
return $file->getPHID(); | return $file->getPHID(); | ||||
} | } | ||||
private function newFileForPaste(PhabricatorUser $actor, $name, $data) { | private function newFileForPaste(PhabricatorUser $actor, $data) { | ||||
return PhabricatorFile::newFromFileData( | return PhabricatorFile::newFromFileData( | ||||
$data, | $data, | ||||
array( | array( | ||||
'name' => $name, | 'name' => 'raw.txt', | ||||
'mime-type' => 'text/plain; charset=utf-8', | 'mime-type' => 'text/plain; charset=utf-8', | ||||
'authorPHID' => $actor->getPHID(), | 'authorPHID' => $actor->getPHID(), | ||||
'viewPolicy' => PhabricatorPolicies::POLICY_NOONE, | 'viewPolicy' => PhabricatorPolicies::POLICY_NOONE, | ||||
)); | )); | ||||
} | } | ||||
public function getIcon() { | public function getIcon() { | ||||
return 'fa-plus'; | return 'fa-plus'; | ||||
▲ Show 20 Lines • Show All 52 Lines • Show Last 20 Lines |