xref: /dokuwiki/bin/striplangs.php (revision 352ec8ce2a661a326f2f6cb747d43bb60119fb3e)
1#!/usr/bin/php
2<?php
3if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
4define('NOSESSION', 1);
5require_once(DOKU_INC . 'inc/init.php');
6
7
8class StripLangsCLI extends DokuCLI {
9
10    /**
11     * Register options and arguments on the given $options object
12     *
13     * @param DokuCLI_Options $options
14     * @return void
15     */
16    protected function setup(DokuCLI_Options $options) {
17
18        $options->setHelp(
19                'Remove all languages from the installation, besides the ones specified. English language ' .
20                'is never removed!'
21        );
22
23        $options->registerOption(
24                'keep',
25                'Comma separated list of languages to keep in addition to English.',
26                'k'
27        );
28        $options->registerOption(
29                'english-only',
30                'Remove all languages except English',
31                'e'
32        );
33    }
34
35    /**
36     * Your main program
37     *
38     * Arguments and options have been parsed when this is run
39     *
40     * @param DokuCLI_Options $options
41     * @return void
42     */
43    protected function main(DokuCLI_Options $options) {
44        if($options->getOpt('keep')) {
45            $keep = explode(',', $options->getOpt('keep'));
46            if(!in_array('en', $keep)) $keep[] = 'en';
47        } elseif($options->getOpt('english-only')) {
48            $keep = array('en');
49        } else {
50            echo $options->help();
51            exit(0);
52        }
53
54        // Kill all language directories in /inc/lang and /lib/plugins besides those in $langs array
55        $this->stripDirLangs(realpath(dirname(__FILE__) . '/../inc/lang'), $keep);
56        $this->processExtensions(realpath(dirname(__FILE__) . '/../lib/plugins'), $keep);
57        $this->processExtensions(realpath(dirname(__FILE__) . '/../lib/tpl'), $keep);
58    }
59
60    /**
61     * Strip languages from extensions
62     *
63     * @param string $path path to plugin or template dir
64     * @param array $keep_langs languages to keep
65     */
66    protected function processExtensions($path, $keep_langs) {
67        if(is_dir($path)) {
68            $entries = scandir($path);
69
70            foreach($entries as $entry) {
71                if($entry != "." && $entry != "..") {
72                    if(is_dir($path . '/' . $entry)) {
73
74                        $plugin_langs = $path . '/' . $entry . '/lang';
75
76                        if(is_dir($plugin_langs)) {
77                            $this->stripDirLangs($plugin_langs, $keep_langs);
78                        }
79                    }
80                }
81            }
82        }
83    }
84
85    /**
86     * Strip languages from path
87     *
88     * @param string $path path to lang dir
89     * @param array $keep_langs languages to keep
90     */
91    protected function stripDirLangs($path, $keep_langs) {
92        $dir = dir($path);
93
94        while(($cur_dir = $dir->read()) !== false) {
95            if($cur_dir != '.' and $cur_dir != '..' and is_dir($path . '/' . $cur_dir)) {
96
97                if(!in_array($cur_dir, $keep_langs, true)) {
98                    io_rmdir($path . '/' . $cur_dir, true);
99                }
100            }
101        }
102        $dir->close();
103    }
104}
105
106$cli = new StripLangsCLI();
107$cli->run();