2014-11-07 11:46:25 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhrictionCreateConduitAPIMethod extends PhrictionConduitAPIMethod {
|
2015-04-12 15:59:07 -07:00
|
|
|
|
2014-11-07 11:46:25 -08:00
|
|
|
public function getAPIMethodName() {
|
|
|
|
return 'phriction.create';
|
|
|
|
}
|
2015-04-12 15:59:07 -07:00
|
|
|
|
2014-11-07 11:46:25 -08:00
|
|
|
public function getMethodDescription() {
|
|
|
|
return pht('Create a Phriction document.');
|
|
|
|
}
|
2015-04-12 15:59:07 -07:00
|
|
|
|
|
|
|
protected function defineParamTypes() {
|
2014-11-07 11:46:25 -08:00
|
|
|
return array(
|
|
|
|
'slug' => 'required string',
|
|
|
|
'title' => 'required string',
|
|
|
|
'content' => 'required string',
|
|
|
|
'description' => 'optional string',
|
|
|
|
);
|
|
|
|
}
|
2015-04-12 15:59:07 -07:00
|
|
|
|
|
|
|
protected function defineReturnType() {
|
2014-11-07 11:46:25 -08:00
|
|
|
return 'nonempty dict';
|
|
|
|
}
|
2015-04-12 15:59:07 -07:00
|
|
|
|
2014-11-07 11:46:25 -08:00
|
|
|
protected function execute(ConduitAPIRequest $request) {
|
|
|
|
$slug = $request->getValue('slug');
|
|
|
|
if (!strlen($slug)) {
|
|
|
|
throw new Exception(pht('No such document.'));
|
|
|
|
}
|
|
|
|
$doc = id(new PhrictionDocumentQuery())
|
|
|
|
->setViewer($request->getUser())
|
|
|
|
->withSlugs(array(PhabricatorSlug::normalize($slug)))
|
|
|
|
->requireCapabilities(
|
|
|
|
array(
|
|
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
|
|
|
PhabricatorPolicyCapability::CAN_EDIT,
|
|
|
|
))
|
|
|
|
->executeOne();
|
|
|
|
if ($doc) {
|
|
|
|
throw new Exception(pht('Document already exists!'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$doc = PhrictionDocument::initializeNewDocument(
|
|
|
|
$request->getUser(),
|
|
|
|
$slug);
|
|
|
|
|
|
|
|
$xactions = array();
|
|
|
|
$xactions[] = id(new PhrictionTransaction())
|
|
|
|
->setTransactionType(PhrictionTransaction::TYPE_TITLE)
|
|
|
|
->setNewValue($request->getValue('title'));
|
|
|
|
$xactions[] = id(new PhrictionTransaction())
|
|
|
|
->setTransactionType(PhrictionTransaction::TYPE_CONTENT)
|
|
|
|
->setNewValue($request->getValue('content'));
|
|
|
|
|
|
|
|
$editor = id(new PhrictionTransactionEditor())
|
|
|
|
->setActor($request->getUser())
|
|
|
|
->setContentSourceFromConduitRequest($request)
|
|
|
|
->setContinueOnNoEffect(true)
|
|
|
|
->setDescription($request->getValue('description'));
|
|
|
|
|
|
|
|
try {
|
|
|
|
$editor->applyTransactions($doc, $xactions);
|
|
|
|
} catch (PhabricatorApplicationTransactionValidationException $ex) {
|
|
|
|
// TODO - some magical hotness via T5873
|
|
|
|
throw $ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->buildDocumentInfoDictionary($doc);
|
|
|
|
}
|
|
|
|
}
|