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