<?PHP
/* Tra() dedup V1.50 20210326 hman                                                  */
/*                                                                                  */
/* Deduplication test script for PHP & templates                                    */
/* Preserves single line comments, array beginning/end and structuring newlines.    */
/* = Limitations:                                                                   */
/*    Does not handle multi line comments!                                          */
/* = Usage: Place into directory with output of tra_extract2.                       */
/* Rename PHP file to be processed into test.php                                    */
/*    Output will be in tra_dedup.php.txt.                                          */
/*    Errors will be logged in duplicate_error_php.txt.                             */
/* If output pre-exists, script terminates, if log pre-exists, it will be appended. */

/* global definitions, so they can be used in functions */
global $fo;
global $outputcache;

function writecache() {
/* Flush contents of cached extracts to file, if any output occurred for the current file */
/* Inform the user about the location of the found string as line number, file name is in */
/* the comment that got inserted for each file.                                           */

global $fo;
global $outputcache;

	if (count($outputcache) > 0) {
		foreach ($outputcache as $element) {
			if ($element[1] == "")
				fwrite($fo, $element[0]."\n");
			else
				/* quotation marks already in read file, don't add again */
				fwrite($fo, $element[0]." => ".$element[1]."\n");
		}
		$outputcache=[];
		/* Add the empty line that caused cache flush to output */
		fwrite($fo, "\n");
	}
}


function chk_file($filename) {
/* Check a given file for duplicates. PHP would only overwrite old key/value pairs, */
/* this function finds them, so they can be manually compared and eliminated */

global $fo;
global $outputcache;

	$outputcache=[];
	if ( file_exists("./duplicate_error_php.txt") ) {
	/* tell user file pre-exists */
		echo "-- Error! File ./duplicate_error_php.txt already exists! Appending.\n";
	}
	/* file for duplicate error logging */
	$fe = fopen("./duplicate_error_php.txt", "a+");

	/* read the input file given, skip \n */
	$fl = file($filename);

	if (!$fl) {
		echo "-- Error! File " . $filename . " cannot be opened!\n";
		return;
	}

	if ( file_exists("./tra_dedup.php.txt") ) {
	/* tell user file pre-exists */
		echo "-- Error! File ./tra_dedup.php.txt already exists! Exiting.\n";
		return;
	} else
		$fo = fopen("./tra_dedup.php.txt", "w");

	$fl = file($filename,FILE_IGNORE_NEW_LINES);
	/* input file given, skip \n */

	if (!$fl) {
		echo "-- Error! File " . $filename . " cannot be opened!\n";
		return;
	}


	echo "== Checking file ". $filename . "\n";
	fwrite ($fe, "== Checking file ". $filename . "\n");

	$array = array();
	$linenos = array();

	$linecount = 0;

	foreach ($fl as $line) {	
		$linecount++;
		if ($line == "") {
			/* copy comments (file paths) and newlines */
			if ((count($outputcache) == 1) && (substr($outputcache[0][0],0,2) == "//")) {
				/* empty line after comment */
				$outputcache = []; }
			else writecache();
		} 

		if ((substr($line,0,2) == "//") ||
		(substr($line,0,2) == "/*") ||
		(substr($line,0,2) == "<?") ||
		(substr($line,0,1) == "$") ||
		(substr($line,0,1) == "]")) {
			$outputcache[]=array($line,"");			

			/* do not output & error log content, just paths */
			if (substr($line,0,2) == "//") {
				echo $line."\n";
				fwrite ($fe, $line."\n");
			}
		}

		/* modify this clause if you named the array differently! */
		If ( ($line != "") AND ($line != "\n") AND ($line != "\$lang_extract_raw = [\n") AND ($line != "]\n") ) {

			list($key, $value) = explode(' => ', $line, 2) + array(NULL, NULL);
			if (array_key_exists ($key, $array)) {
				/* previous definition */
				echo "-- Duplicate: key in line " . $linenos[$key] ."\n";
				echo $key . " => ". $array[$key] . "\n";
				/* duplicate definition */
				echo "   is identical with line " . $linecount . ".\n";
				echo $key . " => ". $value . "\n";

				/* same as above, log in file. 2nd newline for looks */
				fwrite ($fe, "-- Duplicate: key in line " . $linenos[$key] ."\n");
				fwrite ($fe,  $key . " : ". $array[$key] . "\n");
				fwrite ($fe, "   is identical with line " . $linecount . ".\n");
				fwrite ($fe,  $key . " : ". $value . "\n\n");
			}
			else if ($value !== NULL) {
				/* remember keys and their line numbers */
				$array[$key] = $value;
				$linenos[$key] = $linecount;
				$outputcache[]=array($key,$value);			
		    	}	
		}

	}

	writecache();

	echo "== Check completed.\n\n";
	fwrite ($fe, "== Check completed.\n\n"); 

	fflush ($fe);
	if (!fclose($fe)) {
   		echo "-- Error! Output file ./duplicat_error_php.txt is not closeable! Exiting.\n";
		exit;
	}

	fflush ($fo);
	if (!fclose($fo)) {
   		echo "-- Error! Output file ./tra_dedup.php.txt is not closeable! Exiting.\n";
		exit;
	}
}
/* end function */

/* Main program */
echo "\n";
echo "=== Dedup2 PHP & Template V1.50 20210326 hman\n\n";
echo "Eliminates double entries in PHP & Template output of tra_extract2.\n";
echo "To be run in the directory containing the localization files.\n";
echo "Rename file to be processed to test.php. Output will be in tra_dedup.php.txt.\n";
echo "Errors will be logged in duplicate_error_php.txt.\n";
echo "If output pre-exists, script terminates, if log pre-exists, it will be appended.\n\n";

/* Check for double key definitions */
chk_file("./test.php");

/* Separation just for looks */
echo "\n";


/* end of program */
?>
