work around PHP bug #68347: parse ini files in raw mode
[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 $messages = array();
22
23     public function __construct()
24     {
25         $cfg = $GLOBALS['phorkie']['cfg'];
26         $this->writableDirs = array(
27             'gitdir'  => Tools::foldPath($cfg['gitdir']),
28             'workdir' => Tools::foldPath($cfg['workdir']),
29         );
30         $this->elasticsearch = $cfg['elasticsearch'];
31     }
32
33     public static function run()
34     {
35         $sc = new self();
36         $sc->checkConfigFiles();
37         $sc->checkDeps();
38         $sc->checkDirs();
39         $sc->checkGit();
40         $sc->checkDatabase();
41         $sc->checkMimeTypeDetection();
42         $sc->checkRemoteForking();
43
44         return $sc->messages;
45     }
46
47     public function checkConfigFiles()
48     {
49         foreach ($GLOBALS['phorkie']['cfgfiles'] as $file => $loaded) {
50             if ($loaded) {
51                 $this->ok('Loaded config file: ' . Tools::foldPath($file));
52             } else {
53                 $this->info(
54                     'Possible config file: ' . Tools::foldPath($file)
55                     . ' (not loaded)'
56                 );
57             }
58         }
59     }
60
61     public function checkDeps()
62     {
63         foreach ($this->deps as $package => $class) {
64             if (!class_exists($class, true)) {
65                 $this->fail('PEAR package not installed: ' . $package);
66             }
67         }
68
69         if (!class_exists('geshi', true)) {
70             $geshi = stream_resolve_include_path(
71                 $GLOBALS['phorkie']['cfg']['geshi']
72             );
73             if ($geshi === false) {
74                 $this->fail('GeSHi not available');
75             }
76         }
77
78         if (!class_exists('\\Michelf\\Markdown', true)) {
79             //PEAR-installed version 1.0.2 has a different API
80             $markdown = stream_resolve_include_path('markdown.php');
81             if ($markdown === false) {
82                 $this->fail('Markdown renderer not available');
83             }
84         }
85     }
86
87     public function checkDirs()
88     {
89         foreach ($this->writableDirs as $name => $dir) {
90             if (!is_dir($dir)) {
91                 $this->fail($name . ' directory does not exist at ' . $dir);
92             } else if (!is_writable($dir)) {
93                 $this->fail($name . ' directory is not writable at ' . $dir);
94             }
95         }
96     }
97
98     public function checkGit()
99     {
100         $line = exec('git --version', $lines, $retval);
101         if ($retval !== 0) {
102             $this->fail('Running git executable failed.');
103         }
104         if (!preg_match('#^git version ([0-9.]+(rc[0-9]+)?)(?: \(Apple Git-\d+\))?$#', $line, $matches)) {
105             $this->fail('git version output format unexpected: ' . $line);
106             return;
107         }
108         if (version_compare($matches[1], '1.7.5') < 0) {
109             $this->fail(
110                 'git version needs to be at least 1.7.5, got: ' . $matches[1]
111             );
112         }
113     }
114
115     public function checkDatabase()
116     {
117         if ($this->elasticsearch == '') {
118             return;
119         }
120
121         $es = parse_url($this->elasticsearch);
122         if (!preg_match("#/.+/#", $es['path'], $matches)) {
123             $this->fail(
124                 'Improper elasticsearch url.  Elasticsearch requires a'
125                 . ' search domain to store your data.'
126                 . ' (e.g. http://localhost:9200/phorkie/)'
127             );
128         }
129         $dbs = new Database();
130         $dbs->getSetup()->setup();
131     }
132
133     public function checkMimeTypeDetection()
134     {
135         $rp = new Repository_Post();
136         $type = $rp->getType('<?php echo "foo"; ?>', true);
137         if ($type != 'php') {
138             $msg = 'MIME type detection fails';
139             if ($type instanceof \PEAR_Error) {
140                 $msg .= '. Error: ' . $type->getMessage();
141             }
142             $this->fail($msg);
143         }
144     }
145
146     public function checkRemoteForking()
147     {
148         if (!isset($GLOBALS['phorkie']['cfg']['git']['public'])
149             || $GLOBALS['phorkie']['cfg']['git']['public'] == ''
150         ) {
151             $this->fail(
152                 'No public git URL prefix configured.'
153                 . ' Remote forking will not work'
154             );
155         }
156     }
157
158     public function fail($msg)
159     {
160         $this->messages[] = array('error', $msg);
161     }
162
163     public function info($msg)
164     {
165         $this->messages[] = array('info', $msg);
166     }
167
168     public function ok($msg)
169     {
170         $this->messages[] = array('ok', $msg);
171     }
172 }
173
174 ?>