public function render()
 {
     $old = $this->oldText;
     $new = $this->newText;
     // TODO: On mobile, or perhaps by default, we should switch to 1-up once
     // that is built.
     if (strlen($old)) {
         $old = phutil_utf8_hard_wrap($old, 80);
         $old = implode("\n", $old) . "\n";
     }
     if (strlen($new)) {
         $new = phutil_utf8_hard_wrap($new, 80);
         $new = implode("\n", $new) . "\n";
     }
     try {
         $engine = new PhabricatorDifferenceEngine();
         $changeset = $engine->generateChangesetFromFileContent($old, $new);
         $whitespace_mode = DifferentialChangesetParser::WHITESPACE_SHOW_ALL;
         $markup_engine = new PhabricatorMarkupEngine();
         $markup_engine->setViewer($this->getUser());
         $parser = new DifferentialChangesetParser();
         $parser->setUser($this->getUser());
         $parser->setChangeset($changeset);
         $parser->setMarkupEngine($markup_engine);
         $parser->setWhitespaceMode($whitespace_mode);
         return $parser->render(0, PHP_INT_MAX, array());
     } catch (Exception $ex) {
         return $ex->getMessage();
     }
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $document = id(new PhrictionDocumentQuery())->setViewer($user)->withIDs(array($this->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 PhrictionContent())->loadAllWhere('documentID = %d AND version IN (%Ld)', $document->getID(), array($l, $r));
     $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();
     $text_l = phutil_utf8_hard_wrap($text_l, 80);
     $text_l = implode("\n", $text_l);
     $text_r = phutil_utf8_hard_wrap($text_r, 80);
     $text_r = implode("\n", $text_r);
     $engine = new PhabricatorDifferenceEngine();
     $changeset = $engine->generateChangesetFromFileContent($text_l, $text_r);
     $changeset->setOldProperties(array('Title' => $content_l->getTitle()));
     $changeset->setNewProperties(array('Title' => $content_r->getTitle()));
     $whitespace_mode = DifferentialChangesetParser::WHITESPACE_SHOW_ALL;
     $parser = new DifferentialChangesetParser();
     $parser->setChangeset($changeset);
     $parser->setRenderingReference("{$l},{$r}");
     $parser->setWhitespaceMode($whitespace_mode);
     $engine = new PhabricatorMarkupEngine();
     $engine->setViewer($user);
     $engine->process();
     $parser->setMarkupEngine($engine);
     $spec = $request->getStr('range');
     list($range_s, $range_e, $mask) = DifferentialChangesetParser::parseRangeSpecification($spec);
     $output = $parser->render($range_s, $range_e, $mask);
     if ($request->isAjax()) {
         return id(new PhabricatorChangesetResponse())->setRenderedChangeset($output);
     }
     require_celerity_resource('differential-changeset-view-css');
     require_celerity_resource('syntax-highlighting-css');
     require_celerity_resource('phriction-document-css');
     Javelin::initBehavior('differential-show-more', array('uri' => '/phriction/diff/' . $document->getID() . '/', 'whitespace' => $whitespace_mode));
     $slug = $document->getSlug();
     $revert_l = $this->renderRevertButton($content_l, $current);
     $revert_r = $this->renderRevertButton($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);
     $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'), pht("« Previous Change"));
         } else {
             $link_l = phutil_tag('a', array('href' => '#', 'class' => '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'), pht("Next Change »"));
         } else {
             $link_r = phutil_tag('a', array('href' => '#', 'class' => '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>' . '%s' . '</div>', $comparison_table->render(), $navigation_table, $revert_l, $revert_r, $output);
     $object_box = id(new PHUIObjectBoxView())->setHeader($header)->appendChild($output);
     return $this->buildApplicationPage(array($crumbs, $object_box), array('title' => pht('Document History')));
 }
 public function handleRequest(AphrontRequest $request)
 {
     $viewer = $this->getViewer();
     $rendering_reference = $request->getStr('ref');
     $parts = explode('/', $rendering_reference);
     if (count($parts) == 2) {
         list($id, $vs) = $parts;
     } else {
         $id = $parts[0];
         $vs = 0;
     }
     $id = (int) $id;
     $vs = (int) $vs;
     $load_ids = array($id);
     if ($vs && $vs != -1) {
         $load_ids[] = $vs;
     }
     $changesets = id(new DifferentialChangesetQuery())->setViewer($viewer)->withIDs($load_ids)->needHunks(true)->execute();
     $changesets = mpull($changesets, null, 'getID');
     $changeset = idx($changesets, $id);
     if (!$changeset) {
         return new Aphront404Response();
     }
     $vs_changeset = null;
     if ($vs && $vs != -1) {
         $vs_changeset = idx($changesets, $vs);
         if (!$vs_changeset) {
             return new Aphront404Response();
         }
     }
     $view = $request->getStr('view');
     if ($view) {
         $phid = idx($changeset->getMetadata(), "{$view}:binary-phid");
         if ($phid) {
             return id(new AphrontRedirectResponse())->setURI("/file/info/{$phid}/");
         }
         switch ($view) {
             case 'new':
                 return $this->buildRawFileResponse($changeset, $is_new = true);
             case 'old':
                 if ($vs_changeset) {
                     return $this->buildRawFileResponse($vs_changeset, $is_new = true);
                 }
                 return $this->buildRawFileResponse($changeset, $is_new = false);
             default:
                 return new Aphront400Response();
         }
     }
     $old = array();
     $new = array();
     if (!$vs) {
         $right = $changeset;
         $left = null;
         $right_source = $right->getID();
         $right_new = true;
         $left_source = $right->getID();
         $left_new = false;
         $render_cache_key = $right->getID();
         $old[] = $changeset;
         $new[] = $changeset;
     } else {
         if ($vs == -1) {
             $right = null;
             $left = $changeset;
             $right_source = $left->getID();
             $right_new = false;
             $left_source = $left->getID();
             $left_new = true;
             $render_cache_key = null;
             $old[] = $changeset;
             $new[] = $changeset;
         } else {
             $right = $changeset;
             $left = $vs_changeset;
             $right_source = $right->getID();
             $right_new = true;
             $left_source = $left->getID();
             $left_new = true;
             $render_cache_key = null;
             $new[] = $left;
             $new[] = $right;
         }
     }
     if ($left) {
         $left_data = $left->makeNewFile();
         if ($right) {
             $right_data = $right->makeNewFile();
         } else {
             $right_data = $left->makeOldFile();
         }
         $engine = new PhabricatorDifferenceEngine();
         $synthetic = $engine->generateChangesetFromFileContent($left_data, $right_data);
         $choice = clone nonempty($left, $right);
         $choice->attachHunks($synthetic->getHunks());
         $changeset = $choice;
     }
     if ($left_new || $right_new) {
         $diff_map = array();
         if ($left) {
             $diff_map[] = $left->getDiff();
         }
         if ($right) {
             $diff_map[] = $right->getDiff();
         }
         $diff_map = mpull($diff_map, null, 'getPHID');
         $buildables = id(new HarbormasterBuildableQuery())->setViewer($viewer)->withBuildablePHIDs(array_keys($diff_map))->withManualBuildables(false)->needBuilds(true)->needTargets(true)->execute();
         $buildables = mpull($buildables, null, 'getBuildablePHID');
         foreach ($diff_map as $diff_phid => $changeset_diff) {
             $changeset_diff->attachBuildable(idx($buildables, $diff_phid));
         }
     }
     $coverage = null;
     if ($right_new) {
         $coverage = $this->loadCoverage($right);
     }
     $spec = $request->getStr('range');
     list($range_s, $range_e, $mask) = DifferentialChangesetParser::parseRangeSpecification($spec);
     $parser = id(new DifferentialChangesetParser())->setCoverage($coverage)->setChangeset($changeset)->setRenderingReference($rendering_reference)->setRenderCacheKey($render_cache_key)->setRightSideCommentMapping($right_source, $right_new)->setLeftSideCommentMapping($left_source, $left_new);
     $parser->readParametersFromRequest($request);
     if ($left && $right) {
         $parser->setOriginals($left, $right);
     }
     $diff = $changeset->getDiff();
     $revision_id = $diff->getRevisionID();
     $can_mark = false;
     $object_owner_phid = null;
     $revision = null;
     if ($revision_id) {
         $revision = id(new DifferentialRevisionQuery())->setViewer($viewer)->withIDs(array($revision_id))->executeOne();
         if ($revision) {
             $can_mark = $revision->getAuthorPHID() == $viewer->getPHID();
             $object_owner_phid = $revision->getAuthorPHID();
         }
     }
     // Load both left-side and right-side inline comments.
     if ($revision) {
         $query = id(new DifferentialInlineCommentQuery())->setViewer($viewer)->needHidden(true)->withRevisionPHIDs(array($revision->getPHID()));
         $inlines = $query->execute();
         $inlines = $query->adjustInlinesForChangesets($inlines, $old, $new, $revision);
     } else {
         $inlines = array();
     }
     if ($left_new) {
         $inlines = array_merge($inlines, $this->buildLintInlineComments($left));
     }
     if ($right_new) {
         $inlines = array_merge($inlines, $this->buildLintInlineComments($right));
     }
     $phids = array();
     foreach ($inlines as $inline) {
         $parser->parseInlineComment($inline);
         if ($inline->getAuthorPHID()) {
             $phids[$inline->getAuthorPHID()] = true;
         }
     }
     $phids = array_keys($phids);
     $handles = $this->loadViewerHandles($phids);
     $parser->setHandles($handles);
     $engine = new PhabricatorMarkupEngine();
     $engine->setViewer($viewer);
     foreach ($inlines as $inline) {
         $engine->addObject($inline, PhabricatorInlineCommentInterface::MARKUP_FIELD_BODY);
     }
     $engine->process();
     $parser->setUser($viewer)->setMarkupEngine($engine)->setShowEditAndReplyLinks(true)->setCanMarkDone($can_mark)->setObjectOwnerPHID($object_owner_phid)->setRange($range_s, $range_e)->setMask($mask);
     if ($request->isAjax()) {
         // NOTE: We must render the changeset before we render coverage
         // information, since it builds some caches.
         $rendered_changeset = $parser->renderChangeset();
         $mcov = $parser->renderModifiedCoverage();
         $coverage_data = array('differential-mcoverage-' . md5($changeset->getFilename()) => $mcov);
         return id(new PhabricatorChangesetResponse())->setRenderedChangeset($rendered_changeset)->setCoverage($coverage_data)->setUndoTemplates($parser->getRenderer()->renderUndoTemplates());
     }
     $detail = id(new DifferentialChangesetListView())->setUser($this->getViewer())->setChangesets(array($changeset))->setVisibleChangesets(array($changeset))->setRenderingReferences(array($rendering_reference))->setRenderURI('/differential/changeset/')->setDiff($diff)->setTitle(pht('Standalone View'))->setParser($parser);
     if ($revision_id) {
         $detail->setInlineCommentControllerURI('/differential/comment/inline/edit/' . $revision_id . '/');
     }
     $crumbs = $this->buildApplicationCrumbs();
     if ($revision_id) {
         $crumbs->addTextCrumb('D' . $revision_id, '/D' . $revision_id);
     }
     $diff_id = $diff->getID();
     if ($diff_id) {
         $crumbs->addTextCrumb(pht('Diff %d', $diff_id), $this->getApplicationURI('diff/' . $diff_id));
     }
     $crumbs->addTextCrumb($changeset->getDisplayFilename());
     return $this->buildApplicationPage(array($crumbs, $detail), array('title' => pht('Changeset View'), 'device' => false));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $author_phid = $request->getUser()->getPHID();
     $rendering_reference = $request->getStr('ref');
     $parts = explode('/', $rendering_reference);
     if (count($parts) == 2) {
         list($id, $vs) = $parts;
     } else {
         $id = $parts[0];
         $vs = 0;
     }
     $id = (int) $id;
     $vs = (int) $vs;
     $changeset = id(new DifferentialChangeset())->load($id);
     if (!$changeset) {
         return new Aphront404Response();
     }
     $view = $request->getStr('view');
     if ($view) {
         $changeset->attachHunks($changeset->loadHunks());
         switch ($view) {
             case 'new':
                 return $this->buildRawFileResponse($changeset->makeNewFile());
             case 'old':
                 return $this->buildRawFileResponse($changeset->makeOldFile());
             default:
                 return new Aphront400Response();
         }
     }
     if ($vs && $vs != -1) {
         $vs_changeset = id(new DifferentialChangeset())->load($vs);
         if (!$vs_changeset) {
             return new Aphront404Response();
         }
     }
     if (!$vs) {
         $right = $changeset;
         $left = null;
         $right_source = $right->getID();
         $right_new = true;
         $left_source = $right->getID();
         $left_new = false;
         $render_cache_key = $right->getID();
     } else {
         if ($vs == -1) {
             $right = null;
             $left = $changeset;
             $right_source = $left->getID();
             $right_new = false;
             $left_source = $left->getID();
             $left_new = true;
             $render_cache_key = null;
         } else {
             $right = $changeset;
             $left = $vs_changeset;
             $right_source = $right->getID();
             $right_new = true;
             $left_source = $left->getID();
             $left_new = true;
             $render_cache_key = null;
         }
     }
     if ($left) {
         $left->attachHunks($left->loadHunks());
     }
     if ($right) {
         $right->attachHunks($right->loadHunks());
     }
     if ($left) {
         $left_data = $left->makeNewFile();
         if ($right) {
             $right_data = $right->makeNewFile();
         } else {
             $right_data = $left->makeOldFile();
         }
         $engine = new PhabricatorDifferenceEngine();
         $synthetic = $engine->generateChangesetFromFileContent($left_data, $right_data);
         $choice = nonempty($left, $right);
         $choice->attachHunks($synthetic->getHunks());
         $changeset = $choice;
         $changeset->setID(null);
     }
     $spec = $request->getStr('range');
     list($range_s, $range_e, $mask) = DifferentialChangesetParser::parseRangeSpecification($spec);
     $parser = new DifferentialChangesetParser();
     $parser->setChangeset($changeset);
     $parser->setRenderingReference($rendering_reference);
     $parser->setRenderCacheKey($render_cache_key);
     $parser->setRightSideCommentMapping($right_source, $right_new);
     $parser->setLeftSideCommentMapping($left_source, $left_new);
     $parser->setWhitespaceMode($request->getStr('whitespace'));
     // Load both left-side and right-side inline comments.
     $inlines = $this->loadInlineComments(array($left_source, $right_source), $author_phid);
     $phids = array();
     foreach ($inlines as $inline) {
         $parser->parseInlineComment($inline);
         $phids[$inline->getAuthorPHID()] = true;
     }
     $phids = array_keys($phids);
     $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles();
     $parser->setHandles($handles);
     $engine = PhabricatorMarkupEngine::newDifferentialMarkupEngine();
     $parser->setMarkupEngine($engine);
     if ($request->isAjax()) {
         // TODO: This is sort of lazy, the effect is just to not render "Edit"
         // links on the "standalone view".
         $parser->setUser($request->getUser());
     }
     $output = $parser->render($range_s, $range_e, $mask);
     if ($request->isAjax()) {
         return id(new AphrontAjaxResponse())->setContent($output);
     }
     Javelin::initBehavior('differential-show-more', array('uri' => '/differential/changeset/', 'whitespace' => $request->getStr('whitespace')));
     Javelin::initBehavior('differential-comment-jump', array());
     $detail = new DifferentialChangesetDetailView();
     $detail->setChangeset($changeset);
     $detail->appendChild($output);
     if (!$vs) {
         $detail->addButton(phutil_render_tag('a', array('href' => $request->getRequestURI()->alter('view', 'old'), 'class' => 'grey button small'), 'View Raw File (Old Version)'));
         $detail->addButton(phutil_render_tag('a', array('href' => $request->getRequestURI()->alter('view', 'new'), 'class' => 'grey button small'), 'View Raw File (New Version)'));
     }
     $detail->setRevisionID($request->getInt('revision_id'));
     $output = id(new DifferentialPrimaryPaneView())->setLineWidthFromChangesets(array($changeset))->appendChild('<div class="differential-review-stage" ' . 'id="differential-review-stage">' . $detail->render() . '</div>');
     return $this->buildStandardPageResponse(array($output), array('title' => 'Changeset View'));
 }
 /**
  * NOTE: We have to work particularly hard for SVN as compared to other VCS.
  * That's okay but means this shares little code with the other VCS.
  */
 protected function getSVNResult(ConduitAPIRequest $request)
 {
     $drequest = $this->getDiffusionRequest();
     $repository = $drequest->getRepository();
     $effective_commit = $this->getEffectiveCommit($request);
     if (!$effective_commit) {
         return $this->getEmptyResult();
     }
     $drequest = clone $drequest;
     $drequest->updateSymbolicCommit($effective_commit);
     $path_change_query = DiffusionPathChangeQuery::newFromDiffusionRequest($drequest);
     $path_changes = $path_change_query->loadChanges();
     $path = null;
     foreach ($path_changes as $change) {
         if ($change->getPath() == $drequest->getPath()) {
             $path = $change;
         }
     }
     if (!$path) {
         return $this->getEmptyResult();
     }
     $change_type = $path->getChangeType();
     switch ($change_type) {
         case DifferentialChangeType::TYPE_MULTICOPY:
         case DifferentialChangeType::TYPE_DELETE:
             if ($path->getTargetPath()) {
                 $old = array($path->getTargetPath(), $path->getTargetCommitIdentifier());
             } else {
                 $old = array($path->getPath(), $path->getCommitIdentifier() - 1);
             }
             $old_name = $path->getPath();
             $new_name = '';
             $new = null;
             break;
         case DifferentialChangeType::TYPE_ADD:
             $old = null;
             $new = array($path->getPath(), $path->getCommitIdentifier());
             $old_name = '';
             $new_name = $path->getPath();
             break;
         case DifferentialChangeType::TYPE_MOVE_HERE:
         case DifferentialChangeType::TYPE_COPY_HERE:
             $old = array($path->getTargetPath(), $path->getTargetCommitIdentifier());
             $new = array($path->getPath(), $path->getCommitIdentifier());
             $old_name = $path->getTargetPath();
             $new_name = $path->getPath();
             break;
         case DifferentialChangeType::TYPE_MOVE_AWAY:
             $old = array($path->getPath(), $path->getCommitIdentifier() - 1);
             $old_name = $path->getPath();
             $new_name = null;
             $new = null;
             break;
         default:
             $old = array($path->getPath(), $path->getCommitIdentifier() - 1);
             $new = array($path->getPath(), $path->getCommitIdentifier());
             $old_name = $path->getPath();
             $new_name = $path->getPath();
             break;
     }
     $futures = array('old' => $this->buildSVNContentFuture($old), 'new' => $this->buildSVNContentFuture($new));
     $futures = array_filter($futures);
     foreach (new FutureIterator($futures) as $key => $future) {
         $stdout = '';
         try {
             list($stdout) = $future->resolvex();
         } catch (CommandException $e) {
             if ($path->getFileType() != DifferentialChangeType::FILE_DIRECTORY) {
                 throw $e;
             }
         }
         $futures[$key] = $stdout;
     }
     $old_data = idx($futures, 'old', '');
     $new_data = idx($futures, 'new', '');
     $engine = new PhabricatorDifferenceEngine();
     $engine->setOldName($old_name);
     $engine->setNewName($new_name);
     $raw_diff = $engine->generateRawDiffFromFileContent($old_data, $new_data);
     $arcanist_changes = DiffusionPathChange::convertToArcanistChanges($path_changes);
     $parser = $this->getDefaultParser();
     $parser->setChanges($arcanist_changes);
     $parser->forcePath($path->getPath());
     $changes = $parser->parseDiff($raw_diff);
     $change = $changes[$path->getPath()];
     return array($change);
 }
 private function renderFullSummary($transaction)
 {
     switch ($transaction->getTransactionType()) {
         case ManiphestTransactionType::TYPE_DESCRIPTION:
             $id = $transaction->getID();
             $old_text = wordwrap($transaction->getOldValue(), 80);
             $new_text = wordwrap($transaction->getNewValue(), 80);
             $engine = new PhabricatorDifferenceEngine();
             $changeset = $engine->generateChangesetFromFileContent($old_text, $new_text);
             $whitespace_mode = DifferentialChangesetParser::WHITESPACE_SHOW_ALL;
             $parser = new DifferentialChangesetParser();
             $parser->setChangeset($changeset);
             $parser->setRenderingReference($id);
             $parser->setWhitespaceMode($whitespace_mode);
             $spec = $this->getRangeSpecification();
             list($range_s, $range_e, $mask) = DifferentialChangesetParser::parseRangeSpecification($spec);
             $output = $parser->render($range_s, $range_e, $mask);
             return $output;
     }
     return null;
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $document = id(new PhrictionDocument())->load($this->id);
     if (!$document) {
         return new Aphront404Response();
     }
     $current = id(new PhrictionContent())->load($document->getContentID());
     $l = $request->getInt('l');
     $r = $request->getInt('r');
     $ref = $request->getStr('ref');
     if ($ref) {
         list($l, $r) = explode(',', $ref);
     }
     $content = id(new PhrictionContent())->loadAllWhere('documentID = %d AND version IN (%Ld)', $document->getID(), array($l, $r));
     $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();
     $text_l = wordwrap($text_l, 80);
     $text_r = wordwrap($text_r, 80);
     $engine = new PhabricatorDifferenceEngine();
     $changeset = $engine->generateChangesetFromFileContent($text_l, $text_r);
     $changeset->setOldProperties(array('Title' => $content_l->getTitle()));
     $changeset->setNewProperties(array('Title' => $content_r->getTitle()));
     $whitespace_mode = DifferentialChangesetParser::WHITESPACE_SHOW_ALL;
     $parser = new DifferentialChangesetParser();
     $parser->setChangeset($changeset);
     $parser->setRenderingReference("{$l},{$r}");
     $parser->setWhitespaceMode($whitespace_mode);
     $spec = $request->getStr('range');
     list($range_s, $range_e, $mask) = DifferentialChangesetParser::parseRangeSpecification($spec);
     $output = $parser->render($range_s, $range_e, $mask);
     if ($request->isAjax()) {
         return id(new PhabricatorChangesetResponse())->setRenderedChangeset($output);
     }
     require_celerity_resource('differential-changeset-view-css');
     require_celerity_resource('syntax-highlighting-css');
     require_celerity_resource('phriction-document-css');
     Javelin::initBehavior('differential-show-more', array('uri' => '/phriction/diff/' . $document->getID() . '/', 'whitespace' => $whitespace_mode));
     $slug = $document->getSlug();
     $revert_l = $this->renderRevertButton($content_l, $current);
     $revert_r = $this->renderRevertButton($content_r, $current);
     $crumbs = new AphrontCrumbsView();
     $crumbs->setCrumbs(array('Phriction', phutil_render_tag('a', array('href' => PhrictionDocument::getSlugURI($slug)), phutil_escape_html($current->getTitle())), phutil_render_tag('a', array('href' => '/phriction/history/' . $document->getSlug() . '/'), 'History'), phutil_escape_html("Changes Between Version {$l} and Version {$r}")));
     $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_render_tag('a', array('href' => $uri->alter('l', $l - 1)->alter('r', $r - 1)), "« Previous Change");
         } else {
             $link_l = 'Original Change';
         }
         $link_r = null;
         if ($nav_r) {
             $link_r = phutil_render_tag('a', array('href' => $uri->alter('l', $l + 1)->alter('r', $r + 1)), "Next Change »");
         } else {
             $link_r = 'Most Recent Change';
         }
         $navigation_table = '<table class="phriction-history-nav-table">
       <tr>
         <td class="nav-prev">' . $link_l . '</td>
         <td class="nav-next">' . $link_r . '</td>
       </tr>
     </table>';
     }
     $output = '<div class="phriction-document-history-diff">' . $comparison_table->render() . '<br />' . '<br />' . $navigation_table . '<table class="phriction-revert-table">' . '<tr><td>' . $revert_l . '</td><td>' . $revert_r . '</td>' . '</table>' . $output . '</div>';
     return $this->buildStandardPageResponse(array($crumbs, $output), array('title' => 'Document History'));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $author_phid = $request->getUser()->getPHID();
     $rendering_reference = $request->getStr('ref');
     $parts = explode('/', $rendering_reference);
     if (count($parts) == 2) {
         list($id, $vs) = $parts;
     } else {
         $id = $parts[0];
         $vs = 0;
     }
     $id = (int) $id;
     $vs = (int) $vs;
     $changeset = id(new DifferentialChangeset())->load($id);
     if (!$changeset) {
         return new Aphront404Response();
     }
     $view = $request->getStr('view');
     if ($view) {
         $changeset->attachHunks($changeset->loadHunks());
         $phid = idx($changeset->getMetadata(), "{$view}:binary-phid");
         if ($phid) {
             return id(new AphrontRedirectResponse())->setURI("/file/info/{$phid}/");
         }
         switch ($view) {
             case 'new':
                 return $this->buildRawFileResponse($changeset, $is_new = true);
             case 'old':
                 if ($vs && $vs != -1) {
                     $vs_changeset = id(new DifferentialChangeset())->load($vs);
                     if ($vs_changeset) {
                         $vs_changeset->attachHunks($vs_changeset->loadHunks());
                         return $this->buildRawFileResponse($vs_changeset, $is_new = true);
                     }
                 }
                 return $this->buildRawFileResponse($changeset, $is_new = false);
             default:
                 return new Aphront400Response();
         }
     }
     if ($vs && $vs != -1) {
         $vs_changeset = id(new DifferentialChangeset())->load($vs);
         if (!$vs_changeset) {
             return new Aphront404Response();
         }
     }
     if (!$vs) {
         $right = $changeset;
         $left = null;
         $right_source = $right->getID();
         $right_new = true;
         $left_source = $right->getID();
         $left_new = false;
         $render_cache_key = $right->getID();
     } else {
         if ($vs == -1) {
             $right = null;
             $left = $changeset;
             $right_source = $left->getID();
             $right_new = false;
             $left_source = $left->getID();
             $left_new = true;
             $render_cache_key = null;
         } else {
             $right = $changeset;
             $left = $vs_changeset;
             $right_source = $right->getID();
             $right_new = true;
             $left_source = $left->getID();
             $left_new = true;
             $render_cache_key = null;
         }
     }
     if ($left) {
         $left->attachHunks($left->loadHunks());
     }
     if ($right) {
         $right->attachHunks($right->loadHunks());
     }
     if ($left) {
         $left_data = $left->makeNewFile();
         if ($right) {
             $right_data = $right->makeNewFile();
         } else {
             $right_data = $left->makeOldFile();
         }
         $engine = new PhabricatorDifferenceEngine();
         $synthetic = $engine->generateChangesetFromFileContent($left_data, $right_data);
         $choice = clone nonempty($left, $right);
         $choice->attachHunks($synthetic->getHunks());
         $changeset = $choice;
     }
     $coverage = null;
     if ($right && $right->getDiffID()) {
         $unit = id(new DifferentialDiffProperty())->loadOneWhere('diffID = %d AND name = %s', $right->getDiffID(), 'arc:unit');
         if ($unit) {
             $coverage = array();
             foreach ($unit->getData() as $result) {
                 $result_coverage = idx($result, 'coverage');
                 if (!$result_coverage) {
                     continue;
                 }
                 $file_coverage = idx($result_coverage, $right->getFileName());
                 if (!$file_coverage) {
                     continue;
                 }
                 $coverage[] = $file_coverage;
             }
             $coverage = ArcanistUnitTestResult::mergeCoverage($coverage);
         }
     }
     $spec = $request->getStr('range');
     list($range_s, $range_e, $mask) = DifferentialChangesetParser::parseRangeSpecification($spec);
     $parser = new DifferentialChangesetParser();
     $parser->setCoverage($coverage);
     $parser->setChangeset($changeset);
     $parser->setRenderingReference($rendering_reference);
     $parser->setRenderCacheKey($render_cache_key);
     $parser->setRightSideCommentMapping($right_source, $right_new);
     $parser->setLeftSideCommentMapping($left_source, $left_new);
     $parser->setWhitespaceMode($request->getStr('whitespace'));
     if ($left && $right) {
         $parser->setOriginals($left, $right);
     }
     // Load both left-side and right-side inline comments.
     $inlines = $this->loadInlineComments(array($left_source, $right_source), $author_phid);
     if ($left_new) {
         $inlines = array_merge($inlines, $this->buildLintInlineComments($left));
     }
     if ($right_new) {
         $inlines = array_merge($inlines, $this->buildLintInlineComments($right));
     }
     $phids = array();
     foreach ($inlines as $inline) {
         $parser->parseInlineComment($inline);
         if ($inline->getAuthorPHID()) {
             $phids[$inline->getAuthorPHID()] = true;
         }
     }
     $phids = array_keys($phids);
     $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles();
     $parser->setHandles($handles);
     $engine = PhabricatorMarkupEngine::newDifferentialMarkupEngine();
     $parser->setMarkupEngine($engine);
     if ($request->isAjax()) {
         // TODO: This is sort of lazy, the effect is just to not render "Edit"
         // and "Reply" links on the "standalone view".
         $parser->setUser($request->getUser());
     }
     $output = $parser->render($range_s, $range_e, $mask);
     $mcov = $parser->renderModifiedCoverage();
     if ($request->isAjax()) {
         $coverage = array('differential-mcoverage-' . md5($changeset->getFilename()) => $mcov);
         return id(new PhabricatorChangesetResponse())->setRenderedChangeset($output)->setCoverage($coverage);
     }
     Javelin::initBehavior('differential-show-more', array('uri' => '/differential/changeset/', 'whitespace' => $request->getStr('whitespace')));
     Javelin::initBehavior('differential-comment-jump', array());
     $detail = new DifferentialChangesetDetailView();
     $detail->setChangeset($changeset);
     $detail->appendChild($output);
     $detail->setVsChangesetID($left_source);
     $output = id(new DifferentialPrimaryPaneView())->setLineWidthFromChangesets(array($changeset))->appendChild('<div class="differential-review-stage" ' . 'id="differential-review-stage">' . $detail->render() . '</div>');
     return $this->buildStandardPageResponse(array($output), array('title' => 'Changeset View'));
 }
 private function tryCacheStuff()
 {
     $whitespace_mode = $this->whitespaceMode;
     switch ($whitespace_mode) {
         case self::WHITESPACE_SHOW_ALL:
         case self::WHITESPACE_IGNORE_TRAILING:
             break;
         default:
             $whitespace_mode = self::WHITESPACE_IGNORE_ALL;
             break;
     }
     $skip_cache = $whitespace_mode != self::WHITESPACE_IGNORE_ALL;
     $this->whitespaceMode = $whitespace_mode;
     $changeset = $this->changeset;
     if ($changeset->getFileType() == DifferentialChangeType::FILE_TEXT || $changeset->getFileType() == DifferentialChangeType::FILE_SYMLINK) {
         if ($skip_cache || !$this->loadCache()) {
             $ignore_all = $this->whitespaceMode == self::WHITESPACE_IGNORE_ALL;
             if ($ignore_all && $changeset->getWhitespaceMatters()) {
                 $ignore_all = false;
             }
             // The "ignore all whitespace" algorithm depends on rediffing the
             // files, and we currently need complete representations of both
             // files to do anything reasonable. If we only have parts of the files,
             // don't use the "ignore all" algorithm.
             if ($ignore_all) {
                 $hunks = $changeset->getHunks();
                 if (count($hunks) !== 1) {
                     $ignore_all = false;
                 } else {
                     $first_hunk = reset($hunks);
                     if ($first_hunk->getOldOffset() != 1 || $first_hunk->getNewOffset() != 1) {
                         $ignore_all = false;
                     }
                 }
             }
             if ($ignore_all) {
                 $old_file = $changeset->makeOldFile();
                 $new_file = $changeset->makeNewFile();
                 if ($old_file == $new_file) {
                     // If the old and new files are exactly identical, the synthetic
                     // diff below will give us nonsense and whitespace modes are
                     // irrelevant anyway. This occurs when you, e.g., copy a file onto
                     // itself in Subversion (see T271).
                     $ignore_all = false;
                 }
             }
             if ($ignore_all) {
                 // Huge mess. Generate a "-bw" (ignore all whitespace changes) diff,
                 // parse it out, and then play a shell game with the parsed format
                 // in process() so we highlight only changed lines but render
                 // whitespace differences. If we don't do this, we either fail to
                 // render whitespace changes (which is incredibly confusing,
                 // especially for python) or often produce a much larger set of
                 // differences than necessary.
                 $engine = new PhabricatorDifferenceEngine();
                 $engine->setIgnoreWhitespace(true);
                 $no_whitespace_changeset = $engine->generateChangesetFromFileContent($old_file, $new_file);
                 // subparser takes over the current non-whitespace-ignoring changeset
                 $subparser = new DifferentialChangesetParser();
                 $subparser->isSubparser = true;
                 $subparser->setChangeset($changeset);
                 foreach ($changeset->getHunks() as $hunk) {
                     $subparser->parseHunk($hunk);
                 }
                 // We need to call process() so that the subparser's values for
                 // metadata (like 'unchanged') is correct.
                 $subparser->process();
                 $this->subparser = $subparser;
                 // While we aren't updating $this->changeset (since it has a bunch
                 // of metadata we need to preserve, so that headers like "this file
                 // was moved" render correctly), we're overwriting the local
                 // $changeset so that the block below will choose the synthetic
                 // hunks we've built instead of the original hunks.
                 $changeset = $no_whitespace_changeset;
             }
             // This either uses the real hunks, or synthetic hunks we built above.
             foreach ($changeset->getHunks() as $hunk) {
                 $this->parseHunk($hunk);
             }
             $this->process();
             if (!$skip_cache) {
                 $this->saveCache();
             }
         }
     }
 }
 /**
  * Note this code is somewhat similar to the buildPatch method in
  * @{class:DifferentialReviewRequestMail}.
  *
  * @return @{class:AphrontRedirectResponse}
  */
 private function buildRawDiffResponse(array $changesets, array $vs_changesets, array $vs_map, PhabricatorRepository $repository = null)
 {
     assert_instances_of($changesets, 'DifferentialChangeset');
     assert_instances_of($vs_changesets, 'DifferentialChangeset');
     $engine = new PhabricatorDifferenceEngine();
     $generated_changesets = array();
     foreach ($changesets as $changeset) {
         $changeset->attachHunks($changeset->loadHunks());
         $right = $changeset->makeNewFile();
         $choice = $changeset;
         $vs = idx($vs_map, $changeset->getID());
         if ($vs == -1) {
             $left = $right;
             $right = $changeset->makeOldFile();
         } else {
             if ($vs) {
                 $choice = $vs_changeset = $vs_changesets[$vs];
                 $vs_changeset->attachHunks($vs_changeset->loadHunks());
                 $left = $vs_changeset->makeNewFile();
             } else {
                 $left = $changeset->makeOldFile();
             }
         }
         $synthetic = $engine->generateChangesetFromFileContent($left, $right);
         if (!$synthetic->getAffectedLineCount()) {
             $filetype = $choice->getFileType();
             if ($filetype == DifferentialChangeType::FILE_TEXT || $filetype == DifferentialChangeType::FILE_SYMLINK) {
                 continue;
             }
         }
         $choice->attachHunks($synthetic->getHunks());
         $generated_changesets[] = $choice;
     }
     $diff = new DifferentialDiff();
     $diff->attachChangesets($generated_changesets);
     $diff_dict = $diff->getDiffDict();
     $changes = array();
     foreach ($diff_dict['changes'] as $changedict) {
         $changes[] = ArcanistDiffChange::newFromDictionary($changedict);
     }
     $bundle = ArcanistBundle::newFromChanges($changes);
     $bundle->setLoadFileDataCallback(array($this, 'loadFileByPHID'));
     $vcs = $repository ? $repository->getVersionControlSystem() : null;
     switch ($vcs) {
         case PhabricatorRepositoryType::REPOSITORY_TYPE_GIT:
         case PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL:
             $raw_diff = $bundle->toGitPatch();
             break;
         case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN:
         default:
             $raw_diff = $bundle->toUnifiedDiff();
             break;
     }
     $request_uri = $this->getRequest()->getRequestURI();
     // this ends up being something like
     //   D123.diff
     // or the verbose
     //   D123.vs123.id123.whitespaceignore-all.diff
     // lame but nice to include these options
     $file_name = ltrim($request_uri->getPath(), '/') . '.';
     foreach ($request_uri->getQueryParams() as $key => $value) {
         if ($key == 'download') {
             continue;
         }
         $file_name .= $key . $value . '.';
     }
     $file_name .= 'diff';
     $file = PhabricatorFile::buildFromFileDataOrHash($raw_diff, array('name' => $file_name));
     return id(new AphrontRedirectResponse())->setURI($file->getBestURI());
 }
 protected function executeQuery()
 {
     $drequest = $this->getRequest();
     if (!$drequest->getRawCommit()) {
         $effective_commit = $this->getEffectiveCommit();
         if (!$effective_commit) {
             return null;
         }
         // TODO: Sketchy side effect.
         $drequest->setCommit($effective_commit);
     }
     $path_change_query = DiffusionPathChangeQuery::newFromDiffusionRequest($drequest);
     $path_changes = $path_change_query->loadChanges();
     $path = null;
     foreach ($path_changes as $change) {
         if ($change->getPath() == $drequest->getPath()) {
             $path = $change;
         }
     }
     if (!$path) {
         return null;
     }
     $change_type = $path->getChangeType();
     switch ($change_type) {
         case DifferentialChangeType::TYPE_MULTICOPY:
         case DifferentialChangeType::TYPE_DELETE:
             if ($path->getTargetPath()) {
                 $old = array($path->getTargetPath(), $path->getTargetCommitIdentifier());
             } else {
                 $old = array($path->getPath(), $path->getCommitIdentifier() - 1);
             }
             $old_name = $path->getPath();
             $new_name = '';
             $new = null;
             break;
         case DifferentialChangeType::TYPE_ADD:
             $old = null;
             $new = array($path->getPath(), $path->getCommitIdentifier());
             $old_name = '';
             $new_name = $path->getPath();
             break;
         case DifferentialChangeType::TYPE_MOVE_HERE:
         case DifferentialChangeType::TYPE_COPY_HERE:
             $old = array($path->getTargetPath(), $path->getTargetCommitIdentifier());
             $new = array($path->getPath(), $path->getCommitIdentifier());
             $old_name = $path->getTargetPath();
             $new_name = $path->getPath();
             break;
         case DifferentialChangeType::TYPE_MOVE_AWAY:
             $old = array($path->getPath(), $path->getCommitIdentifier() - 1);
             $old_name = $path->getPath();
             $new_name = null;
             $new = null;
             break;
         default:
             $old = array($path->getPath(), $path->getCommitIdentifier() - 1);
             $new = array($path->getPath(), $path->getCommitIdentifier());
             $old_name = $path->getPath();
             $new_name = $path->getPath();
             break;
     }
     $futures = array('old' => $this->buildContentFuture($old), 'new' => $this->buildContentFuture($new));
     $futures = array_filter($futures);
     foreach (Futures($futures) as $key => $future) {
         list($stdout) = $future->resolvex();
         $futures[$key] = $stdout;
     }
     $old_data = idx($futures, 'old', '');
     $new_data = idx($futures, 'new', '');
     $engine = new PhabricatorDifferenceEngine();
     $engine->setOldName($old_name);
     $engine->setNewName($new_name);
     $raw_diff = $engine->generateRawDiffFromFileContent($old_data, $new_data);
     $parser = new ArcanistDiffParser();
     $parser->setDetectBinaryFiles(true);
     $arcanist_changes = DiffusionPathChange::convertToArcanistChanges($path_changes);
     $parser->setChanges($arcanist_changes);
     $parser->forcePath($path->getPath());
     $changes = $parser->parseDiff($raw_diff);
     $change = $changes[$path->getPath()];
     $diff = DifferentialDiff::newFromRawChanges(array($change));
     $changesets = $diff->getChangesets();
     $changeset = reset($changesets);
     $this->renderingReference = $drequest->getPath() . ';' . $drequest->getCommit();
     return $changeset;
 }
 private function process()
 {
     $whitespace_mode = $this->whitespaceMode;
     $changeset = $this->changeset;
     $ignore_all = $whitespace_mode == self::WHITESPACE_IGNORE_MOST || $whitespace_mode == self::WHITESPACE_IGNORE_ALL;
     $force_ignore = $whitespace_mode == self::WHITESPACE_IGNORE_ALL;
     if (!$force_ignore) {
         if ($ignore_all && $changeset->getWhitespaceMatters()) {
             $ignore_all = false;
         }
     }
     // The "ignore all whitespace" algorithm depends on rediffing the
     // files, and we currently need complete representations of both
     // files to do anything reasonable. If we only have parts of the files,
     // don't use the "ignore all" algorithm.
     if ($ignore_all) {
         $hunks = $changeset->getHunks();
         if (count($hunks) !== 1) {
             $ignore_all = false;
         } else {
             $first_hunk = reset($hunks);
             if ($first_hunk->getOldOffset() != 1 || $first_hunk->getNewOffset() != 1) {
                 $ignore_all = false;
             }
         }
     }
     if ($ignore_all) {
         $old_file = $changeset->makeOldFile();
         $new_file = $changeset->makeNewFile();
         if ($old_file == $new_file) {
             // If the old and new files are exactly identical, the synthetic
             // diff below will give us nonsense and whitespace modes are
             // irrelevant anyway. This occurs when you, e.g., copy a file onto
             // itself in Subversion (see T271).
             $ignore_all = false;
         }
     }
     $hunk_parser = new DifferentialHunkParser();
     $hunk_parser->setWhitespaceMode($whitespace_mode);
     $hunk_parser->parseHunksForLineData($changeset->getHunks());
     // Depending on the whitespace mode, we may need to compute a different
     // set of changes than the set of changes in the hunk data (specificaly,
     // we might want to consider changed lines which have only whitespace
     // changes as unchanged).
     if ($ignore_all) {
         $engine = new PhabricatorDifferenceEngine();
         $engine->setIgnoreWhitespace(true);
         $no_whitespace_changeset = $engine->generateChangesetFromFileContent($old_file, $new_file);
         $type_parser = new DifferentialHunkParser();
         $type_parser->parseHunksForLineData($no_whitespace_changeset->getHunks());
         $hunk_parser->setOldLineTypeMap($type_parser->getOldLineTypeMap());
         $hunk_parser->setNewLineTypeMap($type_parser->getNewLineTypeMap());
     }
     $hunk_parser->reparseHunksForSpecialAttributes();
     $unchanged = false;
     if (!$hunk_parser->getHasAnyChanges()) {
         $filetype = $this->changeset->getFileType();
         if ($filetype == DifferentialChangeType::FILE_TEXT || $filetype == DifferentialChangeType::FILE_SYMLINK) {
             $unchanged = true;
         }
     }
     $moveaway = false;
     $changetype = $this->changeset->getChangeType();
     if ($changetype == DifferentialChangeType::TYPE_MOVE_AWAY) {
         $moveaway = true;
     }
     $this->setSpecialAttributes(array(self::ATTR_UNCHANGED => $unchanged, self::ATTR_DELETED => $hunk_parser->getIsDeleted(), self::ATTR_WHITELINES => !$hunk_parser->getHasTextChanges(), self::ATTR_MOVEAWAY => $moveaway));
     $lines_context = $this->getLinesOfContext();
     $hunk_parser->generateIntraLineDiffs();
     $hunk_parser->generateVisibileLinesMask($lines_context);
     $this->setOldLines($hunk_parser->getOldLines());
     $this->setNewLines($hunk_parser->getNewLines());
     $this->setIntraLineDiffs($hunk_parser->getIntraLineDiffs());
     $this->setVisibileLinesMask($hunk_parser->getVisibleLinesMask());
     $this->hunkStartLines = $hunk_parser->getHunkStartLines($changeset->getHunks());
     $new_corpus = $hunk_parser->getNewCorpus();
     $new_corpus_block = implode('', $new_corpus);
     $this->markGenerated($new_corpus_block);
     if ($this->isTopLevel && !$this->comments && ($this->isGenerated() || $this->isUnchanged() || $this->isDeleted())) {
         return;
     }
     $old_corpus = $hunk_parser->getOldCorpus();
     $old_corpus_block = implode('', $old_corpus);
     $old_future = $this->getHighlightFuture($old_corpus_block);
     $new_future = $this->getHighlightFuture($new_corpus_block);
     $futures = array('old' => $old_future, 'new' => $new_future);
     $corpus_blocks = array('old' => $old_corpus_block, 'new' => $new_corpus_block);
     $this->highlightErrors = false;
     foreach (new FutureIterator($futures) as $key => $future) {
         try {
             try {
                 $highlighted = $future->resolve();
             } catch (PhutilSyntaxHighlighterException $ex) {
                 $this->highlightErrors = true;
                 $highlighted = id(new PhutilDefaultSyntaxHighlighter())->getHighlightFuture($corpus_blocks[$key])->resolve();
             }
             switch ($key) {
                 case 'old':
                     $this->oldRender = $this->processHighlightedSource($this->old, $highlighted);
                     break;
                 case 'new':
                     $this->newRender = $this->processHighlightedSource($this->new, $highlighted);
                     break;
             }
         } catch (Exception $ex) {
             phlog($ex);
             throw $ex;
         }
     }
     $this->applyIntraline($this->oldRender, ipull($this->intra, 0), $old_corpus);
     $this->applyIntraline($this->newRender, ipull($this->intra, 1), $new_corpus);
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $author_phid = $request->getUser()->getPHID();
     $rendering_reference = $request->getStr('ref');
     $parts = explode('/', $rendering_reference);
     if (count($parts) == 2) {
         list($id, $vs) = $parts;
     } else {
         $id = $parts[0];
         $vs = 0;
     }
     $id = (int) $id;
     $vs = (int) $vs;
     $load_ids = array($id);
     if ($vs && $vs != -1) {
         $load_ids[] = $vs;
     }
     $changesets = id(new DifferentialChangesetQuery())->setViewer($request->getUser())->withIDs($load_ids)->needHunks(true)->execute();
     $changesets = mpull($changesets, null, 'getID');
     $changeset = idx($changesets, $id);
     if (!$changeset) {
         return new Aphront404Response();
     }
     $vs_changeset = null;
     if ($vs && $vs != -1) {
         $vs_changeset = idx($changesets, $vs);
         if (!$vs_changeset) {
             return new Aphront404Response();
         }
     }
     $view = $request->getStr('view');
     if ($view) {
         $phid = idx($changeset->getMetadata(), "{$view}:binary-phid");
         if ($phid) {
             return id(new AphrontRedirectResponse())->setURI("/file/info/{$phid}/");
         }
         switch ($view) {
             case 'new':
                 return $this->buildRawFileResponse($changeset, $is_new = true);
             case 'old':
                 if ($vs_changeset) {
                     return $this->buildRawFileResponse($vs_changeset, $is_new = true);
                 }
                 return $this->buildRawFileResponse($changeset, $is_new = false);
             default:
                 return new Aphront400Response();
         }
     }
     if (!$vs) {
         $right = $changeset;
         $left = null;
         $right_source = $right->getID();
         $right_new = true;
         $left_source = $right->getID();
         $left_new = false;
         $render_cache_key = $right->getID();
     } else {
         if ($vs == -1) {
             $right = null;
             $left = $changeset;
             $right_source = $left->getID();
             $right_new = false;
             $left_source = $left->getID();
             $left_new = true;
             $render_cache_key = null;
         } else {
             $right = $changeset;
             $left = $vs_changeset;
             $right_source = $right->getID();
             $right_new = true;
             $left_source = $left->getID();
             $left_new = true;
             $render_cache_key = null;
         }
     }
     if ($left) {
         $left_data = $left->makeNewFile();
         if ($right) {
             $right_data = $right->makeNewFile();
         } else {
             $right_data = $left->makeOldFile();
         }
         $engine = new PhabricatorDifferenceEngine();
         $synthetic = $engine->generateChangesetFromFileContent($left_data, $right_data);
         $choice = clone nonempty($left, $right);
         $choice->attachHunks($synthetic->getHunks());
         $changeset = $choice;
     }
     $coverage = null;
     if ($right && $right->getDiffID()) {
         $unit = id(new DifferentialDiffProperty())->loadOneWhere('diffID = %d AND name = %s', $right->getDiffID(), 'arc:unit');
         if ($unit) {
             $coverage = array();
             foreach ($unit->getData() as $result) {
                 $result_coverage = idx($result, 'coverage');
                 if (!$result_coverage) {
                     continue;
                 }
                 $file_coverage = idx($result_coverage, $right->getFileName());
                 if (!$file_coverage) {
                     continue;
                 }
                 $coverage[] = $file_coverage;
             }
             $coverage = ArcanistUnitTestResult::mergeCoverage($coverage);
         }
     }
     $spec = $request->getStr('range');
     list($range_s, $range_e, $mask) = DifferentialChangesetParser::parseRangeSpecification($spec);
     $parser = new DifferentialChangesetParser();
     $parser->setCoverage($coverage);
     $parser->setChangeset($changeset);
     $parser->setRenderingReference($rendering_reference);
     $parser->setRenderCacheKey($render_cache_key);
     $parser->setRightSideCommentMapping($right_source, $right_new);
     $parser->setLeftSideCommentMapping($left_source, $left_new);
     $parser->setWhitespaceMode($request->getStr('whitespace'));
     $parser->setCharacterEncoding($request->getStr('encoding'));
     $parser->setHighlightAs($request->getStr('highlight'));
     if ($request->getStr('renderer') == '1up') {
         $parser->setRenderer(new DifferentialChangesetOneUpRenderer());
     }
     if ($left && $right) {
         $parser->setOriginals($left, $right);
     }
     // Load both left-side and right-side inline comments.
     $inlines = $this->loadInlineComments(array($left_source, $right_source), $author_phid);
     if ($left_new) {
         $inlines = array_merge($inlines, $this->buildLintInlineComments($left));
     }
     if ($right_new) {
         $inlines = array_merge($inlines, $this->buildLintInlineComments($right));
     }
     $phids = array();
     foreach ($inlines as $inline) {
         $parser->parseInlineComment($inline);
         if ($inline->getAuthorPHID()) {
             $phids[$inline->getAuthorPHID()] = true;
         }
     }
     $phids = array_keys($phids);
     $handles = $this->loadViewerHandles($phids);
     $parser->setHandles($handles);
     $engine = new PhabricatorMarkupEngine();
     $engine->setViewer($request->getUser());
     foreach ($inlines as $inline) {
         $engine->addObject($inline, PhabricatorInlineCommentInterface::MARKUP_FIELD_BODY);
     }
     $engine->process();
     $parser->setMarkupEngine($engine);
     if ($request->isAjax()) {
         // TODO: This is sort of lazy, the effect is just to not render "Edit"
         // and "Reply" links on the "standalone view".
         $parser->setUser($request->getUser());
     }
     $output = $parser->render($range_s, $range_e, $mask);
     $mcov = $parser->renderModifiedCoverage();
     if ($request->isAjax()) {
         $coverage = array('differential-mcoverage-' . md5($changeset->getFilename()) => $mcov);
         return id(new PhabricatorChangesetResponse())->setRenderedChangeset($output)->setCoverage($coverage);
     }
     Javelin::initBehavior('differential-show-more', array('uri' => '/differential/changeset/', 'whitespace' => $request->getStr('whitespace')));
     Javelin::initBehavior('differential-comment-jump', array());
     // TODO: [HTML] Clean up DifferentialChangesetParser output, but it's
     // undergoing like six kinds of refactoring anyway.
     $output = phutil_safe_html($output);
     $detail = new DifferentialChangesetDetailView();
     $detail->setChangeset($changeset);
     $detail->appendChild($output);
     $detail->setVsChangesetID($left_source);
     $panel = new DifferentialPrimaryPaneView();
     $panel->appendChild(phutil_tag('div', array('class' => 'differential-review-stage', 'id' => 'differential-review-stage'), $detail->render()));
     $crumbs = $this->buildApplicationCrumbs();
     $revision_id = $changeset->getDiff()->getRevisionID();
     if ($revision_id) {
         $crumbs->addTextCrumb('D' . $revision_id, '/D' . $revision_id);
     }
     $diff_id = $changeset->getDiff()->getID();
     if ($diff_id) {
         $crumbs->addTextCrumb(pht('Diff %d', $diff_id), $this->getApplicationURI('diff/' . $diff_id));
     }
     $crumbs->addTextCrumb($changeset->getDisplayFilename());
     $box = id(new PHUIObjectBoxView())->setHeaderText(pht('Standalone View'))->appendChild($panel);
     return $this->buildApplicationPage(array($crumbs, $box), array('title' => pht('Changeset View'), 'device' => false));
 }