Changeset View
Changeset View
Standalone View
Standalone View
src/applications/files/document/PhabricatorDocumentEngine.php
Show All 16 Lines | abstract class PhabricatorDocumentEngine | ||||
final public function canRenderDocument(PhabricatorDocumentRef $ref) { | final public function canRenderDocument(PhabricatorDocumentRef $ref) { | ||||
return $this->canRenderDocumentType($ref); | return $this->canRenderDocumentType($ref); | ||||
} | } | ||||
abstract protected function canRenderDocumentType( | abstract protected function canRenderDocumentType( | ||||
PhabricatorDocumentRef $ref); | PhabricatorDocumentRef $ref); | ||||
final public function newDocument(PhabricatorDocumentRef $ref) { | final public function newDocument(PhabricatorDocumentRef $ref) { | ||||
$can_complete = $this->canRenderCompleteDocument($ref); | |||||
$can_partial = $this->canRenderPartialDocument($ref); | |||||
if (!$can_complete && !$can_partial) { | |||||
return $this->newMessage( | |||||
pht( | |||||
'This document is too large to be rendered inline. (The document '. | |||||
'is %s bytes, the limit for this engine is %s bytes.)', | |||||
new PhutilNumber($ref->getByteLength()), | |||||
new PhutilNumber($this->getByteLengthLimit()))); | |||||
} | |||||
return $this->newDocumentContent($ref); | return $this->newDocumentContent($ref); | ||||
} | } | ||||
final public function newDocumentIcon(PhabricatorDocumentRef $ref) { | final public function newDocumentIcon(PhabricatorDocumentRef $ref) { | ||||
return id(new PHUIIconView()) | return id(new PHUIIconView()) | ||||
->setIcon($this->getDocumentIconIcon($ref)); | ->setIcon($this->getDocumentIconIcon($ref)); | ||||
} | } | ||||
Show All 13 Lines | return id(new PhutilClassMapQuery()) | ||||
->setAncestorClass(__CLASS__) | ->setAncestorClass(__CLASS__) | ||||
->setUniqueMethod('getDocumentEngineKey') | ->setUniqueMethod('getDocumentEngineKey') | ||||
->execute(); | ->execute(); | ||||
} | } | ||||
final public function newSortVector(PhabricatorDocumentRef $ref) { | final public function newSortVector(PhabricatorDocumentRef $ref) { | ||||
$content_score = $this->getContentScore($ref); | $content_score = $this->getContentScore($ref); | ||||
// Prefer engines which can render the entire file over engines which | |||||
// can only render a header, and engines which can render a header over | |||||
// engines which can't render anything. | |||||
if ($this->canRenderCompleteDocument($ref)) { | |||||
$limit_score = 0; | |||||
} else if ($this->canRenderPartialDocument($ref)) { | |||||
$limit_score = 1; | |||||
} else { | |||||
$limit_score = 2; | |||||
} | |||||
return id(new PhutilSortVector()) | return id(new PhutilSortVector()) | ||||
->addInt($limit_score) | |||||
->addInt(-$content_score); | ->addInt(-$content_score); | ||||
} | } | ||||
protected function getContentScore() { | protected function getContentScore(PhabricatorDocumentRef $ref) { | ||||
return 2000; | return 2000; | ||||
} | } | ||||
abstract public function getViewAsLabel(PhabricatorDocumentRef $ref); | abstract public function getViewAsLabel(PhabricatorDocumentRef $ref); | ||||
public function getViewAsIconIcon(PhabricatorDocumentRef $ref) { | public function getViewAsIconIcon(PhabricatorDocumentRef $ref) { | ||||
$can_complete = $this->canRenderCompleteDocument($ref); | |||||
$can_partial = $this->canRenderPartialDocument($ref); | |||||
if (!$can_complete && !$can_partial) { | |||||
return 'fa-times'; | |||||
} | |||||
return $this->getDocumentIconIcon($ref); | return $this->getDocumentIconIcon($ref); | ||||
} | } | ||||
public function getViewAsIconColor(PhabricatorDocumentRef $ref) { | |||||
$can_complete = $this->canRenderCompleteDocument($ref); | |||||
if (!$can_complete) { | |||||
return 'grey'; | |||||
} | |||||
return null; | |||||
} | |||||
public function getRenderURI(PhabricatorDocumentRef $ref) { | public function getRenderURI(PhabricatorDocumentRef $ref) { | ||||
$file = $ref->getFile(); | $file = $ref->getFile(); | ||||
if (!$file) { | if (!$file) { | ||||
throw new PhutilMethodNotImplementedException(); | throw new PhutilMethodNotImplementedException(); | ||||
} | } | ||||
$engine_key = $this->getDocumentEngineKey(); | $engine_key = $this->getDocumentEngineKey(); | ||||
$file_phid = $file->getPHID(); | $file_phid = $file->getPHID(); | ||||
Show All 26 Lines | final public static function getEnginesForRef( | ||||
foreach ($engines as $key => $usable_engine) { | foreach ($engines as $key => $usable_engine) { | ||||
$vectors[$key] = $usable_engine->newSortVector($ref); | $vectors[$key] = $usable_engine->newSortVector($ref); | ||||
} | } | ||||
$vectors = msortv($vectors, 'getSelf'); | $vectors = msortv($vectors, 'getSelf'); | ||||
return array_select_keys($engines, array_keys($vectors)); | return array_select_keys($engines, array_keys($vectors)); | ||||
} | } | ||||
protected function getByteLengthLimit() { | |||||
return (1024 * 1024 * 8); | |||||
} | |||||
protected function canRenderCompleteDocument(PhabricatorDocumentRef $ref) { | |||||
$limit = $this->getByteLengthLimit(); | |||||
if ($limit) { | |||||
$length = $ref->getByteLength(); | |||||
if ($length > $limit) { | |||||
return false; | |||||
} | |||||
} | |||||
return true; | |||||
} | |||||
protected function canRenderPartialDocument(PhabricatorDocumentRef $ref) { | |||||
return false; | |||||
} | |||||
protected function newMessage($message) { | |||||
return phutil_tag( | |||||
'div', | |||||
array( | |||||
'class' => 'document-engine-error', | |||||
), | |||||
$message); | |||||
} | |||||
} | } |