Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test functions

cleanup_invalid_nodes()

Description

Remove invalid field data from resources, optionally just for the specified resource types and/or fields

Parameters

ColumnTypeDefaultDescription
$fields array []
$restypes array []
$dryrun bool false Don't delete, just return count of rows that will be affected
$fields=[] array Array of resource_type_field refs
$restypes=[] array Array of resource_type refs

Return

int Count of rows deleted/to delete

Location

include/node_functions.php lines 2858 to 2920

Definition

 
function cleanup_invalid_nodes(array $fields = [],array $restypes=[], bool $dryrun=false)
    {
    
$allrestypes get_resource_types('',false,false,true);
    
$allrestyperefs array_column($allrestypes,"ref");
    
$allfields get_resource_type_fields();
    
$fieldglobals array_column($allfields,"global","ref");
    
$joined_fields get_resource_table_joins();

    
$restypes array_filter($restypes,function ($val) {return $val 0;});
    
$fields array_filter($fields,function ($val) {return $val 0;});

    
$fields count($fields)>array_intersect($fields,array_column($allfields,"ref")) : array_column($allfields,"ref");
    
$restypes count($restypes)>array_intersect($restypes,$allrestyperefs) : $allrestyperefs;
    
$restype_mappings get_resource_type_field_resource_types();
    
$deletedrows 0;
    foreach(
$restypes as $restype)
        {
        if(!
in_array($restype,$allrestyperefs))
            {
            continue;
            }
        
// Find invalid fields for this resource type
        
$remove_fields = [];
        foreach(
$fields as $field)
            {
            if(!
in_array($fieldarray_column($allfields,"ref")))
                {
                continue;
                }
            if ((int)
$fieldglobals[$field] == && !in_array($restype,$restype_mappings[$field]))
                {
                
$remove_fields[] = $field;
                }
            }

        if(
count($remove_fields)>0)
            {
            if(
$dryrun)
                {
                
$query "SELECT COUNT(*) AS value FROM resource_node LEFT JOIN resource r ON r.ref=resource_node.resource LEFT JOIN node n ON n.ref=resource_node.node WHERE r.resource_type = ? AND n.resource_type_field IN (" ps_param_insert(count($remove_fields))  . ");";
                
$params array_merge(["i",$restype],ps_param_fill($remove_fields,"i"));
                
$deletedrows ps_value($query,$params,0);
                }
            else
                {
                
$query "DELETE rn.* FROM resource_node rn LEFT JOIN resource r ON r.ref=rn.resource LEFT JOIN node n ON n.ref=rn.node WHERE r.resource_type = ? AND n.resource_type_field IN (" ps_param_insert(count($remove_fields))  . ");";
                
$params array_merge(["i",$restype],ps_param_fill($remove_fields,"i"));
                
ps_query($query,$params);
                
$deletedrows += sql_affected_rows();

                
# Also remove data in joined fields.
                
foreach ($remove_fields as $check_joined_field)
                    {
                    if (
in_array($check_joined_field$joined_fields))
                        {
                        
ps_query("UPDATE resource SET `field" $check_joined_field "` = null WHERE resource_type = ?", array("i"$restype));
                        }
                    }
                }
            }
        }
    return 
$deletedrows ? ((!$dryrun "Deleted " "Found ") . $deletedrows " row(s)") :  "No rows found";
    }

This article was last updated 17th November 2024 15:05 Europe/London time based on the source file dated 28th October 2024 11:15 Europe/London time.