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