bad21e6edba1db40a1a147e60579aa96b7f696c6
[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     public function __construct($path, Repository $repo = null)
21     {
22         $this->path = $path;
23         $this->repo = $repo;
24     }
25
26     /**
27      * Get filename relative to the repository path
28      *
29      * @return string
30      */
31     public function getFilename()
32     {
33         return basename($this->path);
34     }
35
36     /**
37      * Return the full path to the file
38      *
39      * @return string
40      */
41     public function getPath()
42     {
43         return $this->path;
44     }
45
46     /**
47      * Get file extension without dot
48      *
49      * @return string
50      */
51     public function getExt()
52     {
53         return substr($this->path, strrpos($this->path, '.') + 1);
54     }
55
56     public function getContent()
57     {
58         return file_get_contents($this->path);
59     }
60
61     public function getHighlightedContent()
62     {
63         /**
64          * Yes, geshi needs to be in your include path
65          * We use the mediawiki geshi extension package.
66          */
67         require_once 'MediaWiki/geshi/geshi/geshi.php';
68         $geshi = new \GeSHi($this->getContent(), $this->getGeshiType());
69         $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
70         $geshi->set_header_type(GESHI_HEADER_DIV);
71         return $geshi->parse_code();
72     }
73
74     /**
75      * Get a link to the file
76      *
77      * @param string $type Link type. Supported are:
78      *                     - "raw"
79      *                     - "display"
80      *
81      * @return string
82      */
83     public function getLink($type)
84     {
85         if ($type == 'raw') {
86             return '/' . $this->repo->id . '/raw/' . $this->getFilename();
87         }
88         throw new Exception('Unknown type');
89     }
90
91     /**
92      * Returns the type of the file, as used by Geshi
93      *
94      * @return string
95      */
96     public function getGeshiType()
97     {
98         $ext = $this->getExt();
99         if (isset($GLOBALS['phorkie']['languages'][$ext]['geshi'])) {
100             $ext = $GLOBALS['phorkie']['languages'][$ext]['geshi'];
101         }
102
103         return $ext;
104     }
105
106     public function getMimeType()
107     {
108         $ext = $this->getExt();
109         if (!isset($GLOBALS['phorkie']['languages'][$ext])) {
110             return null;
111         }
112         return $GLOBALS['phorkie']['languages'][$ext]['mime'];
113     }
114 }
115
116 ?>