xref: /dokuwiki/bin/striplangs.php (revision 352ec8ce2a661a326f2f6cb747d43bb60119fb3e)
1c0470665SMartin 'E.T.' Misuth#!/usr/bin/php
2c0470665SMartin 'E.T.' Misuth<?php
3c0470665SMartin 'E.T.' Misuthif(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
4*352ec8ceSAndreas Gohrdefine('NOSESSION', 1);
5*352ec8ceSAndreas Gohrrequire_once(DOKU_INC . 'inc/init.php');
6c0470665SMartin 'E.T.' Misuth
7c0470665SMartin 'E.T.' Misuth
8*352ec8ceSAndreas Gohrclass StripLangsCLI extends DokuCLI {
9c0470665SMartin 'E.T.' Misuth
10*352ec8ceSAndreas Gohr    /**
11*352ec8ceSAndreas Gohr     * Register options and arguments on the given $options object
12*352ec8ceSAndreas Gohr     *
13*352ec8ceSAndreas Gohr     * @param DokuCLI_Options $options
14*352ec8ceSAndreas Gohr     * @return void
15*352ec8ceSAndreas Gohr     */
16*352ec8ceSAndreas Gohr    protected function setup(DokuCLI_Options $options) {
17c0470665SMartin 'E.T.' Misuth
18*352ec8ceSAndreas Gohr        $options->setHelp(
19*352ec8ceSAndreas Gohr                'Remove all languages from the installation, besides the ones specified. English language ' .
20*352ec8ceSAndreas Gohr                'is never removed!'
21*352ec8ceSAndreas Gohr        );
22c0470665SMartin 'E.T.' Misuth
23*352ec8ceSAndreas Gohr        $options->registerOption(
24*352ec8ceSAndreas Gohr                'keep',
25*352ec8ceSAndreas Gohr                'Comma separated list of languages to keep in addition to English.',
26*352ec8ceSAndreas Gohr                'k'
27*352ec8ceSAndreas Gohr        );
28*352ec8ceSAndreas Gohr        $options->registerOption(
29*352ec8ceSAndreas Gohr                'english-only',
30*352ec8ceSAndreas Gohr                'Remove all languages except English',
31*352ec8ceSAndreas Gohr                'e'
32*352ec8ceSAndreas Gohr        );
33c0470665SMartin 'E.T.' Misuth    }
34c0470665SMartin 'E.T.' Misuth
35*352ec8ceSAndreas Gohr    /**
36*352ec8ceSAndreas Gohr     * Your main program
37*352ec8ceSAndreas Gohr     *
38*352ec8ceSAndreas Gohr     * Arguments and options have been parsed when this is run
39*352ec8ceSAndreas Gohr     *
40*352ec8ceSAndreas Gohr     * @param DokuCLI_Options $options
41*352ec8ceSAndreas Gohr     * @return void
42*352ec8ceSAndreas Gohr     */
43*352ec8ceSAndreas Gohr    protected function main(DokuCLI_Options $options) {
44*352ec8ceSAndreas Gohr        if($options->getOpt('keep')) {
45*352ec8ceSAndreas Gohr            $keep = explode(',', $options->getOpt('keep'));
46*352ec8ceSAndreas Gohr            if(!in_array('en', $keep)) $keep[] = 'en';
47*352ec8ceSAndreas Gohr        } elseif($options->getOpt('english-only')) {
48*352ec8ceSAndreas Gohr            $keep = array('en');
49*352ec8ceSAndreas Gohr        } else {
50*352ec8ceSAndreas Gohr            echo $options->help();
51*352ec8ceSAndreas Gohr            exit(0);
52c0470665SMartin 'E.T.' Misuth        }
53c0470665SMartin 'E.T.' Misuth
54*352ec8ceSAndreas Gohr        // Kill all language directories in /inc/lang and /lib/plugins besides those in $langs array
55*352ec8ceSAndreas Gohr        $this->stripDirLangs(realpath(dirname(__FILE__) . '/../inc/lang'), $keep);
56*352ec8ceSAndreas Gohr        $this->processExtensions(realpath(dirname(__FILE__) . '/../lib/plugins'), $keep);
57*352ec8ceSAndreas Gohr        $this->processExtensions(realpath(dirname(__FILE__) . '/../lib/tpl'), $keep);
58*352ec8ceSAndreas Gohr    }
59*352ec8ceSAndreas Gohr
60*352ec8ceSAndreas Gohr    /**
61*352ec8ceSAndreas Gohr     * Strip languages from extensions
62*352ec8ceSAndreas Gohr     *
63*352ec8ceSAndreas Gohr     * @param string $path path to plugin or template dir
64*352ec8ceSAndreas Gohr     * @param array $keep_langs languages to keep
65*352ec8ceSAndreas Gohr     */
66*352ec8ceSAndreas Gohr    protected function processExtensions($path, $keep_langs) {
67c0470665SMartin 'E.T.' Misuth        if(is_dir($path)) {
68c0470665SMartin 'E.T.' Misuth            $entries = scandir($path);
69c0470665SMartin 'E.T.' Misuth
70c0470665SMartin 'E.T.' Misuth            foreach($entries as $entry) {
71c0470665SMartin 'E.T.' Misuth                if($entry != "." && $entry != "..") {
7279e197efSAndreas Gohr                    if(is_dir($path . '/' . $entry)) {
73c0470665SMartin 'E.T.' Misuth
7479e197efSAndreas Gohr                        $plugin_langs = $path . '/' . $entry . '/lang';
75c0470665SMartin 'E.T.' Misuth
76c0470665SMartin 'E.T.' Misuth                        if(is_dir($plugin_langs)) {
77*352ec8ceSAndreas Gohr                            $this->stripDirLangs($plugin_langs, $keep_langs);
78c0470665SMartin 'E.T.' Misuth                        }
79c0470665SMartin 'E.T.' Misuth                    }
80c0470665SMartin 'E.T.' Misuth                }
81c0470665SMartin 'E.T.' Misuth            }
82c0470665SMartin 'E.T.' Misuth        }
83c0470665SMartin 'E.T.' Misuth    }
84c0470665SMartin 'E.T.' Misuth
85*352ec8ceSAndreas Gohr    /**
86*352ec8ceSAndreas Gohr     * Strip languages from path
87*352ec8ceSAndreas Gohr     *
88*352ec8ceSAndreas Gohr     * @param string $path path to lang dir
89*352ec8ceSAndreas Gohr     * @param array $keep_langs languages to keep
90*352ec8ceSAndreas Gohr     */
91*352ec8ceSAndreas Gohr    protected function stripDirLangs($path, $keep_langs) {
92c0470665SMartin 'E.T.' Misuth        $dir = dir($path);
93c0470665SMartin 'E.T.' Misuth
94c0470665SMartin 'E.T.' Misuth        while(($cur_dir = $dir->read()) !== false) {
9579e197efSAndreas Gohr            if($cur_dir != '.' and $cur_dir != '..' and is_dir($path . '/' . $cur_dir)) {
96c0470665SMartin 'E.T.' Misuth
97c0470665SMartin 'E.T.' Misuth                if(!in_array($cur_dir, $keep_langs, true)) {
98*352ec8ceSAndreas Gohr                    io_rmdir($path . '/' . $cur_dir, true);
99c0470665SMartin 'E.T.' Misuth                }
100c0470665SMartin 'E.T.' Misuth            }
101c0470665SMartin 'E.T.' Misuth        }
102c0470665SMartin 'E.T.' Misuth        $dir->close();
103c0470665SMartin 'E.T.' Misuth    }
104c0470665SMartin 'E.T.' Misuth}
105c0470665SMartin 'E.T.' Misuth
106*352ec8ceSAndreas Gohr$cli = new StripLangsCLI();
107*352ec8ceSAndreas Gohr$cli->run();