Differential D10813 Diff 25947 src/applications/phriction/conduit/PhrictionCreateConduitAPIMethod.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/phriction/conduit/PhrictionCreateConduitAPIMethod.php
- This file was copied from src/applications/phriction/conduit/PhrictionEditConduitAPIMethod.php.
| <?php | <?php | ||||
| final class PhrictionEditConduitAPIMethod extends PhrictionConduitAPIMethod { | final class PhrictionCreateConduitAPIMethod extends PhrictionConduitAPIMethod { | ||||
| public function getAPIMethodName() { | public function getAPIMethodName() { | ||||
| return 'phriction.edit'; | return 'phriction.create'; | ||||
| } | } | ||||
| public function getMethodDescription() { | public function getMethodDescription() { | ||||
| return 'Update a Phriction document.'; | return pht('Create a Phriction document.'); | ||||
| } | } | ||||
| public function defineParamTypes() { | public function defineParamTypes() { | ||||
| return array( | return array( | ||||
| 'slug' => 'required string', | 'slug' => 'required string', | ||||
| 'title' => 'optional string', | 'title' => 'required string', | ||||
| 'content' => 'optional string', | 'content' => 'required string', | ||||
| 'description' => 'optional string', | 'description' => 'optional string', | ||||
| ); | ); | ||||
| } | } | ||||
| public function defineReturnType() { | public function defineReturnType() { | ||||
| return 'nonempty dict'; | return 'nonempty dict'; | ||||
| } | } | ||||
| public function defineErrorTypes() { | public function defineErrorTypes() { | ||||
| return array( | return array( | ||||
| ); | ); | ||||
| } | } | ||||
| protected function execute(ConduitAPIRequest $request) { | protected function execute(ConduitAPIRequest $request) { | ||||
| $slug = $request->getValue('slug'); | $slug = $request->getValue('slug'); | ||||
| if (!strlen($slug)) { | |||||
| throw new Exception(pht('No such document.')); | |||||
| } | |||||
| $doc = id(new PhrictionDocumentQuery()) | $doc = id(new PhrictionDocumentQuery()) | ||||
| ->setViewer($request->getUser()) | ->setViewer($request->getUser()) | ||||
| ->withSlugs(array(PhabricatorSlug::normalize($slug))) | ->withSlugs(array(PhabricatorSlug::normalize($slug))) | ||||
| ->needContent(true) | |||||
| ->requireCapabilities( | ->requireCapabilities( | ||||
| array( | array( | ||||
| PhabricatorPolicyCapability::CAN_VIEW, | PhabricatorPolicyCapability::CAN_VIEW, | ||||
| PhabricatorPolicyCapability::CAN_EDIT, | PhabricatorPolicyCapability::CAN_EDIT, | ||||
| )) | )) | ||||
| ->executeOne(); | ->executeOne(); | ||||
| if (!$doc) { | if ($doc) { | ||||
| throw new Exception(pht('No such document.')); | throw new Exception(pht('Document already exists!')); | ||||
| } | } | ||||
| $doc = PhrictionDocument::initializeNewDocument( | |||||
| $request->getUser(), | |||||
| $slug); | |||||
| $xactions = array(); | $xactions = array(); | ||||
| $xactions[] = id(new PhrictionTransaction()) | $xactions[] = id(new PhrictionTransaction()) | ||||
| ->setTransactionType(PhrictionTransaction::TYPE_TITLE) | ->setTransactionType(PhrictionTransaction::TYPE_TITLE) | ||||
| ->setNewValue($request->getValue('title')); | ->setNewValue($request->getValue('title')); | ||||
| $xactions[] = id(new PhrictionTransaction()) | $xactions[] = id(new PhrictionTransaction()) | ||||
| ->setTransactionType(PhrictionTransaction::TYPE_CONTENT) | ->setTransactionType(PhrictionTransaction::TYPE_CONTENT) | ||||
| ->setNewValue($request->getValue('content')); | ->setNewValue($request->getValue('content')); | ||||
| $editor = id(new PhrictionTransactionEditor()) | $editor = id(new PhrictionTransactionEditor()) | ||||
| ->setActor($request->getUser()) | ->setActor($request->getUser()) | ||||
| ->setContentSourceFromConduitRequest($request) | ->setContentSourceFromConduitRequest($request) | ||||
| ->setContinueOnNoEffect(true) | ->setContinueOnNoEffect(true) | ||||
| ->setDescription($request->getValue('description')) | ->setDescription($request->getValue('description')); | ||||
| ->applyTransactions($doc, $xactions); | |||||
| return $this->buildDocumentInfoDictionary($doc); | try { | ||||
| $editor->applyTransactions($doc, $xactions); | |||||
| } catch (PhabricatorApplicationTransactionValidationException $ex) { | |||||
| // TODO - some magical hotness via T5873 | |||||
| throw $ex; | |||||
| } | } | ||||
| return $this->buildDocumentInfoDictionary($doc); | |||||
| } | |||||
| } | } | ||||