Coding standards
Security in ResourceSpace
Developer reference
Database
Mcp browse functions
Mcp report functions
Mcp management functions

mcp_resource_audit()

Description

Filter a bounded native search population using recorded facts. Missing metadata requires every selected field to apply.

Parameters

ColumnTypeDefaultDescription
$args: array mcp_report_population$args
false;
$args array Validated tool arguments.

Return

array Structured MCP result or permission/availability error.

Location

include/mcp_management_functions.php lines 618 to 700

Definition

 
function mcp_resource_audit(array $args): array
{
    
$population = mcp_report_population($args, false);
    if (isset(
$args['access'])) {
        
$access = $args['access'] === 'open' ? RESOURCE_ACCESS_FULL : RESOURCE_ACCESS_RESTRICTED;
        
$population = array_filter($population, static fn($row) => $row['access'] === $access);
    }
    
$condition = $args['condition'] ?? 'inventory';
    
$missing = array_unique($args['missing_fields'] ?? []);
    
$catalogue = $missing ? mcp_field_catalogue($args['resource_type'] ?? null) : [];
    foreach (
$missing as $id) {
        if (!isset(
$catalogue[$id])) {
            throw new 
DomainException('Metadata field unavailable.');
        }
        
$field = get_resource_type_field($id);
        
$population = array_filter($population, static function ($row) use ($catalogue, $id, $field) {
            
resource_type_config_override($row['resource_type']);
            return 
in_array($row['resource_type'], $catalogue[$id]['resource_types'], true)
                && 
metadata_field_view_access($id)
                && (!
$field['hide_when_restricted'] || $row['access'] === RESOURCE_ACCESS_FULL);
        });
    }
    
$consent = str_ends_with($condition, 'consent');
    if (
$consent || str_ends_with($condition, 'licence')) {
        
$plugin = $consent ? 'consentmanager' : 'licensemanager';
        if (!
in_array($plugin, $GLOBALS['plugins'], true)) {
            return 
mcp_error_result('Record management unavailable.', 'feature_unavailable');
        }
        if (!
checkperm($consent ? 'cm' : 'lm')) {
            
$population = array_filter($population, static fn($r) => $r['access'] === RESOURCE_ACCESS_FULL);
        }
    }
    
$where = ['r.ref IN (' . ($population ? ps_param_insert(count($population)) : 'NULL') . ')'];
    
$params = ps_param_fill(array_keys($population), 'i');
    if (
$missing) {
        
$where[] = 'NOT EXISTS (SELECT 1 FROM resource_node rn JOIN node n ON n.ref = rn.node WHERE rn.resource = r.ref
            AND n.active = 1 AND TRIM(n.name) <> \'\' AND n.resource_type_field IN (' 
. ps_param_insert(count($missing)) . '))';
        
$params = array_merge($params, ps_param_fill($missing, 'i'));
    }
    
$predicate = match ($condition) {
        
'linked_licence', 'linked_consent', 'multiple_licence', 'multiple_consent' => null,
        
'no_licence' => 'NOT EXISTS (SELECT 1 FROM resource_license rl WHERE rl.resource = r.ref)',
        
'no_consent' => 'NOT EXISTS (SELECT 1 FROM resource_consent rc WHERE rc.resource = r.ref)',
        
'withdrawn_consent' => 'EXISTS (SELECT 1 FROM resource_consent rc JOIN consent_request cr ON cr.consent = rc.consent
            WHERE rc.resource = r.ref AND cr.withdrawn IS NOT NULL)'
,
        
'expired_licence' => 'EXISTS (SELECT 1 FROM resource_license rl JOIN license l ON l.ref = rl.license
            WHERE rl.resource = r.ref AND l.expires < CURDATE())'
,
        
'no_preview' => 'r.has_image = 0',
        
'no_download' => "NOT EXISTS (SELECT 1 FROM resource_log rl WHERE rl.resource = r.ref AND BINARY rl.type = 'd')",
        
'integrity_failure' => 'r.integrity_fail = 1',
        default => 
'1 = 1',
    };
    if (
$predicate === null) {
        
$table = $consent ? 'consent' : 'license';
        
$subquery = " FROM resource_{$table} links WHERE links.resource = r.ref";
        if (
str_starts_with($condition, 'multiple_')) {
            if (isset(
$args['record'])) {
                throw new 
DomainException('Supply record with linked_licence or linked_consent.');
            }
            
$predicate = "(SELECT COUNT(DISTINCT links.{$table}) {$subquery}) > 1";
        } else {
            
$predicate = "EXISTS (SELECT 1 {$subquery}" . (isset($args['record']) ? " AND links.{$table} = ?" : '') . ')';
            if (isset(
$args['record'])) {
                
array_push($params, 'i', $args['record']);
            }
        }
    } elseif (isset(
$args['record'])) {
        throw new 
DomainException('Supply record with linked_licence or linked_consent.');
    }
    if (
$condition === 'integrity_failure' && !checkperm('a')) {
        return 
mcp_error_result('Administrator access is required for integrity checks.', 'permission_denied');
    }
    
$column = ($args['date_basis'] ?? 'created') === 'modified' ? 'r.modified' : 'r.creation_date';
    
$range = mcp_event_dates($args, $column);
    
$order = ($args['order_by'] ?? 'ref') === 'largest' ? 'r.file_size DESC, r.ref' : 'r.ref';
    return 
mcp_query_page(
        
'SELECT r.ref, r.resource_type, r.creation_date, r.modified, r.file_size, r.file_extension FROM resource r WHERE '
        
. implode(' AND ', $where) . " AND {$predicate} AND {$range->sql} ORDER BY {$order}",
        
array_merge($params, $range->parameters),
        
$args,
        [
'condition' => $condition, 'date_basis' => $args['date_basis'] ?? 'created']
    );
}

This article was last updated 4th December 2026 11:35 Europe/London time based on the source file dated 4th December 2026 09:40 Europe/London time.