Using the Joomla framework via command line
Joomla is not only the CMS we're used to see when creating and editing a web site.
Joomla is also the underlying frameworkon the top of which the Joomla CMS is build: in fact, we have two distinct Google Group talking about these 2 different topics: CMS Development and Framework Development.
We can use the classes and functions we do use when creating Joomla extensions, running our standalone programs via cron jobs or command line.
To do that, we've to initialize the framework using the code I've attached - adding the application code in the execute() function.
I've tried to access the configuration data but the db access configuration is not loaded correctly so I've had to insert it manually. If someone knows a workaround for this, (s)he's welcome
So, copy this code and create a file in the Joomla root directory, and use php from the command line to start using the Joomla framework!
#!/usr/bin/php < ?php /** * @version $Id$ * @package Joomla.Example * @subpackage CLI * @copyright Copyright (C) 2008 - 2009 Louis Landry. All rights reserved. * @license GNU General Public License, ; */ // We are a valid Joomla entry point. define('_JEXEC', 1); // Setup the path related constants. define('DS', DIRECTORY_SEPARATOR); define('JPATH_BASE', dirname( __FILE__ )); define('JPATH_ROOT', JPATH_BASE); define('JPATH_CONFIGURATION', JPATH_BASE); define('JPATH_LIBRARIES', JPATH_BASE.DS.'libraries'); define('JPATH_METHODS', JPATH_ROOT.DS.'methods'); // Load the library importer. require_once (JPATH_LIBRARIES.'/joomla/import.php'); require_once (JPATH_CONFIGURATION.'/configuration.php'); // Import library dependencies. jimport('joomla.application.application'); jimport('joomla.utilities.utility'); jimport('joomla.language.language'); jimport('joomla.utilities.string'); jimport('joomla.factory'); /** * Simple command line interface application class for quick bootstrapping * things to test. * * @package Joomla.Example * @subpackage CLI * @version 1.0 */ class JApplicationCLI extends JApplication { /** * The name of the application * * @var array */ public $_name = 'JApplicationCLI'; /** * The client identifier. * * @var integer * @since 1.0 */ public $_clientId = 1000; /** * The current working directory of the application. * * @var string * @since 1.0 */ private $cwd = null; /** * The application argument values. * * @var array * @since 1.0 */ private $argv = array (); /** * The application argument options. * * The options are populated automatically by the private _initializeOptions * function * which sets each command line argument as a member of the options object. * * @var object * @since 1.0 */ private $options = null; private $shortargs = 'i:o:hflp'; // Need to wait for PHP 5.3 private $longargs = array ('help'); public function __construct() { // Initialize the execution arguments. $this->_initializeOptions(); // If the help screen has been requested, print it and exit. if ( isset ($this->options->h)) { $this->help(); exit (0); } // Get the current directory. $this->cwd = getcwd(); } public function execute() { /* * db access variables */ $conf =& JFactory::getConfig(); $conf->setValue('config.user', 'dbuser'); $conf->setValue('config.password', 'dbpass'); $conf->setValue('config.db', 'dbname'); $conf->setValue('config.host', 'mysql.dbhost.com'); /* * let's start our code */ $db =& JFactory::getDBO(); } /** * Method to print a line of text to stdout. * * @param string The line of text to print to stdout. * @return void * @since 1.0 */ function out($text = '') { echo "\n".$text; } /** * Method to build and print the help screen text to stdout. * * @return void * @since 1.0 */ protected function help() { // Initialize variables. $help = array (); // Build the help screen information. $help[] = 'Your Help Screen'; $help[] = ''; $help[] = 'Here is where you put all your help screen information.'; $help[] = ''; // Print out the help information. echo implode("\n", $help); } /** * Initialize the command line options and arguments. * * @return void * @since 1.0 */ private function _initializeOptions() { // Get the options from the command line argument list and argument values. $opts = getopt($this->shortargs /*, $this->longargs*/); @$args = (array)$GLOBALS['argv']; // If the argument value list is not empty, make sure the options are unset. if (! empty($args)) { // Iterate over the found options. foreach ($opts as $o=>$a) { // Search for occurrences of the option with no value or with no space betweenoption and value. while ($k = array_search('-'.$o.$a, $args)) { // Remove any found options from the argument value array. if ($k) { unset ($args[$k]); } } // Search for remaining occurrences of the option (space between option and value). while ($k = array_search('-'.$o, $args)) { // Remove any found options and values from the argument value array. if ($k) { unset ($args[$k]); unset ($args[$k+1]); } } } } // Set the options and argument values to internal members. $this->options = (object)$opts; $this->argv = (array)$args; } } // Set error handling. JError::setErrorHandling(E_ALL, 'echo'); // Create the application object. $app = new JApplicationCLI(); // Execute the application. $app->execute();
April 30th, 2009 at 14:51
Thanks for the code, I would try to configure it myself. I hope its okay to come back here if I’ll miss something. Thanks a lot!
May 14th, 2009 at 14:49
Not unlike what Louis describes in this thread: http://groups.google.com/group/joomlabugsquad/browse_thread/thread/bb403d30684b0d8f
May 14th, 2009 at 19:25
Hi Joseph, yes I took it from there – I thought I said that in the post but in fact I forgot it
September 25th, 2009 at 12:56
Blimey! This is what I’ve been looking for!
I really appreciate that you put it in open space!
Absolutely great article.
I tried this code on Windows + PHP 5.16 and run perfectly, although I had to comment some lines out. It took only less than five minites.
Thanks a lot!
October 17th, 2009 at 02:48
Hello
This looks like exactly what I am looking for! Would you be able to help me with the arguments to use the Joomla installer?
October 21st, 2009 at 08:40
[...] Using the Joomla framework via command line [...]
December 16th, 2009 at 13:31
To get all the configuration parameters in place, instead of setting them manually as in the database, I would do something like this.
$joomla_config = new JConfig();
$conf =& JFactory::getConfig();
$conf->loadObject($joomla_config);
you can check if it worked just doing a:
print_r($conf);



7 Comments for the post “Using the Joomla framework via command line”