tests for baseurl detection
[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' => $cfg['gitdir'],
28             'workdir' => $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
43         return $sc->messages;
44     }
45
46     public function checkConfigFiles()
47     {
48         foreach ($GLOBALS['phorkie']['cfgfiles'] as $file => $loaded) {
49             if ($loaded) {
50                 $this->ok('Loaded config file: ' . $file);
51             } else {
52                 $this->info('Possible config file: ' . $file . ' (not loaded)');
53             }
54         }
55     }
56
57     public function checkDeps()
58     {
59         foreach ($this->deps as $package => $class) {
60             if (!class_exists($class, true)) {
61                 $this->fail('PEAR package not installed: ' . $package);
62             }
63         }
64
65         if (!class_exists('geshi', true)) {
66             $geshi = stream_resolve_include_path(
67                 $GLOBALS['phorkie']['cfg']['geshi']
68             );
69             if ($geshi === false) {
70                 $this->fail('GeSHi not available');
71             }
72         }
73
74         if (!class_exists('\\Michelf\\Markdown', true)) {
75             //PEAR-installed version 1.0.2 has a different API
76             $markdown = stream_resolve_include_path('markdown.php');
77             if ($markdown === false) {
78                 $this->fail('Markdown renderer not available');
79             }
80         }
81     }
82
83     public function checkDirs()
84     {
85         foreach ($this->writableDirs as $name => $dir) {
86             if (!is_dir($dir)) {
87                 $this->fail($name . ' directory does not exist at ' . $dir);
88             } else if (!is_writable($dir)) {
89                 $this->fail($name . ' directory is not writable at ' . $dir);
90             }
91         }
92     }
93
94     public function checkGit()
95     {
96         $line = exec('git --version', $lines, $retval);
97         if ($retval !== 0) {
98             $this->fail('Running git executable failed.');
99         }
100         if (!preg_match('#^git version ([0-9.]+(rc[0-9]+)?)$#', $line, $matches)) {
101             $this->fail('git version output format unexpected: ' . $line);
102         }
103         if (version_compare($matches[1], '1.7.5') < 0) {
104             $this->fail(
105                 'git version needs to be at least 1.7.5, got: ' . $matches[1]
106             );
107         }
108     }
109
110     public function checkDatabase()
111     {
112         if ($this->elasticsearch == '') {
113             return;
114         }
115
116         $es = parse_url($this->elasticsearch);
117         if (!preg_match("#/.+/#", $es['path'], $matches)) {
118             $this->fail(
119                 'Improper elasticsearch url.  Elasticsearch requires a'
120                 . ' search domain to store your data.'
121                 . ' (e.g. http://localhost:9200/phorkie/)'
122             );
123         }
124         $dbs = new Database();
125         $dbs->getSetup()->setup();
126     }
127
128     public function checkMimeTypeDetection()
129     {
130         $rp = new Repository_Post();
131         if ($rp->getType('<?php echo "foo"; ?>') != 'php') {
132             $this->fail('MIME type detection fails');
133         }
134     }
135
136     public function fail($msg)
137     {
138         $this->messages[] = array('error', $msg);
139     }
140
141     public function info($msg)
142     {
143         $this->messages[] = array('info', $msg);
144     }
145
146     public function ok($msg)
147     {
148         $this->messages[] = array('ok', $msg);
149     }
150 }
151
152 ?>