#!/usr/bin/env php * Copyright (c) 2015 Laurent Destailleur * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ $sapi_type = php_sapi_name(); $script_file = basename(__FILE__); $path=dirname(__FILE__).'/'; $web=0; // Test if batch mode if (substr($sapi_type, 0, 3) == 'cgi') { $web=1; } if ($web) { echo ""; echo ""; echo ""; echo ""; } echo "If you call this file with the argument \"?unused=true\" it searches for the translation strings that exist in en_US but are never used.\n"; if ($web) print "
"; echo "IMPORTANT: that can take quite a lot of time (up to 10 minutes), you need to tune the max_execution_time on your php.ini accordingly.\n"; if ($web) print "
"; // STEP 1 - Search duplicates keys // directory containing the php and lang files $htdocs = $path."/../../htdocs/"; // directory containing the english lang files $workdir = $htdocs."langs/en_US/"; $files = scandir($workdir); if (empty($files)) { echo "Can't scan workdir = ".$workdir; exit; } $exludefiles = array('.','..','README'); $files = array_diff($files,$exludefiles); $langstrings_3d = array(); $langstrings_full = array(); foreach ($files AS $file) { $path_file = pathinfo($file); // we're only interested in .lang files if ($path_file['extension']=='lang') { $content = file($workdir.$file); foreach ($content AS $line => $row) { // don't want comment lines if (substr($row,0,1) !== '#') { // don't want lines without the separator (why should those even be here, anyway...) if (strpos($row,'=')!==false) { $row_array = explode('=',$row); // $row_array[0] = key $langstrings_3d[$path_file['basename']][$line+1]=$row_array[0]; $langstrings_3dtrans[$path_file['basename']][$line+1]=$row_array[1]; $langstrings_full[]=$row_array[0]; $langstrings_dist[$row_array[0]]=$row_array[0]; } } } } } foreach ($langstrings_3d AS $filename => $file) { foreach ($file AS $linenum => $value) { $keys = array_keys($langstrings_full, $value); if (count($keys)>1) { foreach ($keys AS $key) { $dups[$value][$filename][$linenum] = trim($langstrings_3dtrans[$filename][$linenum]); } } } } if ($web) print "

"; print "Duplicate strings in lang files in $workdir - ".count($dups)." found\n"; if ($web) print "

"; if ($web) { echo ''."\n"; echo "\n"; echo "\n"; } $sduplicateinsamefile=''; $sinmainandother=''; $sininstallandadmin=''; $sother=''; $count = 0; foreach ($dups as $string => $pages) { $count++; $s=''; // Keyword $string if ($web) $s.=""; if ($web) $s.=""; if ($web) $s.=""; if ($web) $s.=""; $s.="\n"; if ($duplicateinsamefile) $sduplicateinsamefile .= $s; else if ($inmain) $sinmainandother .= $s; else if ($inadmin) $sininstallandadmin .= $s; else $sother .= $s; } if (! $web) print "\n***** Entries duplicated in same file\n"; print $sduplicateinsamefile; if (! $web && empty($sduplicateinsamefile)) print "None\n"; if (! $web) print "\n"; if (! $web) print "***** Entries in main and another (keep only entry in main)\n"; print $sinmainandother; if (! $web && empty($sinmainandother)) print "None\n"; if (! $web) print "\n"; if (! $web) print "***** Entries in admin and another\n"; print $sininstallandadmin; if (! $web && empty($sininstallandadmin)) print "None\n"; if (! $web) print "\n"; if (! $web) print "***** Other\n"; print $sother; if (! $web && empty($sother)) print "None\n"; if (! $web) print "\n"; if ($web) { echo "\n"; echo "
#StringFile and lines
"; if ($web) $s.=$count; if ($web) $s.=""; $s.=$string; if ($web) $s.=""; if (! $web) $s.= ' : '; // Loop on each files keyword was found $duplicateinsamefile=0; $inmain=0; $inadmin=0; foreach ($pages AS $file => $lines) { if ($file == 'main.lang') { $inmain=1; $inadmin=0; } if ($file == 'admin.lang' && ! $inmain) { $inadmin=1; } $s.=$file." "; // Loop on each line keword was found into file. $listoffilesforthisentry=array(); foreach ($lines as $line => $translatedvalue) { if (! empty($listoffilesforthisentry[$file])) $duplicateinsamefile=1; $listoffilesforthisentry[$file]=1; $s.= "(".$line." - ".htmlentities($translatedvalue).") "; } if ($web) $s.="
"; } if ($web) $s.="
\n"; } // STEP 2 - Search key not used if (! empty($_REQUEST['unused']) && $_REQUEST['unused'] == 'true') { foreach ($langstrings_dist AS $value) { $search = '\'trans("'.$value.'")\''; $string = 'grep -R -m 1 -F --exclude=includes/* --include=*.php '.$search.' '.$htdocs.'*'; exec($string,$output); if (empty($output)) { $unused[$value] = true; echo $value.'
'; } } if ($web) print "

\n"; print "Strings in en_US that are never used\n"; if ($web) print "

\n"; if ($web) echo "
";
	print_r($unused);
	if ($web) echo "
\n"; } echo "\n"; if ($web) { echo "\n"; echo "\n"; } exit;