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