7 * Path to the file, relative to repository work directory
14 * Repository this file belongs to
21 * Commit revision this file is at
25 public function __construct($path, Repository $repo = null)
32 * Get filename relative to the repository path
36 public function getFilename()
42 * Get the filename usable as HTML anchor.
46 function getAnchorName()
48 return str_replace(' ', '-', $this->getFilename());
52 * Return the full path to the file
56 public function getFullPath()
58 return $this->repo->workDir . '/' . $this->path;
62 * Get file extension without dot
66 public function getExt()
68 return strtolower(substr($this->path, strrpos($this->path, '.') + 1));
71 public function getContent()
73 if ($this->repo->hash) {
74 //quick hack until https://pear.php.net/bugs/bug.php?id=19385 is fixed
75 $cmd = new GitCommandBinary($this->repo->getVc());
76 $cmd->setSubCommand('show');
78 ->addArgument($this->repo->hash . ':' . $this->path)
82 return file_get_contents($this->getFullPath());
85 public function getRenderedContent(Tool_Result $res = null)
87 $cache = new Renderer_Cache();
88 return $cache->toHtml($this, $res);
92 * Get a link to the file
94 * @param string $type Link type. Supported are:
98 * @param string $option Additional option, e.g. tool name
99 * @param boolean $full Return full URL or normal relative
103 public function getLink($type, $option = null, $full = false)
105 if ($type == 'raw') {
106 if ($this->repo->hash === null) {
107 $link = $this->repo->id . '/raw/' . $this->getFilename();
109 $link = $this->repo->id . '/rev-raw/' . $this->repo->hash
110 . '/' . $this->getFilename();
112 } else if ($type == 'tool') {
113 $link = $this->repo->id
115 . '/' . $this->getFilename();
116 } else if ($type == 'display') {
117 $link = $this->repo->id . '#' . $this->getFilename();
119 throw new Exception('Unknown type');
123 $link = Tools::fullUrl($link);
129 * @return string Mime type of file, NULL if no type detected
131 public function getMimeType()
133 $ext = $this->getExt();
134 if (isset($GLOBALS['phorkie']['languages'][$ext])) {
135 return $GLOBALS['phorkie']['languages'][$ext]['mime'];
138 $mte = new \MIME_Type_Extension();
139 $type = $mte->getMIMEType($this->getFilename());
140 if (!\PEAR::isError($type)) {
147 * @return array Array of Tool_Info objects
149 public function getToolInfos()
151 if ($this->repo->hash !== null) {
155 $tm = new Tool_Manager();
156 return $tm->getSuitable($this);
160 * Tells if the file contains textual content and is editable.
164 public function isText()
166 $ext = $this->getExt();
168 return $this->isNonBinary();
171 $type = $this->getMimeType();
172 if ($type === null) {
173 return $this->isNonBinary();
175 return substr($type, 0, 5) === 'text/'
176 || $type == 'application/javascript'
177 || substr($type, -4) == '+xml'
178 || substr($type, -5) == '+json';
182 * Look at the file's bytes and guess if it's binary or not.
184 * @return boolean True if it's most likely plain text
186 public function isNonBinary()
188 $fp = fopen($this->getFullPath(), 'r');
193 //When multibyte extension is not installed,
194 // we only allow files with ASCII characters.
195 // Files with UTF-8 characters will not be detected as text.
196 $hasMb = function_exists('mb_detect_encoding');
200 while (false !== ($char = fgetc($fp)) && ++$pos < 100) {
202 if (!$hasMb && ord($char) > 128) {
213 if (mb_detect_encoding($data) === false) {