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