update to latest services_libravatar release
[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             //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')
68                 $cmd
69                 ->addArgument($this->repo->hash . ':' . $this->path)
70                 ->execute();
71         }
72
73         return file_get_contents($this->getFullPath());
74     }
75
76     public function getRenderedContent(Tool_Result $res = null)
77     {
78         $ext   = $this->getExt();
79         $class = '\\phorkie\\Renderer_Unknown';
80
81         if (isset($GLOBALS['phorkie']['languages'][$ext]['renderer'])) {
82             $class = $GLOBALS['phorkie']['languages'][$ext]['renderer'];
83         } else if (isset($GLOBALS['phorkie']['languages'][$ext]['mime'])) {
84             $type = $GLOBALS['phorkie']['languages'][$ext]['mime'];
85             if (substr($type, 0, 5) == 'text/') {
86                 $class = '\\phorkie\\Renderer_Geshi';
87             } else if (substr($type, 0, 6) == 'image/') {
88                 $class = '\\phorkie\\Renderer_Image';
89             }
90         }
91
92         $rend = new $class();
93         return $rend->toHtml($this, $res);
94     }
95
96     /**
97      * Get a link to the file
98      *
99      * @param string $type Link type. Supported are:
100      *                     - "raw"
101      *                     - "tool"
102      * @param string $option
103      *
104      * @return string
105      */
106     public function getLink($type, $option = null)
107     {
108         if ($type == 'raw') {
109             if ($this->repo->hash === null) {
110                 return '/' . $this->repo->id . '/raw/' . $this->getFilename();
111             } else {
112                 return '/' . $this->repo->id . '/rev-raw/' . $this->repo->hash
113                     . '/' . $this->getFilename();
114             }
115         } else if ($type == 'tool') {
116             return '/' . $this->repo->id . '/tool/' . $option . '/' . $this->getFilename();
117         }
118         throw new Exception('Unknown type');
119     }
120
121     public function getMimeType()
122     {
123         $ext = $this->getExt();
124         if (!isset($GLOBALS['phorkie']['languages'][$ext])) {
125             return null;
126         }
127         return $GLOBALS['phorkie']['languages'][$ext]['mime'];
128     }
129
130     /**
131      * @return array Array of Tool_Info objects
132      */
133     public function getToolInfos()
134     {
135         if ($this->repo->hash !== null) {
136             return array();
137         }
138
139         $tm = new Tool_Manager();
140         return $tm->getSuitable($this);
141     }
142
143     /**
144      * Tells if the file contains textual content and is editable.
145      *
146      * @return boolean
147      */
148     public function isText()
149     {
150         $ext = $this->getExt();
151         if (!isset($GLOBALS['phorkie']['languages'][$ext]['mime'])) {
152             return false;
153         }
154
155         $type = $GLOBALS['phorkie']['languages'][$ext]['mime'];
156         return substr($type, 0, 5) === 'text/';
157     }
158 }
159
160 ?>