729cbd6aaaf6293bf3e65cc309717915a8df6c29
[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         'xml'  => 'text/xml',
28     );
29
30     public function __construct($path, Repository $repo)
31     {
32         $this->path = $path;
33         $this->repo = $repo;
34     }
35
36     /**
37      * Get filename relative to the repository path
38      *
39      * @return string
40      */
41     public function getFilename()
42     {
43         return basename($this->path);
44     }
45
46     /**
47      * Returns the type of the file, as used internally by Phorkie
48      *
49      * @return string
50      */
51     public function getType()
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 'MediaWiki/geshi/geshi/geshi.php';
68         $geshi = new \GeSHi($this->getContent(), $this->getType());
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     public function getMimeType()
75     {
76         $type = $this->getType();
77         if (!isset(static::$arMimeTypeMap[$type])) {
78             return null;
79         }
80         return static::$arMimeTypeMap[$type];
81     }
82
83     /**
84      * Get a link to the file
85      *
86      * @param string $type Link type. Supported are:
87      *                     - "raw"
88      *                     - "display"
89      *
90      * @return string
91      */
92     public function getLink($type)
93     {
94         if ($type == 'raw') {
95             return '/' . $this->repo->id . '/raw/' . $this->getFilename();
96         }
97         throw new Exception('Unknown type');
98     }
99 }
100
101 ?>