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