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