preparation for file editing
[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      * Get file extension without dot
63      *
64      * @return string
65      */
66     public function getExt()
67     {
68         return substr($this->path, strrpos($this->path, '.') + 1);
69     }
70
71     /**
72      * Returns the type of the file, as used by Geshi
73      *
74      * @return string
75      */
76     public function getType()
77     {
78         $ext = $this->getExt();
79         if (isset(static::$arTypeMap[$ext])) {
80             $ext = static::$arTypeMap[$ext];
81         }
82
83         return $ext;
84     }
85
86     public function getContent()
87     {
88         return file_get_contents($this->path);
89     }
90
91     public function getHighlightedContent()
92     {
93         /**
94          * Yes, geshi needs to be in your include path
95          * We use the mediawiki geshi extension package.
96          */
97         require 'MediaWiki/geshi/geshi/geshi.php';
98         $geshi = new \GeSHi($this->getContent(), $this->getType());
99         $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
100         $geshi->set_header_type(GESHI_HEADER_DIV);
101         return $geshi->parse_code();
102     }
103
104     public function getMimeType()
105     {
106         $ext = $this->getExt();
107         if (!isset(static::$arMimeTypeMap[$ext])) {
108             return null;
109         }
110         return static::$arMimeTypeMap[$ext];
111     }
112
113     /**
114      * Get a link to the file
115      *
116      * @param string $type Link type. Supported are:
117      *                     - "raw"
118      *                     - "display"
119      *
120      * @return string
121      */
122     public function getLink($type)
123     {
124         if ($type == 'raw') {
125             return '/' . $this->repo->id . '/raw/' . $this->getFilename();
126         }
127         throw new Exception('Unknown type');
128     }
129 }
130
131 ?>