striplangs.php 3.2 KB

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