1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-03-12 20:34:54 +01:00
phorge-phorge/src/applications/phriction/xaction/PhrictionDocumentTitleTransaction.php
epriestley 64cee4a902 Move Phriction internal document/content references from IDs to PHIDs
Summary:
Ref T13077. This is mostly just a small cleanup change, even though the actual change is large.

We currently reference content and document objects from one another with `contentID` and `documentID`, but this means that `contentID` must be nullable. Switching to PHIDs allows the column to be non-nullable.

This also supports reorienting some current and future transactions around PHIDs, which is preferable for the API. In particular, I'm adding a "publish version X" transaction soon, and would rather callers pass a PHID than an ID or version number, since this will make the API more consistent and powerful.

Today, `contentID` gets used as a cheaty way to order documents by (content) edit time. Since PHIDs aren't orderable and stuff is going to become actually-revertible soon, replace this with an epoch timestamp.

Test Plan:
  - Created, edited, moved, retitled, and deleted Phriction documents.
  - Grepped for `documentID` and `contentID`.
  - This probably breaks //something// but I'll be in this code for a bit and am likely to catch whatever breaks.

Reviewers: amckinley

Subscribers: PHID-OPKG-gm6ozazyms6q6i22gyam

Maniphest Tasks: T13077

Differential Revision: https://secure.phabricator.com/D19619
2018-08-29 13:41:24 -07:00

120 lines
3.1 KiB
PHP

<?php
final class PhrictionDocumentTitleTransaction
extends PhrictionDocumentVersionTransaction {
const TRANSACTIONTYPE = 'title';
public function generateOldValue($object) {
if ($this->isNewObject()) {
return null;
}
return $this->getEditor()->getOldContent()->getTitle();
}
public function applyInternalEffects($object, $value) {
$object->setStatus(PhrictionDocumentStatus::STATUS_EXISTS);
$content = $this->getNewDocumentContent($object);
$content->setTitle($value);
}
public function getActionStrength() {
return 1.4;
}
public function getActionName() {
$old = $this->getOldValue();
$new = $this->getNewValue();
if ($old === null) {
if ($this->getMetadataValue('stub:create:phid')) {
return pht('Stubbed');
} else {
return pht('Created');
}
}
return pht('Retitled');
}
public function getTitle() {
$old = $this->getOldValue();
$new = $this->getNewValue();
if ($old === null) {
if ($this->getMetadataValue('stub:create:phid')) {
return pht(
'%s stubbed out this document when creating %s.',
$this->renderAuthor(),
$this->renderHandleLink(
$this->getMetadataValue('stub:create:phid')));
} else {
return pht(
'%s created this document.',
$this->renderAuthor());
}
}
return pht(
'%s changed the title from %s to %s.',
$this->renderAuthor(),
$this->renderOldValue(),
$this->renderNewValue());
}
public function getTitleForFeed() {
$old = $this->getOldValue();
$new = $this->getNewValue();
if ($old === null) {
return pht(
'%s created %s.',
$this->renderAuthor(),
$this->renderObject());
}
return pht(
'%s renamed %s from %s to %s.',
$this->renderAuthor(),
$this->renderObject(),
$this->renderOldValue(),
$this->renderNewValue());
}
public function validateTransactions($object, array $xactions) {
$errors = array();
$title = $object->getContent()->getTitle();
if ($this->isEmptyTextTransaction($title, $xactions)) {
$errors[] = $this->newRequiredError(
pht('Documents must have a title.'));
}
if ($this->isNewObject()) {
// No ancestral slugs is "/". No ancestry checks apply when creating the
// root document.
$ancestral_slugs = PhabricatorSlug::getAncestry($object->getSlug());
if ($ancestral_slugs) {
// You must be able to view and edit the parent document to create a new
// child.
$parent_document = id(new PhrictionDocumentQuery())
->setViewer($this->getActor())
->withSlugs(array(last($ancestral_slugs)))
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
))
->executeOne();
if (!$parent_document) {
$errors[] = $this->newInvalidError(
pht('You can not create a document which does not have a parent.'));
}
}
}
return $errors;
}
}