Remove all contents of a class

Author Message

Ted Striker

Monday 15 May 2006 12:35:11 am

Hi there,

I have a large site (that runs ezp 3.4.2) wich a I have to duplicate, but "clean", that is remove all contents. It has rons of articles, images, links....

Is there any way to authomatically remove all contents (artcles for instance) all site wide without needing to remove then 1 by 1 ?? Some scipt..

Really need help on this....

Thanks in advance!!!

Can you feel it Mr. Anderson? Closing in on you? Oh I can, I really should thank you after all. It was, after all, it was your life that taught me the purpose of all life. Purpose of life is to end.

Ɓukasz Serwatka

Monday 15 May 2006 1:13:45 am

You can use SQL script which comes with eZ publish, look on kernel/sql/mysql/cleandata.sql. This script contains default installation information. Before run you need to clear tables. Useful might be also /bin/php/ezsqlinsertschema.php.

Personal website -> http://serwatka.net
Blog (about eZ Publish) -> http://serwatka.net/blog

Xavier Dutoit

Monday 15 May 2006 1:22:57 am

Hi,

No "out of the box" functionnalities for that. It is quite simple to develop one. We are working on an extension to ease the admin of a website (so far focussing on the creation of content), it looks like a nice add on.

This being said, that's not my priority so far, unless you persuade me otherwise ;)

Plan B: export the class definitions as a package, delete the classes (it's going to delete the content too) and import the package containing the classes. It <b>should</b> work.

Let us know...

X+

http://www.sydesy.com

Kristof Coomans

Monday 15 May 2006 2:31:47 am

Hi Ted

I once wrote such a command line script. I haven't used it for a long time, but it will probably still work with the latest eZ publish releases.

#!/usr/bin/env php
<?php

// include needed kernel classes
include_once( 'kernel/classes/ezscript.php' );
include_once( 'kernel/classes/ezcontentclass.php' );
include_once( 'kernel/classes/ezcontentobject.php' );

// include needed library classes
include_once( 'lib/ezutils/classes/ezcli.php' );

$cli = &eZCLI::instance( );

$script = &eZScript::instance( array( 'description' => 'this script automates the removal of objects, usefull when removing corrupt items or cleaning up test data on development portals',
                                      'use-session' => false,
                                      'use-modules' => false,
                                      'use-extensions' => true ) );

$script->startup( );
$options =& $script->getOptions( '[classidentifier:][objectid:][exclude:][preview]', '', array( 'classidentifier' => 'class identifier of the objects you want to remove', 'exclude' => 'comma seperated list of object id\'s to exclude from removal', 'preview' => 'don\'t really remove objects, just show what would happen', 'objectid' => 'comma seperated list of the object id\'s you want to remove' ), false, array( 'user' => true ) );

$script->initialize( );

$objects = array( );

if ( $options['classidentifier'] or $options['objectid'] )
{
    // get the objects according to specified options
    if ( $options['classidentifier'] )
    {
        $class = &eZContentClass::fetchByIdentifier( $options['classidentifier'] );

        if ( $class )
        {
            $objects = &eZContentObject::fetchSameClassList( $class->attribute( 'id' ) );
        }
        else
        {
            $cli->output( 'could not find class' );
        }
    }
    elseif ( $options['objectid'] )
    {
        $idList = explode( ',', $options['objectid'] );

        if ( is_array( $idList ) )
        {
            $objects = &eZContentObject::fetchIDArray( $idList );
        }
        else
        {
            $objects[] = &eZContentObject::fetch( $idList );
        }
    }

    if ( count( $objects ) > 0 )
    {
        $removedCount = 0;
        $excludes = array( );
    
        if ( $options['exclude'] )
        {
            $excludes = explode( ',', $options['exclude'] );
        }

        foreach( array_keys( $objects ) as $key )
        {
            if ( in_array( $objects[$key]->attribute( 'id' ), $excludes ) )
            {
                $cli->output( 'excluding object ' . $objects[$key]->attribute( 'id' ) . ': ' . $objects[$key]->attribute( 'name' ) );
            }
            else
            {
                $cli->output( 'removing object ' . $objects[$key]->attribute( 'id' ) . ': ' . $objects[$key]->attribute( 'name' ) );

                if ( $options['preview'] !== true )
                {
                    if ( $objects[$key]->attribute( 'status' ) == EZ_CONTENT_OBJECT_STATUS_PUBLISHED )
                    {
                        $assignedNodes = $objects[$key]->attribute( 'assigned_nodes' );

                        $nodeIdArray = array( );
                        foreach ( array_keys( $assignedNodes ) as $assignedNodeKey )
                        {
                            if ( $assignedNodes[$assignedNodeKey]->attribute( 'can_remove' ) )
                            {
                                $nodeIdArray[] = $assignedNodes[$assignedNodeKey]->attribute( 'node_id' );
                            }
                            else
                            {
                                $nodeIdArray = false;
                                break;
                            }
                        }

                        if ( $nodeIdArray )
                        {
                            eZContentObjectTreeNode::removeSubtrees( $nodeIdArray, false );
                        }
                        else
                        {
                            $cli->output( 'you have insufficient rights to remove this object' );
                        }
                    }
                    else
                    {
                        $objects[$key]->remove( );
                        $objects[$key]->purge( );
                    }
                }
                $removedCount++;
            }
        }

        $cli->output( 'removed items: ' . $removedCount );
    }
    else
    {
        $cli->output( 'no objects of this class were found' );
    }
}
else
{
    $script->showHelp( );
}

$script->shutdown( );

?>

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

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 18 2025 19:28:52
Script start
Timing: Jan 18 2025 19:28:52
Module start 'layout'
Timing: Jan 18 2025 19:28:52
Module start 'content'
Timing: Jan 18 2025 19:28:52
Module end 'content'
Timing: Jan 18 2025 19:28:52
Script end

Main resources:

Total runtime0.9835 sec
Peak memory usage4,096.0000 KB
Database Queries64

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0080 589.0313152.6250
Module start 'layout' 0.00800.0042 741.656339.4609
Module start 'content' 0.01220.9698 781.1172664.9219
Module end 'content' 0.98210.0015 1,446.039120.1641
Script end 0.9835  1,466.2031 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00340.3419160.0002
Check MTime0.00140.1403160.0001
Mysql Total
Database connection0.00240.247010.0024
Mysqli_queries0.917393.2659640.0143
Looping result0.00070.0678620.0000
Template Total0.949196.520.4746
Template load0.00190.197520.0010
Template processing0.947196.299220.4736
Template load and register function0.00010.010710.0001
states
state_id_array0.00070.072710.0007
state_identifier_array0.00220.222020.0011
Override
Cache load0.00160.1648250.0001
Sytem overhead
Fetch class attribute can translate value0.00070.073240.0002
Fetch class attribute name0.00170.169880.0002
XML
Image XML parsing0.00290.294540.0007
class_abstraction
Instantiating content class attribute0.00000.001280.0000
General
dbfile0.00220.2224360.0001
String conversion0.00000.001040.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
4content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
4content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
4content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
1content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 15
 Number of unique templates used: 6

Time used to render debug report: 0.0001 secs