Deprecated messages from Joomla 3 script in Joomla 4 Topic is solved

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
colinj
Joomla! Apprentice
Joomla! Apprentice
Posts: 38
Joined: Mon Dec 28, 2009 6:49 am

Deprecated messages from Joomla 3 script in Joomla 4

Post by colinj » Wed May 31, 2023 1:32 am

Hi,

I am not very familiar with PHP scripting, but I hope you can help me.

I had some scripts written for me a while ago when my site was Joomla 3. Having just upgraded, they are not working in Joomla 4, the error is showing that they are "deprecated".

The head for each PHP file is:

define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__) . '/'));
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';

$app = JFactory::getApplication('site');
$db = JFactory::getDbo();

What changes would be needed to work with Joomla 4? (Versions: Joomla 4.3.1, PHP 8.1).
Last edited by toivo on Wed May 31, 2023 3:53 am, edited 1 time in total.
Reason: mod note: retitled

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17443
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Deprecated messages from Joomla 3 script in Joomla 4

Post by toivo » Wed May 31, 2023 3:51 am

Here is an example, tested in Joomla 4.3.2 and PHP 8.2.3. Runs from a subfolder, accessed from a browser or the command prompt.

Code: Select all

<?php
// run from a subfolder under the main joomla folder
// lists the PHP version and number of users in the users table
define('_JEXEC', 1);    
use Joomla\CMS\Factory;
define('JPATH_BASE', realpath(dirname(__FILE__) . '/..'));
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);
$crlf		= PHP_EOL;
$app	= new Joomla\CMS\Application\SiteApplication();
\Joomla\CMS\Factory::$application = $app;
// database connection
$db     = Factory::getDbo();
$query = $db->getQuery(true);
$query
   ->select('COUNT(*)')
   ->from($db->quoteName('#__users'));
$db->setQuery($query);
$count   = $db->loadResult();
echo 'PHP version ' . phpversion() . $crlf;
echo $count . ' users' . $crlf;
Toivo Talikka, Global Moderator

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

Re: Deprecated messages from Joomla 3 script in Joomla 4

Post by pe7er » Wed May 31, 2023 6:33 am

The following line works in J4 but it's deprecated:
$db = Factory::getDbo();

You can replace it with:
$db = Factory::getContainer()->get('DatabaseDriver');
Kind Regards,
Peter Martin, Global Moderator
Company website: https://db8.nl/en/ - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com

colinj
Joomla! Apprentice
Joomla! Apprentice
Posts: 38
Joined: Mon Dec 28, 2009 6:49 am

Re: Deprecated messages from Joomla 3 script in Joomla 4

Post by colinj » Thu Jun 01, 2023 10:31 am

So should this work?

<?php
define('_JEXEC', 1);
use Joomla\CMS\Factory;
define('JPATH_BASE', realpath(dirname(__FILE__) . '/..'));
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';

$app = new Joomla\CMS\Application\SiteApplication();
\Joomla\CMS\Factory::$application = $app;
$db = Factory::getContainer()->get('DatabaseDriver');

... code follows

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17443
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Deprecated messages from Joomla 3 script in Joomla 4

Post by toivo » Thu Jun 01, 2023 10:56 am

Yes, it should work and it does. Just tested it in PHP 8.2.6.
Toivo Talikka, Global Moderator

colinj
Joomla! Apprentice
Joomla! Apprentice
Posts: 38
Joined: Mon Dec 28, 2009 6:49 am

Re: Deprecated messages from Joomla 3 script in Joomla 4

Post by colinj » Thu Jun 01, 2023 11:23 am

Changed the code, but it seems to have a problem finding files. I get an error message:

Warning: require_once(/home3/sitename/includes/defines.php): Failed to open stream: No such file or directory in /home3/sitename/public_html/process_csv.php on line 5

Fatal error: Uncaught Error: Failed opening required '/home3/sitename/includes/defines.php' (include_path='.:/opt/alt/php81/usr/share/pear:/opt/alt/php81/usr/share/php:/usr/share/pear:/usr/share/php') in /home3/sitename/public_html/process_csv.php:5 Stack trace: #0 {main} thrown in /home3/sitename/public_html/process_csv.php on line 5

Line 5 is: require_once JPATH_BASE . '/includes/defines.php';

Server is using php 8.1, tried switching to 8.2 and the site crashes, obviously something doesn't like it.

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17443
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Deprecated messages from Joomla 3 script in Joomla 4

Post by toivo » Thu Jun 01, 2023 11:55 am

Your is stored in the main Joomla folder, instead of a subfolder under it. Change the following line in the example:

Code: Select all

define('JPATH_BASE', realpath(dirname(__FILE__) . '/..'));
to this:

Code: Select all

define('JPATH_BASE', realpath(dirname(__FILE__)));
Toivo Talikka, Global Moderator

colinj
Joomla! Apprentice
Joomla! Apprentice
Posts: 38
Joined: Mon Dec 28, 2009 6:49 am

Re: Deprecated messages from Joomla 3 script in Joomla 4

Post by colinj » Fri Jun 02, 2023 12:18 am

Thanks, done that.

Now I get error message further down in the code:

