Forums / Developer / php script problem

php script problem

Author Message

Aurore Hublé

Tuesday 24 October 2006 6:50:08 am

Hi everydoby,

I need to check dayly if object must be move in another folder. So, I found the corresponding php script. But, it doesn't work. It's not the first time I meet this error when I use script but in this case I don't find another way to do.
I have this code in ez publish folder :

<?php
include_once("kernel/classes/ezcontentobjecttreenode.php");
	$params = array( 'ClassFilterType' => 'include', 
				  'ClassFilterArray' => array(17,18)
			     );
	$objects =& eZContentObjectTreeNode::subTree( $params, 69 );
	echo(count($objects));
	foreach ($objects as $node)
		$node->attribute('name');
?>

I run that with the command : /opt/php/bin/php script.php

And I have the result : "0".

When I try this code in an extension, it's work very well !

I don't understand why it doesn't work in a script. Did I forget something ?

Kristof Coomans

Tuesday 24 October 2006 7:33:30 am

Hello Aurore

You need to initialize the eZ environment. Please take a look at some existing CLI scripts (which use the classes eZCLI and eZScript) in the bin/php folder.

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org

Aurore Hublé

Tuesday 24 October 2006 7:57:31 am

Thanks I will try that !

Aurore Hublé

Tuesday 24 October 2006 8:16:40 am

In fact, I try that before posted but as I don't know what's script initializing, I cut that.
So, my code is now :

<?php
// librairies eZ publish
	include_once( 'lib/ezutils/classes/ezcli.php' );
	include_once( 'kernel/classes/ezscript.php' );

	$cli =& eZCLI::instance();
	$script = & eZScript :: instance(array("debug-message" => "", "use-session" => false, "use-modules" => true, "use-extensions" => true));
	$script->startup();

	$script->initialize();

	include_once("kernel/classes/ezcontentobjecttreenode.php");

	$params = array( 'ClassFilterType' => 'include', 
				  'ClassFilterArray' => array(17,18)
			     );
	$objects =& eZContentObjectTreeNode::subTree( $params, 69 );
	echo(count($objects));
	foreach ($objects as $node)
		$node->attribute('name');

	$script->shutdown();
?>

and always says "0"...

Kristof Coomans

Tuesday 24 October 2006 9:12:30 am

Are the nodes you want to fetch accessible by the anonymous user? Otherwise, you need to tell your script to enable the user command line options:

$script->startup();
$options = $script->getOptions( '', '', false, false, array( 'user' => true ) );
$script->initialize();

and login with the --user and --password command line options. eZScript will process these options for you and log in the user with the credentials specified.

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org

Aurore Hublé

Wednesday 25 October 2006 12:43:37 am

Thanks for your answer.
They aren't accessible by anonymous user. I add :

$script =& eZScript::instance( array( '' , 'use-session' => false, 'use-modules' => true, 'use-extensions' => true, 'user' => true ) );

$script->startup();
$scriptOptions = $script->getOptions( '','', false, false, array( 'user' => true ) );

If I launch the script with :

 /opt/php/bin/php script.php -v -l admin -p pwdadmin

(because in the help, option --user corresponds to -l, --login)
The result is still "0".
And
If I launch the script with :

 /opt/php/bin/php script.php -v --login admin -p pwdadmin

