Open Journal Systems  3.3.0
removeLocaleKey.php
1 <?php
2 
16 require(dirname(dirname(dirname(dirname(__FILE__)))) . '/tools/bootstrap.inc.php');
17 
19 
21  public $localeKey = '';
22 
24  public $dirs = ['locale', 'lib/pkp/locale'];
25 
29  function __construct($argv = array()) {
30  parent::__construct($argv);
31 
32  if (!sizeof($this->argv)) {
33  $this->usage();
34  exit(1);
35  }
36 
37  array_shift($argv);
38 
39  $this->localeKey = array_shift($argv);
40 
41  if (sizeof($this->argv) > 2) {
42  $this->dirs = $argv;
43  }
44  }
45 
49  function usage() {
50  echo "\nRemove a locale key from all locale files.\n\n"
51  . " Usage: php {$this->scriptName} [localeKey] ([path] [path])\n\n"
52  . " Remove locale keys from app:\n php {$this->scriptName} locale.key locale\n\n"
53  . " Remove locale keys from pkp-lib:\n php {$this->scriptName} locale.key lib/pkp/locale\n\n"
54  . " If no path is specified it will remove the locale\n key from files in both directories.\n\n";
55  }
56 
60  function execute() {
61 
62  $localeKeyLine = 'msgid "' . $this->localeKey . '"';
63  $rootDir = dirname(dirname(dirname(dirname(__FILE__))));
64 
65  foreach ($this->dirs as $dir) {
66  $locales = scandir($rootDir . '/' . $dir);
67  foreach ($locales as $locale) {
68  if ($locale === '.' || $locale === '..') {
69  continue;
70  }
71  $localeDir = join('/', [$rootDir, $dir, $locale]);
72  $files = scandir($localeDir);
73  foreach ($files as $file) {
74  if ($file === '.' || $file === '..' || substr($file, -2) !== 'po') {
75  continue;
76  }
77  $content = file_get_contents($localeDir . '/' . $file);
78  $lines = explode("\n", $content);
79  $newLines = [];
80  $removing = false;
81  foreach($lines as $line) {
82  if ($localeKeyLine === substr($line, 0, strlen($localeKeyLine))) {
83  $removing = true;
84  } elseif ($removing && 'msgid' === substr($line, 0, strlen('msgid'))) {
85  $removing = false;
86  }
87  if (!$removing) {
88  $newLines[] = $line;
89  }
90  }
91  if (count($lines) !== count($newLines)) {
92  file_put_contents($localeDir . '/' . $file, join("\n", $newLines));
93  echo (count($lines) - count($newLines)) . " lines removed from $localeDir/$file.\n";
94  }
95  }
96  }
97  }
98  }
99 }
100 
101 $tool = new RemoveLocaleKey(isset($argv) ? $argv : array());
102 $tool->execute();
103 
104 
RemoveLocaleKey\$localeKey
$localeKey
Definition: removeLocaleKey.php:24
RemoveLocaleKey\__construct
__construct($argv=array())
Definition: removeLocaleKey.php:35
CommandLineTool
Initialization code for command-line scripts.
Definition: CliTool.inc.php:44
$tool
$tool
Definition: mergeCoverageReportTool.php:120
RemoveLocaleKey\execute
execute()
Definition: removeLocaleKey.php:66
RemoveLocaleKey\$dirs
$dirs
Definition: removeLocaleKey.php:30
Seboettg\Collection\count
count()
Definition: ArrayListTrait.php:253
RemoveLocaleKey\usage
usage()
Definition: removeLocaleKey.php:55
RemoveLocaleKey
Remove a locale key from all locale files.
Definition: removeLocaleKey.php:18
CommandLineTool\$argv
$argv
Definition: CliTool.inc.php:53