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
131 public function getMimeType()
133 $ext = $this->getExt();
134 if (!isset($GLOBALS['phorkie']['languages'][$ext])) {
137 return $GLOBALS['phorkie']['languages'][$ext]['mime'];
141 * @return array Array of Tool_Info objects
143 public function getToolInfos()
145 if ($this->repo->hash !== null) {
149 $tm = new Tool_Manager();
150 return $tm->getSuitable($this);
154 * Tells if the file contains textual content and is editable.
158 public function isText()
160 $ext = $this->getExt();
162 //no file extension? then consider the size
163 $size = filesize($this->getFullPath());
164 //files <= 4kiB are considered to be text
165 return $size <= 4096;
168 if (!isset($GLOBALS['phorkie']['languages'][$ext]['mime'])) {
172 $type = $GLOBALS['phorkie']['languages'][$ext]['mime'];
173 return substr($type, 0, 5) === 'text/'
174 || $type == 'application/javascript'
175 || substr($type, -4) == '+xml'
176 || substr($type, -5) == '+json';