557d85c5a3d002c0ec04f1cdc110c625d47c4e98
[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]+)?)$#', $line, $matches)) {
105             $this->fail('git version output format unexpected: ' . $line);
106         }
107         if (version_compare($matches[1], '1.7.5') < 0) {
108             $this->fail(
109                 'git version needs to be at least 1.7.5, got: ' . $matches[1]
110             );
111         }
112     }
113
114     public function checkDatabase()
115     {
116         if ($this->elasticsearch == '') {
117             return;
118         }
119
120         $es = parse_url($this->elasticsearch);
121         if (!preg_match("#/.+/#", $es['path'], $matches)) {
122             $this->fail(
123                 'Improper elasticsearch url.  Elasticsearch requires a'
124                 . ' search domain to store your data.'
125                 . ' (e.g. http://localhost:9200/phorkie/)'
126             );
127         }
128         $dbs = new Database();
129         $dbs->getSetup()->setup();
130     }
131
132     public function checkMimeTypeDetection()
133     {
134         $rp = new Repository_Post();
135         if ($rp->getType('<?php echo "foo"; ?>') != 'php') {
136             $this->fail('MIME type detection fails');
137         }
138     }
139
140     public function checkRemoteForking()
141     {
142         if (!isset($GLOBALS['phorkie']['cfg']['git']['public'])
143             || $GLOBALS['phorkie']['cfg']['git']['public'] == ''
144         ) {
145             $this->fail(
146                 'No public git URL prefix configured.'
147                 . ' Remote forking will not work'
148             );
149         }
150     }
151
152     public function fail($msg)
153     {
154         $this->messages[] = array('error', $msg);
155     }
156
157     public function info($msg)
158     {
159         $this->messages[] = array('info', $msg);
160     }
161
162     public function ok($msg)
163     {
164         $this->messages[] = array('ok', $msg);
165     }
166 }
167
168 ?>