finish todo item
[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      * Return the full path to the file
43      *
44      * @return string
45      */
46     public function getFullPath()
47     {
48         return $this->repo->workDir . '/' . $this->path;
49     }
50
51     /**
52      * Get file extension without dot
53      *
54      * @return string
55      */
56     public function getExt()
57     {
58         return substr($this->path, strrpos($this->path, '.') + 1);
59     }
60
61     public function getContent()
62     {
63         if ($this->repo->hash) {
64             return $this->repo->getVc()->getCommand('show')
65                 ->addArgument($this->repo->hash . ':' . $this->path)
66                 ->execute();
67         }
68
69         return file_get_contents($this->getFullPath());
70     }
71
72     public function getRenderedContent(Tool_Result $res = null)
73     {
74         $ext   = $this->getExt();
75         $class = '\\phorkie\\Renderer_Unknown';
76
77         if (isset($GLOBALS['phorkie']['languages'][$ext]['renderer'])) {
78             $class = $GLOBALS['phorkie']['languages'][$ext]['renderer'];
79         } else if (isset($GLOBALS['phorkie']['languages'][$ext]['mime'])) {
80             $type = $GLOBALS['phorkie']['languages'][$ext]['mime'];
81             if (substr($type, 0, 5) == 'text/') {
82                 $class = '\\phorkie\\Renderer_Geshi';
83             } else if (substr($type, 0, 6) == 'image/') {
84                 $class = '\\phorkie\\Renderer_Image';
85             }
86         }
87
88         $rend = new $class();
89         return $rend->toHtml($this, $res);
90     }
91
92     /**
93      * Get a link to the file
94      *
95      * @param string $type Link type. Supported are:
96      *                     - "raw"
97      *                     - "tool"
98      * @param string $option
99      *
100      * @return string
101      */
102     public function getLink($type, $option = null)
103     {
104         if ($type == 'raw') {
105             if ($this->repo->hash === null) {
106                 return '/' . $this->repo->id . '/raw/' . $this->getFilename();
107             } else {
108                 return '/' . $this->repo->id . '/rev-raw/' . $this->repo->hash
109                     . '/' . $this->getFilename();
110             }
111         } else if ($type == 'tool') {
112             return '/' . $this->repo->id . '/tool/' . $option . '/' . $this->getFilename();
113         }
114         throw new Exception('Unknown type');
115     }
116
117     public function getMimeType()
118     {
119         $ext = $this->getExt();
120         if (!isset($GLOBALS['phorkie']['languages'][$ext])) {
121             return null;
122         }
123         return $GLOBALS['phorkie']['languages'][$ext]['mime'];
124     }
125
126     /**
127      * @return array Array of Tool_Info objects
128      */
129     public function getToolInfos()
130     {
131         if ($this->repo->hash !== null) {
132             return array();
133         }
134
135         $tm = new Tool_Manager();
136         return $tm->getSuitable($this);
137     }
138
139     /**
140      * Tells if the file contains textual content and is editable.
141      *
142      * @return boolean
143      */
144     public function isText()
145     {
146         $ext = $this->getExt();
147         if (!isset($GLOBALS['phorkie']['languages'][$ext]['mime'])) {
148             return false;
149         }
150
151         $type = $GLOBALS['phorkie']['languages'][$ext]['mime'];
152         return substr($type, 0, 5) === 'text/';
153     }
154 }
155
156 ?>