Forums / Developer / deleteing a node tree via PHP

deleteing a node tree via PHP

Author Message

Jim Reverend

Thursday 05 February 2004 5:06:48 am

I have the following code:
function deleteallkids($nodeid) {
$folderobject = ezcontentobjecttreenode::fetch($nodeid);
$children = $folderobject->subTree();
foreach(array_keys($children) as $key) {
$children[$key]->remove();
}
print "\n";

}
-----

This SEEMS to work, kind of. It seems to have deleted all the nodes in the tree. When I run the script again, it finds nothing to delete. However, in the admin interface, the items are still there.

What am I doing wrong?

Is there any basic documentation for handling Objects, Nodes, and DataTypes in PHP? I've generated the Doxygen docs... but they don't help much at all.

Wenyue Yu

Thursday 05 February 2004 5:39:39 am

Hi,

Doing $node->remove() will only remove the content object from ezcontentobject_tree table. To completely remove this object, you need to do the following:
$objectID = $node->attribute( 'contentobject_id' );
$node->remove();
$object =& eZContentObject::fetch( $objectID );
$object->purge();

Regards,
wenyue

Jim Reverend

Thursday 05 February 2004 5:49:41 am

Thank you kindly for your quick response.

I tried what you said in the past, and I tried it again now, to no avail.

It seems that $node->subTree() isn't returning any items when there clearly ARE items in that node.

In one "folder" I have nothing but "folder"s. And they all seem to remove just fine. However, in another folder, I have objects belonging to a custom class... those items are not being returned by SubTree nor are they being removed. I built the custom class using the Admin interface. Could this be the problem?

Jim Reverend

Thursday 05 February 2004 5:52:46 am

To make things more interesting, I just added a "folder" to the node that it full of objects of a custom class. When I ran the script, the folder was deleted, yet the other items remained.

Jim Reverend

Thursday 05 February 2004 5:57:45 am

A few more tests.

I added a whole bunch of objects to the folder and then tried to delete them. And only one item type will not delete.

One of the items was "comment". I edited it via the admin interface, and it deleted just fine. The item that wont delete is "blog". I made it from a copy of "article" that I edited to suit my needs (got rid of Intro and Thumbnail and renamed Body). I hope this helps you figure out what is going on here.

Wenyue Yu

Thursday 05 February 2004 7:51:57 am

Hi,

I think the problem is of permission. When you run this script, you are treated as anoymous user, so you need to give content read * permission to anoymous user.

Regards,
wenyue

Jim Reverend

Thursday 05 February 2004 10:53:00 am

I gave read * to Anonymous user. This didn't seem to help. However, I believe that I must not be running the script as Anonymous. If I were, I don't think I would be capable of deleting ANYTHING at all. At least... that doesn't seem right.

I installed PLAIN system. That's what I've been editing to create the system I have now. And I've only made the changes I laid out to you earlier. I am convinced that it has something to do with the creation of new classes of content and the ability for those items to appear in the default data listed by subTree().

Is there anything more you can think of?

Wenyue Yu

Friday 06 February 2004 2:41:19 am

Hi,

Could you added print( "Here is my sql query-" . $query ); in function subTree() of class eZContentObjectTreeNode around line 1090 before "if ( !$offset && !$limit )"?

What sql query do you get when you run your script?

Regards,
wenyue

Jim Reverend

Friday 06 February 2004 4:19:45 am

Yesterday evening I figured it out. Or rather, you figured it out. It was a permissions issue, like you said. I wasn't pushing the right button to make the permissions changes active. It looked like they were... but they weren't. AS soon as I made them active, the script started working just fine.

Of course, this brings a whole host of new worries... like... if the script runs as an anonymous user... why is that anonymous user capable of deleting anything?? They didn't have any kind of edit permissions. Just "read" and "login".

I'd still really like to see some sample code for working with the different Objects in EZ (Node, Object, Attribute, Version, DataType, etc).

Thank you again, for all of your help. It really is appreciated.

Wenyue Yu

Friday 06 February 2004 6:03:09 am

Hi,

Actually, if you can run a script in your system, you can do everything. The permission issues in your script is only because you used function subTree() which will check current user ( this function is used in admin interface ) . Since you are not logged in, you are anonymous user. The actual deleting function you called will not check permissions and that is why you can delete anything. Permission will be checked in admin/user interface, so don't worry about that. There is no security hole.

Best regards,
wenyue

Jim Reverend

Friday 06 February 2004 6:44:49 am

Wenyue--

Thank you again for all of your help. I have 2 more questions:

1) Are any of the permissionless methods exported to the templates in any way?

2) Is there another function I can use that does the same thing as subTree() but without the permissions checking? I'd rather not have to make everything that is going to be removed by this script "readable" by anonymous.

3) In case there isn't another option for #2, is there a way that I can make my script "login" as a different user?

Thanks, again!

Wenyue Yu

Friday 06 February 2004 8:11:00 am

Hi,

To escape permision check in subTree function, use following:

$node->subTree( array( 'Limitation' => array() ) );

Regards,
wenyue

eZ debug

Timing: Jan 18 2025 21:06:32
Script start
Timing: Jan 18 2025 21:06:32
Module start 'content'
Timing: Jan 18 2025 21:06:32
Module end 'content'
Timing: Jan 18 2025 21:06:32
Script end

Main resources:

Total runtime0.1621 sec
Peak memory usage4,096.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0085 587.7109180.8359
Module start 'content' 0.00850.0059 768.5469105.7578
Module end 'content' 0.01440.1476 874.3047535.2734
Script end 0.1620  1,409.5781 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00392.4226200.0002
Check MTime0.00160.9697200.0001
Mysql Total
Database connection0.00080.474410.0008
Mysqli_queries0.116271.71431410.0008
Looping result0.00130.81851390.0000
Template Total0.147290.810.1472
Template load0.00100.594810.0010
Template processing0.146290.224710.1462
Override
Cache load0.00070.418410.0007
Sytem overhead
Fetch class attribute can translate value0.00080.497210.0008
XML
Image XML parsing0.00020.118010.0002
General
dbfile0.00150.9409200.0001
String conversion0.00000.003730.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0002 secs