Page MenuHomec4science

PhabricatorProjectWikiCreate.php
No OneTemporary

File Metadata

Created
Sun, Dec 22, 21:09

PhabricatorProjectWikiCreate.php

<?php
final class PhabricatorProjectWikiCreate
extends PhabricatorController {
public function shouldAllowPublic() {
return false;
}
public function handleRequest(AphrontRequest $request) {
$viewer = $request->getViewer();
$id = (int)$request->getURIData('id');
$project = id(new PhabricatorProjectQuery())
->withIDs(array($id))
->setViewer($viewer)
->needSlugs(true)
->executeOne();
if(!$project) {
return new Aphront404Response();
}
$project_slug = PhabricatorProjectWikiCreate::getAllSlugs($project);
if ($request->isFormPost()) {
$slug = $project_slug . PhabricatorSlug::normalize($request->getStr('slug'));
try {
$this->createDocument($slug, $viewer, $project);
} catch(PhabricatorApplicationTransactionValidationException $ex) {
return id(new PhabricatorApplicationTransactionValidationResponse())
->setCancelURI('/project/view/' . $id)
->setException($ex);
} catch(AphrontDuplicateKeyQueryException $ex) {
}
return id(new AphrontRedirectResponse())
->setURI('/phriction/edit/?slug='. $slug);
}
$wiki_document = id(new PhrictionDocumentQuery())
->setViewer($viewer)
->withStatus(PhrictionDocumentQuery::STATUS_NONSTUB)
->withSlugPrefix($project_slug)
->execute();
if(!$wiki_document){
$view = id(new PHUIFormLayoutView())
->appendChild(phutil_tag(
'p',
array(),
pht('/w/' . $project_slug)));
} else {
$view = id(new PHUIFormLayoutView())
->appendChild(id(new AphrontFormTextControl())
->setLabel('Name')
->setValue('')
->setName('slug'));
}
return $this->newDialog()
->setTitle(pht('New Document'))
->setSubmitURI('/project/wiki/create/' . $id . '/')
->appendChild(phutil_tag('p',
array(),
pht('Create a new document')))
->appendChild($view)
->addSubmitButton(pht('Create'))
->addCancelButton('/project/view/' . $id);
}
protected function buildApplicationCrumbs() {
$crumbs = parent::buildApplicationCrumbs();
id(new AlmanacServiceEditEngine())
->setViewer($this->getViewer())
->addActionToCrumbs($crumbs);
return $crumbs;
}
private function createDocument($slugs, $viewer, $project) {
// Get admin user
$admin = id(new PhabricatorUser())
->loadOneWhere('isAdmin = 1');
// Taken from conduit/PhrictionCreateConduitAPIMethod.php
$doc = PhrictionDocument::initializeNewDocument($viewer, $slugs);
$xactions = array();
$xactions[] = id(new PhrictionTransaction())
->setTransactionType(PhrictionTransaction::TYPE_TITLE)
->setNewValue(PhabricatorSlug::getDefaultTitle($slugs));
$xactions[] = id(new PhrictionTransaction())
->setTransactionType(PhrictionTransaction::TYPE_CONTENT)
->setNewValue(' ');
$xactions[] = id(new PhrictionTransaction())
->setTransactionType(PhabricatorTransactions::TYPE_EDIT_POLICY)
->setNewValue($project->getPHID());
$xactions[] = id(new PhrictionTransaction())
->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)
->setNewValue($project->getPHID());
$proj_edge_type = PhabricatorProjectObjectHasProjectEdgeType::EDGECONST;
$xactions[] = id(new PhrictionTransaction())
->setTransactionType(PhabricatorTransactions::TYPE_EDGE)
->setMetadataValue('edge:type', $proj_edge_type)
->setNewValue(array('=' => array_fuse(array($project->getPHID()))));
$editor = id(new PhrictionTransactionEditor())
->setActor($admin)
->setActingAsPHID($viewer->getPHID())
->setContentSource(PhabricatorContentSource::newForSource('web'))
->applyTransactions($doc, $xactions);
}
static function getParentSlug($project, $slug_stack=NULL) {
if(!$slug_stack){
$slug_stack = array($project->getPrimarySlug());
}
$parent = $project->getParentProject();
if($parent) {
$slug = $parent->getPrimarySlug();
$slug_stack[] = $slug;
return PhabricatorProjectWikiCreate::getParentSlug($parent, $slug_stack);
}
return $slug_stack;
}
static function getAllSlugs($project){
$slug = PhabricatorProjectWikiCreate::getParentSlug($project);
$slug = array_reverse($slug);
$slug = implode('/', $slug) . '/';
return $slug;
}
}

Event Timeline