MDL-23590 new installer for Penny :-)
[moodle.git] / admin / cli / install_database.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * This installs Moodle into empty database, config.php must already exist.
20  *
21  * This script is intended for advanced usage such as in Debian packages.
22  * - sudo to www-data (apache acount) before
23  * - not compatible with Windows platform
24  *
25  * @package    core
26  * @subpackage cli
27  * @copyright  2010 Petr Skoda (http://skodak.org)
28  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29  */
31 if (isset($_SERVER['REMOTE_ADDR'])) {
32     error_log("admin/cli/install_database.php can not be called from web server!");
33     exit;
34 }
36 $help =
37 "Advanced command line Moodle database installer.
38 Please note you must execute this script with the same uid as apache.
40 Site defaults may be changed via local/defaults.php.
42 Options:
43 --lang=CODE           Installation and default site language. Default is en.
44 --adminuser=USERNAME  Username for the moodle admin account. Default is admin.
45 --adminpass=PASSWORD  Password for the moodle admin account.
46 --agree-license       Indicates agreement with software license.
47 -h, --help            Print out this help
49 Example: \$sudo -u wwwrun /usr/bin/php admin/cli/install_database.php --lang=cs --adminpass=soMePass123 --agree-license
50 ";
52 // Check that PHP is of a sufficient version
53 if (version_compare(phpversion(), "5.2.8") < 0) {
54     $phpversion = phpversion();
55     // do NOT localise - lang strings would not work here and we CAN NOT move it after installib
56     fwrite(STDERR, "Sorry, Moodle 2.0 requires PHP 5.2.8 or later (currently using version $phpversion).\n");
57     fwrite(STDERR, "Please upgrade your server software or install latest Moodle 1.9.x instead.\n");
58     die(1);
59 }
61 // Nothing to do if config.php does not exist
62 $configfile = dirname(dirname(dirname(__FILE__))).'/config.php';
63 if (!file_exists($configfile)) {
64     fwrite(STDERR, 'config.php does not exist, can not continue'); // do not localize
65     fwrite(STDERR, "\n");
66     die(1);
67 }
69 // Include necessary libs
70 require($configfile);
72 require_once($CFG->libdir.'/clilib.php');
73 require_once($CFG->libdir.'/installlib.php');
74 require_once($CFG->libdir.'/adminlib.php');
75 require_once($CFG->libdir.'/componentlib.class.php');
77 // make sure no tables are installed yet
78 if ($DB->get_tables() ) {
79     cli_error(get_string('clitablesexist', 'install'));
80 }
82 $CFG->early_install_lang = true;
83 get_string_manager(true);
85 @raise_memory_limit('128M');
87 // now get cli options
88 list($options, $unrecognized) = cli_get_params(
89     array(
90         'lang'              => 'en',
91         'adminuser'         => 'admin',
92         'adminpass'         => '',
93         'agree-license'     => false,
94         'help'              => false
95     ),
96     array(
97         'h' => 'help'
98     )
99 );
102 if ($options['help']) {
103     echo $help;
104     die;
107 if (!$options['agree-license']) {
108     cli_error('You have to agree to the license. --help prints out the help'); // TODO: localize
111 if ($options['adminpass'] === true or $options['adminpass'] === '') {
112     cli_error('You have to specify admin password. --help prints out the help'); // TODO: localize
115 $options['lang'] = clean_param($options['lang'], PARAM_SAFEDIR);
116 if (!file_exists($CFG->dirroot.'/install/lang/'.$options['lang'])) {
117     $options['lang'] = 'en';
119 $CFG->lang = $options['lang'];
121 //download lang pack with optional notification
122 if ($CFG->lang != 'en') {
123     make_upload_directory('lang', false);
124     if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $CFG->lang.'.zip', 'languages.md5', 'lang')) {
125         if ($cd->install() == COMPONENT_ERROR) {
126             if ($cd->get_error() == 'remotedownloaderror') {
127                 $a = new stdClass();
128                 $a->url  = 'http://download.moodle.org/langpack/2.0/'.$CFG->lang.'.zip';
129                 $a->dest = $CFG->dataroot.'/lang';
130                 cli_problem(get_string($cd->get_error(), 'error', $a));
131             } else {
132                 cli_problem(get_string($cd->get_error(), 'error'));
133             }
134         } else {
135             // install parent lang if defined
136             if ($parentlang = get_parent_language()) {
137                 if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $parentlang.'.zip', 'languages.md5', 'lang')) {
138                     $cd->install();
139                 }
140             }
141         }
142     }
145 // switch the string_manager instance to stop using install/lang/
146 $CFG->early_install_lang = false;
147 get_string_manager(true);
150 install_cli_database($options, true);
152 echo get_string('cliinstallfinished', 'install')."\n";
153 exit(0); // 0 means success