simple cache for rendered files
[phorkie.git] / src / phorkie / File.php
1 <?php
2 namespace phorkie;
3
4 class File
5 {
6     /**
7      * Path to the file, relative to repository work directory
8      *
9      * @var string
10      */
11     public $path;
12
13     /**
14      * Repository this file belongs to
15      *
16      * @var string
17      */
18     public $repo;
19
20     /**
21      * Commit revision this file is at
22      */
23     public $hash;
24
25     public function __construct($path, Repository $repo = null)
26     {
27         $this->path = $path;
28         $this->repo = $repo;
29     }
30
31     /**
32      * Get filename relative to the repository path
33      *
34      * @return string
35      */
36     public function getFilename()
37     {
38         return $this->path;
39     }
40
41     /**
42      * Get the filename usable as HTML anchor.
43      *
44      * @return string
45      */
46     function getAnchorName()
47     {
48         return str_replace(' ', '-', $this->getFilename());
49     }
50
51     /**
52      * Return the full path to the file
53      *
54      * @return string
55      */
56     public function getFullPath()
57     {
58         return $this->repo->workDir . '/' . $this->path;
59     }
60
61     /**
62      * Get file extension without dot
63      *
64      * @return string
65      */
66     public function getExt()
67     {
68         return strtolower(substr($this->path, strrpos($this->path, '.') + 1));
69     }
70
71     public function getContent()
72     {
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');
77             return $cmd
78                 ->addArgument($this->repo->hash . ':' . $this->path)
79                 ->execute();
80         }
81
82         return file_get_contents($this->getFullPath());
83     }
84
85     public function getRenderedContent(Tool_Result $res = null)
86     {
87         $cache = new Renderer_Cache();
88         return $cache->toHtml($this, $res);
89     }
90
91     /**
92      * Get a link to the file
93      *
94      * @param string $type   Link type. Supported are:
95      *                       - "display"
96      *                       - "raw"
97      *                       - "tool"
98      * @param string $option Additional option, e.g. tool name
99      * @param boolean $full   Return full URL or normal relative
100      *
101      * @return string
102      */
103     public function getLink($type, $option = null, $full = false)
104     {
105         if ($type == 'raw') {
106             if ($this->repo->hash === null) {
107                 $link = $this->repo->id . '/raw/' . $this->getFilename();
108             } else {
109                 $link = $this->repo->id . '/rev-raw/' . $this->repo->hash
110                     . '/' . $this->getFilename();
111             }
112         } else if ($type == 'tool') {
113             $link = $this->repo->id
114                 . '/tool/' . $option
115                 . '/' . $this->getFilename();
116         } else if ($type == 'display') {
117             $link = $this->repo->id . '#' . $this->getFilename();
118         } else {
119             throw new Exception('Unknown type');
120         }
121
122         if ($full) {
123             $link = Tools::fullUrl($link);
124         }
125         return $link;
126     }
127
128     /**
129      * @return string Mime type of file
130      */
131     public function getMimeType()
132     {
133         $ext = $this->getExt();
134         if (!isset($GLOBALS['phorkie']['languages'][$ext])) {
135             return null;
136         }
137         return $GLOBALS['phorkie']['languages'][$ext]['mime'];
138     }
139
140     /**
141      * @return array Array of Tool_Info objects
142      */
143     public function getToolInfos()
144     {
145         if ($this->repo->hash !== null) {
146             return array();
147         }
148
149         $tm = new Tool_Manager();
150         return $tm->getSuitable($this);
151     }
152
153     /**
154      * Tells if the file contains textual content and is editable.
155      *
156      * @return boolean
157      */
158     public function isText()
159     {
160         $ext = $this->getExt();
161         if ($ext == '') {
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;
166         }
167
168         if (!isset($GLOBALS['phorkie']['languages'][$ext]['mime'])) {
169             return false;
170         }
171
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';
177     }
178 }
179
180 ?>