xref: /dokuwiki/bin/striplangs.php (revision 8c7c53b0321a3cd3116b8d3b2ad27863a38dece7)
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    /**
18     * Register options and arguments on the given $options object
19     *
20     * @param Options $options
21     * @return void
22     */
23    protected function setup(Options $options) {
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        if($options->getOpt('keep')) {
53            $keep = explode(',', $options->getOpt('keep'));
54            if(!in_array('en', $keep)) $keep[] = 'en';
55        } elseif($options->getOpt('english-only')) {
56            $keep = ['en'];
57        } else {
58            echo $options->help();
59            exit(0);
60        }
61
62        // Kill all language directories in /inc/lang and /lib/plugins besides those in $langs array
63        $this->stripDirLangs(realpath(__DIR__ . '/../inc/lang'), $keep);
64        $this->processExtensions(realpath(__DIR__ . '/../lib/plugins'), $keep);
65        $this->processExtensions(realpath(__DIR__ . '/../lib/tpl'), $keep);
66    }
67
68    /**
69     * Strip languages from extensions
70     *
71     * @param string $path path to plugin or template dir
72     * @param array $keep_langs languages to keep
73     */
74    protected function processExtensions($path, $keep_langs) {
75        if(is_dir($path)) {
76            $entries = scandir($path);
77
78            foreach($entries as $entry) {
79                if($entry != "." && $entry != "..") {
80                    if(is_dir($path . '/' . $entry)) {
81
82                        $plugin_langs = $path . '/' . $entry . '/lang';
83
84                        if(is_dir($plugin_langs)) {
85                            $this->stripDirLangs($plugin_langs, $keep_langs);
86                        }
87                    }
88                }
89            }
90        }
91    }
92
93    /**
94     * Strip languages from path
95     *
96     * @param string $path path to lang dir
97     * @param array $keep_langs languages to keep
98     */
99    protected function stripDirLangs($path, $keep_langs) {
100        $dir = dir($path);
101
102        while(($cur_dir = $dir->read()) !== false) {
103            if($cur_dir != '.' && $cur_dir != '..' && is_dir($path . '/' . $cur_dir)) {
104
105                if(!in_array($cur_dir, $keep_langs, true)) {
106                    io_rmdir($path . '/' . $cur_dir, true);
107                }
108            }
109        }
110        $dir->close();
111    }
112}
113
114$cli = new StripLangsCLI();
115$cli->run();
116