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(__DIR__ . '/../') . '/');
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
25        $options->setHelp(
26            'Remove all languages from the installation, besides the ones specified. English language ' .
27            'is never removed!'
28        );
29
30        $options->registerOption(
31            'keep',
32            'Comma separated list of languages to keep in addition to English.',
33            'k',
34            'langcodes'
35        );
36        $options->registerOption(
37            'english-only',
38            'Remove all languages except English',
39            'e'
40        );
41    }
42
43    /**
44     * Your main program
45     *
46     * Arguments and options have been parsed when this is run
47     *
48     * @param Options $options
49     * @return void
50     */
51    protected function main(Options $options)
52    {
53        if ($options->getOpt('keep')) {
54            $keep = explode(',', $options->getOpt('keep'));
55            if (!in_array('en', $keep)) $keep[] = 'en';
56        } elseif ($options->getOpt('english-only')) {
57            $keep = ['en'];
58        } else {
59            echo $options->help();
60            exit(0);
61        }
62
63        // Kill all language directories in /inc/lang and /lib/plugins besides those in $langs array
64        $this->stripDirLangs(realpath(__DIR__ . '/../inc/lang'), $keep);
65        $this->processExtensions(realpath(__DIR__ . '/../lib/plugins'), $keep);
66        $this->processExtensions(realpath(__DIR__ . '/../lib/tpl'), $keep);
67    }
68
69    /**
70     * Strip languages from extensions
71     *
72     * @param string $path path to plugin or template dir
73     * @param array $keep_langs languages to keep
74     */
75    protected function processExtensions($path, $keep_langs)
76    {
77        if (is_dir($path)) {
78            $entries = scandir($path);
79
80            foreach ($entries as $entry) {
81                if ($entry != "." && $entry != "..") {
82                    if (is_dir($path . '/' . $entry)) {
83                        $plugin_langs = $path . '/' . $entry . '/lang';
84
85                        if (is_dir($plugin_langs)) {
86                            $this->stripDirLangs($plugin_langs, $keep_langs);
87                        }
88                    }
89                }
90            }
91        }
92    }
93
94    /**
95     * Strip languages from path
96     *
97     * @param string $path path to lang dir
98     * @param array $keep_langs languages to keep
99     */
100    protected function stripDirLangs($path, $keep_langs)
101    {
102        $dir = dir($path);
103
104        while (($cur_dir = $dir->read()) !== false) {
105            if ($cur_dir != '.' && $cur_dir != '..' && is_dir($path . '/' . $cur_dir)) {
106                if (!in_array($cur_dir, $keep_langs, true)) {
107                    io_rmdir($path . '/' . $cur_dir, true);
108                }
109            }
110        }
111        $dir->close();
112    }
113}
114
115$cli = new StripLangsCLI();
116$cli->run();
117