make autoloading geshi work
[phorkie.git] / src / phorkie / SetupCheck.php
1 <?php
2 namespace phorkie;
3
4 class SetupCheck
5 {
6     protected $deps = array(
7         'pear.php.net/VersionControl_Git'  => 'VersionControl_Git',
8         'pear.twig-project.org/Twig'       => 'Twig_Autoloader',
9         'pear.php.net/Date_HumanDiff'      => 'Date_HumanDiff',
10         'pear.php.net/HTTP_Request2'       => 'HTTP_Request2',
11         'pear.php.net/OpenID'              => 'OpenID',
12         'pear.php.net/Pager'               => 'Pager',
13         'pear.php.net/Services_Libravatar' => 'Services_Libravatar',
14         'pear2.php.net/PEAR2_Services_Linkback'  => '\\PEAR2\\Services\\Linkback\\Client',
15         'zustellzentrum.cweiske.de/MIME_Type_PlainDetect' => 'MIME_Type_PlainDetect',
16     );
17
18     protected $writableDirs;
19     protected $elasticsearch;
20
21     public function __construct()
22     {
23         $cfg = $GLOBALS['phorkie']['cfg'];
24         $this->writableDirs = array(
25             'gitdir' => $cfg['gitdir'],
26             'workdir' => $cfg['workdir'],
27         );
28         $this->elasticsearch = $cfg['elasticsearch'];
29     }
30
31     public static function run()
32     {
33         $sc = new self();
34         $sc->checkDeps();
35         $sc->checkDirs();
36         $sc->checkGit();
37         $sc->checkDatabase();
38         $sc->checkMimeTypeDetection();
39     }
40
41     public function checkDeps()
42     {
43         foreach ($this->deps as $package => $class) {
44             if (!class_exists($class, true)) {
45                 $this->fail('PEAR package not installed: ' . $package);
46             }
47         }
48
49         if (!class_exists('geshi', true)) {
50             $geshi = stream_resolve_include_path(
51                 $GLOBALS['phorkie']['cfg']['geshi']
52             );
53             if ($geshi === false) {
54                 $this->fail('GeSHi not available');
55             }
56         }
57
58         if (!class_exists('\\Michelf\\Markdown', true)) {
59             //PEAR-installed version 1.0.2 has a different API
60             $markdown = stream_resolve_include_path('markdown.php');
61             if ($markdown === false) {
62                 $this->fail('Markdown renderer not available');
63             }
64         }
65     }
66
67     public function checkDirs()
68     {
69         foreach ($this->writableDirs as $name => $dir) {
70             if (!is_dir($dir)) {
71                 $this->fail($name . ' directory does not exist at ' . $dir);
72             }
73             if (!is_writable($dir)) {
74                 $this->fail($name . ' directory is not writable at ' . $dir);
75             }
76         }
77     }
78
79     public function checkGit()
80     {
81         $line = exec('git --version', $lines, $retval);
82         if ($retval !== 0) {
83             $this->fail('Running git executable failed.');
84         }
85         if (!preg_match('#^git version ([0-9.]+(rc[0-9]+)?)$#', $line, $matches)) {
86             $this->fail('git version output format unexpected: ' . $line);
87         }
88         if (version_compare($matches[1], '1.7.5') < 0) {
89             $this->fail(
90                 'git version needs to be at least 1.7.5, got: ' . $matches[1]
91             );
92         }
93     }
94
95     public function checkDatabase()
96     {
97         if ($this->elasticsearch == '') {
98             return;
99         }
100
101         $es = parse_url($this->elasticsearch);
102         if (!preg_match("#/.+/#", $es['path'], $matches)) {
103             $this->fail(
104                 'Improper elasticsearch url.  Elasticsearch requires a'
105                 . ' search domain to store your data.'
106                 . ' (e.g. http://localhost:9200/phorkie/)'
107             );
108         }
109         $dbs = new Database();
110         $dbs->getSetup()->setup();
111     }
112
113     public function checkMimeTypeDetection()
114     {
115         $rp = new Repository_Post();
116         if ($rp->getType('<?php echo "foo"; ?>') != 'php') {
117             $this->fail('MIME type detection fails');
118         }
119     }
120
121     public function fail($msg)
122     {
123         throw new Exception($msg);
124     }
125 }
126
127 ?>