Forums / Setup & design / Remove all contents of a class

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