Path: blob/master/src/applications/files/document/PhabricatorDocumentEngine.php
12241 views
<?php12abstract class PhabricatorDocumentEngine3extends Phobject {45private $viewer;6private $highlightedLines = array();7private $encodingConfiguration;8private $highlightingConfiguration;9private $blameConfiguration = true;1011final public function setViewer(PhabricatorUser $viewer) {12$this->viewer = $viewer;13return $this;14}1516final public function getViewer() {17return $this->viewer;18}1920final public function setHighlightedLines(array $highlighted_lines) {21$this->highlightedLines = $highlighted_lines;22return $this;23}2425final public function getHighlightedLines() {26return $this->highlightedLines;27}2829final public function canRenderDocument(PhabricatorDocumentRef $ref) {30return $this->canRenderDocumentType($ref);31}3233public function canDiffDocuments(34PhabricatorDocumentRef $uref = null,35PhabricatorDocumentRef $vref = null) {36return false;37}3839public function newBlockDiffViews(40PhabricatorDocumentRef $uref,41PhabricatorDocumentEngineBlock $ublock,42PhabricatorDocumentRef $vref,43PhabricatorDocumentEngineBlock $vblock) {4445$u_content = $this->newBlockContentView($uref, $ublock);46$v_content = $this->newBlockContentView($vref, $vblock);4748return id(new PhabricatorDocumentEngineBlockDiff())49->setOldContent($u_content)50->addOldClass('old')51->addOldClass('old-full')52->setNewContent($v_content)53->addNewClass('new')54->addNewClass('new-full');55}5657public function newBlockContentView(58PhabricatorDocumentRef $ref,59PhabricatorDocumentEngineBlock $block) {60return $block->getContent();61}6263public function newEngineBlocks(64PhabricatorDocumentRef $uref,65PhabricatorDocumentRef $vref) {66throw new PhutilMethodNotImplementedException();67}6869public function canConfigureEncoding(PhabricatorDocumentRef $ref) {70return false;71}7273public function canConfigureHighlighting(PhabricatorDocumentRef $ref) {74return false;75}7677public function canBlame(PhabricatorDocumentRef $ref) {78return false;79}8081final public function setEncodingConfiguration($config) {82$this->encodingConfiguration = $config;83return $this;84}8586final public function getEncodingConfiguration() {87return $this->encodingConfiguration;88}8990final public function setHighlightingConfiguration($config) {91$this->highlightingConfiguration = $config;92return $this;93}9495final public function getHighlightingConfiguration() {96return $this->highlightingConfiguration;97}9899final public function setBlameConfiguration($blame_configuration) {100$this->blameConfiguration = $blame_configuration;101return $this;102}103104final public function getBlameConfiguration() {105return $this->blameConfiguration;106}107108final protected function getBlameEnabled() {109return $this->blameConfiguration;110}111112public function shouldRenderAsync(PhabricatorDocumentRef $ref) {113return false;114}115116abstract protected function canRenderDocumentType(117PhabricatorDocumentRef $ref);118119final public function newDocument(PhabricatorDocumentRef $ref) {120$can_complete = $this->canRenderCompleteDocument($ref);121$can_partial = $this->canRenderPartialDocument($ref);122123if (!$can_complete && !$can_partial) {124return $this->newMessage(125pht(126'This document is too large to be rendered inline. (The document '.127'is %s bytes, the limit for this engine is %s bytes.)',128new PhutilNumber($ref->getByteLength()),129new PhutilNumber($this->getByteLengthLimit())));130}131132return $this->newDocumentContent($ref);133}134135final public function newDocumentIcon(PhabricatorDocumentRef $ref) {136return id(new PHUIIconView())137->setIcon($this->getDocumentIconIcon($ref));138}139140abstract protected function newDocumentContent(141PhabricatorDocumentRef $ref);142143protected function getDocumentIconIcon(PhabricatorDocumentRef $ref) {144return 'fa-file-o';145}146147protected function getDocumentRenderingText(PhabricatorDocumentRef $ref) {148return pht('Loading...');149}150151final public function getDocumentEngineKey() {152return $this->getPhobjectClassConstant('ENGINEKEY');153}154155final public static function getAllEngines() {156return id(new PhutilClassMapQuery())157->setAncestorClass(__CLASS__)158->setUniqueMethod('getDocumentEngineKey')159->execute();160}161162final public function newSortVector(PhabricatorDocumentRef $ref) {163$content_score = $this->getContentScore($ref);164165// Prefer engines which can render the entire file over engines which166// can only render a header, and engines which can render a header over167// engines which can't render anything.168if ($this->canRenderCompleteDocument($ref)) {169$limit_score = 0;170} else if ($this->canRenderPartialDocument($ref)) {171$limit_score = 1;172} else {173$limit_score = 2;174}175176return id(new PhutilSortVector())177->addInt($limit_score)178->addInt(-$content_score);179}180181protected function getContentScore(PhabricatorDocumentRef $ref) {182return 2000;183}184185abstract public function getViewAsLabel(PhabricatorDocumentRef $ref);186187public function getViewAsIconIcon(PhabricatorDocumentRef $ref) {188$can_complete = $this->canRenderCompleteDocument($ref);189$can_partial = $this->canRenderPartialDocument($ref);190191if (!$can_complete && !$can_partial) {192return 'fa-times';193}194195return $this->getDocumentIconIcon($ref);196}197198public function getViewAsIconColor(PhabricatorDocumentRef $ref) {199$can_complete = $this->canRenderCompleteDocument($ref);200201if (!$can_complete) {202return 'grey';203}204205return null;206}207208final public static function getEnginesForRef(209PhabricatorUser $viewer,210PhabricatorDocumentRef $ref) {211$engines = self::getAllEngines();212213foreach ($engines as $key => $engine) {214$engine = id(clone $engine)215->setViewer($viewer);216217if (!$engine->canRenderDocument($ref)) {218unset($engines[$key]);219continue;220}221222$engines[$key] = $engine;223}224225if (!$engines) {226throw new Exception(pht('No content engine can render this document.'));227}228229$vectors = array();230foreach ($engines as $key => $usable_engine) {231$vectors[$key] = $usable_engine->newSortVector($ref);232}233$vectors = msortv($vectors, 'getSelf');234235return array_select_keys($engines, array_keys($vectors));236}237238protected function getByteLengthLimit() {239return (1024 * 1024 * 8);240}241242protected function canRenderCompleteDocument(PhabricatorDocumentRef $ref) {243$limit = $this->getByteLengthLimit();244if ($limit) {245$length = $ref->getByteLength();246if ($length > $limit) {247return false;248}249}250251return true;252}253254protected function canRenderPartialDocument(PhabricatorDocumentRef $ref) {255return false;256}257258protected function newMessage($message) {259return phutil_tag(260'div',261array(262'class' => 'document-engine-error',263),264$message);265}266267final public function newLoadingContent(PhabricatorDocumentRef $ref) {268$spinner = id(new PHUIIconView())269->setIcon('fa-gear')270->addClass('ph-spin');271272return phutil_tag(273'div',274array(275'class' => 'document-engine-loading',276),277array(278$spinner,279$this->getDocumentRenderingText($ref),280));281}282283public function shouldSuggestEngine(PhabricatorDocumentRef $ref) {284return false;285}286287}288289290