ReleaseCleaner.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Elgg\I18n;
  3. /**
  4. * Removes invalid language files from an installation
  5. *
  6. * @access private
  7. */
  8. class ReleaseCleaner {
  9. /**
  10. * @var string[]
  11. */
  12. private $codes;
  13. /**
  14. * @var string[]
  15. */
  16. public $log = [];
  17. /**
  18. * Constructor
  19. *
  20. * @param string[] $codes Valid language codes
  21. */
  22. public function __construct(array $codes = []) {
  23. if (!$codes) {
  24. $codes = Translator::getAllLanguageCodes();
  25. }
  26. $this->codes = $codes;
  27. }
  28. /**
  29. * Clean up within an installation
  30. *
  31. * @param string $dir The installation dir
  32. *
  33. * @return void
  34. */
  35. public function cleanInstallation($dir) {
  36. $dir = rtrim($dir, '/\\');
  37. if (is_dir("$dir/languages")) {
  38. $this->cleanLanguagesDir("$dir/languages");
  39. }
  40. $dir = "$dir/mod";
  41. foreach (scandir($dir) as $entry) {
  42. if ($entry[0] === '.') {
  43. continue;
  44. }
  45. $path = "$dir/$entry";
  46. if (is_dir("$path/languages")) {
  47. $this->cleanLanguagesDir("$path/languages");
  48. }
  49. }
  50. }
  51. /**
  52. * Clean up a languages dir
  53. *
  54. * @param string $dir Languages dir
  55. *
  56. * @return void
  57. */
  58. public function cleanLanguagesDir($dir) {
  59. $dir = rtrim($dir, '/\\');
  60. foreach (scandir($dir) as $entry) {
  61. if ($entry[0] === '.') {
  62. continue;
  63. }
  64. if (pathinfo($entry, PATHINFO_EXTENSION) !== 'php') {
  65. continue;
  66. }
  67. $path = "$dir/$entry";
  68. $code = basename($entry, '.php');
  69. if (!in_array($code, $this->codes)) {
  70. $code = Translator::normalizeLanguageCode($code);
  71. if (in_array($code, $this->codes)) {
  72. // rename file to lowercase
  73. rename($path, "$dir/$code.php");
  74. $this->log[] = "Renamed $path to $code.php";
  75. continue;
  76. }
  77. unlink($path);
  78. $this->log[] = "Removed $path";
  79. }
  80. }
  81. }
  82. }