xref: /dokuwiki/bin/striplangs.php (revision cbfa4829d9bcd40d1cc3b9220fe78fa37c385c02)
1*cbfa4829SPhy#!/usr/bin/env php
2c0470665SMartin 'E.T.' Misuth<?php
3cbeaa4a0SAndreas Gohr
4cbeaa4a0SAndreas Gohruse splitbrain\phpcli\CLI;
5cbeaa4a0SAndreas Gohruse splitbrain\phpcli\Options;
6cbeaa4a0SAndreas Gohr
7c0470665SMartin 'E.T.' Misuthif(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
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 */
14cbeaa4a0SAndreas Gohrclass StripLangsCLI extends CLI {
15c0470665SMartin 'E.T.' Misuth
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     */
22cbeaa4a0SAndreas Gohr    protected function setup(Options $options) {
23c0470665SMartin 'E.T.' Misuth
24352ec8ceSAndreas Gohr        $options->setHelp(
25352ec8ceSAndreas Gohr            'Remove all languages from the installation, besides the ones specified. English language ' .
26352ec8ceSAndreas Gohr            'is never removed!'
27352ec8ceSAndreas Gohr        );
28c0470665SMartin 'E.T.' Misuth
29352ec8ceSAndreas Gohr        $options->registerOption(
30352ec8ceSAndreas Gohr            'keep',
31352ec8ceSAndreas Gohr            'Comma separated list of languages to keep in addition to English.',
3247914fdfSAndreas Gohr            'k',
3347914fdfSAndreas Gohr            'langcodes'
34352ec8ceSAndreas Gohr        );
35352ec8ceSAndreas Gohr        $options->registerOption(
36352ec8ceSAndreas Gohr            'english-only',
37352ec8ceSAndreas Gohr            'Remove all languages except English',
38352ec8ceSAndreas Gohr            'e'
39352ec8ceSAndreas Gohr        );
40c0470665SMartin 'E.T.' Misuth    }
41c0470665SMartin 'E.T.' Misuth
42352ec8ceSAndreas Gohr    /**
43352ec8ceSAndreas Gohr     * Your main program
44352ec8ceSAndreas Gohr     *
45352ec8ceSAndreas Gohr     * Arguments and options have been parsed when this is run
46352ec8ceSAndreas Gohr     *
47cbeaa4a0SAndreas Gohr     * @param Options $options
48352ec8ceSAndreas Gohr     * @return void
49352ec8ceSAndreas Gohr     */
50cbeaa4a0SAndreas Gohr    protected function main(Options $options) {
51352ec8ceSAndreas Gohr        if($options->getOpt('keep')) {
52352ec8ceSAndreas Gohr            $keep = explode(',', $options->getOpt('keep'));
53352ec8ceSAndreas Gohr            if(!in_array('en', $keep)) $keep[] = 'en';
54352ec8ceSAndreas Gohr        } elseif($options->getOpt('english-only')) {
55352ec8ceSAndreas Gohr            $keep = array('en');
56352ec8ceSAndreas Gohr        } else {
57352ec8ceSAndreas Gohr            echo $options->help();
58352ec8ceSAndreas Gohr            exit(0);
59c0470665SMartin 'E.T.' Misuth        }
60c0470665SMartin 'E.T.' Misuth
61352ec8ceSAndreas Gohr        // Kill all language directories in /inc/lang and /lib/plugins besides those in $langs array
62352ec8ceSAndreas Gohr        $this->stripDirLangs(realpath(dirname(__FILE__) . '/../inc/lang'), $keep);
63352ec8ceSAndreas Gohr        $this->processExtensions(realpath(dirname(__FILE__) . '/../lib/plugins'), $keep);
64352ec8ceSAndreas Gohr        $this->processExtensions(realpath(dirname(__FILE__) . '/../lib/tpl'), $keep);
65352ec8ceSAndreas Gohr    }
66352ec8ceSAndreas Gohr
67352ec8ceSAndreas Gohr    /**
68352ec8ceSAndreas Gohr     * Strip languages from extensions
69352ec8ceSAndreas Gohr     *
70352ec8ceSAndreas Gohr     * @param string $path path to plugin or template dir
71352ec8ceSAndreas Gohr     * @param array $keep_langs languages to keep
72352ec8ceSAndreas Gohr     */
73352ec8ceSAndreas Gohr    protected function processExtensions($path, $keep_langs) {
74c0470665SMartin 'E.T.' Misuth        if(is_dir($path)) {
75c0470665SMartin 'E.T.' Misuth            $entries = scandir($path);
76c0470665SMartin 'E.T.' Misuth
77c0470665SMartin 'E.T.' Misuth            foreach($entries as $entry) {
78c0470665SMartin 'E.T.' Misuth                if($entry != "." && $entry != "..") {
7979e197efSAndreas Gohr                    if(is_dir($path . '/' . $entry)) {
80c0470665SMartin 'E.T.' Misuth
8179e197efSAndreas Gohr                        $plugin_langs = $path . '/' . $entry . '/lang';
82c0470665SMartin 'E.T.' Misuth
83c0470665SMartin 'E.T.' Misuth                        if(is_dir($plugin_langs)) {
84352ec8ceSAndreas Gohr                            $this->stripDirLangs($plugin_langs, $keep_langs);
85c0470665SMartin 'E.T.' Misuth                        }
86c0470665SMartin 'E.T.' Misuth                    }
87c0470665SMartin 'E.T.' Misuth                }
88c0470665SMartin 'E.T.' Misuth            }
89c0470665SMartin 'E.T.' Misuth        }
90c0470665SMartin 'E.T.' Misuth    }
91c0470665SMartin 'E.T.' Misuth
92352ec8ceSAndreas Gohr    /**
93352ec8ceSAndreas Gohr     * Strip languages from path
94352ec8ceSAndreas Gohr     *
95352ec8ceSAndreas Gohr     * @param string $path path to lang dir
96352ec8ceSAndreas Gohr     * @param array $keep_langs languages to keep
97352ec8ceSAndreas Gohr     */
98352ec8ceSAndreas Gohr    protected function stripDirLangs($path, $keep_langs) {
99c0470665SMartin 'E.T.' Misuth        $dir = dir($path);
100c0470665SMartin 'E.T.' Misuth
101c0470665SMartin 'E.T.' Misuth        while(($cur_dir = $dir->read()) !== false) {
10279e197efSAndreas Gohr            if($cur_dir != '.' and $cur_dir != '..' and is_dir($path . '/' . $cur_dir)) {
103c0470665SMartin 'E.T.' Misuth
104c0470665SMartin 'E.T.' Misuth                if(!in_array($cur_dir, $keep_langs, true)) {
105352ec8ceSAndreas Gohr                    io_rmdir($path . '/' . $cur_dir, true);
106c0470665SMartin 'E.T.' Misuth                }
107c0470665SMartin 'E.T.' Misuth            }
108c0470665SMartin 'E.T.' Misuth        }
109c0470665SMartin 'E.T.' Misuth        $dir->close();
110c0470665SMartin 'E.T.' Misuth    }
111c0470665SMartin 'E.T.' Misuth}
112c0470665SMartin 'E.T.' Misuth
113352ec8ceSAndreas Gohr$cli = new StripLangsCLI();
114352ec8ceSAndreas Gohr$cli->run();
115