Creating repository for dokuwiki modifications for sudaraka.org
[sudaraka-org:dokuwiki-mods.git] / bin / striplangs.php
1 #!/usr/bin/php
2 <?php
3 /**
4  * Strip unwanted languages from the DokuWiki install
5  *
6  * @author Martin 'E.T.' Misuth <et.github@ethome.sk>
7  */
8 if ('cli' != php_sapi_name()) die();
9
10 #------------------------------------------------------------------------------
11 if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
12 require_once DOKU_INC.'inc/cliopts.php';
13
14 #------------------------------------------------------------------------------
15 function usage($show_examples = false) {
16     print "Usage: striplangs.php [-h [-x]] [-e] [-k lang1[,lang2]..[,langN]]
17
18     Removes all languages from the instalation, besides the ones
19     after the -k option. English language is never removed!
20
21     OPTIONS
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 ) {
27         print "\n
28     EXAMPLES
29         Strips all languages, but keeps 'en' and 'de':
30          striplangs -k de
31
32         Strips all but 'en','ca-valencia','cs','de','is','sk':
33          striplangs --keep ca-valencia,cs,de,is,sk
34
35         Strips all but 'en':
36          striplangs -e
37
38         No option specified, prints usage and throws error:
39          striplangs\n";
40     }
41 }
42
43 function getSuppliedArgument($OPTS, $short, $long) {
44     $arg = $OPTS->get($short);
45     if ( is_null($arg) ) {
46         $arg = $OPTS->get($long);
47     }
48     return $arg;
49 }
50
51 function processPlugins($path, $keep_langs) {
52     if (is_dir($path)) {
53         $entries = scandir($path);
54
55         foreach ($entries as $entry) {
56             if ($entry != "." && $entry != "..") {
57                 if ( is_dir($path.'/'.$entry) ) {
58
59                     $plugin_langs = $path.'/'.$entry.'/lang';
60
61                     if ( is_dir( $plugin_langs ) ) {
62                         stripDirLangs($plugin_langs, $keep_langs);
63                     }
64                 }
65             }
66         }
67     }
68 }
69
70 function stripDirLangs($path, $keep_langs) {
71     $dir = dir($path);
72
73     while(($cur_dir = $dir->read()) !== false) {
74         if( $cur_dir != '.' and $cur_dir != '..' and is_dir($path.'/'.$cur_dir)) {
75
76             if ( !in_array($cur_dir, $keep_langs, true ) ) {
77                 killDir($path.'/'.$cur_dir);
78             }
79         }
80     }
81     $dir->close();
82 }
83
84 function killDir($dir) {
85     if (is_dir($dir)) {
86         $entries = scandir($dir);
87
88         foreach ($entries as $entry) {
89             if ($entry != "." && $entry != "..") {
90                 if ( is_dir($dir.'/'.$entry) ) {
91                     killDir($dir.'/'.$entry);
92                 } else {
93                     unlink($dir.'/'.$entry);
94                 }
95             }
96         }
97         reset($entries);
98         rmdir($dir);
99     }
100 }
101 #------------------------------------------------------------------------------
102
103 // handle options
104 $short_opts = 'hxk:e';
105 $long_opts  = array('help', 'examples', 'keep=','english');
106
107 $OPTS = Doku_Cli_Opts::getOptions(__FILE__, $short_opts, $long_opts);
108
109 if ( $OPTS->isError() ) {
110     fwrite( STDERR, $OPTS->getMessage() . "\n");
111     exit(1);
112 }
113
114 // handle '--examples' option
115 $show_examples = ( $OPTS->has('x') or $OPTS->has('examples') ) ? true : false;
116
117 // handle '--help' option
118 if ( $OPTS->has('h') or $OPTS->has('help') ) {
119     usage($show_examples);
120     exit(0);
121 }
122
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);
127
128     // ! always enforce 'en' lang when using '--keep' (DW relies on it)
129     if ( !isset($langs['en']) ) {
130       $langs[]='en';
131     }
132 } elseif ( $OPTS->has('e') or $OPTS->has('english') ) {
133     // '--english' was specified strip everything besides 'en'
134     $langs = array ('en');
135 } else {
136     // no option was specified, print usage but don't do anything as
137     // this run might not be intented
138     usage();
139     print "\n
140     ERROR
141         No option specified, use either -h -x to get more info,
142         or -e to strip every language besides english.\n";
143     exit(1);
144 }
145
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);