xref: /dokuwiki/bin/striplangs.php (revision 1c36b3d86f90185519cadaad85251922dc771fe1)
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
8*1c36b3d8SAndreas Gohr/**
9*1c36b3d8SAndreas Gohr * Remove unwanted languages from a DokuWiki install
10*1c36b3d8SAndreas 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.',
29352ec8ceSAndreas Gohr            'k'
30352ec8ceSAndreas Gohr        );
31352ec8ceSAndreas Gohr        $options->registerOption(
32352ec8ceSAndreas Gohr            'english-only',
33352ec8ceSAndreas Gohr            'Remove all languages except English',
34352ec8ceSAndreas Gohr            'e'
35352ec8ceSAndreas Gohr        );
36c0470665SMartin 'E.T.' Misuth    }
37c0470665SMartin 'E.T.' Misuth
38352ec8ceSAndreas Gohr    /**
39352ec8ceSAndreas Gohr     * Your main program
40352ec8ceSAndreas Gohr     *
41352ec8ceSAndreas Gohr     * Arguments and options have been parsed when this is run
42352ec8ceSAndreas Gohr     *
43352ec8ceSAndreas Gohr     * @param DokuCLI_Options $options
44352ec8ceSAndreas Gohr     * @return void
45352ec8ceSAndreas Gohr     */
46352ec8ceSAndreas Gohr    protected function main(DokuCLI_Options $options) {
47352ec8ceSAndreas Gohr        if($options->getOpt('keep')) {
48352ec8ceSAndreas Gohr            $keep = explode(',', $options->getOpt('keep'));
49352ec8ceSAndreas Gohr            if(!in_array('en', $keep)) $keep[] = 'en';
50352ec8ceSAndreas Gohr        } elseif($options->getOpt('english-only')) {
51352ec8ceSAndreas Gohr            $keep = array('en');
52352ec8ceSAndreas Gohr        } else {
53352ec8ceSAndreas Gohr            echo $options->help();
54352ec8ceSAndreas Gohr            exit(0);
55c0470665SMartin 'E.T.' Misuth        }
56c0470665SMartin 'E.T.' Misuth
57352ec8ceSAndreas Gohr        // Kill all language directories in /inc/lang and /lib/plugins besides those in $langs array
58352ec8ceSAndreas Gohr        $this->stripDirLangs(realpath(dirname(__FILE__).'/../inc/lang'), $keep);
59352ec8ceSAndreas Gohr        $this->processExtensions(realpath(dirname(__FILE__).'/../lib/plugins'), $keep);
60352ec8ceSAndreas Gohr        $this->processExtensions(realpath(dirname(__FILE__).'/../lib/tpl'), $keep);
61352ec8ceSAndreas Gohr    }
62352ec8ceSAndreas Gohr
63352ec8ceSAndreas Gohr    /**
64352ec8ceSAndreas Gohr     * Strip languages from extensions
65352ec8ceSAndreas Gohr     *
66352ec8ceSAndreas Gohr     * @param string $path       path to plugin or template dir
67352ec8ceSAndreas Gohr     * @param array  $keep_langs languages to keep
68352ec8ceSAndreas Gohr     */
69352ec8ceSAndreas Gohr    protected function processExtensions($path, $keep_langs) {
70c0470665SMartin 'E.T.' Misuth        if(is_dir($path)) {
71c0470665SMartin 'E.T.' Misuth            $entries = scandir($path);
72c0470665SMartin 'E.T.' Misuth
73c0470665SMartin 'E.T.' Misuth            foreach($entries as $entry) {
74c0470665SMartin 'E.T.' Misuth                if($entry != "." && $entry != "..") {
7579e197efSAndreas Gohr                    if(is_dir($path.'/'.$entry)) {
76c0470665SMartin 'E.T.' Misuth
7779e197efSAndreas Gohr                        $plugin_langs = $path.'/'.$entry.'/lang';
78c0470665SMartin 'E.T.' Misuth
79c0470665SMartin 'E.T.' Misuth                        if(is_dir($plugin_langs)) {
80352ec8ceSAndreas Gohr                            $this->stripDirLangs($plugin_langs, $keep_langs);
81c0470665SMartin 'E.T.' Misuth                        }
82c0470665SMartin 'E.T.' Misuth                    }
83c0470665SMartin 'E.T.' Misuth                }
84c0470665SMartin 'E.T.' Misuth            }
85c0470665SMartin 'E.T.' Misuth        }
86c0470665SMartin 'E.T.' Misuth    }
87c0470665SMartin 'E.T.' Misuth
88352ec8ceSAndreas Gohr    /**
89352ec8ceSAndreas Gohr     * Strip languages from path
90352ec8ceSAndreas Gohr     *
91352ec8ceSAndreas Gohr     * @param string $path       path to lang dir
92352ec8ceSAndreas Gohr     * @param array  $keep_langs languages to keep
93352ec8ceSAndreas Gohr     */
94352ec8ceSAndreas Gohr    protected function stripDirLangs($path, $keep_langs) {
95c0470665SMartin 'E.T.' Misuth        $dir = dir($path);
96c0470665SMartin 'E.T.' Misuth
97c0470665SMartin 'E.T.' Misuth        while(($cur_dir = $dir->read()) !== false) {
9879e197efSAndreas Gohr            if($cur_dir != '.' and $cur_dir != '..' and is_dir($path.'/'.$cur_dir)) {
99c0470665SMartin 'E.T.' Misuth
100c0470665SMartin 'E.T.' Misuth                if(!in_array($cur_dir, $keep_langs, true)) {
101352ec8ceSAndreas Gohr                    io_rmdir($path.'/'.$cur_dir, true);
102c0470665SMartin 'E.T.' Misuth                }
103c0470665SMartin 'E.T.' Misuth            }
104c0470665SMartin 'E.T.' Misuth        }
105c0470665SMartin 'E.T.' Misuth        $dir->close();
106c0470665SMartin 'E.T.' Misuth    }
107c0470665SMartin 'E.T.' Misuth}
108c0470665SMartin 'E.T.' Misuth
109352ec8ceSAndreas Gohr$cli = new StripLangsCLI();
110352ec8ceSAndreas Gohr$cli->run();