Remove product literal strings in "pht()", part 5
[phabricator.git] / src / applications / files / document / PhabricatorDocumentEngine.php
blob3a13c5eb4ab64adebf9feccd60099fbad614eb9c
1 <?php
3 abstract class PhabricatorDocumentEngine
4 extends Phobject {
6 private $viewer;
7 private $highlightedLines = array();
8 private $encodingConfiguration;
9 private $highlightingConfiguration;
10 private $blameConfiguration = true;
12 final public function setViewer(PhabricatorUser $viewer) {
13 $this->viewer = $viewer;
14 return $this;
17 final public function getViewer() {
18 return $this->viewer;
21 final public function setHighlightedLines(array $highlighted_lines) {
22 $this->highlightedLines = $highlighted_lines;
23 return $this;
26 final public function getHighlightedLines() {
27 return $this->highlightedLines;
30 final public function canRenderDocument(PhabricatorDocumentRef $ref) {
31 return $this->canRenderDocumentType($ref);
34 public function canDiffDocuments(
35 PhabricatorDocumentRef $uref = null,
36 PhabricatorDocumentRef $vref = null) {
37 return false;
40 public function newBlockDiffViews(
41 PhabricatorDocumentRef $uref,
42 PhabricatorDocumentEngineBlock $ublock,
43 PhabricatorDocumentRef $vref,
44 PhabricatorDocumentEngineBlock $vblock) {
46 $u_content = $this->newBlockContentView($uref, $ublock);
47 $v_content = $this->newBlockContentView($vref, $vblock);
49 return id(new PhabricatorDocumentEngineBlockDiff())
50 ->setOldContent($u_content)
51 ->addOldClass('old')
52 ->addOldClass('old-full')
53 ->setNewContent($v_content)
54 ->addNewClass('new')
55 ->addNewClass('new-full');
58 public function newBlockContentView(
59 PhabricatorDocumentRef $ref,
60 PhabricatorDocumentEngineBlock $block) {
61 return $block->getContent();
64 public function newEngineBlocks(
65 PhabricatorDocumentRef $uref,
66 PhabricatorDocumentRef $vref) {
67 throw new PhutilMethodNotImplementedException();
70 public function canConfigureEncoding(PhabricatorDocumentRef $ref) {
71 return false;
74 public function canConfigureHighlighting(PhabricatorDocumentRef $ref) {
75 return false;
78 public function canBlame(PhabricatorDocumentRef $ref) {
79 return false;
82 final public function setEncodingConfiguration($config) {
83 $this->encodingConfiguration = $config;
84 return $this;
87 final public function getEncodingConfiguration() {
88 return $this->encodingConfiguration;
91 final public function setHighlightingConfiguration($config) {
92 $this->highlightingConfiguration = $config;
93 return $this;
96 final public function getHighlightingConfiguration() {
97 return $this->highlightingConfiguration;
100 final public function setBlameConfiguration($blame_configuration) {
101 $this->blameConfiguration = $blame_configuration;
102 return $this;
105 final public function getBlameConfiguration() {
106 return $this->blameConfiguration;
109 final protected function getBlameEnabled() {
110 return $this->blameConfiguration;
113 public function shouldRenderAsync(PhabricatorDocumentRef $ref) {
114 return false;
117 abstract protected function canRenderDocumentType(
118 PhabricatorDocumentRef $ref);
120 final public function newDocument(PhabricatorDocumentRef $ref) {
121 $can_complete = $this->canRenderCompleteDocument($ref);
122 $can_partial = $this->canRenderPartialDocument($ref);
124 if (!$can_complete && !$can_partial) {
125 return $this->newMessage(
126 pht(
127 'This document is too large to be rendered inline. (The document '.
128 'is %s bytes, the limit for this engine is %s bytes.)',
129 new PhutilNumber($ref->getByteLength()),
130 new PhutilNumber($this->getByteLengthLimit())));
133 return $this->newDocumentContent($ref);
136 final public function newDocumentIcon(PhabricatorDocumentRef $ref) {
137 return id(new PHUIIconView())
138 ->setIcon($this->getDocumentIconIcon($ref));
141 abstract protected function newDocumentContent(
142 PhabricatorDocumentRef $ref);
144 protected function getDocumentIconIcon(PhabricatorDocumentRef $ref) {
145 return 'fa-file-o';
148 protected function getDocumentRenderingText(PhabricatorDocumentRef $ref) {
149 return pht('Loading...');
152 final public function getDocumentEngineKey() {
153 return $this->getPhobjectClassConstant('ENGINEKEY');
156 final public static function getAllEngines() {
157 return id(new PhutilClassMapQuery())
158 ->setAncestorClass(__CLASS__)
159 ->setUniqueMethod('getDocumentEngineKey')
160 ->execute();
163 final public function newSortVector(PhabricatorDocumentRef $ref) {
164 $content_score = $this->getContentScore($ref);
166 // Prefer engines which can render the entire file over engines which
167 // can only render a header, and engines which can render a header over
168 // engines which can't render anything.
169 if ($this->canRenderCompleteDocument($ref)) {
170 $limit_score = 0;
171 } else if ($this->canRenderPartialDocument($ref)) {
172 $limit_score = 1;
173 } else {
174 $limit_score = 2;
177 return id(new PhutilSortVector())
178 ->addInt($limit_score)
179 ->addInt(-$content_score);
182 protected function getContentScore(PhabricatorDocumentRef $ref) {
183 return 2000;
186 abstract public function getViewAsLabel(PhabricatorDocumentRef $ref);
188 public function getViewAsIconIcon(PhabricatorDocumentRef $ref) {
189 $can_complete = $this->canRenderCompleteDocument($ref);
190 $can_partial = $this->canRenderPartialDocument($ref);
192 if (!$can_complete && !$can_partial) {
193 return 'fa-times';
196 return $this->getDocumentIconIcon($ref);
199 public function getViewAsIconColor(PhabricatorDocumentRef $ref) {
200 $can_complete = $this->canRenderCompleteDocument($ref);
202 if (!$can_complete) {
203 return 'grey';
206 return null;
209 final public static function getEnginesForRef(
210 PhabricatorUser $viewer,
211 PhabricatorDocumentRef $ref) {
212 $engines = self::getAllEngines();
214 foreach ($engines as $key => $engine) {
215 $engine = id(clone $engine)
216 ->setViewer($viewer);
218 if (!$engine->canRenderDocument($ref)) {
219 unset($engines[$key]);
220 continue;
223 $engines[$key] = $engine;
226 if (!$engines) {
227 throw new Exception(pht('No content engine can render this document.'));
230 $vectors = array();
231 foreach ($engines as $key => $usable_engine) {
232 $vectors[$key] = $usable_engine->newSortVector($ref);
234 $vectors = msortv($vectors, 'getSelf');
236 return array_select_keys($engines, array_keys($vectors));
239 protected function getByteLengthLimit() {
240 return (1024 * 1024 * 8);
243 protected function canRenderCompleteDocument(PhabricatorDocumentRef $ref) {
244 $limit = $this->getByteLengthLimit();
245 if ($limit) {
246 $length = $ref->getByteLength();
247 if ($length > $limit) {
248 return false;
252 return true;
255 protected function canRenderPartialDocument(PhabricatorDocumentRef $ref) {
256 return false;
259 protected function newMessage($message) {
260 return phutil_tag(
261 'div',
262 array(
263 'class' => 'document-engine-error',
265 $message);
268 final public function newLoadingContent(PhabricatorDocumentRef $ref) {
269 $spinner = id(new PHUIIconView())
270 ->setIcon('fa-gear')
271 ->addClass('ph-spin');
273 return phutil_tag(
274 'div',
275 array(
276 'class' => 'document-engine-loading',
278 array(
279 $spinner,
280 $this->getDocumentRenderingText($ref),
284 public function shouldSuggestEngine(PhabricatorDocumentRef $ref) {
285 return false;