@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3abstract class PhabricatorTextDocumentEngine
4 extends PhabricatorDocumentEngine {
5
6 private $encodingMessage = null;
7
8 protected function canRenderDocumentType(PhabricatorDocumentRef $ref) {
9 return $ref->isProbablyText();
10 }
11
12 public function canConfigureEncoding(PhabricatorDocumentRef $ref) {
13 return true;
14 }
15
16 protected function newTextDocumentContent(
17 PhabricatorDocumentRef $ref,
18 $content,
19 array $options = array()) {
20
21 PhutilTypeSpec::checkMap(
22 $options,
23 array(
24 'blame' => 'optional wild',
25 'coverage' => 'optional list<wild>',
26 ));
27
28 if (is_array($content)) {
29 $lines = $content;
30 } else {
31 $lines = phutil_split_lines($content);
32 }
33
34 $view = id(new PhabricatorSourceCodeView())
35 ->setHighlights($this->getHighlightedLines())
36 ->setLines($lines)
37 ->setSymbolMetadata($ref->getSymbolMetadata());
38
39 $blame = idx($options, 'blame');
40 if ($blame !== null) {
41 $view->setBlameMap($blame);
42 }
43
44 $coverage = idx($options, 'coverage');
45 if ($coverage !== null) {
46 $view->setCoverage($coverage);
47 }
48
49 $message = null;
50 if ($this->encodingMessage !== null) {
51 $message = $this->newMessage($this->encodingMessage);
52 }
53
54 $container = phutil_tag(
55 'div',
56 array(
57 'class' => 'document-engine-text',
58 ),
59 array(
60 $message,
61 $view,
62 ));
63
64 return $container;
65 }
66
67 protected function loadTextData(PhabricatorDocumentRef $ref) {
68 $content = $ref->loadData();
69
70 $encoding = $this->getEncodingConfiguration();
71 if ($encoding !== null) {
72 if (function_exists('mb_convert_encoding')) {
73 try {
74 $content = mb_convert_encoding($content, 'UTF-8', $encoding);
75 $this->encodingMessage = pht(
76 'This document was converted from %s to UTF8 for display.',
77 $encoding);
78 } catch (Throwable $ex) {
79 $this->encodingMessage = pht(
80 'Unable to convert from requested encoding %s to UTF8.',
81 $encoding);
82 }
83 } else {
84 $this->encodingMessage = pht(
85 'Unable to perform text encoding conversion: mbstring extension '.
86 'is not available.');
87 }
88 } else {
89 if (!phutil_is_utf8($content)) {
90 if (function_exists('mb_detect_encoding')) {
91 $try_encodings = array(
92 'JIS' => pht('JIS'),
93 'EUC-JP' => pht('EUC-JP'),
94 'SJIS' => pht('Shift JIS'),
95 'ISO-8859-1' => pht('ISO-8859-1 (Latin 1)'),
96 );
97
98 $guess = mb_detect_encoding($content, array_keys($try_encodings));
99 if ($guess) {
100 $content = mb_convert_encoding($content, 'UTF-8', $guess);
101 $this->encodingMessage = pht(
102 'This document is not UTF8. It was detected as %s and '.
103 'converted to UTF8 for display.',
104 idx($try_encodings, $guess, $guess));
105 }
106 }
107 }
108 }
109
110 if (!phutil_is_utf8($content)) {
111 $content = phutil_utf8ize($content);
112 $this->encodingMessage = pht(
113 'This document is not UTF8 and its text encoding could not be '.
114 'detected automatically. Use "Change Text Encoding..." to choose '.
115 'an encoding.');
116 }
117
118 return $content;
119 }
120
121}