do not show tools when looking at revision
[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             return '/' . $this->repo->id . '/raw/' . $this->getFilename();
106         } else if ($type == 'tool') {
107             return '/' . $this->repo->id . '/tool/' . $option . '/' . $this->getFilename();
108         }
109         throw new Exception('Unknown type');
110     }
111
112     public function getMimeType()
113     {
114         $ext = $this->getExt();
115         if (!isset($GLOBALS['phorkie']['languages'][$ext])) {
116             return null;
117         }
118         return $GLOBALS['phorkie']['languages'][$ext]['mime'];
119     }
120
121     /**
122      * @return array Array of Tool_Info objects
123      */
124     public function getToolInfos()
125     {
126         if ($this->repo->hash !== null) {
127             return array();
128         }
129
130         $tm = new Tool_Manager();
131         return $tm->getSuitable($this);
132     }
133
134     /**
135      * Tells if the file contains textual content and is editable.
136      *
137      * @return boolean
138      */
139     public function isText()
140     {
141         $ext = $this->getExt();
142         if (!isset($GLOBALS['phorkie']['languages'][$ext]['mime'])) {
143             return false;
144         }
145
146         $type = $GLOBALS['phorkie']['languages'][$ext]['mime'];
147         return substr($type, 0, 5) === 'text/';
148     }
149 }
150
151 ?>