3 abstract class PhabricatorDocumentEngine
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;
17 final public function getViewer() {
21 final public function setHighlightedLines(array $highlighted_lines) {
22 $this->highlightedLines
= $highlighted_lines;
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) {
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)
52 ->addOldClass('old-full')
53 ->setNewContent($v_content)
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) {
74 public function canConfigureHighlighting(PhabricatorDocumentRef
$ref) {
78 public function canBlame(PhabricatorDocumentRef
$ref) {
82 final public function setEncodingConfiguration($config) {
83 $this->encodingConfiguration
= $config;
87 final public function getEncodingConfiguration() {
88 return $this->encodingConfiguration
;
91 final public function setHighlightingConfiguration($config) {
92 $this->highlightingConfiguration
= $config;
96 final public function getHighlightingConfiguration() {
97 return $this->highlightingConfiguration
;
100 final public function setBlameConfiguration($blame_configuration) {
101 $this->blameConfiguration
= $blame_configuration;
105 final public function getBlameConfiguration() {
106 return $this->blameConfiguration
;
109 final protected function getBlameEnabled() {
110 return $this->blameConfiguration
;
113 public function shouldRenderAsync(PhabricatorDocumentRef
$ref) {
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(
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) {
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')
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)) {
171 } else if ($this->canRenderPartialDocument($ref)) {
177 return id(new PhutilSortVector())
178 ->addInt($limit_score)
179 ->addInt(-$content_score);
182 protected function getContentScore(PhabricatorDocumentRef
$ref) {
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) {
196 return $this->getDocumentIconIcon($ref);
199 public function getViewAsIconColor(PhabricatorDocumentRef
$ref) {
200 $can_complete = $this->canRenderCompleteDocument($ref);
202 if (!$can_complete) {
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]);
223 $engines[$key] = $engine;
227 throw new Exception(pht('No content engine can render this document.'));
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();
246 $length = $ref->getByteLength();
247 if ($length > $limit) {
255 protected function canRenderPartialDocument(PhabricatorDocumentRef
$ref) {
259 protected function newMessage($message) {
263 'class' => 'document-engine-error',
268 final public function newLoadingContent(PhabricatorDocumentRef
$ref) {
269 $spinner = id(new PHUIIconView())
271 ->addClass('ph-spin');
276 'class' => 'document-engine-loading',
280 $this->getDocumentRenderingText($ref),
284 public function shouldSuggestEngine(PhabricatorDocumentRef
$ref) {