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');
67 return //$this->repo->getVc()->getCommand('show')
69 ->addArgument($this->repo->hash . ':' . $this->path)
73 return file_get_contents($this->getFullPath());
76 public function getRenderedContent(Tool_Result $res = null)
78 $ext = $this->getExt();
79 $class = '\\phorkie\\Renderer_Unknown';
81 if (isset($GLOBALS['phorkie']['languages'][$ext]['renderer'])) {
82 $class = $GLOBALS['phorkie']['languages'][$ext]['renderer'];
83 } else if ($this->isText()) {
84 $class = '\\phorkie\\Renderer_Geshi';
85 } else if (isset($GLOBALS['phorkie']['languages'][$ext]['mime'])) {
86 $type = $GLOBALS['phorkie']['languages'][$ext]['mime'];
87 if (substr($type, 0, 6) == 'image/') {
88 $class = '\\phorkie\\Renderer_Image';
93 return $rend->toHtml($this, $res);
97 * Get a link to the file
99 * @param string $type Link type. Supported are:
102 * @param string $option
106 public function getLink($type, $option = null)
108 if ($type == 'raw') {
109 if ($this->repo->hash === null) {
110 return '/' . $this->repo->id . '/raw/' . $this->getFilename();
112 return '/' . $this->repo->id . '/rev-raw/' . $this->repo->hash
113 . '/' . $this->getFilename();
115 } else if ($type == 'tool') {
116 return '/' . $this->repo->id . '/tool/' . $option . '/' . $this->getFilename();
118 throw new Exception('Unknown type');
121 public function getMimeType()
123 $ext = $this->getExt();
124 if (!isset($GLOBALS['phorkie']['languages'][$ext])) {
127 return $GLOBALS['phorkie']['languages'][$ext]['mime'];
131 * @return array Array of Tool_Info objects
133 public function getToolInfos()
135 if ($this->repo->hash !== null) {
139 $tm = new Tool_Manager();
140 return $tm->getSuitable($this);
144 * Tells if the file contains textual content and is editable.
148 public function isText()
150 $ext = $this->getExt();
152 //no file extension? then consider the size
153 $size = filesize($this->getFullPath());
154 //files <= 4kiB are considered to be text
155 return $size <= 4096;
158 if (!isset($GLOBALS['phorkie']['languages'][$ext]['mime'])) {
162 $type = $GLOBALS['phorkie']['languages'][$ext]['mime'];
163 return substr($type, 0, 5) === 'text/'
164 || $type == 'application/javascript';