JApplicationCli JUserHelper Topic is solved

General questions relating to Joomla! 3.x.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10
Locked
swdev1
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Fri Aug 21, 2020 12:00 pm

JApplicationCli JUserHelper

Post by swdev1 » Fri Aug 21, 2020 12:17 pm

OS: Linux jrlp02 5.8.1-arch1-1 #1 SMP PREEMPT Wed, 12 Aug 2020 18:50:43 +0000 x86_64 GNU/Linux
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();
Any help, pointers in the right direction will be useful.
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

swdev1
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Fri Aug 21, 2020 12:00 pm

Re: JApplicationCli JUserHelper

Post by swdev1 » Fri Aug 21, 2020 12:33 pm

Typical, a few minutes after I posted, I saw an error. I was using

Code: Select all

JUserHelper()::getUserId('Super User');
I removed the brackets (), and tried

Code: Select all

JUserHelper::getProfile(723);
723 is the user id for Super User according to the #__users table

I now get the following error

php73 test.php
Test CLI
--------
With JUserHelper
Error: Failed to start application: Failed to start application

swdev1
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Fri Aug 21, 2020 12:00 pm

Re: JApplicationCli JUserHelper

Post by swdev1 » Fri Aug 21, 2020 12:43 pm

Oh man

I tried adding the following code to the start of the doExecute() method

Code: Select all

JFactory::$application = $this;
as per this forum post (https://issues.joomla.org/tracker/jooml ... ent-146170), and I started getting back some data.
So, it seems, I have solved the problem for the time being.

User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 23791
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, Netherlands
Contact:

Re: JApplicationCli JUserHelper

Post by pe7er » Fri Aug 21, 2020 3:35 pm

Good to see that you've solved it yourself. Thanks for sharing your solution.

btw: since Joomla 3.8 (and PHP 7.x) you can use Name Spacing.
instead of using

Code: Select all

JFactory::getApplication()
you can use the more modern

Code: Select all

// at the top of the file
use Joomla\CMS\Factory;

// in your code:
Factory::getApplication()
Kind Regards,
Peter Martin, Global Moderator
https://db8.nl - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com

swdev1
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Fri Aug 21, 2020 12:00 pm

Re: JApplicationCli JUserHelper

Post by swdev1 » Sat Aug 22, 2020 4:35 am

Thanks for the tip, Peter.
I guess it is be better to use namespaces.


Locked

Return to “General Questions/New to Joomla! 3.x”