Elgg  Version 4.3
ReleaseCleaner.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\I18n;
4 
6 use Elgg\Includer;
8 
15 
19  private $codes;
20 
24  public $log = [];
25 
31  public function __construct(array $codes = []) {
32  if (empty($codes)) {
33  $codes = _elgg_services()->locale->getLanguageCodes();
34  }
35  $this->codes = $codes;
36  }
37 
45  public function cleanInstallation(string $dir): void {
46  $dir = Paths::sanitize($dir, false);
47 
48  if (is_dir("{$dir}/install/languages")) {
49  $this->cleanLanguagesDir("{$dir}/install/languages");
50  }
51 
52  if (is_dir("{$dir}/languages")) {
53  $this->cleanLanguagesDir("{$dir}/languages");
54  }
55 
56  $mods = new \DirectoryIterator("{$dir}/mod");
57 
58  foreach ($mods as $mod) {
59  if ($mod->isDot() || !$mod->isDir()) {
60  continue;
61  }
62 
63  if (!in_array($mod->getFilename(), Plugins::BUNDLED_PLUGINS)) {
64  // not a core plugin
65  continue;
66  }
67 
68  if (is_dir("{$mod->getPathname()}/languages")) {
69  // only process plugins which have translations
70  $this->cleanLanguagesDir("{$mod->getPathname()}/languages");
71  }
72  }
73  }
74 
82  public function cleanLanguagesDir(string $dir): void {
83  $dir = Paths::sanitize($dir, false);
84 
85  $files = new \DirectoryIterator($dir);
86  foreach ($files as $file) {
87  if ($file->isDot() || !$file->isFile()) {
88  continue;
89  }
90 
91  if ($file->getExtension() !== 'php') {
92  continue;
93  }
94 
95  $code = $file->getBasename('.php');
96  if (!in_array($code, $this->codes)) {
98 
99  if (in_array($code, $this->codes)) {
100  // rename file to lowercase
101  rename($file->getPathname(), "{$dir}/{$code}.php");
102  $this->log[] = "Renamed {$file->getPathname()} to {$code}.php";
103  } else {
104  unlink($file->getPathname());
105  $this->log[] = "Removed {$file->getPathname()}";
106  }
107  }
108 
109  if ($code !== 'en' && file_exists("{$dir}/{$code}.php")) {
111  $this->cleanupEmptyTranslations("{$dir}/{$code}.php");
112  }
113  }
114  }
115 
124  protected function cleanupMissingTranslationParameters(string $directory, string $language_code): void {
125  $english = Includer::includeFile("{$directory}/en.php");
126  $translation = Includer::includeFile("{$directory}/{$language_code}.php");
127 
128  foreach ($english as $key => $value) {
129  $english_matches = preg_match_all('/%[a-zA-Z]/m', $value);
130  if (!array_key_exists($key, $translation) || $english_matches === false) {
131  continue;
132  }
133 
134  $translation_matches = preg_match_all('/%[a-zA-Z]/m', $translation[$key]);
135  if ($translation_matches !== false && $english_matches === $translation_matches) {
136  continue;
137  }
138 
139  $file_contents = file_get_contents("{$directory}/{$language_code}.php");
140 
141  $pattern = '/^\s*[\'"]' . $key . '[\'"] => [\'"]' . preg_quote($translation[$key], '/') . '[\'"],{0,1}\R/m';
142  $count = 0;
143  $file_contents = preg_replace($pattern, '', $file_contents, -1, $count);
144  if ($count < 1) {
145  // try to add slashes for quotes
146  $pattern = '/^\s*[\'"]' . $key . '[\'"] => [\'"]' . preg_quote(addslashes($translation[$key]), '/') . '[\'"],{0,1}\R/m';
147  $count = 0;
148  $file_contents = preg_replace($pattern, '', $file_contents, -1, $count);
149  }
150 
151  if ($count > 0) {
152  file_put_contents("{$directory}/{$language_code}.php", $file_contents);
153  } else {
154  $this->log[] = "Unable to repair mismatch in translation argument count in {$directory}/{$language_code}.php for the key '{$key}'";
155  }
156  }
157  }
158 
166  protected function cleanupEmptyTranslations(string $translation_file): void {
167  $contents = file_get_contents($translation_file);
168  if (empty($contents)) {
169  return;
170  }
171 
172  $pattern = '/^\s*[\'"].*[\'"] => [\'"]{2},{0,1}\R/m';
173  $count = 0;
174  $contents = preg_replace($pattern, '', $contents, -1, $count);
175 
176  if ($count > 0) {
177  // something was changed
178  file_put_contents($translation_file, $contents);
179 
180  $this->log[] = "Cleaned empty translations from {$translation_file}";
181  }
182  }
183 
191  protected function normalizeLanguageCode(string $code): string {
192  $code = strtolower($code);
193  $code = preg_replace('~[^a-z0-9]~', '_', $code);
194 
195  return $code;
196  }
197 }
static includeFile($file)
Include a file with as little context as possible.
Definition: Includer.php:18
Removes invalid language files from an installation.
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
cleanupMissingTranslationParameters(string $directory, string $language_code)
Try to cleanup translations with a different argument count than English as this can cause failed tra...
cleanupEmptyTranslations(string $translation_file)
Remove empty translations from a translation file.
cleanLanguagesDir(string $dir)
Clean up a languages dir.
$value
Definition: generic.php:51
normalizeLanguageCode(string $code)
Normalize a language code (e.g.
__construct(array $codes=[])
Constructor.
$count
Definition: ban.php:24
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
log($level, $message, array $context=[])
Log a message.
Definition: Loggable.php:58
cleanInstallation(string $dir)
Clean up within an installation.
if(!$plugin) $file_contents
static sanitize($path, $append_slash=true)
Sanitize file paths ensuring that they begin and end with slashes etc.
Definition: Paths.php:76
_elgg_services()
Get the global service provider.
Definition: elgglib.php:638
if(!empty($title)&&!empty($icon_name)) if(!empty($title)) if(!empty($menu)) if(!empty($header)) if(!empty($body)) $contents
Definition: message.php:73