4 * Strip unwanted languages from the DokuWiki install
6 * @author Martin 'E.T.' Misuth <et.github@ethome.sk>
8 if ('cli' != php_sapi_name()) die();
10 #------------------------------------------------------------------------------
11 if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
12 require_once DOKU_INC.'inc/cliopts.php';
14 #------------------------------------------------------------------------------
15 function usage($show_examples = false) {
16 print "Usage: striplangs.php [-h [-x]] [-e] [-k lang1[,lang2]..[,langN]]
18 Removes all languages from the instalation, besides the ones
19 after the -k option. English language is never removed!
22 -h, --help get this help
23 -x, --examples get also usage examples
24 -k, --keep comma separated list of languages, -e is always implied
25 -e, --english keeps english, dummy to use without -k\n";
26 if ( $show_examples ) {
29 Strips all languages, but keeps 'en' and 'de':
32 Strips all but 'en','ca-valencia','cs','de','is','sk':
33 striplangs --keep ca-valencia,cs,de,is,sk
38 No option specified, prints usage and throws error:
43 function getSuppliedArgument($OPTS, $short, $long) {
44 $arg = $OPTS->get($short);
45 if ( is_null($arg) ) {
46 $arg = $OPTS->get($long);
51 function processPlugins($path, $keep_langs) {
53 $entries = scandir($path);
55 foreach ($entries as $entry) {
56 if ($entry != "." && $entry != "..") {
57 if ( is_dir($path.'/'.$entry) ) {
59 $plugin_langs = $path.'/'.$entry.'/lang';
61 if ( is_dir( $plugin_langs ) ) {
62 stripDirLangs($plugin_langs, $keep_langs);
70 function stripDirLangs($path, $keep_langs) {
73 while(($cur_dir = $dir->read()) !== false) {
74 if( $cur_dir != '.' and $cur_dir != '..' and is_dir($path.'/'.$cur_dir)) {
76 if ( !in_array($cur_dir, $keep_langs, true ) ) {
77 killDir($path.'/'.$cur_dir);
84 function killDir($dir) {
86 $entries = scandir($dir);
88 foreach ($entries as $entry) {
89 if ($entry != "." && $entry != "..") {
90 if ( is_dir($dir.'/'.$entry) ) {
91 killDir($dir.'/'.$entry);
93 unlink($dir.'/'.$entry);
101 #------------------------------------------------------------------------------
104 $short_opts = 'hxk:e';
105 $long_opts = array('help', 'examples', 'keep=','english');
107 $OPTS = Doku_Cli_Opts::getOptions(__FILE__, $short_opts, $long_opts);
109 if ( $OPTS->isError() ) {
110 fwrite( STDERR, $OPTS->getMessage() . "\n");
114 // handle '--examples' option
115 $show_examples = ( $OPTS->has('x') or $OPTS->has('examples') ) ? true : false;
117 // handle '--help' option
118 if ( $OPTS->has('h') or $OPTS->has('help') ) {
119 usage($show_examples);
123 // handle both '--keep' and '--english' options
124 if ( $OPTS->has('k') or $OPTS->has('keep') ) {
125 $preserved_langs = getSuppliedArgument($OPTS,'k','keep');
126 $langs = explode(',', $preserved_langs);
128 // ! always enforce 'en' lang when using '--keep' (DW relies on it)
129 if ( !isset($langs['en']) ) {
132 } elseif ( $OPTS->has('e') or $OPTS->has('english') ) {
133 // '--english' was specified strip everything besides 'en'
134 $langs = array ('en');
136 // no option was specified, print usage but don't do anything as
137 // this run might not be intented
141 No option specified, use either -h -x to get more info,
142 or -e to strip every language besides english.\n";
146 // Kill all language directories in /inc/lang and /lib/plugins besides those in $langs array
147 stripDirLangs(realpath(dirname(__FILE__).'/../inc/lang'), $langs);
148 processPlugins(realpath(dirname(__FILE__).'/../lib/plugins'), $langs);