6e71de37ca6cfef60fa7d62372ffc1a95921fbec
[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 static $arMimeTypeMap = array(
21         'css'  => 'text/css',
22         'htm'  => 'text/html',
23         'html' => 'text/html',
24         'js'   => 'application/javascript',
25         'php'  => 'text/x-php',
26         'txt'  => 'text/plain',
27     );
28
29     public function __construct($path, Repository $repo)
30     {
31         $this->path = $path;
32         $this->repo = $repo;
33     }
34
35     /**
36      * Get filename relative to the repository path
37      *
38      * @return string
39      */
40     public function getFilename()
41     {
42         return basename($this->path);
43     }
44
45     /**
46      * Returns the type of the file, as used internally by Phorkie
47      *
48      * @return string
49      */
50     public function getType()
51     {
52         return substr($this->path, strrpos($this->path, '.') + 1);
53     }
54
55     public function getContent()
56     {
57         return file_get_contents($this->path);
58     }
59
60     public function getHighlightedContent()
61     {
62         /**
63          * Yes, geshi needs to be in your include path
64          * We use the mediawiki geshi extension package.
65          */
66         require 'MediaWiki/geshi/geshi/geshi.php';
67         $geshi = new \GeSHi($this->getContent(), $this->getType());
68         $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
69         $geshi->set_header_type(GESHI_HEADER_DIV);
70         return $geshi->parse_code();
71     }
72
73     public function getMimeType()
74     {
75         $type = $this->getType();
76         if (!isset(static::$arMimeTypeMap[$type])) {
77             return null;
78         }
79         return static::$arMimeTypeMap[$type];
80     }
81
82     /**
83      * Get a link to the file
84      *
85      * @param string $type Link type. Supported are:
86      *                     - "raw"
87      *                     - "display"
88      *
89      * @return string
90      */
91     public function getLink($type)
92     {
93         if ($type == 'raw') {
94             return '/' . $this->repo->id . '/raw/' . $this->getFilename();
95         }
96         throw new Exception('Unknown type');
97     }
98 }
99
100 ?>