xref: /dokuwiki/bin/striplangs.php (revision d4f83172d9533c4d84f450fe22ef630816b21d75)
1cbfa4829SPhy#!/usr/bin/env php
2c0470665SMartin 'E.T.' Misuth<?php
3cbeaa4a0SAndreas Gohr
4cbeaa4a0SAndreas Gohruse splitbrain\phpcli\CLI;
5cbeaa4a0SAndreas Gohruse splitbrain\phpcli\Options;
6cbeaa4a0SAndreas Gohr
7b1f206e1SAndreas Gohrif (!defined('DOKU_INC')) define('DOKU_INC', realpath(__DIR__ . '/../') . '/');
8352ec8ceSAndreas Gohrdefine('NOSESSION', 1);
9352ec8ceSAndreas Gohrrequire_once(DOKU_INC . 'inc/init.php');
10c0470665SMartin 'E.T.' Misuth
111c36b3d8SAndreas Gohr/**
121c36b3d8SAndreas Gohr * Remove unwanted languages from a DokuWiki install
131c36b3d8SAndreas Gohr */
148c7c53b0SAndreas Gohrclass StripLangsCLI extends CLI
158c7c53b0SAndreas Gohr{
16352ec8ceSAndreas Gohr    /**
17352ec8ceSAndreas Gohr     * Register options and arguments on the given $options object
18352ec8ceSAndreas Gohr     *
19cbeaa4a0SAndreas Gohr     * @param Options $options
20352ec8ceSAndreas Gohr     * @return void
21352ec8ceSAndreas Gohr     */
22*d868eb89SAndreas Gohr    protected function setup(Options $options)
23*d868eb89SAndreas Gohr    {
24c0470665SMartin 'E.T.' Misuth
25352ec8ceSAndreas Gohr        $options->setHelp(
26352ec8ceSAndreas Gohr            'Remove all languages from the installation, besides the ones specified. English language ' .
27352ec8ceSAndreas Gohr            'is never removed!'
28352ec8ceSAndreas Gohr        );
29c0470665SMartin 'E.T.' Misuth
30352ec8ceSAndreas Gohr        $options->registerOption(
31352ec8ceSAndreas Gohr            'keep',
32352ec8ceSAndreas Gohr            'Comma separated list of languages to keep in addition to English.',
3347914fdfSAndreas Gohr            'k',
3447914fdfSAndreas Gohr            'langcodes'
35352ec8ceSAndreas Gohr        );
36352ec8ceSAndreas Gohr        $options->registerOption(
37352ec8ceSAndreas Gohr            'english-only',
38352ec8ceSAndreas Gohr            'Remove all languages except English',
39352ec8ceSAndreas Gohr            'e'
40352ec8ceSAndreas Gohr        );
41c0470665SMartin 'E.T.' Misuth    }
42c0470665SMartin 'E.T.' Misuth
43352ec8ceSAndreas Gohr    /**
44352ec8ceSAndreas Gohr     * Your main program
45352ec8ceSAndreas Gohr     *
46352ec8ceSAndreas Gohr     * Arguments and options have been parsed when this is run
47352ec8ceSAndreas Gohr     *
48cbeaa4a0SAndreas Gohr     * @param Options $options
49352ec8ceSAndreas Gohr     * @return void
50352ec8ceSAndreas Gohr     */
51*d868eb89SAndreas Gohr    protected function main(Options $options)
52*d868eb89SAndreas Gohr    {
53352ec8ceSAndreas Gohr        if ($options->getOpt('keep')) {
54352ec8ceSAndreas Gohr            $keep = explode(',', $options->getOpt('keep'));
55352ec8ceSAndreas Gohr            if (!in_array('en', $keep)) $keep[] = 'en';
56352ec8ceSAndreas Gohr        } elseif ($options->getOpt('english-only')) {
57b1f206e1SAndreas Gohr            $keep = ['en'];
58352ec8ceSAndreas Gohr        } else {
59352ec8ceSAndreas Gohr            echo $options->help();
60352ec8ceSAndreas Gohr            exit(0);
61c0470665SMartin 'E.T.' Misuth        }
62c0470665SMartin 'E.T.' Misuth
63352ec8ceSAndreas Gohr        // Kill all language directories in /inc/lang and /lib/plugins besides those in $langs array
64b1f206e1SAndreas Gohr        $this->stripDirLangs(realpath(__DIR__ . '/../inc/lang'), $keep);
65b1f206e1SAndreas Gohr        $this->processExtensions(realpath(__DIR__ . '/../lib/plugins'), $keep);
66b1f206e1SAndreas Gohr        $this->processExtensions(realpath(__DIR__ . '/../lib/tpl'), $keep);
67352ec8ceSAndreas Gohr    }
68352ec8ceSAndreas Gohr
69352ec8ceSAndreas Gohr    /**
70352ec8ceSAndreas Gohr     * Strip languages from extensions
71352ec8ceSAndreas Gohr     *
72352ec8ceSAndreas Gohr     * @param string $path path to plugin or template dir
73352ec8ceSAndreas Gohr     * @param array $keep_langs languages to keep
74352ec8ceSAndreas Gohr     */
75*d868eb89SAndreas Gohr    protected function processExtensions($path, $keep_langs)
76*d868eb89SAndreas Gohr    {
77c0470665SMartin 'E.T.' Misuth        if (is_dir($path)) {
78c0470665SMartin 'E.T.' Misuth            $entries = scandir($path);
79c0470665SMartin 'E.T.' Misuth
80c0470665SMartin 'E.T.' Misuth            foreach ($entries as $entry) {
81c0470665SMartin 'E.T.' Misuth                if ($entry != "." && $entry != "..") {
8279e197efSAndreas Gohr                    if (is_dir($path . '/' . $entry)) {
8379e197efSAndreas Gohr                        $plugin_langs = $path . '/' . $entry . '/lang';
84c0470665SMartin 'E.T.' Misuth
85c0470665SMartin 'E.T.' Misuth                        if (is_dir($plugin_langs)) {
86352ec8ceSAndreas Gohr                            $this->stripDirLangs($plugin_langs, $keep_langs);
87c0470665SMartin 'E.T.' Misuth                        }
88c0470665SMartin 'E.T.' Misuth                    }
89c0470665SMartin 'E.T.' Misuth                }
90c0470665SMartin 'E.T.' Misuth            }
91c0470665SMartin 'E.T.' Misuth        }
92c0470665SMartin 'E.T.' Misuth    }
93c0470665SMartin 'E.T.' Misuth
94352ec8ceSAndreas Gohr    /**
95352ec8ceSAndreas Gohr     * Strip languages from path
96352ec8ceSAndreas Gohr     *
97352ec8ceSAndreas Gohr     * @param string $path path to lang dir
98352ec8ceSAndreas Gohr     * @param array $keep_langs languages to keep
99352ec8ceSAndreas Gohr     */
100*d868eb89SAndreas Gohr    protected function stripDirLangs($path, $keep_langs)
101*d868eb89SAndreas Gohr    {
102c0470665SMartin 'E.T.' Misuth        $dir = dir($path);
103c0470665SMartin 'E.T.' Misuth
104c0470665SMartin 'E.T.' Misuth        while (($cur_dir = $dir->read()) !== false) {
105b1f206e1SAndreas Gohr            if ($cur_dir != '.' && $cur_dir != '..' && is_dir($path . '/' . $cur_dir)) {
106c0470665SMartin 'E.T.' Misuth                if (!in_array($cur_dir, $keep_langs, true)) {
107352ec8ceSAndreas Gohr                    io_rmdir($path . '/' . $cur_dir, true);
108c0470665SMartin 'E.T.' Misuth                }
109c0470665SMartin 'E.T.' Misuth            }
110c0470665SMartin 'E.T.' Misuth        }
111c0470665SMartin 'E.T.' Misuth        $dir->close();
112c0470665SMartin 'E.T.' Misuth    }
113c0470665SMartin 'E.T.' Misuth}
114c0470665SMartin 'E.T.' Misuth
115352ec8ceSAndreas Gohr$cli = new StripLangsCLI();
116352ec8ceSAndreas Gohr$cli->run();
117