Finding the objects of a class with non-null values

Thursday 18 August 2011 9:22:58 am

By : Steven E. Bailey

For a customer i had to find the objects (out of hundreds) that had an attribute filled out, I figured an attribute filter would be the easiest:

 

 <?php


require 'autoload.php';
$cli = eZCLI::instance();
$script = eZScript::instance( array(    'description' => (
                                "Returns the path and value of objects of <class> that have a non-null value for <attribute>" ),
                                'use-session' => false,
                                'use-modules' => true,
                                'use-extensions' => true ) );
$script->startup();

 $options = $script->getOptions( "[class:][attribute:][C:][a:]",
                                                         "",
                                                       array( 'class'  => "class",
                                                                  'attribute'  => "attribute"
                                                      ) );
$script->initialize();

$identifier = $options['class'] ? $options['class'] : $options['C'];
$attidentifier = $options['attribute'] ? $options['attribute'] : $options['a'];

if (!$identifier OR !$attidentifier) {
    $cli->error( "You must have one --class=<class> identifier and one --attribute=<attribute> identifier");
    $script->shutdown();
    exit;
} 

if ( is_numeric( $identifier ) ) {
    $ContentObjectClass = eZContentClass::fetch( $identifier );
    $identifier = $ContentObjectClass->attribute('identifier');
}

if ($attidentifier) {
    if ( is_numeric( $attidentifier ) ) {
        $attidentifier =  eZContentClassAttribute::classAttributeIdentifierByID( $attidentifier );
    }

    $attributefilterstring = $identifier."/".$attidentifier;
    $attributefilter = array( array( $attributefilterstring ,'!=', null ) );
}

$params = array();

$params['Limitation']    = array();        // access override
$params['MainNodeOnly']    = true;    // dont fecth dupes
$params['IgnoreVisibility'] = true;       // ignore hidden
if ($identifier) {
    $params['ClassFilterType'] = "include";        // "include" or "exclude"
    $params['ClassFilterArray'] = array($identifier);    // array of class_identifiers
}
$params['AttributeFilter']    = ($attributefilter) ? $attributefilter : false;

$topNodeID = 1;
$objects = eZContentObjectTreeNode::subTreeByNodeID( $params, $topNodeID );

if ( count($objects) == 0 ) $cli->output( "NO MATCHES" );
else {
    $cli->output( count($objects) );

    foreach($objects as $object) {
        $cli->output( $object->attribute( 'path_identification_string') );
        $datamap = $object->DataMap();
        $cli->output( $datamap[$attidentifier]->content() );
    }
}
$cli->output();
$script->shutdown();
?>
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 17 2025 23:55:39
Script start
Timing: Jan 17 2025 23:55:39
Module start 'layout'
Timing: Jan 17 2025 23:55:39
Module start 'content'
Timing: Jan 17 2025 23:55:39
Module end 'content'
Timing: Jan 17 2025 23:55:39
Script end

Main resources:

Total runtime0.0693 sec
Peak memory usage6,144.0000 KB
Database Queries42

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0061 588.1641152.6563
Module start 'layout' 0.00620.0026 740.820339.5156
Module start 'content' 0.00880.0591 780.3359361.1875
Module end 'content' 0.06780.0014 1,141.523415.7031
Script end 0.0693  1,157.2266 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00294.1209150.0002
Check MTime0.00121.7892150.0001
Mysql Total
Database connection0.00101.382510.0010
Mysqli_queries0.030443.8219420.0007
Looping result0.00020.3358400.0000
Template Total0.043062.020.0215
Template load0.00213.016120.0010
Template processing0.040958.997720.0204
Template load and register function0.00010.127010.0001
states
state_id_array0.00030.437310.0003
state_identifier_array0.00141.992820.0007
Override
Cache load0.00172.384480.0002
Sytem overhead
Fetch class attribute name0.00192.716130.0006
class_abstraction
Instantiating content class attribute0.00000.008330.0000
General
dbfile0.00060.8289100.0001
String conversion0.00000.012740.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.tplblog_entry/full.tplextension/community_design/design/suncana/override/templates/blog_entry/full.tplEdit templateOverride template
2content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
2content/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
1content/datatype/view/ezkeyword.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezkeyword.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 8
 Number of unique templates used: 6

Time used to render debug report: 0.0001 secs