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

check_resources()

Description

Check integrity of primary resource files

then file checksums will be checked

Parameters

ColumnTypeDefaultDescription
$resources array [] Array of resource data e.g. from search results
$presenceonly bool Check for file presence only? If false (and if $file_checksums is enabled)

Return

array Array of resource IDs that have failed to verify

Location

include/resource_functions.php lines 9494 to 9560

Definition

 
function check_resources(array $resources = [], bool $presenceonly false): array
{
    if (
count($resources) === 0) {
        
$resources get_resources_to_validate();
    }
    if (
count($resources) === 0)
        {
        echo 
" - No resources require integrity checks" PHP_EOL;
        return [];
        }

    
$existingfailed array_column(array_filter($resources, function($resource) {
        return 
$resource["integrity_fail"] == 1;
    }),
"ref");

    
$return_failed = [];
    foreach (
array_chunk($resources,1000) as $checkresources) {
        
$checks = [];
        
$checks["is_readable"] = true// Always check for file presence as checksums may not have been generated yet
        
if (!$presenceonly && $GLOBALS["file_checksums"]) {
            
$checks["get_checksum"]  = "%RESOURCE%file_checksum";
            }
        
$results validate_resource_files($checkresources,$checks);

        
$failed = [];
        
$succeeded = [];
        foreach (
$results as $ref=>$result) {
            if(
$result === false) {
                
$failed[] = $ref;
            } else {
                
$succeeded[] = $ref;
            }
        }

        
db_begin_transaction("checkresources");
        if (
count($failed) > 0) {
            
$failed_sql "UPDATE resource SET integrity_fail = 1 WHERE ref IN (" ps_param_insert(count($failed)) . ")";
            
$failed_params ps_param_fill($failed,"i");
            
ps_query($failed_sql,$failed_params);
        }
        if (
count($succeeded) > 0) {
            
$success_sql "UPDATE resource SET integrity_fail = 0,no_file=0, last_verified=NOW() WHERE ref IN (" ps_param_insert(count($succeeded)) . ")";
            
$success_params ps_param_fill($succeeded,"i");
            
ps_query($success_sql,$success_params);
        }
        
// Add any failures to the array to return
        
$return_failed array_merge($return_failed,$failed);
        
db_end_transaction("checkresources");

        
// Log any newly failed resources
        
db_begin_transaction("logfailedresources");
        
$arr_newfails array_diff($failed,$existingfailed);
        foreach (
$arr_newfails as $newfail) {
            
resource_log($newfailLOG_CODE_SYSTEM0"Failed file integrity check"0,1);
        }
        
db_end_transaction("logfailedresources");

         
// Log any recovered resources
         
db_begin_transaction("logrecoveredresources");
         
$arr_recovered array_intersect($succeeded,$existingfailed);
         foreach (
$arr_recovered as $recovered) {
             
resource_log($recoveredLOG_CODE_SYSTEM0"Passed file integrity check"1,0);
         }
         
db_end_transaction("logrecoveredresources");
    }
    return 
$return_failed;
}

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