5eae1f39f9a528a766a508db5b109fd5b4ed8909
[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/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         $geshi = stream_resolve_include_path(
50             $GLOBALS['phorkie']['cfg']['geshi']
51         );
52         if ($geshi === false) {
53             $this->fail('GeSHi not available');
54         }
55
56         $markdown = stream_resolve_include_path('markdown.php');
57         if ($markdown === false) {
58             $this->fail('Markdown renderer not available');
59         }
60     }
61
62     public function checkDirs()
63     {
64         foreach ($this->writableDirs as $name => $dir) {
65             if (!is_dir($dir)) {
66                 $this->fail($name . ' directory does not exist at ' . $dir);
67             }
68             if (!is_writable($dir)) {
69                 $this->fail($name . ' directory is not writable at ' . $dir);
70             }
71         }
72     }
73
74     public function checkGit()
75     {
76         $line = exec('git --version', $lines, $retval);
77         if ($retval !== 0) {
78             $this->fail('Running git executable failed.');
79         }
80         if (!preg_match('#^git version ([0-9.]+)$#', $line, $matches)) {
81             $this->fail('git version output format unexpected: ' . $line);
82         }
83         if (version_compare($matches[1], '1.7.5') < 0) {
84             $this->fail(
85                 'git version needs to be at least 1.7.5, got: ' . $matches[1]
86             );
87         }
88     }
89
90     public function checkDatabase()
91     {
92         if ($this->elasticsearch == '') {
93             return;
94         }
95
96         $es = parse_url($this->elasticsearch);
97         if (!preg_match("#/.+/#", $es['path'], $matches)) {
98             $this->fail(
99                 'Improper elasticsearch url.  Elasticsearch requires a'
100                 . ' search domain to store your data.'
101                 . ' (e.g. http://localhost:9200/phorkie/)'
102             );
103         }
104         $dbs = new Database();
105         $dbs->getSetup()->setup();
106     }
107
108     public function checkMimeTypeDetection()
109     {
110         $rp = new Repository_Post();
111         if ($rp->getType('<?php echo "foo"; ?>') != 'php') {
112             $this->fail('MIME type detection fails');
113         }
114     }
115
116     public function fail($msg)
117     {
118         throw new Exception($msg);
119     }
120 }
121
122 ?>