Path: blob/master/src/applications/files/document/PhabricatorTextDocumentEngine.php
12241 views
<?php12abstract class PhabricatorTextDocumentEngine3extends PhabricatorDocumentEngine {45private $encodingMessage = null;67protected function canRenderDocumentType(PhabricatorDocumentRef $ref) {8return $ref->isProbablyText();9}1011public function canConfigureEncoding(PhabricatorDocumentRef $ref) {12return true;13}1415protected function newTextDocumentContent(16PhabricatorDocumentRef $ref,17$content,18array $options = array()) {1920PhutilTypeSpec::checkMap(21$options,22array(23'blame' => 'optional wild',24'coverage' => 'optional list<wild>',25));2627if (is_array($content)) {28$lines = $content;29} else {30$lines = phutil_split_lines($content);31}3233$view = id(new PhabricatorSourceCodeView())34->setHighlights($this->getHighlightedLines())35->setLines($lines)36->setSymbolMetadata($ref->getSymbolMetadata());3738$blame = idx($options, 'blame');39if ($blame !== null) {40$view->setBlameMap($blame);41}4243$coverage = idx($options, 'coverage');44if ($coverage !== null) {45$view->setCoverage($coverage);46}4748$message = null;49if ($this->encodingMessage !== null) {50$message = $this->newMessage($this->encodingMessage);51}5253$container = phutil_tag(54'div',55array(56'class' => 'document-engine-text',57),58array(59$message,60$view,61));6263return $container;64}6566protected function loadTextData(PhabricatorDocumentRef $ref) {67$content = $ref->loadData();6869$encoding = $this->getEncodingConfiguration();70if ($encoding !== null) {71if (function_exists('mb_convert_encoding')) {72$content = mb_convert_encoding($content, 'UTF-8', $encoding);73$this->encodingMessage = pht(74'This document was converted from %s to UTF8 for display.',75$encoding);76} else {77$this->encodingMessage = pht(78'Unable to perform text encoding conversion: mbstring extension '.79'is not available.');80}81} else {82if (!phutil_is_utf8($content)) {83if (function_exists('mb_detect_encoding')) {84$try_encodings = array(85'JIS' => pht('JIS'),86'EUC-JP' => pht('EUC-JP'),87'SJIS' => pht('Shift JIS'),88'ISO-8859-1' => pht('ISO-8859-1 (Latin 1)'),89);9091$guess = mb_detect_encoding($content, array_keys($try_encodings));92if ($guess) {93$content = mb_convert_encoding($content, 'UTF-8', $guess);94$this->encodingMessage = pht(95'This document is not UTF8. It was detected as %s and '.96'converted to UTF8 for display.',97idx($try_encodings, $guess, $guess));98}99}100}101}102103if (!phutil_is_utf8($content)) {104$content = phutil_utf8ize($content);105$this->encodingMessage = pht(106'This document is not UTF8 and its text encoding could not be '.107'detected automatically. Use "Change Text Encoding..." to choose '.108'an encoding.');109}110111return $content;112}113114}115116117