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 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:
101 * @param string $option Additional option, e.g. tool name
105 public function getLink($type, $option = null)
107 if ($type == 'raw') {
108 if ($this->repo->hash === null) {
109 return $this->repo->id . '/raw/' . $this->getFilename();
111 return $this->repo->id . '/rev-raw/' . $this->repo->hash
112 . '/' . $this->getFilename();
114 } else if ($type == 'tool') {
115 return $this->repo->id
117 . '/' . $this->getFilename();
119 throw new Exception('Unknown type');
123 * @return string Mime type of file
125 public function getMimeType()
127 $ext = $this->getExt();
128 if (!isset($GLOBALS['phorkie']['languages'][$ext])) {
131 return $GLOBALS['phorkie']['languages'][$ext]['mime'];
135 * @return array Array of Tool_Info objects
137 public function getToolInfos()
139 if ($this->repo->hash !== null) {
143 $tm = new Tool_Manager();
144 return $tm->getSuitable($this);
148 * Tells if the file contains textual content and is editable.
152 public function isText()
154 $ext = $this->getExt();
156 //no file extension? then consider the size
157 $size = filesize($this->getFullPath());
158 //files <= 4kiB are considered to be text
159 return $size <= 4096;
162 if (!isset($GLOBALS['phorkie']['languages'][$ext]['mime'])) {
166 $type = $GLOBALS['phorkie']['languages'][$ext]['mime'];
167 return substr($type, 0, 5) === 'text/'
168 || $type == 'application/javascript';