Remove product literal strings in "pht()", part 5
[phabricator.git] / src / applications / files / document / PhabricatorHexdumpDocumentEngine.php
blob4217c24d62f9e7268daeabda05a398c9b4332468
1 <?php
3 final class PhabricatorHexdumpDocumentEngine
4 extends PhabricatorDocumentEngine {
6 const ENGINEKEY = 'hexdump';
8 public function getViewAsLabel(PhabricatorDocumentRef $ref) {
9 return pht('View as Hexdump');
12 protected function getDocumentIconIcon(PhabricatorDocumentRef $ref) {
13 return 'fa-microchip';
16 protected function getByteLengthLimit() {
17 return (1024 * 1024 * 1);
20 protected function getContentScore(PhabricatorDocumentRef $ref) {
21 return 500;
24 protected function canRenderDocumentType(PhabricatorDocumentRef $ref) {
25 return true;
28 protected function canRenderPartialDocument(PhabricatorDocumentRef $ref) {
29 return true;
32 protected function newDocumentContent(PhabricatorDocumentRef $ref) {
33 $limit = $this->getByteLengthLimit();
34 $length = $ref->getByteLength();
36 $is_partial = false;
37 if ($limit) {
38 if ($length > $limit) {
39 $is_partial = true;
40 $length = $limit;
44 $content = $ref->loadData(null, $length);
46 $output = array();
47 $offset = 0;
49 $lines = str_split($content, 16);
50 foreach ($lines as $line) {
51 $output[] = sprintf(
52 '%08x %- 23s %- 23s %- 16s',
53 $offset,
54 $this->renderHex(substr($line, 0, 8)),
55 $this->renderHex(substr($line, 8)),
56 $this->renderBytes($line));
58 $offset += 16;
61 $output = implode("\n", $output);
63 $container = phutil_tag(
64 'div',
65 array(
66 'class' => 'document-engine-hexdump PhabricatorMonospaced',
68 $output);
70 $message = null;
71 if ($is_partial) {
72 $message = $this->newMessage(
73 pht(
74 'This document is too large to be completely rendered inline. The '.
75 'first %s bytes are shown.',
76 new PhutilNumber($limit)));
79 return array(
80 $message,
81 $container,
85 private function renderHex($bytes) {
86 $length = strlen($bytes);
88 $output = array();
89 for ($ii = 0; $ii < $length; $ii++) {
90 $output[] = sprintf('%02x', ord($bytes[$ii]));
93 return implode(' ', $output);
96 private function renderBytes($bytes) {
97 $length = strlen($bytes);
99 $output = array();
100 for ($ii = 0; $ii < $length; $ii++) {
101 $chr = $bytes[$ii];
102 $ord = ord($chr);
104 if ($ord < 0x20 || $ord >= 0x7F) {
105 $chr = '.';
108 $output[] = $chr;
111 return implode('', $output);