You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

117 lines
3.2 KiB

  1. #!/usr/bin/env php
  2. <?php
  3. use splitbrain\phpcli\CLI;
  4. use splitbrain\phpcli\Options;
  5. if (!defined('DOKU_INC')) define('DOKU_INC', realpath(__DIR__ . '/../') . '/');
  6. define('NOSESSION', 1);
  7. require_once(DOKU_INC . 'inc/init.php');
  8. /**
  9. * Remove unwanted languages from a DokuWiki install
  10. */
  11. class StripLangsCLI extends CLI
  12. {
  13. /**
  14. * Register options and arguments on the given $options object
  15. *
  16. * @param Options $options
  17. * @return void
  18. */
  19. protected function setup(Options $options)
  20. {
  21. $options->setHelp(
  22. 'Remove all languages from the installation, besides the ones specified. English language ' .
  23. 'is never removed!'
  24. );
  25. $options->registerOption(
  26. 'keep',
  27. 'Comma separated list of languages to keep in addition to English.',
  28. 'k',
  29. 'langcodes'
  30. );
  31. $options->registerOption(
  32. 'english-only',
  33. 'Remove all languages except English',
  34. 'e'
  35. );
  36. }
  37. /**
  38. * Your main program
  39. *
  40. * Arguments and options have been parsed when this is run
  41. *
  42. * @param Options $options
  43. * @return void
  44. */
  45. protected function main(Options $options)
  46. {
  47. if ($options->getOpt('keep')) {
  48. $keep = explode(',', $options->getOpt('keep'));
  49. if (!in_array('en', $keep)) $keep[] = 'en';
  50. } elseif ($options->getOpt('english-only')) {
  51. $keep = ['en'];
  52. } else {
  53. echo $options->help();
  54. exit(0);
  55. }
  56. // Kill all language directories in /inc/lang and /lib/plugins besides those in $langs array
  57. $this->stripDirLangs(realpath(__DIR__ . '/../inc/lang'), $keep);
  58. $this->processExtensions(realpath(__DIR__ . '/../lib/plugins'), $keep);
  59. $this->processExtensions(realpath(__DIR__ . '/../lib/tpl'), $keep);
  60. }
  61. /**
  62. * Strip languages from extensions
  63. *
  64. * @param string $path path to plugin or template dir
  65. * @param array $keep_langs languages to keep
  66. */
  67. protected function processExtensions($path, $keep_langs)
  68. {
  69. if (is_dir($path)) {
  70. $entries = scandir($path);
  71. foreach ($entries as $entry) {
  72. if ($entry != "." && $entry != "..") {
  73. if (is_dir($path . '/' . $entry)) {
  74. $plugin_langs = $path . '/' . $entry . '/lang';
  75. if (is_dir($plugin_langs)) {
  76. $this->stripDirLangs($plugin_langs, $keep_langs);
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. /**
  84. * Strip languages from path
  85. *
  86. * @param string $path path to lang dir
  87. * @param array $keep_langs languages to keep
  88. */
  89. protected function stripDirLangs($path, $keep_langs)
  90. {
  91. $dir = dir($path);
  92. while (($cur_dir = $dir->read()) !== false) {
  93. if ($cur_dir != '.' && $cur_dir != '..' && is_dir($path . '/' . $cur_dir)) {
  94. if (!in_array($cur_dir, $keep_langs, true)) {
  95. io_rmdir($path . '/' . $cur_dir, true);
  96. }
  97. }
  98. }
  99. $dir->close();
  100. }
  101. }
  102. $cli = new StripLangsCLI();
  103. $cli->run();