Page MenuHomec4science

PhabricatorProjectWikiCreate.php
No OneTemporary

File Metadata

Created
Wed, Nov 27, 16:18

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)
->withStatuses(array(PhrictionDocumentStatus::STATUS_EXISTS))
->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('username = %s', 'admin');
$doc = PhrictionDocument::initializeNewDocument($viewer, $slugs);
$xactions = array();
$xactions[] = id(new PhrictionTransaction())
->setTransactionType(PhrictionDocumentTitleTransaction::TRANSACTIONTYPE)
->setNewValue(PhabricatorSlug::getDefaultTitle($slugs));
$xactions[] = id(new PhrictionTransaction())
->setTransactionType(
PhrictionDocumentContentTransaction::TRANSACTIONTYPE)
->setNewValue(' ');
$xactions[] = id(new PhrictionTransaction())
->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)
->setNewValue($project->getPHID());
$xactions[] = id(new PhrictionTransaction())
->setTransactionType(PhabricatorTransactions::TYPE_EDIT_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_stack[] = $parent->getPrimarySlug();
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