Stack Trace: Attempted to call an undefined method named "query" of class "Joomla\Database\Mysqli\MysqliDriver".
Undefinedmethod error: $db->query();

Think I'll need to get someone to review the whole code.

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17443
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Deprecated messages from Joomla 3 script in Joomla 4

Post by toivo » Fri Jun 02, 2023 5:15 am

Go to the cPanel or similar, provided by your host, then access the Software - PHP section and check from the list of PHP extensions that mysqli is enabled.

colinj wrote:
Fri Jun 02, 2023 12:18 am
Stack Trace: Attempted to call an undefined method named "query" of class "Joomla\Database\Mysqli\MysqliDriver".
Undefinedmethod error: $db->query();
Does the script perhaps use Joomla 3 database functions directly, rather than using the database object?
Toivo Talikka, Global Moderator

colinj
Joomla! Apprentice
Joomla! Apprentice
Posts: 38
Joined: Mon Dec 28, 2009 6:49 am

Re: Deprecated messages from Joomla 3 script in Joomla 4

Post by colinj » Fri Jun 02, 2023 5:27 am

Yes, it's enabled, and the database type in Joomla is set to mysqli.

One of the files started working when I changed $db->query(); to $db->execute();

I get an error message with the mailer in a line that says $mailer->setSender($sender);

If I comment that line out, the php file completes, but there's obviously no sender name in the email.

"Joomla 3 database functions directly, rather than using the database object" is somewhat beyond my pay grade. :)

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17443
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Deprecated messages from Joomla 3 script in Joomla 4

Post by toivo » Fri Jun 02, 2023 7:14 am

colinj wrote:
Fri Jun 02, 2023 5:27 am
I get an error message with the mailer in a line that says $mailer->setSender($sender);
As seen in the comments starting from line 172 of libraries/src/Mail/Mail.php, the variable $sender must contain an array of the email address and the name of the sender, or just the email address as a string, for example '[email protected]'. Otherwise an error with the sender email address is reported.

Code: Select all

    /**
     * Set the email sender
     *
     * @param   mixed  $from  email address and Name of sender
     *                        <code>array([0] => email Address, [1] => Name)</code>
     *                        or as a string
Toivo Talikka, Global Moderator

colinj
Joomla! Apprentice
Joomla! Apprentice
Posts: 38
Joined: Mon Dec 28, 2009 6:49 am

Re: Deprecated messages from Joomla 3 script in Joomla 4

Post by colinj » Sun Jun 04, 2023 3:05 am

This file is supposed be called by Javascript in an RSForm so it inserts data from a record into two other fields when the Member_ID is entered.

I've changed the first lines to what is suggested, but it has stopped working with Joomla 4. Any advice would be greatly appreciated:

<?php
define('_JEXEC', 1);
use Joomla\CMS\Factory;
define('JPATH_BASE', realpath(dirname(__FILE__)));
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';

$app = new Joomla\CMS\Application\SiteApplication();
\Joomla\CMS\Factory::$application = $app;
$db = Factory::getContainer()->get('DatabaseDriver');

$member_id = JRequest::getVar('member_id', '');
if($member_id != ''){
$query = 'SELECT * FROM jos_credits WHERE Member_ID = ' . $db->quote($member_id)
;
$db->setQuery($query);
$member_data = $db->loadAssoc();
if($member_data['Member_ID'] == ''){
echo "This member number doesn't exist, please check your number";
}else{
echo json_encode($member_data);
}
}else{
echo '';
}
exit;
?>

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17443
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Deprecated messages from Joomla 3 script in Joomla 4

Post by toivo » Sun Jun 04, 2023 4:44 am

The method JRequest was deprecated already in Joomla 3. Here is the Joomla 4 replacement and the data flow:

Code: Select all

$member_id = $app->input->getVar('member_id', '');
if ($member_id != '') {
	$query = 'SELECT * FROM jos_credits WHERE Member_ID = ' . $db->quote($member_id);
	$db->setQuery($query);
	$member_data = $db->loadAssoc();
	if (is_null($member_data)) {
		echo "This member number doesn't exist, please check your number";
		exit;
	}
	echo json_encode($member_data);
}
Ref. Potential backward compatibility issues in Joomla 3 and Joomla Platform 12.2: JRequest

Tip
Insert the following lines after the line starting with 'define' in the first part of the script so that PHP errors are reported:

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', 1);
Toivo Talikka, Global Moderator

colinj
Joomla! Apprentice
Joomla! Apprentice
Posts: 38
Joined: Mon Dec 28, 2009 6:49 am

Re: Deprecated messages from Joomla 3 script in Joomla 4

Post by colinj » Sun Jun 04, 2023 6:43 am

Thanks a heap. That has fixed it.

I still have a number of deprecated or other issues that have stopped functioning with Joomla 4.

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17443
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Deprecated messages from Joomla 3 script in Joomla 4

Post by toivo » Sun Jun 04, 2023 7:14 am

Thank you for the update. Feel free to create new topics with the questions or issues here, the Joomla! 4.x Coding subforum.
Toivo Talikka, Global Moderator


Post Reply

Return to “Joomla! 4.x Coding”