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