Corrupt Content Objects in eZ Publish

On my eZDB I’ve found two different scenarios of corrupt objects, they must have become corrupt when not having transaction enabled, and eZp or the user breaks out of a content object creation, og content object edit:

  1. a content object exist only in table ezcontentobject, no referernces to object in any other ezcontentobject*-tables. SOLUTION: delete row from ezcontentobject.
  2. a content object has attributes etc., but it has a ezcontentobject.current_version that doesn’t have attributes. SOLUTION: roll back version number.

This script report, and does the required job:

<?php

// Script for finding and handling content_objects that are not completely created
// That may occur under some circustances when using a database without transations enabled
//
//
// 2007.09.20, jonny.bergkvist@hit.no

// $doUpdate, true or false. Set to false for at dry test-run
$doUpdate = true;

include_once( 'kernel/common/template.php' );
include_once( "lib/ezutils/classes/ezhttptool.php" );
include_once( 'lib/ezutils/classes/ezcli.php' );
include_once( 'kernel/classes/ezscript.php' );
include_once( 'lib/ezdb/classes/ezdb.php' );

$cli =& eZCLI::instance();
$script =& eZScript::instance();
$script->initialize();
$db =& eZDB::instance();
set_time_limit( 0 );

$arrayResult1 = $db->arrayQuery( "SELECT id, current_version FROM ezcontentobject" );
echo "First checking for content objects that has no contentobject_attributes at all...n";

$i = 0;

foreach( $arrayResult1 as $item) {
        //check if object has no attributes of any version stored
        $hasAttribute = $db->arrayQuery( "SELECT contentobject_id FROM ezcontentobject_attribute WHERE contentobject_id = " . $item['id'] );

        if ( empty( $hasAttribute ) ) {
                echo "Corrupt object: " . $item['id'] . ". ";
                if ( $doUpdate ) {
                        echo "Deleting corrupt object with no attributes...n";
                        $db->query( "DELETE FROM ezcontentobject WHERE ezcontentobject.id = " . $item['id'] );
                }
        $i++;
        }
}

echo "Total corrupt objects with no attributes: " . $i . "nn";

$arrayResult2 = $db->arrayQuery( "SELECT id, current_version FROM ezcontentobject" );
echo "Then checking for content objects that has contentobject_attributes, but not of the current_version...n";
$i = 0;
foreach( $arrayResult2 as $item) {
        //check if current_version has content attributes
        $hasAttribute = $db->arrayQuery( "SELECT contentobject_id FROM ezcontentobject_attribute WHERE contentobject_id = " . $item['id'] . " AND version = " . $item['current_version'] );

        if ( empty( $hasAttribute ) ) {
        echo "Corrupt object: " . $item['id'] . ", current_version: " . $item['current_version'] . ". ";
                if ( $doUpdate ) {
                        $previousCurrentVersion = $item['current_version'] - 1;
                        echo "Setting back to version: " . $previousCurrentVersion . "n";
                        $db->query( "UPDATE ezcontentobject SET current_version = " . $previousCurrentVersion . " WHERE id = " . $item['id'] );
                }
        $i++;
        }
}
echo "Total objects with wrong current_version: " . $i . "n";

$script->shutdown();
?>

Source: http://ez.no/developer/forum/general/problem_corrupt_contentobjects