Fix bug #24: verify geshi installation, Fix bug #25: setupcheck: verify markdown
[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         'zustellzentrum.cweiske.de/MIME_Type_PlainDetect' => 'MIME_Type_PlainDetect',
15     );
16
17     protected $writableDirs;
18     protected $elasticsearch;
19
20     public function __construct()
21     {
22         $cfg = $GLOBALS['phorkie']['cfg'];
23         $this->writableDirs = array(
24             'gitdir' => $cfg['gitdir'],
25             'workdir' => $cfg['workdir'],
26         );
27         $this->elasticsearch = $cfg['elasticsearch'];
28     }
29
30     public static function run()
31     {
32         $sc = new self();
33         $sc->checkDeps();
34         $sc->checkDirs();
35         $sc->checkGit();
36         $sc->checkDatabase();
37     }
38
39     public function checkDeps()
40     {
41         foreach ($this->deps as $package => $class) {
42             if (!class_exists($class, true)) {
43                 $this->fail('PEAR package not installed: ' . $package);
44             }
45         }
46
47         $geshi = stream_resolve_include_path(
48             $GLOBALS['phorkie']['cfg']['geshi']
49         );
50         if ($geshi === false) {
51             $this->fail('GeSHi not available');
52         }
53
54         $markdown = stream_resolve_include_path('markdown.php');
55         if ($markdown === false) {
56             $this->fail('Markdown renderer not available');
57         }
58     }
59
60     public function checkDirs()
61     {
62         foreach ($this->writableDirs as $name => $dir) {
63             if (!is_dir($dir)) {
64                 $this->fail($name . ' directory does not exist at ' . $dir);
65             }
66             if (!is_writable($dir)) {
67                 $this->fail($name . ' directory is not writable at ' . $dir);
68             }
69         }
70     }
71
72     public function checkGit()
73     {
74         $line = exec('git --version', $lines, $retval);
75         if ($retval !== 0) {
76             $this->fail('Running git executable failed.');
77         }
78         if (!preg_match('#^git version ([0-9.]+)$#', $line, $matches)) {
79             $this->fail('git version output format unexpected: ' . $line);
80         }
81         if (version_compare($matches[1], '1.7.5') < 0) {
82             $this->fail(
83                 'git version needs to be at least 1.7.5, got: ' . $matches[1]
84             );
85         }
86     }
87
88     public function checkDatabase()
89     {
90         if ($this->elasticsearch == '') {
91             return;
92         }
93
94         $es = parse_url($this->elasticsearch);
95         if (!preg_match("#/.+/#", $es['path'], $matches)) {
96             $this->fail(
97                 'Improper elasticsearch url.  Elasticsearch requires a'
98                 . ' search domain to store your data.'
99                 . ' (e.g. http://localhost:9200/phorkie/)'
100             );
101         }
102         $dbs = new Database();
103         $dbs->getSetup()->setup();
104     }
105
106     public function fail($msg)
107     {
108         throw new Exception($msg);
109     }
110 }
111
112 ?>