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

log_diff()

Description

Check changes made to a metadata field and create a nice user friendly summary

Parameters

ColumnTypeDefaultDescription
$fromvalue string - if nodes then values will be separated by NODE_NAME_STRING_SEPARATOR
$tovalue string - if nodes then values will be separated by NODE_NAME_STRING_SEPARATOR

Return

string

Location

include/resource_functions.php lines 5717 to 5776

Definition

 
function log_diff($fromvalue$tovalue)
    {
    
$return '';
    
debug_function_call("log_diff",func_get_args());

    
// Trim values as it can cause out of memory errors with class.Diff.php e.g. when saving extracted text or creating previews for large PDF files
    
if(strlen($fromvalue)>10000)
        {
        
$fromvalue mb_substr($fromvalue,10000);
        }
    if(
strlen($tovalue)>10000)
        {
        
$tovalue mb_substr($tovalue,10000);
        }

    
// Remove any database escaping
    
$fromvalue str_replace("\\"''$fromvalue);
    
$tovalue   str_replace("\\"''$tovalue);

    
// Work a different way for fixed lists
    
if(strpos($fromvalueNODE_NAME_STRING_SEPARATOR) !== false || strpos($tovalueNODE_NAME_STRING_SEPARATOR) !== false)
        {
        
$fromvalue array_filter(explode(NODE_NAME_STRING_SEPARATOR$fromvalue));
        
$tovalue   array_filter(explode(NODE_NAME_STRING_SEPARATOR$tovalue));

        
// Empty arrays if either side is blank.
        
if (count($fromvalue)==0) {$fromvalue=array();}
        if (
count($tovalue)==0)   {$tovalue=array();}

        
// Get diffs
        
$inserts array_diff($tovalue$fromvalue);
        
$deletes array_diff($fromvalue$tovalue);

        
// Process array diffs into meaningful strings
        
if(count($deletes))
            {
            
$return .= '- ' join("\n- " $deletes);
            }

        if(
count($inserts))
            {
            if(
'' != $return)
                {
                
$return .= "\n";
                }

            
$return .= '+ ' join("\n+ "$inserts);
            }

        return 
$return;
        }

    
// Simple return when either side is blank (the user is adding or removing all the text)
    
if ($fromvalue=="") {return "+ " $tovalue;}
    if (
$tovalue=="") {return "- " $fromvalue;}

    
// For standard strings, use Diff library
    
require_once dirname(__FILE__) . '/../lib/Diff/class.Diff.php';
    return 
Diff::toString(Diff::compare($fromvalue$tovalue));
    }

This article was last updated 19th March 2024 08:05 Europe/London time based on the source file dated 11th March 2024 14:25 Europe/London time.