Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/files/document/PhabricatorTextDocumentEngine.php
12241 views
1
<?php
2
3
abstract 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
$content = mb_convert_encoding($content, 'UTF-8', $encoding);
74
$this->encodingMessage = pht(
75
'This document was converted from %s to UTF8 for display.',
76
$encoding);
77
} else {
78
$this->encodingMessage = pht(
79
'Unable to perform text encoding conversion: mbstring extension '.
80
'is not available.');
81
}
82
} else {
83
if (!phutil_is_utf8($content)) {
84
if (function_exists('mb_detect_encoding')) {
85
$try_encodings = array(
86
'JIS' => pht('JIS'),
87
'EUC-JP' => pht('EUC-JP'),
88
'SJIS' => pht('Shift JIS'),
89
'ISO-8859-1' => pht('ISO-8859-1 (Latin 1)'),
90
);
91
92
$guess = mb_detect_encoding($content, array_keys($try_encodings));
93
if ($guess) {
94
$content = mb_convert_encoding($content, 'UTF-8', $guess);
95
$this->encodingMessage = pht(
96
'This document is not UTF8. It was detected as %s and '.
97
'converted to UTF8 for display.',
98
idx($try_encodings, $guess, $guess));
99
}
100
}
101
}
102
}
103
104
if (!phutil_is_utf8($content)) {
105
$content = phutil_utf8ize($content);
106
$this->encodingMessage = pht(
107
'This document is not UTF8 and its text encoding could not be '.
108
'detected automatically. Use "Change Text Encoding..." to choose '.
109
'an encoding.');
110
}
111
112
return $content;
113
}
114
115
}
116
117