2011-03-08 18:54:55 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 Facebook, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class DiffusionBrowseFileController extends DiffusionController {
|
|
|
|
|
|
|
|
public function processRequest() {
|
2011-03-13 01:17:34 +01:00
|
|
|
|
2011-03-23 03:34:47 +01:00
|
|
|
// Build the view selection form.
|
2011-03-22 07:57:32 +01:00
|
|
|
$select_map = array(
|
2011-03-23 03:34:47 +01:00
|
|
|
'highlighted' => 'View as Highlighted Text',
|
2011-03-26 20:42:12 +01:00
|
|
|
'blame' => 'View as Highlighted Text with Blame',
|
2011-03-22 07:57:32 +01:00
|
|
|
'plain' => 'View as Plain Text',
|
2011-03-26 20:42:12 +01:00
|
|
|
'plainblame' => 'View as Plain Text with Blame',
|
2011-03-22 07:57:32 +01:00
|
|
|
);
|
2011-03-23 03:34:47 +01:00
|
|
|
|
|
|
|
$request = $this->getRequest();
|
|
|
|
|
|
|
|
$selected = $request->getStr('view');
|
2011-03-22 07:57:32 +01:00
|
|
|
$select = '<select name="view">';
|
|
|
|
foreach ($select_map as $k => $v) {
|
|
|
|
$option = phutil_render_tag(
|
|
|
|
'option',
|
|
|
|
array(
|
|
|
|
'value' => $k,
|
|
|
|
'selected' => ($k == $selected) ? 'selected' : null,
|
|
|
|
),
|
|
|
|
phutil_escape_html($v));
|
|
|
|
|
|
|
|
$select .= $option;
|
|
|
|
}
|
|
|
|
$select .= '</select>';
|
|
|
|
|
2011-03-23 03:34:47 +01:00
|
|
|
$view_select_panel = new AphrontPanelView();
|
|
|
|
$view_select_form = phutil_render_tag(
|
2011-03-22 07:57:32 +01:00
|
|
|
'form',
|
|
|
|
array(
|
2011-03-23 03:34:47 +01:00
|
|
|
'action' => $request->getRequestURI(),
|
2011-03-22 07:57:32 +01:00
|
|
|
'method' => 'get',
|
|
|
|
'style' => 'display: inline',
|
|
|
|
),
|
|
|
|
$select.
|
|
|
|
'<button>view</button>');
|
2011-03-23 03:34:47 +01:00
|
|
|
$view_select_panel->appendChild($view_select_form);
|
2011-03-22 07:57:32 +01:00
|
|
|
|
2011-03-26 20:42:12 +01:00
|
|
|
// Build the content of the file.
|
|
|
|
$corpus = $this->buildCorpus($selected);
|
|
|
|
|
|
|
|
// Render the page.
|
|
|
|
$content = array();
|
|
|
|
$content[] = $this->buildCrumbs(
|
|
|
|
array(
|
|
|
|
'branch' => true,
|
|
|
|
'path' => true,
|
|
|
|
'view' => 'browse',
|
|
|
|
));
|
|
|
|
$content[] = $view_select_panel;
|
|
|
|
$content[] = $corpus;
|
|
|
|
|
|
|
|
$nav = $this->buildSideNav('browse', true);
|
|
|
|
$nav->appendChild($content);
|
|
|
|
|
|
|
|
return $this->buildStandardPageResponse(
|
|
|
|
$nav,
|
|
|
|
array(
|
|
|
|
'title' => 'Browse',
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function buildCorpus($selected) {
|
2011-04-01 03:28:24 +02:00
|
|
|
$needs_blame = ($selected == 'blame' || $selected == 'plainblame');
|
2011-03-26 20:42:12 +01:00
|
|
|
|
2011-03-08 23:29:02 +01:00
|
|
|
$file_query = DiffusionFileContentQuery::newFromDiffusionRequest(
|
|
|
|
$this->diffusionRequest);
|
2011-04-01 03:28:24 +02:00
|
|
|
$file_query->setNeedsBlame($needs_blame);
|
|
|
|
$file_query->loadFileContent();
|
2011-03-08 18:54:55 +01:00
|
|
|
|
2011-03-23 03:34:47 +01:00
|
|
|
// TODO: image
|
2011-03-26 20:42:12 +01:00
|
|
|
// TODO: blame of blame.
|
2011-03-23 03:34:47 +01:00
|
|
|
switch ($selected) {
|
|
|
|
case 'plain':
|
|
|
|
$style =
|
|
|
|
"margin: 1em 2em; width: 90%; height: 80em; font-family: monospace";
|
|
|
|
$corpus = phutil_render_tag(
|
|
|
|
'textarea',
|
|
|
|
array(
|
|
|
|
'style' => $style,
|
|
|
|
),
|
2011-03-26 20:42:12 +01:00
|
|
|
phutil_escape_html($file_query->getRawData()));
|
2011-04-01 03:28:24 +02:00
|
|
|
|
2011-03-23 03:34:47 +01:00
|
|
|
break;
|
|
|
|
|
2011-04-01 03:28:24 +02:00
|
|
|
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];
|
|
|
|
$author = $blame_dict[$rev]['author'];
|
|
|
|
$rows[] =
|
|
|
|
sprintf("%-10s %-15s %s", substr($rev, 0, 7), $author, $line);
|
|
|
|
}
|
|
|
|
|
|
|
|
$corpus = phutil_render_tag(
|
|
|
|
'textarea',
|
|
|
|
array(
|
|
|
|
'style' => $style,
|
|
|
|
),
|
|
|
|
phutil_escape_html(implode("\n", $rows)));
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2011-03-23 03:34:47 +01:00
|
|
|
case 'highlighted':
|
2011-03-26 20:42:12 +01:00
|
|
|
case 'blame':
|
2011-03-23 03:34:47 +01:00
|
|
|
default:
|
|
|
|
require_celerity_resource('syntax-highlighting-css');
|
|
|
|
require_celerity_resource('diffusion-source-css');
|
2011-03-08 18:54:55 +01:00
|
|
|
|
2011-04-01 03:28:24 +02:00
|
|
|
list($text_list, $rev_list, $blame_dict) = $file_query->getBlameData();
|
2011-03-26 20:42:12 +01:00
|
|
|
|
|
|
|
$drequest = $this->getDiffusionRequest();
|
2011-03-23 03:34:47 +01:00
|
|
|
$path = $drequest->getPath();
|
|
|
|
$highlightEngine = new PhutilDefaultSyntaxHighlighterEngine();
|
|
|
|
|
2011-04-01 03:28:24 +02:00
|
|
|
$text_list = explode("\n", $highlightEngine->highlightSource($path,
|
|
|
|
implode("\n", $text_list)));
|
2011-03-23 03:34:47 +01:00
|
|
|
|
2011-04-01 03:28:24 +02:00
|
|
|
$rows = $this->buildDisplayRows($text_list, $rev_list, $blame_dict,
|
|
|
|
$needs_blame, $drequest);
|
2011-03-23 03:34:47 +01:00
|
|
|
|
|
|
|
$corpus_table = phutil_render_tag(
|
|
|
|
'table',
|
|
|
|
array(
|
2011-03-31 04:21:09 +02:00
|
|
|
'class' => "diffusion-source remarkup-code PhabricatorMonospaced",
|
2011-03-23 03:34:47 +01:00
|
|
|
),
|
|
|
|
implode("\n", $rows));
|
|
|
|
$corpus = phutil_render_tag(
|
|
|
|
'div',
|
|
|
|
array(
|
|
|
|
'style' => 'padding: 0pt 2em;',
|
|
|
|
),
|
|
|
|
$corpus_table);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-03-26 20:42:12 +01:00
|
|
|
return $corpus;
|
|
|
|
}
|
2011-03-13 01:17:34 +01:00
|
|
|
|
|
|
|
|
2011-04-01 03:28:24 +02:00
|
|
|
private static function buildDisplayRows($text_list, $rev_list, $blame_dict,
|
|
|
|
$needs_blame, DiffusionRequest $drequest) {
|
|
|
|
$last_rev = null;
|
2011-03-26 20:42:12 +01:00
|
|
|
$color = null;
|
|
|
|
$rows = array();
|
|
|
|
$n = 1;
|
2011-04-01 03:28:24 +02:00
|
|
|
|
|
|
|
$epoch_list = ipull($blame_dict, 'epoch');
|
|
|
|
$max = max($epoch_list);
|
|
|
|
$min = min($epoch_list);
|
|
|
|
$range = $max - $min + 1;
|
|
|
|
|
|
|
|
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 =
|
2011-03-26 20:42:12 +01:00
|
|
|
'<th style="background: '.$color.'; width: 9em;"></th>'.
|
|
|
|
'<th style="background: '.$color.'"></th>';
|
|
|
|
} else {
|
2011-04-01 03:28:24 +02:00
|
|
|
|
|
|
|
$color_number = (int)(0xEE -
|
|
|
|
0xEE * ($blame_dict[$rev]['epoch'] - $min) / $range);
|
|
|
|
$color = sprintf('#%02xee%02x', $color_number, $color_number);
|
|
|
|
|
2011-03-26 20:42:12 +01:00
|
|
|
$revision_link = self::renderRevision(
|
2011-04-01 03:28:24 +02:00
|
|
|
$drequest,
|
|
|
|
substr($rev, 0, 7));
|
2011-03-26 20:42:12 +01:00
|
|
|
|
2011-04-01 03:28:24 +02:00
|
|
|
$author_link = $blame_dict[$rev]['author'];
|
|
|
|
$blame_info =
|
2011-03-26 20:42:12 +01:00
|
|
|
'<th style="background: '.$color.
|
|
|
|
'; width: 9em;">'.$revision_link.'</th>'.
|
|
|
|
'<th style="background: '.$color.
|
|
|
|
'; font-weight: normal; color: #333;">'.$author_link.'</th>';
|
2011-04-01 03:28:24 +02:00
|
|
|
$last_rev = $rev;
|
2011-03-26 20:42:12 +01:00
|
|
|
}
|
|
|
|
} else {
|
2011-04-01 03:28:24 +02:00
|
|
|
$blame_info = null;
|
2011-03-26 20:42:12 +01:00
|
|
|
}
|
|
|
|
|
2011-04-01 03:28:24 +02:00
|
|
|
// Highlight the line of interest if needed.
|
2011-03-26 20:42:12 +01:00
|
|
|
if ($n == $drequest->getLine()) {
|
|
|
|
$tr = '<tr style="background: #ffff00;">';
|
|
|
|
$targ = '<a id="scroll_target"></a>';
|
|
|
|
Javelin::initBehavior('diffusion-jump-to',
|
|
|
|
array('target' => 'scroll_target'));
|
|
|
|
} else {
|
|
|
|
$tr = '<tr>';
|
|
|
|
$targ = null;
|
|
|
|
}
|
|
|
|
|
2011-04-01 03:28:24 +02:00
|
|
|
// Create the row display.
|
2011-03-26 20:42:12 +01:00
|
|
|
$uri_path = $drequest->getUriPath();
|
|
|
|
$uri_rev = $drequest->getCommit();
|
|
|
|
|
|
|
|
$l = phutil_render_tag(
|
|
|
|
'a',
|
|
|
|
array(
|
|
|
|
'href' => $uri_path.';'.$uri_rev.'$'.$n,
|
|
|
|
),
|
|
|
|
$n);
|
|
|
|
|
2011-04-01 03:28:24 +02:00
|
|
|
$rows[] = $tr.$blame_info.'<th>'.$l.'</th><td>'.$targ.$line.'</td></tr>';
|
2011-03-26 20:42:12 +01:00
|
|
|
++$n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rows;
|
2011-03-08 18:54:55 +01:00
|
|
|
}
|
2011-04-01 03:28:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
private static function renderRevision(DiffusionRequest $drequest,
|
|
|
|
$revision) {
|
|
|
|
|
|
|
|
$callsign = $drequest->getCallsign();
|
|
|
|
|
|
|
|
$name = 'r'.$callsign.$revision;
|
|
|
|
return phutil_render_tag(
|
|
|
|
'a',
|
|
|
|
array(
|
|
|
|
'href' => '/'.$name,
|
|
|
|
),
|
|
|
|
$name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-03-08 18:54:55 +01:00
|
|
|
}
|