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 * Return the full path to the file
46 public function getFullPath()
48 return $this->repo->workDir . '/' . $this->path;
52 * Get file extension without dot
56 public function getExt()
58 return strtolower(substr($this->path, strrpos($this->path, '.') + 1));
61 public function getContent()
63 if ($this->repo->hash) {
64 //quick hack until https://pear.php.net/bugs/bug.php?id=19385 is fixed
65 $cmd = new GitCommandBinary($this->repo->getVc());
66 $cmd->setSubCommand('show');
68 ->addArgument($this->repo->hash . ':' . $this->path)
72 return file_get_contents($this->getFullPath());
75 public function getRenderedContent(Tool_Result $res = null)
77 $ext = $this->getExt();
78 $class = '\\phorkie\\Renderer_Unknown';
80 if (isset($GLOBALS['phorkie']['languages'][$ext]['renderer'])) {
81 $class = $GLOBALS['phorkie']['languages'][$ext]['renderer'];
82 } else if ($this->isText()) {
83 $class = '\\phorkie\\Renderer_Geshi';
84 } else if (isset($GLOBALS['phorkie']['languages'][$ext]['mime'])) {
85 $type = $GLOBALS['phorkie']['languages'][$ext]['mime'];
86 if (substr($type, 0, 6) == 'image/') {
87 $class = '\\phorkie\\Renderer_Image';
92 return $rend->toHtml($this, $res);
96 * Get a link to the file
98 * @param string $type Link type. Supported are:
102 * @param string $option Additional option, e.g. tool name
103 * @param boolean $full Return full URL or normal relative
107 public function getLink($type, $option = null, $full = false)
109 if ($type == 'raw') {
110 if ($this->repo->hash === null) {
111 $link = $this->repo->id . '/raw/' . $this->getFilename();
113 $link = $this->repo->id . '/rev-raw/' . $this->repo->hash
114 . '/' . $this->getFilename();
116 } else if ($type == 'tool') {
117 $link = $this->repo->id
119 . '/' . $this->getFilename();
120 } else if ($type == 'display') {
121 $link = $this->repo->id . '#' . $this->getFilename();
123 throw new Exception('Unknown type');
127 $link = Tools::fullUrl($link);
133 * @return string Mime type of file
135 public function getMimeType()
137 $ext = $this->getExt();
138 if (!isset($GLOBALS['phorkie']['languages'][$ext])) {
141 return $GLOBALS['phorkie']['languages'][$ext]['mime'];
145 * @return array Array of Tool_Info objects
147 public function getToolInfos()
149 if ($this->repo->hash !== null) {
153 $tm = new Tool_Manager();
154 return $tm->getSuitable($this);
158 * Tells if the file contains textual content and is editable.
162 public function isText()
164 $ext = $this->getExt();
166 //no file extension? then consider the size
167 $size = filesize($this->getFullPath());
168 //files <= 4kiB are considered to be text
169 return $size <= 4096;
172 if (!isset($GLOBALS['phorkie']['languages'][$ext]['mime'])) {
176 $type = $GLOBALS['phorkie']['languages'][$ext]['mime'];
177 return substr($type, 0, 5) === 'text/'
178 || $type == 'application/javascript'
179 || substr($type, -4) == '+xml'
180 || substr($type, -5) == '+json';