1
#!/usr/bin/env php
2
<?php
3
/**
4
 * StatusNet - the distributed open-source microblogging tool
5
 * Copyright (C) 2010, StatusNet, Inc.
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * @category Installation
21
 * @package  Installation
22
 *
23
 * @author   Brion Vibber <brion@status.net>
24
 * @license  GNU Affero General Public License http://www.gnu.org/licenses/
25
 * @version  0.9.x
26
 * @link     http://status.net
27
 */
28
29
if (php_sapi_name() !== 'cli') {
30
    exit(1);
31
}
32
33
define('INSTALLDIR', dirname(dirname(__FILE__)));
34
set_include_path(get_include_path() . PATH_SEPARATOR . INSTALLDIR . '/extlib');
35
36
require_once INSTALLDIR . '/lib/installer.php';
37
require_once 'Console/Getopt.php';
38
39
class CliInstaller extends Installer
40
{
41
    public $verbose = true;
42
43
    /**
44
     * Go for it!
45
     * @return boolean success
46
     */
47
    function main()
48
    {
49
        if (!$this->checkPrereqs()) {
50
            return false;
51
        }
52
        if ($this->prepare()) {
53
            return $this->handle();
54
       } else {
55
            $this->showHelp();
56
            return false;
57
        }
58
    }
59
60
    /**
61
     * Get our input parameters...
62
     * @return boolean success
63
     */
64
    function prepare()
65
    {
66
        $shortoptions = 'qvh';
67
        $longoptions = array('quiet', 'verbose', 'help', 'skip-config');
68
        $map = array(
69
            '-s'         => 'server',
70
            '--server'   => 'server',
71
            '-p'         => 'path',
72
            '--path'     => 'path',
73
            '--sitename' => 'sitename',
74
            '--fancy'    => 'fancy',
75
76
            '--dbtype'   => 'dbtype',
77
            '--host'     => 'host',
78
            '--database' => 'database',
79
            '--username' => 'username',
80
            '--password' => 'password',
81
82
            '--admin-nick' => 'adminNick',
83
            '--admin-pass' => 'adminPass',
84
            '--admin-email' => 'adminEmail',
85
            '--admin-updates' => 'adminUpdates'
86
        );
87
        foreach ($map as $arg => $target) {
88
            if (substr($arg, 0, 2) == '--') {
89
                $longoptions[] = substr($arg, 2) . '=';
90
            } else {
91
                $shortoptions .= substr($arg, 1) . ':';
92
            }
93
        }
94
95
        $parser = new Console_Getopt();
96
        $result = $parser->getopt($_SERVER['argv'], $shortoptions, $longoptions);
97
        if (PEAR::isError($result)) {
98
            $this->warning($result->getMessage());
99
            return false;
100
        }
101
        list($options, $args) = $result;
102
103
        // defaults
104
        $this->dbtype = 'mysql';
105
        $this->adminUpdates = true;
106
        $this->verbose = true;
107
108
        foreach ($options as $option) {
109
            $arg = $option[0];
110
            if (isset($map[$arg])) {
111
                $var = $map[$arg];
112
                $this->$var = $option[1];
113
                if ($var == 'adminUpdates' || $arg == '--fancy') {
114
                    $this->$var = ($option[1] != 'false') && ($option[1] != 'no');
115
                }
116
            } else if ($arg == '--skip-config') {
117
                $this->skipConfig = true;
118
            } else if ($arg == 'q' || $arg == '--quiet') {
119
                $this->verbose = false;
120
            } else if ($arg == 'v' || $arg == '--verbose') {
121
                $this->verbose = true;
122
            } else if ($arg == 'h' || $arg == '--help') {
123
                // will go back to show help
124
                return false;
125
            }
126
        }
127
128
        $fail = false;
129
        if (empty($this->server)) {
130
            $this->updateStatus("You must specify a web server for the site.", true);
131
            // path is optional though
132
            $fail = true;
133
        }
134
135
        if (!$this->validateDb()) {
136
            $fail = true;
137
        }
138
139
        if (!$this->validateAdmin()) {
140
            $fail = true;
141
        }
142
143
        return !$fail;
144
    }
145
146
    function handle()
147
    {
148
        return $this->doInstall();
149
    }
150
151
    function showHelp()
152
    {
153
        echo <<<END_HELP
154
install_cli.php - StatusNet command-line installer
155
156
    -s --server=<name>   Use <name> as server name (required)
157
    -p --path=<path>     Use <path> as path name
158
       --sitename        User-friendly site name (required)
159
       --fancy           Whether to use fancy URLs (default no)
160
161
       --dbtype          'mysql' (default) or 'pgsql'
162
       --host            Database hostname (required)
163
       --database        Database/schema name (required)
164
       --username        Database username (required)
165
       --password        Database password (required)
166
167
       --admin-nick      Administrator nickname (required)
168
       --admin-pass      Initial password for admin user (required)
169
       --admin-email     Initial email address for admin user
170
       --admin-updates   'yes' (default) or 'no', whether to subscribe
171
                         admin to update@status.net (default yes)
172
       
173
       --skip-config     Don't write a config.php -- use with caution,
174
                         requires a global configuration file.
175
176
      General options:
177
178
    -q --quiet           Quiet (little output)
179
    -v --verbose         Verbose (lots of output)
180
    -h --help            Show this message and quit.
181
182
END_HELP;
183
    }
184
185
    function warning($message, $submessage='')
186
    {
187
        print $this->html2text($message) . "\n";
188
        if ($submessage != '') {
189
            print "  " . $this->html2text($submessage) . "\n";
190
        }
191
        print "\n";
192
    }
193
194
    function updateStatus($status, $error=false)
195
    {
196
        if ($this->verbose || $error) {
197
            if ($error) {
198
                print "ERROR: ";
199
            }
200
            print $this->html2text($status);
201
            print "\n";
202
        }
203
    }
204
205
    private function html2text($html)
206
    {
207
        // break out any links for text legibility
208
        $breakout = preg_replace('/<a[^>+]\bhref="(.*)"[^>]*>(.*)<\/a>/',
209
                                 '\2 &lt;\1&gt;',
210
                                 $html);
211
        return html_entity_decode(strip_tags($breakout), ENT_QUOTES, 'UTF-8');
212
    }
213
}
214
215
$installer = new CliInstaller();
216
$ok = $installer->main();
217
exit($ok ? 0 : 1);