fix comment formating
[dokuwiki.git] / bin / striplangs.php
blob1800971e547f6583467e8bdb895051a7314e428a
1 #!/usr/bin/php
2 <?php
4 use splitbrain\phpcli\CLI;
5 use splitbrain\phpcli\Options;
7 if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
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) {
24 $options->setHelp(
25 'Remove all languages from the installation, besides the ones specified. English language ' .
26 'is never removed!'
29 $options->registerOption(
30 'keep',
31 'Comma separated list of languages to keep in addition to English.',
32 'k',
33 'langcodes'
35 $options->registerOption(
36 'english-only',
37 'Remove all languages except English',
38 'e'
42 /**
43 * Your main program
45 * Arguments and options have been parsed when this is run
47 * @param Options $options
48 * @return void
50 protected function main(Options $options) {
51 if($options->getOpt('keep')) {
52 $keep = explode(',', $options->getOpt('keep'));
53 if(!in_array('en', $keep)) $keep[] = 'en';
54 } elseif($options->getOpt('english-only')) {
55 $keep = array('en');
56 } else {
57 echo $options->help();
58 exit(0);
61 // Kill all language directories in /inc/lang and /lib/plugins besides those in $langs array
62 $this->stripDirLangs(realpath(dirname(__FILE__) . '/../inc/lang'), $keep);
63 $this->processExtensions(realpath(dirname(__FILE__) . '/../lib/plugins'), $keep);
64 $this->processExtensions(realpath(dirname(__FILE__) . '/../lib/tpl'), $keep);
67 /**
68 * Strip languages from extensions
70 * @param string $path path to plugin or template dir
71 * @param array $keep_langs languages to keep
73 protected function processExtensions($path, $keep_langs) {
74 if(is_dir($path)) {
75 $entries = scandir($path);
77 foreach($entries as $entry) {
78 if($entry != "." && $entry != "..") {
79 if(is_dir($path . '/' . $entry)) {
81 $plugin_langs = $path . '/' . $entry . '/lang';
83 if(is_dir($plugin_langs)) {
84 $this->stripDirLangs($plugin_langs, $keep_langs);
92 /**
93 * Strip languages from path
95 * @param string $path path to lang dir
96 * @param array $keep_langs languages to keep
98 protected function stripDirLangs($path, $keep_langs) {
99 $dir = dir($path);
101 while(($cur_dir = $dir->read()) !== false) {
102 if($cur_dir != '.' and $cur_dir != '..' and is_dir($path . '/' . $cur_dir)) {
104 if(!in_array($cur_dir, $keep_langs, true)) {
105 io_rmdir($path . '/' . $cur_dir, true);
109 $dir->close();
113 $cli = new StripLangsCLI();
114 $cli->run();