Need to use Joomla database credentials/objs in a cron job

General questions relating to Joomla! 2.5. Note: All 1.6 and 1.7 releases have reached end of life and should be updated to 2.5. There are other boards for more specific help on Joomla! features and extensions.

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.
Locked
sneakyimp
Joomla! Intern
Joomla! Intern
Posts: 70
Joined: Mon Jun 21, 2010 12:26 am

Need to use Joomla database credentials/objs in a cron job

Post by sneakyimp » Thu Nov 29, 2012 3:45 am

I'm migrating a Joomla 1.7 site to use Joomla 2.5. The prior developers did a lot of regrettable things, one of which was to create a nightly cron job that ran dozens of queries directly on the Joomla db using mysql_query.

I'm hoping to modify the database access in this cron job such that is uses the Joomla classes and credentials to access the database rather than having a separate database connect/query structure unto itself. In particular:
* db credentials in configuration.php
* JDatabase object (and any related exceptions or other classes)

Is there some standardized way to do this?

Please note that:
* I do not want this script to live in the web root, lest someone set it off by accessing the script in a web browser
* I don't expect to create a component/module/plugin here and am hoping to avoid any additional overhead required by these more formalized concepts.
* ideally whatever technique I use will be forward-compatible with Joomla 3, etc. so that we don't face another rewrite when we upgrade again.
* I don't want to modify any of the Joomla code so that my project cannot be easily upgraded -- i.e., I don't want to alter any files to break compatibility with Joomla.

Any help you could provide would be much appreciated. I very much want to get this done quickly and am currently looking at around 60 queries that must be adapted (and quickly!) -- I don't want to have to fix them again in the future.
make multiplayer flash games: http://flashmog.net

User avatar
alikon
Joomla! Champion
Joomla! Champion
Posts: 5941
Joined: Fri Aug 19, 2005 10:46 am
Location: Roma
Contact:

Re: Need to use Joomla database credentials/objs in a cron j

Post by alikon » Thu Nov 29, 2012 5:40 am

https://github.com/joomla/joomla-platfo ... i/database
an example on how to work with db from a joomla platform CLI
Nicola Galgano
i know that i don't know
www.alikonweb.it

sneakyimp
Joomla! Intern
Joomla! Intern
Posts: 70
Joined: Mon Jun 21, 2010 12:26 am

Re: Need to use Joomla database credentials/objs in a cron j

Post by sneakyimp » Thu Nov 29, 2012 10:15 pm

alikon wrote:https://github.com/joomla/joomla-platfo ... i/database
an example on how to work with db from a joomla platform CLI
I appreciate the suggestion, but have noticed a variety of problems:
1) The file configuration.php defines different members than the one in my Joomla project. E.g., my JConfig class has class var $db while theirs has $dbName, etc.
2) The file run.php includes a file boostrap.php. The example they have refers to a joomla-platform project and I can't determine whether this refers to my Joomla 2.5 install or whether this is some separate library I must download from github.

Can you explain a bit more to me about this thing you have suggested? What version of Joomla is it for? Is it a distinct library that must be downloaded?
make multiplayer flash games: http://flashmog.net

sneakyimp
Joomla! Intern
Joomla! Intern
Posts: 70
Joined: Mon Jun 21, 2010 12:26 am

Re: Need to use Joomla database credentials/objs in a cron j

Post by sneakyimp » Fri Nov 30, 2012 1:45 am

I'm also having trouble formulating a procedural (i.e., non-OOP) script from this example. I'm having trouble accessing the JConfig values. How does one gain access to the JConfig values? This is returning a NULL value for $user:

// We are a valid Joomla entry point.
define("_JEXEC", 1);
// Setup the base path related constant.
define("JPATH_BASE", "/path/to/joomla/install");
// bootstrap the Joomla application
require JPATH_BASE . "/libraries/import.php";

$reg =& JFactory::getConfig();
$user = $reg->get("config.user"); // returns NULL
var_dump($user);
make multiplayer flash games: http://flashmog.net

sneakyimp
Joomla! Intern
Joomla! Intern
Posts: 70
Joined: Mon Jun 21, 2010 12:26 am

Re: Need to use Joomla database credentials/objs in a cron j

Post by sneakyimp » Sat Dec 01, 2012 7:06 pm

It's so hard to get help on this forum. I believe I have managed to boostrap DB access in my script using this code:

Code: Select all

// We are a valid Joomla entry point.
defined("_JEXEC") or define("_JEXEC", 1);
// Setup the base path related constant.
defined("JPATH_BASE") or define("JPATH_BASE", "/var/www/erep_v2/html");

// bootstrap the Joomla application
require_once JPATH_BASE . "/libraries/import.php";
// define necessary constants:
require_once JPATH_BASE . '/includes/defines.php';
// Import the configuration.
require_once JPATH_CONFIGURATION . '/configuration.php';

// System configuration.
$joomla_config = new JConfig;

// this will throw a RuntimeException on failure.
$jdbo = JDatabase::getInstance(
	array(
		"driver" => $joomla_config->dbtype,
		"host" => $joomla_config->host,
		"user" => $joomla_config->user,
		"password" => $joomla_config->password,
		"database" => $joomla_config->db,
		"prefix" => $joomla_config->dbprefix,
	)
);
I find it puzzling that I need to explicitly instantate the JConfig object.
make multiplayer flash games: http://flashmog.net


Locked

Return to “General Questions/New to Joomla! 2.5”