PHP: PHP 7.3.21 (cli) (built: Aug 12 2020 13:01:15) ( NTS )
Joomla: 3.9.20 (I think)
I am trying to copy data from a legacy system (not Joomla) and create users, groups, forum posts, articles etc. via code from the command line.
I have tried to follow some of the examples in the cli directory, but am running into the following problem.
I create a JUser object. This works.
I then try to get the user id for the Super User via JUserHelper()::getUserId('Super User');
I always get the following message on my terminal
php73 test.php
Test CLI
--------
With JUserHelper
Error: Failed to start application: Call to undefined function JUserHelper()
Here is the full code that I am using
Code: Select all
<?php
const _JEXEC = 1;
if (file_exists(dirname(__DIR__) . '/defines.php'))
{
require_once dirname(__DIR__) . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', dirname(__DIR__));
require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_BASE . '/includes/framework.php';
require_once JPATH_LIBRARIES . '/import.legacy.php';
require_once JPATH_LIBRARIES . '/cms.php';
require_once JPATH_CONFIGURATION . '/configuration.php';
use Joomla\CMS\User\User;
use Joomla\CMS\User\UserHelper;
require_once (JPATH_LIBRARIES . '/src/User/UserHelper.php');
error_reporting(E_ALL);
ini_set('display_errors', 1);
$lang = JFactory::getLanguage();
$lang->load('files_joomla.sys', JPATH_SITE, null, false, false)
|| $lang->load('files_joomla.sys', JPATH_SITE, null, true);
class TestCli extends JApplicationCli
{
public function doExecute()
{
// https://issues.joomla.org/tracker/joomla-cms/9660#event-146170 says to assigns $this to JFactory::$application. However this just gives another error -Call to undefined method TestCli::getTemplate()
// JFactory::$application = $this;
// $_SERVER['HTTP_HOST'] = 'world.jrlp02';
// $app = JFactory::getApplication('administrator');
// Import the dependencies
// $component = JComponentHelper::getComponent('com_installer');
$this->out('Test CLI');
$this->out('--------');
/*
$this->out('With JUser', true);
$user = new JUser(0);
$str = '***user object***' . PHP_EOL . var_export($user, true) . PHP_EOL . '========';
$this->out($str, true);
*/
$this->out('With JUserHelper', true);
$h = JUserHelper()::getUserId('Super User');
$str = '***UH::User Id for super user***' . PHP_EOL . var_export($h, true) . PHP_EOL . '========';
$this->out($str, true);
/*
$this->out('With JsnHelper', true);
$ep = JsnHelper::getUser();
echo 'Easy Profile', PHP_EOL;
$str = '***JSN::getUSer***' . PHP_EOL . var_export($ep, true) . PHP_EOL . '========';
$this->out($str, true);
*/
}
}
// run the application.
JApplicationCli::getInstance('TestCli')->execute();
Note that as I progress, I will eventually install EasyProfile,, Kunena Forums and other extensions, so I will need to be able to call the relevant functions from those extensions as well.
Thanks