]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/install_cli.php
Merge 1.1.x into master
[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  * @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             '--site-profile' => 'siteProfile'
88         );
89         foreach ($map as $arg => $target) {
90             if (substr($arg, 0, 2) == '--') {
91                 $longoptions[] = substr($arg, 2) . '=';
92             } else {
93                 $shortoptions .= substr($arg, 1) . ':';
94             }
95         }
96
97         $parser = new Console_Getopt();
98         $result = $parser->getopt($_SERVER['argv'], $shortoptions, $longoptions);
99         if (PEAR::isError($result)) {
100             $this->warning($result->getMessage());
101             return false;
102         }
103         list($options, $args) = $result;
104
105         // defaults
106         $this->dbtype = 'mysql';
107         $this->adminUpdates = true;
108         $this->verbose = true;
109
110         foreach ($options as $option) {
111             $arg = $option[0];
112             if (isset($map[$arg])) {
113                 $var = $map[$arg];
114                 $this->$var = $option[1];
115                 if ($var == 'adminUpdates' || $arg == '--fancy') {
116                     $this->$var = ($option[1] != 'false') && ($option[1] != 'no');
117                 }
118             } else if ($arg == '--skip-config') {
119                 $this->skipConfig = true;
120             } else if ($arg == 'q' || $arg == '--quiet') {
121                 $this->verbose = false;
122             } else if ($arg == 'v' || $arg == '--verbose') {
123                 $this->verbose = true;
124             } else if ($arg == 'h' || $arg == '--help') {
125                 // will go back to show help
126                 return false;
127             }
128         }
129
130         $fail = false;
131         if (empty($this->server)) {
132             $this->updateStatus("You must specify a web server for the site.", true);
133             // path is optional though
134             $fail = true;
135         }
136
137         if (!$this->validateDb()) {
138             $fail = true;
139         }
140
141         if (!$this->validateAdmin()) {
142             $fail = true;
143         }
144
145         return !$fail;
146     }
147
148     function handle()
149     {
150         return $this->doInstall();
151     }
152
153     function showHelp()
154     {
155         echo <<<END_HELP
156 install_cli.php - StatusNet command-line installer
157
158     -s --server=<name>   Use <name> as server name (required)
159     -p --path=<path>     Use <path> as path name
160        --sitename        User-friendly site name (required)
161        --fancy           Whether to use fancy URLs (default no)
162
163        --dbtype          'mysql' (default) or 'pgsql'
164        --host            Database hostname (required)
165        --database        Database/schema name (required)
166        --username        Database username (required)
167        --password        Database password (required)
168
169        --admin-nick      Administrator nickname (required)
170        --admin-pass      Initial password for admin user (required)
171        --admin-email     Initial email address for admin user
172        --admin-updates   'yes' (default) or 'no', whether to subscribe
173                          admin to update@status.net (default yes)
174        
175        --site-profile    site profile ['public', 'private' (default), 'community', 'singleuser']
176        
177        --skip-config     Don't write a config.php -- use with caution,
178                          requires a global configuration file.
179
180       General options:
181
182     -q --quiet           Quiet (little output)
183     -v --verbose         Verbose (lots of output)
184     -h --help            Show this message and quit.
185
186 END_HELP;
187     }
188
189     function warning($message, $submessage='')
190     {
191         print $this->html2text($message) . "\n";
192         if ($submessage != '') {
193             print "  " . $this->html2text($submessage) . "\n";
194         }
195         print "\n";
196     }
197
198     function updateStatus($status, $error=false)
199     {
200         if ($this->verbose || $error) {
201             if ($error) {
202                 print "ERROR: ";
203             }
204             print $this->html2text($status);
205             print "\n";
206         }
207     }
208
209     private function html2text($html)
210     {
211         // break out any links for text legibility
212         $breakout = preg_replace('/<a[^>+]\bhref="(.*)"[^>]*>(.*)<\/a>/',
213                                  '\2 &lt;\1&gt;',
214                                  $html);
215         return html_entity_decode(strip_tags($breakout), ENT_QUOTES, 'UTF-8');
216     }
217 }
218
219 $installer = new CliInstaller();
220 $ok = $installer->main();
221 exit($ok ? 0 : 1);