getRequest(); $drequest = $this->getDiffusionRequest(); $path = $drequest->getPath(); $selected = $request->getStr('view'); $needs_blame = ($selected == 'blame' || $selected == 'plainblame'); $file_query = DiffusionFileContentQuery::newFromDiffusionRequest( $this->diffusionRequest); $file_query->setNeedsBlame($needs_blame); $file_query->loadFileContent(); $data = $file_query->getRawData(); if ($selected === 'raw') { return $this->buildRawResponse($path, $data); } // Build the content of the file. $corpus = $this->buildCorpus( $selected, $file_query, $needs_blame, $drequest, $path, $data); require_celerity_resource('diffusion-source-css'); if ($this->corpusType == 'text') { $view_select_panel = $this->renderViewSelectPanel(); } else { $view_select_panel = null; } // Render the page. $content = array(); $content[] = $this->buildCrumbs( array( 'branch' => true, 'path' => true, 'view' => 'browse', )); $content[] = $view_select_panel; $content[] = $corpus; $content[] = $this->buildOpenRevisions(); $nav = $this->buildSideNav('browse', true); $nav->appendChild($content); $basename = basename($this->getDiffusionRequest()->getPath()); return $this->buildStandardPageResponse( $nav, array( 'title' => $basename, )); } private function buildCorpus($selected, DiffusionFileContentQuery $file_query, $needs_blame, DiffusionRequest $drequest, $path, $data) { if (ArcanistDiffUtils::isHeuristicBinaryFile($data)) { $file = $this->loadFileForData($path, $data); $file_uri = $file->getBestURI(); if ($file->isViewableImage()) { $this->corpusType = 'image'; return $this->buildImageCorpus($file_uri); } else { $this->corpusType = 'binary'; return $this->buildBinaryCorpus($file_uri, $data); } } // TODO: blame of blame. switch ($selected) { case 'plain': $style = "margin: 1em 2em; width: 90%; height: 80em; font-family: monospace"; $corpus = phutil_render_tag( 'textarea', array( 'style' => $style, ), phutil_escape_html($file_query->getRawData())); break; case 'plainblame': $style = "margin: 1em 2em; width: 90%; height: 80em; font-family: monospace"; list($text_list, $rev_list, $blame_dict) = $file_query->getBlameData(); $rows = array(); foreach ($text_list as $k => $line) { $rev = $rev_list[$k]; if (isset($blame_dict[$rev]['handle'])) { $author = $blame_dict[$rev]['handle']->getName(); } else { $author = $blame_dict[$rev]['author']; } $rows[] = sprintf("%-10s %-20s %s", substr($rev, 0, 7), $author, $line); } $corpus = phutil_render_tag( 'textarea', array( 'style' => $style, ), phutil_escape_html(implode("\n", $rows))); break; case 'highlighted': case 'blame': default: require_celerity_resource('syntax-highlighting-css'); list($text_list, $rev_list, $blame_dict) = $file_query->getBlameData(); $text_list = implode("\n", $text_list); $text_list = PhabricatorSyntaxHighlighter::highlightWithFilename( $path, $text_list); $text_list = explode("\n", $text_list); $rows = $this->buildDisplayRows($text_list, $rev_list, $blame_dict, $needs_blame, $drequest, $file_query, $selected); $corpus_table = phutil_render_tag( 'table', array( 'class' => "diffusion-source remarkup-code PhabricatorMonospaced", ), implode("\n", $rows)); $corpus = phutil_render_tag( 'div', array( 'style' => 'padding: 0pt 2em;', ), $corpus_table); break; } return $corpus; } private function renderViewSelectPanel() { $request = $this->getRequest(); $select = AphrontFormSelectControl::renderSelectTag( $request->getStr('view'), array( 'highlighted' => 'View as Highlighted Text', 'blame' => 'View as Highlighted Text with Blame', 'plain' => 'View as Plain Text', 'plainblame' => 'View as Plain Text with Blame', 'raw' => 'View as raw document', ), array( 'name' => 'view', )); $view_select_panel = new AphrontPanelView(); $view_select_form = phutil_render_tag( 'form', array( 'action' => $request->getRequestURI(), 'method' => 'get', 'class' => 'diffusion-browse-type-form', ), $select. ' '. $this->renderEditButton()); $view_select_panel->appendChild($view_select_form); $view_select_panel->appendChild('
'); return $view_select_panel; } private function renderEditButton() { $request = $this->getRequest(); $user = $request->getUser(); $drequest = $this->getDiffusionRequest(); $repository = $drequest->getRepository(); $path = $drequest->getPath(); $line = 1; $callsign = $repository->getCallsign(); $editor_link = $user->loadEditorLink($path, $line, $callsign); if (!$editor_link) { return null; } return phutil_render_tag( 'a', array( 'href' => $editor_link, 'class' => 'button', ), 'Edit'); } private function buildDisplayRows( array $text_list, array $rev_list, array $blame_dict, $needs_blame, DiffusionRequest $drequest, DiffusionFileContentQuery $file_query, $selected) { $last_rev = null; $color = '#eeeeee'; $rows = array(); $n = 1; $view = $this->getRequest()->getStr('view'); if ($blame_dict) { $epoch_list = ipull($blame_dict, 'epoch'); $epoch_max = max($epoch_list); $epoch_min = min($epoch_list); $epoch_range = $epoch_max - $epoch_min + 1; } $targ = ''; $min_line = 0; $line = $drequest->getLine(); if (strpos($line, '-') !== false) { list($min, $max) = explode('-', $line, 2); $min_line = min($min, $max); $max_line = max($min, $max); } else if (strlen($line)) { $min_line = $line; $max_line = $line; } foreach ($text_list as $k => $line) { if ($needs_blame) { // If the line's rev is same as the line above, show empty content // with same color; otherwise generate blame info. The newer a change // is, the darker the color. $rev = $rev_list[$k]; if ($last_rev == $rev) { $blame_info = ($file_query->getSupportsBlameOnBlame() ? ''. 'This is a binary file. '. 'It is '.number_format(strlen($data)).' bytes in length.'. '
'); $panel->addButton( phutil_render_tag( 'a', array( 'href' => $file_uri, 'class' => 'button green', ), 'Download Binary File...')); return $panel; } }