4 if(php_sapi_name()!==
'cli' || !
defined(
'STDIN')){
5 echo
"Only cli execution mode supported\n";
19 $ignoredPaths = array(
'0:crawler_time',
'crawler_time',
'0:scraper_time',
'scraper_time',
'0:pubdate',
'pubdate',
'pubdate:',
'0:dc_date',
'dc_date:',
'dc_date',
'dbFields:ProcessingTime');
22 echo
"Usage: ".$argv[0].
" --original=<json_file_1> --compare=<json_file_1> [--path=<custom field_path for check>] [--format=<answer format (json, csv, xml, html, sql, text), default=json>] [--type=<compare type (text, md5 or text), default=text>] [--input_type=<type of input format (news or rss), default=news>]\n";
27 echo
"File ".$originalFile.
" not found. Exit.\n";
31 echo
"File ".$compareFile.
" not found. Exit.\n";
35 function errHandle($errNo, $errStr, $errFile, $errLine) {
36 $msg =
"Error $errNo: $errStr in $errFile on line $errLine";
37 if ($errNo == E_WARNING or $errNo == UPLOAD_ERR_EXTENSION) {
38 exitScript(
"Wrong format or wrong validation of input data\n");
44 set_error_handler(
'errHandle');
47 if (crc32(serialize($first)) === crc32(serialize($second)))
return true;
52 if (md5(serialize($first)) === md5(serialize($second)))
return true;
57 if ($first === $second)
return true;
61 function check($first, $second){
63 switch ($compareType) {
80 if (!is_array($array))
return false;
81 $ritit =
new RecursiveIteratorIterator(
new RecursiveArrayIterator($array));
83 foreach ($ritit as $leafValue) {
85 foreach (range(0, $ritit->getDepth()) as $depth) {
86 $path[] = $ritit->getSubIterator($depth)->key();
88 $results[join(
':', $path)] = $leafValue;
94 if(is_array($input_json)){
97 $dir=rawurldecode($dir);
98 if(isset($input_json[$dir])){
99 $input_json=$input_json[$dir];
109 foreach ($argv as $arg) {
110 if (preg_match(
'/--([^=]+)=(.*)/',$arg,$reg)) {
111 $_ARG[$reg[1]] = $reg[2];
112 } elseif(preg_match(
'/-([a-zA-Z0-9])/',$arg,$reg)) {
113 $_ARG[$reg[1]] =
'true';
120 if (is_array($data))
return array(
'type'=>
'array',
'data' => $data);
121 if (base64_encode(base64_decode($data)) === $data){
122 if (json_decode(base64_decode($data),
true))
return array(
'type'=>
'array',
'data' => json_decode(base64_decode($data),
true));
123 else return array(
'type'=>
'data',
'data' => base64_decode($data));
125 if (json_decode($data,
true))
return array(
'type'=>
'json',
'data' => json_decode($data,
true));
126 else return array(
'type'=>
'data',
'data' => $data);
131 if(!file_exists($filename) || !is_readable($filename))
return false;
134 if (($handle = fopen($filename,
'r')) !== FALSE) {
135 while (($row = fgetcsv($handle, 0, $delimiter)) !== FALSE) {
139 $data[] = array_combine($header, $row);
144 if (!empty($data))
return $data;
149 if(!file_exists($filename) || !is_readable($filename))
return false;
151 if (($handle = fopen($filename,
'r')) !== FALSE) {
152 while (($row = fgetcsv($handle, 0, $delimiter)) !== FALSE) {
157 if (!empty($data))
return $data;
162 if(!file_exists($filename) || !is_readable($filename))
return false;
164 if (($handle = fopen($filename,
'r')) !== FALSE) {
165 while (($row = fgetcsv($handle, 0, $delimiter)) !== FALSE) {
167 for ($c=0; $c < $num; $c++) {
168 array_push($data, $row[$c]);
173 if (!empty($data))
return $data;
178 #if (stripos($html, '<!DOCTYPE html>') === false) return false;
181 $dom =
new domDocument;
182 $dom->recover =
true;
183 $dom->strictErrorChecking =
false;
184 #libxml_use_internal_errors(true);
185 #@$dom->loadHTML(rawurlencode($html));
186 @$dom->loadHTML($html);
187 #libxml_clear_errors();
188 $dom->preserveWhiteSpace =
false;
189 $tables = $dom->getElementsByTagName(
'table');
190 $rows = $tables->item(0)->getElementsByTagName(
'tr');
191 if ($rows->length == 0)
return false;
192 foreach ($rows as $row) {
193 $cols = $row->getElementsByTagName(
'td');
194 $array[$cols->item(0)->textContent] = $cols->item(1)->textContent;
201 if ($tempfile) unlink($tempfile);
207 $json = json_decode(file_get_contents($file),
true);
221 if ($originalJson[
'type'] ==
'array' and $compareJson[
'type'] ==
'array') {
222 $originalNewArray =
getPathValue($originalJson[
'data']);
225 if ($originalNewArray ==
false or $compareNewArray ==
false) {
229 if (count($originalNewArray) != count($compareNewArray)) {
230 exitScript(
"Number of elements of array not compare\n");
232 foreach ($originalNewArray as $key => $value) {
233 if (array_key_exists($key, $compareNewArray)) {
234 if (in_array($key, $ignoredPaths)){
235 if (stripos(
'%', trim($value)) or stripos(
'%', trim($compareNewArray[$key]))){
236 $check =
check(trim($value), trim($compareNewArray[$key]));
237 if ($check ==
false) print
"Error in path '".$key.
"'. Original value: '".$value.
"', compare value: '".$compareNewArray[$key].
"'\n\n";
240 $check =
check(trim($value), trim($compareNewArray[$key]));
241 if ($check ==
false) print
"Error in path '".$key.
"'. Original value: '".$value.
"', compare value: '".$compareNewArray[$key].
"'\n\n";
244 print
"Error. Key ".$key.
" not exist in compare file\n";
247 } elseif ($originalJson[
'type'] ==
'data' and $compareJson[
'type'] ==
'data') {
260 if(empty($compareCsv[
'data'])) {
264 if ($originalCsv[
'type'] ==
'array' or $compareCsv[
'type'] ==
'array') {
268 $tempfile = tempnam(
"/tmp", pathinfo(__FILE__,PATHINFO_FILENAME));
270 if ($type ==
'rss') {
271 file_put_contents($tempfile, str_replace(array(
"\"\r\n,\"",
"\"\r,\"",
"\"\n,\""),
'","', $originalCsv[
'data']));
274 file_put_contents($tempfile, str_replace(array(
"\"\r\n,\"",
"\"\r,\"",
"\"\n,\""),
'","', $compareCsv[
'data']));
277 file_put_contents($tempfile, str_replace(array(
"\"\r\n,\"",
"\"\r,\"",
"\"\n,\""),
'","', $originalCsv[
'data']));
278 #$originalNewArray = getPathValue(csv_to_array($tempfile));
281 file_put_contents($tempfile, str_replace(array(
"\"\r\n,\"",
"\"\r,\"",
"\"\n,\""),
'","', $compareCsv[
'data']));
282 #$compareNewArray = getPathValue(csv_to_array($tempfile));
288 if ($originalNewArray ==
false or $compareNewArray ==
false) {
292 if (count($originalNewArray) != count($compareNewArray)) {
293 exitScript(
"Number of elements of csv not compare");
296 foreach ($originalNewArray as $key => $value) {
297 if (array_key_exists($key, $compareNewArray)) {
298 if (array_key_exists($key, $compareNewArray)) {
299 if (in_array($key, $ignoredPaths)){
300 if (stripos(
'%', trim($value)) or stripos(
'%', trim($compareNewArray[$key]))){
301 $check =
check(trim($value), trim($compareNewArray[$key]));
302 if ($check ==
false) print
"Error in path '".$key.
"'. Original value: '".$value.
"', compare value: '".$compareNewArray[$key].
"'\n\n";
305 $check =
check(trim($value), trim($compareNewArray[$key]));
306 if ($check ==
false) print
"Error in path '".$key.
"'. Original value: '".$value.
"', compare value: '".$compareNewArray[$key].
"'\n\n";
309 print
"Error. Key ".$key.
" not exist in compare file\n";
312 print
"Error. Key ".$key.
" not exist in compare file\n";
322 if ($originalCsv[
'type'] ==
'array' or $compareCsv[
'type'] ==
'array' or stripos($originalCsv[
'data'],
'<?xml version=') ===
false or stripos($compareCsv[
'data'],
'<?xml version=') ===
false) {
326 $xmlOriginal = simplexml_load_string($originalCsv[
'data'],
'SimpleXMLElement', LIBXML_NOCDATA);
327 $originalArray = (array)$xmlOriginal->item;
328 $xmlCompare = simplexml_load_string($compareCsv[
'data'],
'SimpleXMLElement', LIBXML_NOCDATA);
329 $compareArray = (array)$xmlCompare->item;
331 if ($xmlOriginal ===
false or $xmlCompare ===
false) {
334 if (count($originalArray) != count($compareArray)) {
335 exitScript(
"Number of elements of array not compare\n");
337 foreach ($originalArray as $key => $value) {
338 if (array_key_exists($key, $compareArray)) {
339 if (in_array($key, $ignoredPaths)){
340 if (stripos(
'%', trim($value)) or stripos(
'%', trim($compareArray[$key]))){
341 $check =
check(trim($value), trim($compareArray[$key]));
342 if ($check ==
false) print
"Error in path '".$key.
"'. Original value: '".$value.
"', compare value: '".$compareArray[$key].
"'\n\n";
345 $check =
check(trim($value), trim($compareArray[$key]));
346 if ($check ==
false) print
"Error in path '".$key.
"'. Original value: '".$value.
"', compare value: '".$compareArray[$key].
"'\n\n";
349 print
"Error. Key ".$key.
" not exist in compare file\n";
360 if ($originalHtml[
'type'] ==
'array' or $compareHtml[
'type'] ==
'array') {
364 #$originalArray = htmlParse(str_replace(array("\r\n", "\r", "\n"), '', $originalHtml['data']));
365 #$compareArray = htmlParse(str_replace(array("\r\n", "\r", "\n"), '', $compareHtml['data']));
366 $originalArray =
htmlParse($originalHtml[
'data']);
367 $compareArray =
htmlParse($compareHtml[
'data']);
369 if ($originalArray ===
false or $compareArray ===
false) {
372 if (count($originalArray) != count($compareArray)) {
373 exitScript(
"Number of elements of array not compare\n");
375 foreach ($originalArray as $key => $value) {
376 if (array_key_exists($key, $compareArray)) {
377 if (in_array($key, $ignoredPaths)){
378 if (stripos(
'%', trim($value)) or stripos(
'%', trim($compareArray[$key]))){
379 $check =
check(trim($value), trim($compareArray[$key]));
380 if ($check ==
false) print
"Error in path '".$key.
"'. Original value: '".$value.
"', compare value: '".$compareArray[$key].
"'\n\n";
383 $check =
check(trim($value), trim($compareArray[$key]));
384 if ($check ==
false) print
"Error in path '".$key.
"'. Original value: '".$value.
"', compare value: '".$compareArray[$key].
"'\n\n";
387 print
"Error. Key ".$key.
" not exist in compare file\n";
397 if ($originalSql[
'type'] ==
'array' or $compareSql[
'type'] ==
'array') {
400 if (stripos($originalSql[
'data'],
'INSERT INTO') ===
false or stripos($compareSql[
'data'],
'INSERT INTO') ===
false) {
404 preg_match(
'/INSERT INTO.*\((.*)\).*VALUES \n(.*).*/', $originalSql[
'data'], $originalOut);
405 preg_match(
'/INSERT INTO.*\((.*)\).*VALUES \n\((.*)\).*/', $compareSql[
'data'], $compareOut);
407 $originalOutHeader = $originalOut[1];
408 $originalCsvHeader = explode(
",", $originalOutHeader);
409 $originalOut = ltrim($originalOut[2],
'(');
410 $originalOut = rtrim($originalOut,
');');
411 $originalCsvVal = explode(
'), (', $originalOut);
413 $compareOutHeader = $compareOut[1];
414 $compareCsvHeader = explode(
",", $compareOutHeader);
415 $compareOut = ltrim($compareOut[2],
'(');
416 $compareOut = rtrim($compareOut,
');');
417 $compareCsvVal = explode(
'), (', $compareOut);
419 if (count($originalCsvHeader) != count($compareCsvHeader)) {
420 exitScript(
"Number of elements in INSERT not compare\n");
423 if (count($originalCsvVal) != count($compareCsvVal)) {
424 exitScript(
"Number of elements in VALUE not compare\n");
427 $check =
check($originalCsvHeader, $compareCsvHeader);
428 if ($check ==
false)
exitScript(
"Not compare INSERT");
430 $check =
check($originalCsvVal, $compareCsvVal);
431 if ($check ==
false)
exitScript(
"Not compare VALUE");
438 if ($originalText[
'type'] ==
'array' or $compareText[
'type'] ==
'array') {
441 $check =
check($originalText, $compareText);
442 if ($check ==
false)
exitScript(
"Not compare");
465 print
"Wrong format\n";