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 'langcodes' 31 ); 32 $options->registerOption( 33 'english-only', 34 'Remove all languages except English', 35 'e' 36 ); 37 } 38 39 /** 40 * Your main program 41 * 42 * Arguments and options have been parsed when this is run 43 * 44 * @param DokuCLI_Options $options 45 * @return void 46 */ 47 protected function main(DokuCLI_Options $options) { 48 if($options->getOpt('keep')) { 49 $keep = explode(',', $options->getOpt('keep')); 50 if(!in_array('en', $keep)) $keep[] = 'en'; 51 } elseif($options->getOpt('english-only')) { 52 $keep = array('en'); 53 } else { 54 echo $options->help(); 55 exit(0); 56 } 57 58 // Kill all language directories in /inc/lang and /lib/plugins besides those in $langs array 59 $this->stripDirLangs(realpath(dirname(__FILE__).'/../inc/lang'), $keep); 60 $this->processExtensions(realpath(dirname(__FILE__).'/../lib/plugins'), $keep); 61 $this->processExtensions(realpath(dirname(__FILE__).'/../lib/tpl'), $keep); 62 } 63 64 /** 65 * Strip languages from extensions 66 * 67 * @param string $path path to plugin or template dir 68 * @param array $keep_langs languages to keep 69 */ 70 protected function processExtensions($path, $keep_langs) { 71 if(is_dir($path)) { 72 $entries = scandir($path); 73 74 foreach($entries as $entry) { 75 if($entry != "." && $entry != "..") { 76 if(is_dir($path.'/'.$entry)) { 77 78 $plugin_langs = $path.'/'.$entry.'/lang'; 79 80 if(is_dir($plugin_langs)) { 81 $this->stripDirLangs($plugin_langs, $keep_langs); 82 } 83 } 84 } 85 } 86 } 87 } 88 89 /** 90 * Strip languages from path 91 * 92 * @param string $path path to lang dir 93 * @param array $keep_langs languages to keep 94 */ 95 protected function stripDirLangs($path, $keep_langs) { 96 $dir = dir($path); 97 98 while(($cur_dir = $dir->read()) !== false) { 99 if($cur_dir != '.' and $cur_dir != '..' and is_dir($path.'/'.$cur_dir)) { 100 101 if(!in_array($cur_dir, $keep_langs, true)) { 102 io_rmdir($path.'/'.$cur_dir, true); 103 } 104 } 105 } 106 $dir->close(); 107 } 108} 109 110$cli = new StripLangsCLI(); 111$cli->run();