Merge pull request #4188 from dokuwiki/tablefix
[dokuwiki.git] / bin / striplangs.php
blobb7895b56f7fb8ccf550d85716e71ab117567d273
1 #!/usr/bin/env php
2 <?php
4 use splitbrain\phpcli\CLI;
5 use splitbrain\phpcli\Options;
7 if (!defined('DOKU_INC')) define('DOKU_INC', realpath(__DIR__ . '/../') . '/');
8 define('NOSESSION', 1);
9 require_once(DOKU_INC . 'inc/init.php');
11 /**
12 * Remove unwanted languages from a DokuWiki install
14 class StripLangsCLI extends CLI
16 /**
17 * Register options and arguments on the given $options object
19 * @param Options $options
20 * @return void
22 protected function setup(Options $options)
25 $options->setHelp(
26 'Remove all languages from the installation, besides the ones specified. English language ' .
27 'is never removed!'
30 $options->registerOption(
31 'keep',
32 'Comma separated list of languages to keep in addition to English.',
33 'k',
34 'langcodes'
36 $options->registerOption(
37 'english-only',
38 'Remove all languages except English',
39 'e'
43 /**
44 * Your main program
46 * Arguments and options have been parsed when this is run
48 * @param Options $options
49 * @return void
51 protected function main(Options $options)
53 if ($options->getOpt('keep')) {
54 $keep = explode(',', $options->getOpt('keep'));
55 if (!in_array('en', $keep)) $keep[] = 'en';
56 } elseif ($options->getOpt('english-only')) {
57 $keep = ['en'];
58 } else {
59 echo $options->help();
60 exit(0);
63 // Kill all language directories in /inc/lang and /lib/plugins besides those in $langs array
64 $this->stripDirLangs(realpath(__DIR__ . '/../inc/lang'), $keep);
65 $this->processExtensions(realpath(__DIR__ . '/../lib/plugins'), $keep);
66 $this->processExtensions(realpath(__DIR__ . '/../lib/tpl'), $keep);
69 /**
70 * Strip languages from extensions
72 * @param string $path path to plugin or template dir
73 * @param array $keep_langs languages to keep
75 protected function processExtensions($path, $keep_langs)
77 if (is_dir($path)) {
78 $entries = scandir($path);
80 foreach ($entries as $entry) {
81 if ($entry != "." && $entry != "..") {
82 if (is_dir($path . '/' . $entry)) {
83 $plugin_langs = $path . '/' . $entry . '/lang';
85 if (is_dir($plugin_langs)) {
86 $this->stripDirLangs($plugin_langs, $keep_langs);
94 /**
95 * Strip languages from path
97 * @param string $path path to lang dir
98 * @param array $keep_langs languages to keep
100 protected function stripDirLangs($path, $keep_langs)
102 $dir = dir($path);
104 while (($cur_dir = $dir->read()) !== false) {
105 if ($cur_dir != '.' && $cur_dir != '..' && is_dir($path . '/' . $cur_dir)) {
106 if (!in_array($cur_dir, $keep_langs, true)) {
107 io_rmdir($path . '/' . $cur_dir, true);
111 $dir->close();
115 $cli = new StripLangsCLI();
116 $cli->run();