Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

validate_resource_files()

Description

Validate the files on disk that are associated with the given resources

array of resource data e.g, from search results
required return values in order to pass the check e.g.
'file_exists" =>true for a file presence only check

e.g. ["1234" => true, "1235" => false]

Parameters

ColumnTypeDefaultDescription
$resources array Array of resource IDs or
$criteria array Array with an array of callables for each resource with the

Return

array $results An array with resource ID as the index and the results of the check as the value (boolean)

Location

include/file_functions.php lines 325 to 365

Definition

 
function validate_resource_files(array $resources,array $criteria = []): array
{
    
$checkresources = isset($resources[0]["ref"]) ? $resources get_resource_data_batch($resources);
    
$results = [];
    foreach(
$checkresources as $resource) {
        if (!
is_int_loose($resource["ref"])) {
            
$results[$resource["ref"]] = false;
            continue;
        }
        
$filepath get_resource_path($resource["ref"],true,'',false,$resource["file_extension"] ?? "jpg");
        
$results[$resource["ref"]] = false;
        foreach (
$criteria as $criterion=>$expected) {
            if(!
is_callable($criterion)) {
                
$results[$resource["ref"]] = false;
                
// Not a valid check
                
continue 2;
            }
            
$cscheck $expected === "%RESOURCE%file_checksum";
            if(
substr($expected,0,10) == "%RESOURCE%") {
                
// $expected is a resource table column
                
$expected $resource[substr($expected,10)];
            }
            
$testresult call_user_func($criterion,$filepath);
            if (
$cscheck && ($expected === null || $expected === "")) {
                
// No checksum is currently recorded. Update it now that it's been calculated
                
$results[$resource["ref"]] = true;
                
debug("Skipping checksum check for resource " $resource["ref"] . " - no existing checksum");
                
ps_query("UPDATE resource SET file_checksum = ? WHERE ref = ?", ['s'$testresult'i'$resource["ref"]]);
                continue;
            }

            
$results[$resource["ref"]] = $testresult === $expected;
            if (
$results[$resource["ref"]] === false) {
                
debug($resource["ref"] . " failed integrity check. Expected: " $criterion "="$expected ", got : " $testresult);
                
// No need to continue with other $criteria as check has failed
                
continue 2;
            }
        }
    }
    return 
$results;
}

This article was last updated 29th May 2024 10:05 Europe/London time based on the source file dated 28th May 2024 17:05 Europe/London time.