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
260 lines
6.8 KiB
PHP
260 lines
6.8 KiB
PHP
<?php
|
|
|
|
final class PhrictionDiffController extends PhrictionController {
|
|
|
|
public function shouldAllowPublic() {
|
|
return true;
|
|
}
|
|
|
|
public function handleRequest(AphrontRequest $request) {
|
|
$viewer = $request->getViewer();
|
|
$id = $request->getURIData('id');
|
|
|
|
$document = id(new PhrictionDocumentQuery())
|
|
->setViewer($viewer)
|
|
->withIDs(array($id))
|
|
->needContent(true)
|
|
->executeOne();
|
|
if (!$document) {
|
|
return new Aphront404Response();
|
|
}
|
|
|
|
$current = $document->getContent();
|
|
|
|
$l = $request->getInt('l');
|
|
$r = $request->getInt('r');
|
|
|
|
$ref = $request->getStr('ref');
|
|
if ($ref) {
|
|
list($l, $r) = explode(',', $ref);
|
|
}
|
|
|
|
$content = id(new PhrictionContentQuery())
|
|
->setViewer($viewer)
|
|
->withDocumentPHIDs(array($document->getPHID()))
|
|
->withVersions(array($l, $r))
|
|
->execute();
|
|
$content = mpull($content, null, 'getVersion');
|
|
|
|
$content_l = idx($content, $l, null);
|
|
$content_r = idx($content, $r, null);
|
|
|
|
if (!$content_l || !$content_r) {
|
|
return new Aphront404Response();
|
|
}
|
|
|
|
$text_l = $content_l->getContent();
|
|
$text_r = $content_r->getContent();
|
|
|
|
$diff_view = id(new PhabricatorApplicationTransactionTextDiffDetailView())
|
|
->setOldText($text_l)
|
|
->setNewText($text_r);
|
|
|
|
$changes = id(new PHUIObjectBoxView())
|
|
->setHeaderText(pht('Content Changes'))
|
|
->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
|
|
->appendChild(
|
|
phutil_tag(
|
|
'div',
|
|
array(
|
|
'class' => 'prose-diff-frame',
|
|
),
|
|
$diff_view));
|
|
|
|
require_celerity_resource('phriction-document-css');
|
|
|
|
$slug = $document->getSlug();
|
|
|
|
$revert_l = $this->renderRevertButton($document, $content_l, $current);
|
|
$revert_r = $this->renderRevertButton($document, $content_r, $current);
|
|
|
|
$crumbs = $this->buildApplicationCrumbs();
|
|
$crumb_views = $this->renderBreadcrumbs($slug);
|
|
foreach ($crumb_views as $view) {
|
|
$crumbs->addCrumb($view);
|
|
}
|
|
|
|
$crumbs->addTextCrumb(
|
|
pht('History'),
|
|
PhrictionDocument::getSlugURI($slug, 'history'));
|
|
|
|
$title = pht('Version %s vs %s', $l, $r);
|
|
|
|
$header = id(new PHUIHeaderView())
|
|
->setHeader($title)
|
|
->setHeaderIcon('fa-history');
|
|
|
|
$crumbs->addTextCrumb($title, $request->getRequestURI());
|
|
|
|
$comparison_table = $this->renderComparisonTable(
|
|
array(
|
|
$content_r,
|
|
$content_l,
|
|
));
|
|
|
|
$navigation_table = null;
|
|
if ($l + 1 == $r) {
|
|
$nav_l = ($l > 1);
|
|
$nav_r = ($r != $current->getVersion());
|
|
|
|
$uri = $request->getRequestURI();
|
|
|
|
if ($nav_l) {
|
|
$link_l = phutil_tag(
|
|
'a',
|
|
array(
|
|
'href' => $uri->alter('l', $l - 1)->alter('r', $r - 1),
|
|
'class' => 'button button-grey',
|
|
),
|
|
pht("\xC2\xAB Previous Change"));
|
|
} else {
|
|
$link_l = phutil_tag(
|
|
'a',
|
|
array(
|
|
'href' => '#',
|
|
'class' => 'button button-grey disabled',
|
|
),
|
|
pht('Original Change'));
|
|
}
|
|
|
|
$link_r = null;
|
|
if ($nav_r) {
|
|
$link_r = phutil_tag(
|
|
'a',
|
|
array(
|
|
'href' => $uri->alter('l', $l + 1)->alter('r', $r + 1),
|
|
'class' => 'button button-grey',
|
|
),
|
|
pht("Next Change \xC2\xBB"));
|
|
} else {
|
|
$link_r = phutil_tag(
|
|
'a',
|
|
array(
|
|
'href' => '#',
|
|
'class' => 'button button-grey disabled',
|
|
),
|
|
pht('Most Recent Change'));
|
|
}
|
|
|
|
$navigation_table = phutil_tag(
|
|
'table',
|
|
array('class' => 'phriction-history-nav-table'),
|
|
phutil_tag('tr', array(), array(
|
|
phutil_tag('td', array('class' => 'nav-prev'), $link_l),
|
|
phutil_tag('td', array('class' => 'nav-next'), $link_r),
|
|
)));
|
|
}
|
|
|
|
$output = hsprintf(
|
|
'<div class="phriction-document-history-diff">'.
|
|
'%s%s'.
|
|
'<table class="phriction-revert-table">'.
|
|
'<tr><td>%s</td><td>%s</td>'.
|
|
'</table>'.
|
|
'</div>',
|
|
$comparison_table->render(),
|
|
$navigation_table,
|
|
$revert_l,
|
|
$revert_r);
|
|
|
|
$object_box = id(new PHUIObjectBoxView())
|
|
->setHeaderText(pht('Edits'))
|
|
->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
|
|
->appendChild($output);
|
|
|
|
$crumbs->setBorder(true);
|
|
|
|
$view = id(new PHUITwoColumnView())
|
|
->setHeader($header)
|
|
->setFooter(array(
|
|
$object_box,
|
|
$changes,
|
|
));
|
|
|
|
return $this->newPage()
|
|
->setTitle(pht('Document History'))
|
|
->setCrumbs($crumbs)
|
|
->appendChild($view);
|
|
|
|
}
|
|
|
|
private function renderRevertButton(
|
|
PhrictionDocument $document,
|
|
PhrictionContent $content,
|
|
PhrictionContent $current) {
|
|
|
|
$document_id = $document->getID();
|
|
$version = $content->getVersion();
|
|
|
|
$hidden_statuses = array(
|
|
PhrictionChangeType::CHANGE_DELETE => true, // Silly
|
|
PhrictionChangeType::CHANGE_MOVE_AWAY => true, // Plain silly
|
|
PhrictionChangeType::CHANGE_STUB => true, // Utterly silly
|
|
);
|
|
if (isset($hidden_statuses[$content->getChangeType()])) {
|
|
// Don't show an edit/revert button for changes which deleted, moved or
|
|
// stubbed the content since it's silly.
|
|
return null;
|
|
}
|
|
|
|
if ($content->getID() == $current->getID()) {
|
|
return phutil_tag(
|
|
'a',
|
|
array(
|
|
'href' => '/phriction/edit/'.$document_id.'/',
|
|
'class' => 'button button-grey',
|
|
),
|
|
pht('Edit Current Version'));
|
|
}
|
|
|
|
|
|
return phutil_tag(
|
|
'a',
|
|
array(
|
|
'href' => '/phriction/edit/'.$document_id.'/?revert='.$version,
|
|
'class' => 'button button-grey',
|
|
),
|
|
pht('Revert to Version %s...', $version));
|
|
}
|
|
|
|
private function renderComparisonTable(array $content) {
|
|
assert_instances_of($content, 'PhrictionContent');
|
|
|
|
$viewer = $this->getViewer();
|
|
|
|
$phids = mpull($content, 'getAuthorPHID');
|
|
$handles = $this->loadViewerHandles($phids);
|
|
|
|
$list = new PHUIObjectItemListView();
|
|
|
|
$first = true;
|
|
foreach ($content as $c) {
|
|
$author = $handles[$c->getAuthorPHID()]->renderLink();
|
|
$item = id(new PHUIObjectItemView())
|
|
->setHeader(pht('%s by %s, %s',
|
|
PhrictionChangeType::getChangeTypeLabel($c->getChangeType()),
|
|
$author,
|
|
pht('Version %s', $c->getVersion())))
|
|
->addAttribute(pht('%s %s',
|
|
phabricator_date($c->getDateCreated(), $viewer),
|
|
phabricator_time($c->getDateCreated(), $viewer)));
|
|
|
|
if ($c->getDescription()) {
|
|
$item->addAttribute($c->getDescription());
|
|
}
|
|
|
|
if ($first == true) {
|
|
$item->setStatusIcon('fa-file green');
|
|
$first = false;
|
|
} else {
|
|
$item->setStatusIcon('fa-file red');
|
|
}
|
|
|
|
$list->addItem($item);
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
|
|
}
|