editing works basically
[phorkie.git] / src / Phorkie / File.php
1 <?php
2 namespace Phorkie;
3
4 class File
5 {
6     /**
7      * Full path to the file
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      * Maps file extensions to MIME Types
22      *
23      * @var array
24      */
25     public static $arMimeTypeMap = array(
26         'css'  => 'text/css',
27         'htm'  => 'text/html',
28         'html' => 'text/html',
29         'js'   => 'application/javascript',
30         'php'  => 'text/x-php',
31         'txt'  => 'text/plain',
32         'xml'  => 'text/xml',
33     );
34
35     /**
36      * Maps file extensions to geshi types
37      *
38      * @var array
39      */
40     public static $arTypeMap = array(
41         'htm'  => 'xml',
42         'html' => 'xml',
43     );
44
45     public function __construct($path, Repository $repo = null)
46     {
47         $this->path = $path;
48         $this->repo = $repo;
49     }
50
51     /**
52      * Get filename relative to the repository path
53      *
54      * @return string
55      */
56     public function getFilename()
57     {
58         return basename($this->path);
59     }
60
61     /**
62      * Return the full path to the file
63      *
64      * @return string
65      */
66     public function getPath()
67     {
68         return $this->path;
69     }
70
71     /**
72      * Get file extension without dot
73      *
74      * @return string
75      */
76     public function getExt()
77     {
78         return substr($this->path, strrpos($this->path, '.') + 1);
79     }
80
81     /**
82      * Returns the type of the file, as used by Geshi
83      *
84      * @return string
85      */
86     public function getType()
87     {
88         $ext = $this->getExt();
89         if (isset(static::$arTypeMap[$ext])) {
90             $ext = static::$arTypeMap[$ext];
91         }
92
93         return $ext;
94     }
95
96     public function getContent()
97     {
98         return file_get_contents($this->path);
99     }
100
101     public function getHighlightedContent()
102     {
103         /**
104          * Yes, geshi needs to be in your include path
105          * We use the mediawiki geshi extension package.
106          */
107         require 'MediaWiki/geshi/geshi/geshi.php';
108         $geshi = new \GeSHi($this->getContent(), $this->getType());
109         $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
110         $geshi->set_header_type(GESHI_HEADER_DIV);
111         return $geshi->parse_code();
112     }
113
114     public function getMimeType()
115     {
116         $ext = $this->getExt();
117         if (!isset(static::$arMimeTypeMap[$ext])) {
118             return null;
119         }
120         return static::$arMimeTypeMap[$ext];
121     }
122
123     /**
124      * Get a link to the file
125      *
126      * @param string $type Link type. Supported are:
127      *                     - "raw"
128      *                     - "display"
129      *
130      * @return string
131      */
132     public function getLink($type)
133     {
134         if ($type == 'raw') {
135             return '/' . $this->repo->id . '/raw/' . $this->getFilename();
136         }
137         throw new Exception('Unknown type');
138     }
139 }
140
141 ?>