=> script.php: option `--login' requires an argument
(It's the same with '--password')

Aurore Hublé

Wednesday 25 October 2006 1:21:56 am

I try an other thing.

include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
$user =&eZUser::loginUser("admin","pwdadmin");
if ($user)
  echo("user id: " . $user->id()p);

And login seems work. But not the fetch. :'(

Kristof Coomans

Wednesday 25 October 2006 1:55:10 am

To log in as admin from inside the script, you should use:

include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
$user = eZUser::fetchByName('admin');
$userID = $user->attribute( 'contentobject_id' );
eZUser::setCurrentlyLoggedInUser( $user, $userID );

If you use the long cli options then you have to specify their values with =

--login=admin --password=pwdadmin

Do you have any special chars in your password? You should escape them for your shell.

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org

Aurore Hublé

Wednesday 25 October 2006 2:01:43 am

So the problem seem be on
$objects =& eZContentObjectTreeNode::subTree( false, 69);
It don't return all the node.

Result depend on user right. But in admin I must have right on all nodes !
(I don't have special character in password)

Aurore Hublé

Thursday 26 October 2006 12:13:54 am

No idea ?
I don't have this problem in an extension. If I try this code :

$objects =& eZContentObjectTreeNode::subTree( false, 69);

It's work perfectly, but in the script, it returns incomplete result. I don't found links between the returned nodes. There are in different sections and under different rights.

Kristof Coomans

Thursday 26 October 2006 1:18:49 am

Hello Aurore

Can you post the whole script you are using? Or send it by e-mail to kristof.coomans@telenet.be

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org

Xavier Dutoit

Thursday 26 October 2006 1:47:27 am

Hi,

Is your default siteaccess different than the one you want to use ? In that case, you have to pass as a parameter the siteaccess your want (-s<siteaccess>)

do a print_r instead of a print(count) you might have more info.

X+

http://www.sydesy.com

Aurore Hublé

Thursday 26 October 2006 2:05:06 am

Thanks for your answer.

The default siteaccess is the same I use.

The script :

<?php
	include_once( 'lib/ezutils/classes/ezcli.php' );
	include_once( 'kernel/classes/ezscript.php' );
	
	$cli =& eZCLI::instance();
	$script =& eZScript::instance( array( '',
	                                      'use-session' => false,
	                                      'use-modules' => true,
	                                      'use-extensions' => true,
					      'user' => true ) );
	$script->startup();
	$scriptOptions = $script->getOptions( '','', false, false, array( 'user' => true ) );
	$script->initialize();
	
	if ( ! $script->isInitialized() )
	{
		echo("Erreur : Impossible d'initialiser le script.");
		$script->shutdown();
		exit ();
	}
	include_once("kernel/classes/ezcontentobjecttreenode.php");
	$params = array( 'ClassFilterType' => 'include', 
					 'ClassFilterArray' => array(17,18)
			     );
	
	$objects =& eZContentObjectTreeNode::subTree( $false, 69 );
	echo(count($objects));
	foreach ( $objects as $child){
		echo($child->attribute('name'). " | " $child->attribute('class_identifier'). "\n");	
	}
?>

Launch with :
/opt/php/bin/php script.php -l admin -p pwdadmin

Result : "20
pro-distributeur | folder
Spare parts | rubrique
....
Autre | rubrique
Bloc gauche | bloc"

And if I launch the same fetch in extension or template, I have all nodes.

(print_r give the same result)

Xavier Dutoit

Thursday 26 October 2006 2:16:57 am

Salut à nouveau,

Not sure about what you want to move, but have a look at Lukasz contrib:

http://ez.no/community/contribs/cronjobs/archive

If it doesn't fit the bill 100% that might be easier to start with it and adapt it to your needs.

X+

http://www.sydesy.com

Aurore Hublé

Thursday 26 October 2006 2:29:54 am

Thanks I will try that.

Aurore Hublé

Thursday 26 October 2006 8:27:11 am

It works well on 3.7 eZ publish site but not in 3.8.
In this site, I have another problem with runcronjob, when I activate notification on approval content.
Fatal error: Call to a member function on a non-object in /.../kernel/classes/notification/handler/ezsubtree/ezsubtreehandler.php on line 123

I think the two errors are linked but if it runs on 3.7, it will run on 3.8.

Xavier Dutoit

Friday 27 October 2006 1:42:50 am

Hi,

Don't know about 3.8. Start a new thread with this specific problem, chances are higher Lucasz will jump into the problem.

X+

http://www.sydesy.com