xref: /dokuwiki/bin/striplangs.php (revision 47914fdf9a86d3ce2b75fc6f7743bc604711e6aa)
1c0470665SMartin 'E.T.' Misuth#!/usr/bin/php
2c0470665SMartin 'E.T.' Misuth<?php
3c0470665SMartin 'E.T.' Misuthif(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__).'/../').'/');
4352ec8ceSAndreas Gohrdefine('NOSESSION', 1);
5352ec8ceSAndreas Gohrrequire_once(DOKU_INC.'inc/init.php');
6c0470665SMartin 'E.T.' Misuth
7c0470665SMartin 'E.T.' Misuth
81c36b3d8SAndreas Gohr/**
91c36b3d8SAndreas Gohr * Remove unwanted languages from a DokuWiki install
101c36b3d8SAndreas Gohr */
11352ec8ceSAndreas Gohrclass StripLangsCLI extends DokuCLI {
12c0470665SMartin 'E.T.' Misuth
13352ec8ceSAndreas Gohr    /**
14352ec8ceSAndreas Gohr     * Register options and arguments on the given $options object
15352ec8ceSAndreas Gohr     *
16352ec8ceSAndreas Gohr     * @param DokuCLI_Options $options
17352ec8ceSAndreas Gohr     * @return void
18352ec8ceSAndreas Gohr     */
19352ec8ceSAndreas Gohr    protected function setup(DokuCLI_Options $options) {
20c0470665SMartin 'E.T.' Misuth
21352ec8ceSAndreas Gohr        $options->setHelp(
22352ec8ceSAndreas Gohr            'Remove all languages from the installation, besides the ones specified. English language '.
23352ec8ceSAndreas Gohr            'is never removed!'
24352ec8ceSAndreas Gohr        );
25c0470665SMartin 'E.T.' Misuth
26352ec8ceSAndreas Gohr        $options->registerOption(
27352ec8ceSAndreas Gohr            'keep',
28352ec8ceSAndreas Gohr            'Comma separated list of languages to keep in addition to English.',
29*47914fdfSAndreas Gohr            'k',
30*47914fdfSAndreas Gohr            'langcodes'
31352ec8ceSAndreas Gohr        );
32352ec8ceSAndreas Gohr        $options->registerOption(
33352ec8ceSAndreas Gohr            'english-only',
34352ec8ceSAndreas Gohr            'Remove all languages except English',
35352ec8ceSAndreas Gohr            'e'
36352ec8ceSAndreas Gohr        );
37c0470665SMartin 'E.T.' Misuth    }
38c0470665SMartin 'E.T.' Misuth
39352ec8ceSAndreas Gohr    /**
40352ec8ceSAndreas Gohr     * Your main program
41352ec8ceSAndreas Gohr     *
42352ec8ceSAndreas Gohr     * Arguments and options have been parsed when this is run
43352ec8ceSAndreas Gohr     *
44352ec8ceSAndreas Gohr     * @param DokuCLI_Options $options
45352ec8ceSAndreas Gohr     * @return void
46352ec8ceSAndreas Gohr     */
47352ec8ceSAndreas Gohr    protected function main(DokuCLI_Options $options) {
48352ec8ceSAndreas Gohr        if($options->getOpt('keep')) {
49352ec8ceSAndreas Gohr            $keep = explode(',', $options->getOpt('keep'));
50352ec8ceSAndreas Gohr            if(!in_array('en', $keep)) $keep[] = 'en';
51352ec8ceSAndreas Gohr        } elseif($options->getOpt('english-only')) {
52352ec8ceSAndreas Gohr            $keep = array('en');
53352ec8ceSAndreas Gohr        } else {
54352ec8ceSAndreas Gohr            echo $options->help();
55352ec8ceSAndreas Gohr            exit(0);
56c0470665SMartin 'E.T.' Misuth        }
57c0470665SMartin 'E.T.' Misuth
58352ec8ceSAndreas Gohr        // Kill all language directories in /inc/lang and /lib/plugins besides those in $langs array
59352ec8ceSAndreas Gohr        $this->stripDirLangs(realpath(dirname(__FILE__).'/../inc/lang'), $keep);
60352ec8ceSAndreas Gohr        $this->processExtensions(realpath(dirname(__FILE__).'/../lib/plugins'), $keep);
61352ec8ceSAndreas Gohr        $this->processExtensions(realpath(dirname(__FILE__).'/../lib/tpl'), $keep);
62352ec8ceSAndreas Gohr    }
63352ec8ceSAndreas Gohr
64352ec8ceSAndreas Gohr    /**
65352ec8ceSAndreas Gohr     * Strip languages from extensions
66352ec8ceSAndreas Gohr     *
67352ec8ceSAndreas Gohr     * @param string $path       path to plugin or template dir
68352ec8ceSAndreas Gohr     * @param array  $keep_langs languages to keep
69352ec8ceSAndreas Gohr     */
70352ec8ceSAndreas Gohr    protected function processExtensions($path, $keep_langs) {
71c0470665SMartin 'E.T.' Misuth        if(is_dir($path)) {
72c0470665SMartin 'E.T.' Misuth            $entries = scandir($path);
73c0470665SMartin 'E.T.' Misuth
74c0470665SMartin 'E.T.' Misuth            foreach($entries as $entry) {
75c0470665SMartin 'E.T.' Misuth                if($entry != "." && $entry != "..") {
7679e197efSAndreas Gohr                    if(is_dir($path.'/'.$entry)) {
77c0470665SMartin 'E.T.' Misuth
7879e197efSAndreas Gohr                        $plugin_langs = $path.'/'.$entry.'/lang';
79c0470665SMartin 'E.T.' Misuth
80c0470665SMartin 'E.T.' Misuth                        if(is_dir($plugin_langs)) {
81352ec8ceSAndreas Gohr                            $this->stripDirLangs($plugin_langs, $keep_langs);
82c0470665SMartin 'E.T.' Misuth                        }
83c0470665SMartin 'E.T.' Misuth                    }
84c0470665SMartin 'E.T.' Misuth                }
85c0470665SMartin 'E.T.' Misuth            }
86c0470665SMartin 'E.T.' Misuth        }
87c0470665SMartin 'E.T.' Misuth    }
88c0470665SMartin 'E.T.' Misuth
89352ec8ceSAndreas Gohr    /**
90352ec8ceSAndreas Gohr     * Strip languages from path
91352ec8ceSAndreas Gohr     *
92352ec8ceSAndreas Gohr     * @param string $path       path to lang dir
93352ec8ceSAndreas Gohr     * @param array  $keep_langs languages to keep
94352ec8ceSAndreas Gohr     */
95352ec8ceSAndreas Gohr    protected function stripDirLangs($path, $keep_langs) {
96c0470665SMartin 'E.T.' Misuth        $dir = dir($path);
97c0470665SMartin 'E.T.' Misuth
98c0470665SMartin 'E.T.' Misuth        while(($cur_dir = $dir->read()) !== false) {
9979e197efSAndreas Gohr            if($cur_dir != '.' and $cur_dir != '..' and is_dir($path.'/'.$cur_dir)) {
100c0470665SMartin 'E.T.' Misuth
101c0470665SMartin 'E.T.' Misuth                if(!in_array($cur_dir, $keep_langs, true)) {
102352ec8ceSAndreas Gohr                    io_rmdir($path.'/'.$cur_dir, true);
103c0470665SMartin 'E.T.' Misuth                }
104c0470665SMartin 'E.T.' Misuth            }
105c0470665SMartin 'E.T.' Misuth        }
106c0470665SMartin 'E.T.' Misuth        $dir->close();
107c0470665SMartin 'E.T.' Misuth    }
108c0470665SMartin 'E.T.' Misuth}
109c0470665SMartin 'E.T.' Misuth
110352ec8ceSAndreas Gohr$cli = new StripLangsCLI();
111352ec8ceSAndreas Gohr$cli->run();