summaryrefslogtreecommitdiff
path: root/platform/www/bin/striplangs.php
blob: 91805d59e8680cc274d0a82075f502e48c80373a (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
#!/usr/bin/env php
<?php

use splitbrain\phpcli\CLI;
use splitbrain\phpcli\Options;

if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
define('NOSESSION', 1);
require_once(DOKU_INC . 'inc/init.php');

/**
 * Remove unwanted languages from a DokuWiki install
 */
class StripLangsCLI extends CLI {

    /**
     * Register options and arguments on the given $options object
     *
     * @param Options $options
     * @return void
     */
    protected function setup(Options $options) {

        $options->setHelp(
            'Remove all languages from the installation, besides the ones specified. English language ' .
            'is never removed!'
        );

        $options->registerOption(
            'keep',
            'Comma separated list of languages to keep in addition to English.',
            'k',
            'langcodes'
        );
        $options->registerOption(
            'english-only',
            'Remove all languages except English',
            'e'
        );
    }

    /**
     * Your main program
     *
     * Arguments and options have been parsed when this is run
     *
     * @param Options $options
     * @return void
     */
    protected function main(Options $options) {
        if($options->getOpt('keep')) {
            $keep = explode(',', $options->getOpt('keep'));
            if(!in_array('en', $keep)) $keep[] = 'en';
        } elseif($options->getOpt('english-only')) {
            $keep = array('en');
        } else {
            echo $options->help();
            exit(0);
        }

        // Kill all language directories in /inc/lang and /lib/plugins besides those in $langs array
        $this->stripDirLangs(realpath(dirname(__FILE__) . '/../inc/lang'), $keep);
        $this->processExtensions(realpath(dirname(__FILE__) . '/../lib/plugins'), $keep);
        $this->processExtensions(realpath(dirname(__FILE__) . '/../lib/tpl'), $keep);
    }

    /**
     * Strip languages from extensions
     *
     * @param string $path path to plugin or template dir
     * @param array $keep_langs languages to keep
     */
    protected function processExtensions($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)) {
                            $this->stripDirLangs($plugin_langs, $keep_langs);
                        }
                    }
                }
            }
        }
    }

    /**
     * Strip languages from path
     *
     * @param string $path path to lang dir
     * @param array $keep_langs languages to keep
     */
    protected 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)) {
                    io_rmdir($path . '/' . $cur_dir, true);
                }
            }
        }
        $dir->close();
    }
}

$cli = new StripLangsCLI();
$cli->run();