Source for file Cli.php
Documentation is available at Cli.php
* Copyright (C) 2009 Mai (Me Artificial Intelligence)
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* @author Wouter Bulten (wouterbulten@mai-ai.org)
* @copyright Copyright (C) 2009 Mai (Me Artificial Intelligence)
* @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
* @link http://www.launchpad.net/mai
* Command line interface for Mai. (Mai Manager)
* @author Wouter Bulten (wouterbulten@mai-ai.org)
* @copyright Copyright (C) 2009 Mai (Me Artificial Intelligence)
* @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
* @link http://www.launchpad.net/mai
* Private constructor to prevent creation of this object.
* Starts up the bootstrap.
static public function run()
self::output("\n---\n Mai Command Line Interface \n\n");
self::output(" Copyright (C) 2009 Mai (Me Artificial Intelligence) \n");
self::output(" Licensed under the GNU General Public License version 3 (GPLv3) \n");
self::output("This CLI can install, update or change your Mai installation. \n");
self::output("Please proceed with caution! \n");
self::output("\nWhat do you want to do?\n\n");
self::output(" [1] Install \n");
self::output(" [2] Backup database \n");
self::output(" [q] Exit \n\n");
self::output("Option: ");
while(!in_array($option, array(1,2, 'q', '[1]', '[2]', '[q]')));
self::output("\n\nCLI finished.\n\n");
self::output("\n\nCLI closed.");
//Ask to exit the manager
self::output("\n\nDo you want to exit the manager? (y/n) ");
* Ask the user to confirm.
static private function askToConfirm($message = "\n\nDo you wish to continue? (y\n) ")
//Loop until the answer is 'y' or 'n'
while($continue != 'n' && $continue != 'y');
* Ask the user to continue
//Loop until the answer is 'y' or 'n'
fwrite(STDOUT, "\n\nDo you wish to continue? (y/n) ");
fwrite(STDOUT, "\n" . "Exiting manager..." . "\n");
while($continue != 'n' && $continue != 'y');
static private function output($message)
* Look for a loaded database extension.
self::output("FrontBase: OK\n");
self::output("FrontBase: Not found!\n");
self::output("InterBase / Firebird: OK\n");
self::output("InterBase / Firebird: Not found!\n");
self::output("Microsoft SQL Server: OK\n");
self::output("Microsoft SQL Server: Not found!\n");
self::output("MySQL: OK\n");
self::output("MySQL: Not found!\n");
self::output("MySQLi: OK\n");
self::output("MySQLi: Not found!\n");
self::output("Oracle: OK\n");
self::output("Oracle: Not found!\n");
self::output("PostgreSQL: OK\n");
self::output("PostgreSQL: Not found!\n");
self::output("QuerySim: OK\n");
self::output("QuerySim: Not found!\n");
self::output("SQLite 2: OK\n");
self::output("SQLite 2: Not found!\n");
* Create a config file for Mai
//Ask wich db extension to use
self::output("\nMai creates 2 databases. Wich extension should be used? (e.g. mysql or psql) ");
$config['db']['db_user']['dbtype'] = $extension;
$config['db']['db_user']['connection_name'] = 'mai_userdb_connection';
$config['db']['db_base']['dbtype'] = $extension;
$config['db']['db_base']['connection_name'] = 'mai_basedb_connection';
while(!in_array($extension, array('mysql', 'fbsql', 'ibase', 'mssql', 'mysqli', 'oci', 'pgsql', 'querysim', 'sqlite')));
self::output("\nWhat host should be used? (default is localhost) ");
$config['db']['db_user']['host'] = $host;
$config['db']['db_base']['host'] = $host;
self::output("\nWhat databse name should be used for the base database? (default is mai_base) ");
$config['db']['db_base']['dbname'] = trim(fgets(STDIN));
self::output("\nWhat databse name should be used for the user database? (default is mai_user) ");
$config['db']['db_user']['dbname'] = trim(fgets(STDIN));
//Ask for user and password (base)
self::output("\nA database user is needed with rights to create the base database.");
self::output("\nPlease create a database named ". $config['db']['db_base']['dbname']. "and a user with rights on it.");
self::output("\nAlternatively you can create a user wich has the right to create a database.");
self::output("\nWhat is the username of the user for the BASE db? ");
$config['db']['db_base']['username'] = $host = trim(fgets(STDIN));
self::output("\nWhat is the password of the user for the BASE db? ");
$config['db']['db_base']['password'] = $host = trim(fgets(STDIN));
//Ask for user and password (user)
self::output("\nA database user is needed with rights to create the user database.");
self::output("\nPlease create a database named ". $config['db']['db_user']['dbname']. "and a user with rights on it.");
self::output("\nAlternatively you can create a user wich has the right to create a database.");
self::output("\nWhat is the username of the user for the USER db? ");
$config['db']['db_user']['username'] = $host = trim(fgets(STDIN));;
self::output("\nWhat is the password of the user for the USER db? ");
$config['db']['db_user']['password'] = $host = trim(fgets(STDIN));
self::output("\nThe config file will now be created. These values can be changed by editing the config file:");
self::output("\n" . APPLICATION_PATH . "\config\config.ini");
$config['global']['application']['development'] = 1;
//Open the ini file to write
$iniFile = fopen(APPLICATION_PATH . "/config/config.ini", "w");
//Try to write to the file
fwrite($iniFile, ";Config file created by Mai Manager at .". date("d-m-Y H:i"). "\n\n");
self::output("\n\nWARNING: " . APPLICATION_PATH . "/config/config.ini is not writable!");
self::output("\nPlease make this file writable.");
while($success == false);
foreach($config as $section => $sectionContent)
fwrite($iniFile, "[$section]\n");
foreach($sectionContent as $head => $sub)
//Create name of the variable (head.sub = value)
foreach($sub as $name => $value)
fwrite($iniFile, "$head.$name = $value\n");
* Test the connection with the base db.
$config->db_base->dbtype,
$config->db_base->username,
$config->db_base->password,
$config->db_base->dbname);
$conn = Doctrine_Manager::connection($dsn, $config->db_base->connection_name);
$conn->beginTransaction();
Zend_Registry::set('conn_dbbase', $conn);
catch (Doctrine_Connection_Exception $e)
* Test the connection with the user db.
$config->db_user->dbtype,
$config->db_user->username,
$config->db_user->password,
$config->db_user->dbname);
$conn = Doctrine_Manager::connection($dsn, $config->db_user->connection_name);
$conn->beginTransaction();
Zend_Registry::set('conn_dbuser', $conn);
catch (Doctrine_Connection_Exception $e)
self::output("\nChecking requirements.. ");
if(!self::testPhpVersion())
self::output("\n\nPHP 5.2.4 or higher is recommended! Your version is ". PHP_VERSION.
"\nMai could run but this is not tested. No support is given!");
self::output("\nPHP: OK (". PHP_VERSION. ") \n");
if(!self::testDatabaseDrivers())
self::output("\n\nNo database extension is found. Please enable or install an extension.");
self::output("\nMai cannot run without an database! Installation aborted.");
self::output("\nValid database extension found.");
self::output("\nCreating new config..");
//Ask to create a custom config file
if(self::askToConfirm("\n\nDo you want to configure Mai? (recommended) (y/n) "))
self::output("\nBootstrapping started..");
self::output("\nBootstrapping completed.");
self::output("\nTesting database connection: base");
//Start base db connection
self::output("\n\nWARNING: Failed to connect to the base db.");
self::output("\nYou can configure Mai yourself and update the database using this installer.");
self::output("\nAlternatively you can reconfigure Mai.");
if(self::askToConfirm("\n\nDo you want to reconfigure Mai? (recommended) (y/n) "))
if(!self::askToConfirm("\n\nDo you want to continue with the installation? (y/n) "))
self::output("Installation cancelled.");
self::output("\nBase Database connection: OK");
//Doctrine Command Line interface
if(!self::askToConfirm("\nDoes the base database already excist? (y/n) "))
$cli->run(array('mai-manager', 'create-db basedb'));
$cli->run(array('mai-manager', 'generate-sql basedb'));
$cli->run(array('mai-manager', 'create-tables basedb'));
self::output("\nTesting database connection: user");
//Start user db connection
self::output("\n\nWARNING: Failed to connect to the user db.");
self::output("\nYou can configure Mai yourself and update the database using this installer.");
self::output("\nAlternatively you can reconfigure Mai.");
if(self::askToConfirm("\n\nDo you want to reconfigure Mai? (recommended) (y/n) "))
if(!self::askToConfirm("\n\nDo you want to continue with the installation? (y/n) "))
self::output("Installation cancelled.");
self::output("\nUser Database connection: OK");
//Doctrine Command Line interface
if(!self::askToConfirm("\nDoes the user databse already excist? (y/n) "))
$cli->run(array('mai-manager', 'create-db'));
$cli->run(array('mai-manager', 'generate-sql'));
$cli->run(array('mai-manager', 'create-tables'));
if(self::askToConfirm("\nDo you want to test your Mai installation? (cURL extension required) (y/n) "))
//Use the URL to test Mai
self::output("\nWhat is the url of your Mai installation? (default is http://localhost) ");
//The TalkController should output invalid-session
if($result == 'invalid-session')
self::output("\nTest: OK");
self::output("\nTest failed. Response:\n\n". $result);
self::output("\nTest failed, cURL is not activated.");
self::output("\n\n\nMai installation finished! Enjoy!");
* Backup the Mai database
//@Mai Todo: Backup function not yet finished.
self::output("\n\nMai Manager is not yet able to backup your database.");
|