summaryrefslogtreecommitdiff
path: root/bin/striplangs.php
blob: 288859c6a2608403a1ead9ef4b0729fbcc95e8aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/php
<?php
/**
 * Strip unwanted languages from the DokuWiki install
 *
 * @author Martin 'E.T.' Misuth <et.github@ethome.sk>
 */
if ('cli' != php_sapi_name()) die();

#------------------------------------------------------------------------------
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
require_once DOKU_INC.'inc/cliopts.php';

#------------------------------------------------------------------------------
function usage($show_examples = false) {
    print "Usage: striplangs.php [-h [-x]] [-e] [-k lang1[,lang2]..[,langN]]

    Removes all languages from the instalation, besides the ones
    after the -k option. English language is never removed!

    OPTIONS
        -h, --help     get this help
        -x, --examples get also usage examples
        -k, --keep     comma separated list of languages, -e is always implied
        -e, --english  keeps english, dummy to use without -k";
    if ( $show_examples ) {
        print "\n
    EXAMPLES
        Strips all languages, but keeps 'en' and 'de':
         striplangs -k de

        Strips all but 'en','ca-valencia','cs','de','is','sk':
         striplangs --keep ca-valencia,cs,de,is,sk

        Strips all but 'en':
         striplangs -e

        No option specified, prints usage and throws error:
         striplangs\n";
    }
}

function getSuppliedArgument($OPTS, $short, $long) {
    $arg = $OPTS->get($short);
    if ( is_null($arg) ) {
        $arg = $OPTS->get($long);
    }
    return $arg;
}

function processPlugins($path, $keep_langs) {
    if (is_dir($path)) {
        $entries = scandir($path);

        foreach ($entries as $entry) {
            if ($entry != "." && $entry != "..") {
                if ( is_dir($path.'/'.$entry) ) {

                    $plugin_langs = $path.'/'.$entry.'/lang';

                    if ( is_dir( $plugin_langs ) ) {
                        stripDirLangs($plugin_langs, $keep_langs);
                    }
                }
            }
        }
    }
}

function stripDirLangs($path, $keep_langs) {
    $dir = dir($path);

    while(($cur_dir = $dir->read()) !== false) {
        if( $cur_dir != '.' and $cur_dir != '..' and is_dir($path.'/'.$cur_dir)) {

            if ( !in_array($cur_dir, $keep_langs, true ) ) {
                killDir($path.'/'.$cur_dir);
            }
        }
    }
    $dir->close();
}

function killDir($dir) {
    if (is_dir($dir)) {
        $entries = scandir($dir);

        foreach ($entries as $entry) {
            if ($entry != "." && $entry != "..") {
                if ( is_dir($dir.'/'.$entry) ) {
                    killDir($dir.'/'.$entry);
                } else {
                    unlink($dir.'/'.$entry);
                }
            }
        }
        reset($entries);
        rmdir($dir);
    }
}
#------------------------------------------------------------------------------

// handle options
$short_opts = 'hxk:e';
$long_opts  = array('help', 'examples', 'keep=','english');

$OPTS = Doku_Cli_Opts::getOptions(__FILE__, $short_opts, $long_opts);

if ( $OPTS->isError() ) {
    fwrite( STDERR, $OPTS->getMessage() . "\n");
    exit(1);
}

// handle '--examples' option
$show_examples = ( $OPTS->has('x') or $OPTS->has('examples') ) ? true : false;

// handle '--help' option
if ( $OPTS->has('h') or $OPTS->has('help') ) {
    usage($show_examples);
    exit(0);
}

// handle both '--keep' and '--english' options
if ( $OPTS->has('k') or $OPTS->has('keep') ) {
    $preserved_langs = getSuppliedArgument($OPTS,'k','keep');
    $langs = explode(',', $preserved_langs);

    // ! always enforce 'en' lang when using '--keep' (DW relies on it)
    if ( !isset($langs['en']) ) {
      $langs[]='en';
    }
} elseif ( $OPTS->has('e') or $OPTS->has('english') ) {
    // '--english' was specified strip everything besides 'en'
    $langs = array ('en');
} else {
    // no option was specified, print usage but don't do anything as
    // this run might not be intented
    usage();
    print "\n
    ERROR
        No option specified, use either -h -x to get more info,
        or -e to strip every language besides english.\n";
    exit(1);
}

// Kill all language directories in /inc/lang and /lib/plugins besides those in $langs array
stripDirLangs(realpath(dirname(__FILE__).'/../inc/lang'), $langs);
processPlugins(realpath(dirname(__FILE__).'/../lib/plugins'), $langs);