Path: blob/master/src/applications/files/document/PhabricatorDocumentRef.php
12241 views
<?php12final class PhabricatorDocumentRef3extends Phobject {45private $name;6private $mimeType;7private $file;8private $byteLength;9private $snippet;10private $symbolMetadata = array();11private $blameURI;12private $coverage = array();13private $data;1415public function setFile(PhabricatorFile $file) {16$this->file = $file;17return $this;18}1920public function getFile() {21return $this->file;22}2324public function setMimeType($mime_type) {25$this->mimeType = $mime_type;26return $this;27}2829public function getMimeType() {30if ($this->mimeType !== null) {31return $this->mimeType;32}3334if ($this->file) {35return $this->file->getMimeType();36}3738return null;39}4041public function setName($name) {42$this->name = $name;43return $this;44}4546public function getName() {47if ($this->name !== null) {48return $this->name;49}5051if ($this->file) {52return $this->file->getName();53}5455return null;56}5758public function setByteLength($length) {59$this->byteLength = $length;60return $this;61}6263public function getByteLength() {64if ($this->byteLength !== null) {65return $this->byteLength;66}6768if ($this->data !== null) {69return strlen($this->data);70}7172if ($this->file) {73return (int)$this->file->getByteSize();74}7576return null;77}7879public function setData($data) {80$this->data = $data;81return $this;82}8384public function loadData($begin = null, $end = null) {85if ($this->data !== null) {86$data = $this->data;8788if ($begin !== null && $end !== null) {89$data = substr($data, $begin, $end - $begin);90} else if ($begin !== null) {91$data = substr($data, $begin);92} else if ($end !== null) {93$data = substr($data, 0, $end);94}9596return $data;97}9899if ($this->file) {100$iterator = $this->file->getFileDataIterator($begin, $end);101102$result = '';103foreach ($iterator as $chunk) {104$result .= $chunk;105}106return $result;107}108109throw new PhutilMethodNotImplementedException();110}111112public function hasAnyMimeType(array $candidate_types) {113$mime_full = $this->getMimeType();114115if (!phutil_nonempty_string($mime_full)) {116return false;117}118119$mime_parts = explode(';', $mime_full);120121$mime_type = head($mime_parts);122$mime_type = $this->normalizeMimeType($mime_type);123124foreach ($candidate_types as $candidate_type) {125if ($this->normalizeMimeType($candidate_type) === $mime_type) {126return true;127}128}129130return false;131}132133private function normalizeMimeType($mime_type) {134$mime_type = trim($mime_type);135$mime_type = phutil_utf8_strtolower($mime_type);136return $mime_type;137}138139public function isProbablyText() {140$snippet = $this->getSnippet();141return (strpos($snippet, "\0") === false);142}143144public function isProbablyJSON() {145if (!$this->isProbablyText()) {146return false;147}148149$snippet = $this->getSnippet();150151// If the file is longer than the snippet, we don't detect the content152// as JSON. We could use some kind of heuristic here if we wanted, but153// see PHI749 for a false positive.154if (strlen($snippet) < $this->getByteLength()) {155return false;156}157158// If the snippet is the whole file, just check if the snippet is valid159// JSON. Note that `phutil_json_decode()` only accepts arrays and objects160// as JSON, so this won't misfire on files with content like "3".161try {162phutil_json_decode($snippet);163return true;164} catch (Exception $ex) {165return false;166}167}168169public function getSnippet() {170if ($this->snippet === null) {171$this->snippet = $this->loadData(null, (1024 * 1024 * 1));172}173174return $this->snippet;175}176177public function setSymbolMetadata(array $metadata) {178$this->symbolMetadata = $metadata;179return $this;180}181182public function getSymbolMetadata() {183return $this->symbolMetadata;184}185186public function setBlameURI($blame_uri) {187$this->blameURI = $blame_uri;188return $this;189}190191public function getBlameURI() {192return $this->blameURI;193}194195public function addCoverage($coverage) {196$this->coverage[] = array(197'data' => $coverage,198);199return $this;200}201202public function getCoverage() {203return $this->coverage;204}205206}207208209