summaryrefslogtreecommitdiffstats
path: root/tools/global-candidates.php
blob: 12e900a564853770ca80af2b24b22cf6033c969e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php

$langs = $argc > 1 && $argv[1] ? $argv[1] : 'en';

echo "Scanning for $langs... (pass as comma separated list, no spaces)\n";

$tags = [];
$strings = [];

foreach (glob('./modules-available/*/lang/{' . $langs . '}/template-tags.json', GLOB_NOSORT | GLOB_BRACE) as $file) {
	preg_match('#modules-available/([^/]+)/lang/(..)#', $file, $out);
	$module = $out[1];
	$lang = $out[2];
	$j = json_decode(file_get_contents($file), true);
	if (!is_array($j)) continue;
	foreach ($j as $k => $v) {
		if (!isset($tags[$k])) {
			$tags[$k] = ['modules' => [], 'lang' => []];
		}
		$tags[$k]['modules'][$module] = true;
		if (!isset($tags[$k]['lang'][$lang])) {
			$tags[$k]['lang'][$lang] = [];
		}
		$tags[$k]['lang'][$lang][$v] = true;
		if (!isset($strings[$v])) {
			$strings[$v] = [];
		}
		if (!isset($strings[$v][$k])) {
			$strings[$v][$k] = [];
		}
		$strings[$v][$k][$module] = true;
	}
}

if ($argc > 1) {
	$find = array_flip(array_slice($argv, 2));
	print_r($find);
} else $find = [];

echo "\n\nDUPLICATE TAG NAME ACROSS DIFFERENT MODULES:\n";
foreach ($tags as $k => &$tag) {
	if (isset($find[$k])) {
		echo "## LOOKUP: '$k'\n";
		print_r($tag['lang']);
	}
	if (count($tag['modules']) < 4) continue;
	$tag['modules'] = array_keys($tag['modules']);
	foreach ($tag['lang'] as &$lang) {
		$lang = array_keys($lang);
	}
	unset($lang);
	echo "## Common tag '$k'\n";
	echo "   In " . count($tag['modules']) . " modules: " . implode(', ', $tag['modules']) . "\n";
	foreach ($tag['lang'] as $lang => $str) {
		echo "   " . count($str) . " in $lang: '" . implode("', '", $str) . "'\n";
		if (count($str) / count($tag['modules']) < 0.26) {
			echo "   +++ Possible candidate +++\n";
		}
	}
}

echo "\n\nDUPLICATE STRINGS WITH DIFFERENT NAMES:\n";
foreach ($strings as $text => $data) {
	if (count($data) < 3) continue;
	echo "## '$text' ##\n";
	foreach ($data as $tag => $mods) {
		echo "   As $tag in";
		foreach($mods as $mod => $count) {
			echo " $mod($count) ";
		}
		echo "\n";
	}
}