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