]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/install_cli.php
Merge branch 'group-members-pending' into 'nightly'
[quix0rs-gnu-social.git] / scripts / install_cli.php
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  * @author   Mikael Nordfeldth <mmn@hethane.se>
25  * @license  GNU Affero General Public License http://www.gnu.org/licenses/
26  * @version  1.1.x
27  * @link     http://status.net
28  */
29
30 if (php_sapi_name() !== 'cli') {
31     exit(1);
32 }
33
34 define('INSTALLDIR', dirname(dirname(__FILE__)));
35 set_include_path(get_include_path() . PATH_SEPARATOR . INSTALLDIR . '/extlib');
36
37 require_once INSTALLDIR . '/lib/installer.php';
38 require_once 'Console/Getopt.php';
39
40 class CliInstaller extends Installer
41 {
42     public $verbose = true;
43
44     /**
45      * Go for it!
46      * @return boolean success
47      */
48     function main()
49     {
50         if (!$this->checkPrereqs()) {
51             return false;
52         }
53         if ($this->prepare()) {
54             return $this->handle();
55        } else {
56             $this->showHelp();
57             return false;
58         }
59     }
60
61     /**
62      * Get our input parameters...
63      * @return boolean success
64      */
65     function prepare()
66     {
67         $shortoptions = 'qvh';
68         $longoptions = array('quiet', 'verbose', 'help', 'skip-config');
69         $map = array(
70             '-s'         => 'server',
71             '--server'   => 'server',
72             '-p'         => 'path',
73             '--path'     => 'path',
74             '--sitename' => 'sitename',
75             '--fancy'    => 'fancy',
76             '--ssl'      => 'ssl',
77
78             '--dbtype'   => 'dbtype',
79             '--host'     => 'host',
80             '--database' => 'database',
81             '--username' => 'username',
82             '--password' => 'password',
83
84             '--admin-nick' => 'adminNick',
85             '--admin-pass' => 'adminPass',
86             '--admin-email' => 'adminEmail',
87
88             '--site-profile' => 'siteProfile'
89         );
90         foreach ($map as $arg => $target) {
91             if (substr($arg, 0, 2) == '--') {
92                 $longoptions[] = substr($arg, 2) . '=';
93             } else {
94                 $shortoptions .= substr($arg, 1) . ':';
95             }
96         }
97
98         $parser = new Console_Getopt();
99         $result = $parser->getopt($_SERVER['argv'], $shortoptions, $longoptions);
100         if (PEAR::isError($result)) {
101             $this->warning($result->getMessage());
102             return false;
103         }
104         list($options, $args) = $result;
105
106         // defaults
107         $this->dbtype = 'mysql';
108         $this->verbose = true;
109         // ssl is defaulted in lib/installer.php
110
111         foreach ($options as $option) {
112             $arg = $option[0];
113             if (isset($map[$arg])) {
114                 $var = $map[$arg];
115                 $this->$var = $option[1];
116                 if ($arg == '--fancy') {
117                     $this->$var = ($option[1] != 'false') && ($option[1] != 'no');
118                 }
119             } else if ($arg == '--skip-config') {
120                 $this->skipConfig = true;
121             } else if ($arg == 'q' || $arg == '--quiet') {
122                 $this->verbose = false;
123             } else if ($arg == 'v' || $arg == '--verbose') {
124                 $this->verbose = true;
125             } else if ($arg == 'h' || $arg == '--help') {
126                 // will go back to show help
127                 return false;
128             }
129         }
130
131         $fail = false;
132         if (empty($this->server)) {
133             $this->updateStatus("You must specify a web server for the site.", true);
134             // path is optional though
135             $fail = true;
136         }
137
138         if (!$this->validateDb()) {
139             $fail = true;
140         }
141
142         if (!$this->validateAdmin()) {
143             $fail = true;
144         }
145
146         return !$fail;
147     }
148
149     function handle()
150     {
151         return $this->doInstall();
152     }
153
154     function showHelp()
155     {
156         echo <<<END_HELP
157 install_cli.php - StatusNet command-line installer
158
159     -s --server=<name>   Use <name> as server name (required)
160     -p --path=<path>     Use <path> as path name
161        --sitename        User-friendly site name (required)
162        --fancy           Whether to use fancy URLs (default no)
163        --ssl             Server SSL enabled (default never), 
164                          [never | sometimes | always]
165
166        --dbtype          'mysql' (default) or 'pgsql'
167        --host            Database hostname (required)
168        --database        Database/schema name (required)
169        --username        Database username (required)
170        --password        Database password (required)
171
172        --admin-nick      Administrator nickname (required)
173        --admin-pass      Initial password for admin user (required)
174        --admin-email     Initial email address for admin user
175        --admin-updates   'yes' (default) or 'no', whether to subscribe
176                          admin to update@status.net (default yes)
177        
178        --site-profile    site profile ['public', 'private' (default), 'community', 'singleuser']
179        
180        --skip-config     Don't write a config.php -- use with caution,
181                          requires a global configuration file.
182
183       General options:
184
185     -q --quiet           Quiet (little output)
186     -v --verbose         Verbose (lots of output)
187     -h --help            Show this message and quit.
188
189 END_HELP;
190     }
191
192     function warning($message, $submessage='')
193     {
194         print $this->html2text($message) . "\n";
195         if ($submessage != '') {
196             print "  " . $this->html2text($submessage) . "\n";
197         }
198         print "\n";
199     }
200
201     function updateStatus($status, $error=false)
202     {
203         if ($this->verbose || $error) {
204             if ($error) {
205                 print "ERROR: ";
206             }
207             print $this->html2text($status);
208             print "\n";
209         }
210     }
211
212     private function html2text($html)
213     {
214         // break out any links for text legibility
215         $breakout = preg_replace('/<a[^>+]\bhref="(.*)"[^>]*>(.*)<\/a>/',
216                                  '\2 &lt;\1&gt;',
217                                  $html);
218         return html_entity_decode(strip_tags($breakout), ENT_QUOTES, 'UTF-8');
219     }
220 }
221
222 $installer = new CliInstaller();
223 $ok = $installer->main();
224 exit($ok ? 0 : 1);