1cbfa4829SPhy#!/usr/bin/env php 2c0470665SMartin 'E.T.' Misuth<?php 3cbeaa4a0SAndreas Gohr 4cbeaa4a0SAndreas Gohruse splitbrain\phpcli\CLI; 5cbeaa4a0SAndreas Gohruse splitbrain\phpcli\Options; 6cbeaa4a0SAndreas Gohr 7b1f206e1SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC', realpath(__DIR__ . '/../') . '/'); 8352ec8ceSAndreas Gohrdefine('NOSESSION', 1); 9352ec8ceSAndreas Gohrrequire_once(DOKU_INC . 'inc/init.php'); 10c0470665SMartin 'E.T.' Misuth 111c36b3d8SAndreas Gohr/** 121c36b3d8SAndreas Gohr * Remove unwanted languages from a DokuWiki install 131c36b3d8SAndreas Gohr */ 14*8c7c53b0SAndreas Gohrclass StripLangsCLI extends CLI 15*8c7c53b0SAndreas Gohr{ 16c0470665SMartin 'E.T.' Misuth 17352ec8ceSAndreas Gohr /** 18352ec8ceSAndreas Gohr * Register options and arguments on the given $options object 19352ec8ceSAndreas Gohr * 20cbeaa4a0SAndreas Gohr * @param Options $options 21352ec8ceSAndreas Gohr * @return void 22352ec8ceSAndreas Gohr */ 23cbeaa4a0SAndreas Gohr protected function setup(Options $options) { 24c0470665SMartin 'E.T.' Misuth 25352ec8ceSAndreas Gohr $options->setHelp( 26352ec8ceSAndreas Gohr 'Remove all languages from the installation, besides the ones specified. English language ' . 27352ec8ceSAndreas Gohr 'is never removed!' 28352ec8ceSAndreas Gohr ); 29c0470665SMartin 'E.T.' Misuth 30352ec8ceSAndreas Gohr $options->registerOption( 31352ec8ceSAndreas Gohr 'keep', 32352ec8ceSAndreas Gohr 'Comma separated list of languages to keep in addition to English.', 3347914fdfSAndreas Gohr 'k', 3447914fdfSAndreas Gohr 'langcodes' 35352ec8ceSAndreas Gohr ); 36352ec8ceSAndreas Gohr $options->registerOption( 37352ec8ceSAndreas Gohr 'english-only', 38352ec8ceSAndreas Gohr 'Remove all languages except English', 39352ec8ceSAndreas Gohr 'e' 40352ec8ceSAndreas Gohr ); 41c0470665SMartin 'E.T.' Misuth } 42c0470665SMartin 'E.T.' Misuth 43352ec8ceSAndreas Gohr /** 44352ec8ceSAndreas Gohr * Your main program 45352ec8ceSAndreas Gohr * 46352ec8ceSAndreas Gohr * Arguments and options have been parsed when this is run 47352ec8ceSAndreas Gohr * 48cbeaa4a0SAndreas Gohr * @param Options $options 49352ec8ceSAndreas Gohr * @return void 50352ec8ceSAndreas Gohr */ 51cbeaa4a0SAndreas Gohr protected function main(Options $options) { 52352ec8ceSAndreas Gohr if($options->getOpt('keep')) { 53352ec8ceSAndreas Gohr $keep = explode(',', $options->getOpt('keep')); 54352ec8ceSAndreas Gohr if(!in_array('en', $keep)) $keep[] = 'en'; 55352ec8ceSAndreas Gohr } elseif($options->getOpt('english-only')) { 56b1f206e1SAndreas Gohr $keep = ['en']; 57352ec8ceSAndreas Gohr } else { 58352ec8ceSAndreas Gohr echo $options->help(); 59352ec8ceSAndreas Gohr exit(0); 60c0470665SMartin 'E.T.' Misuth } 61c0470665SMartin 'E.T.' Misuth 62352ec8ceSAndreas Gohr // Kill all language directories in /inc/lang and /lib/plugins besides those in $langs array 63b1f206e1SAndreas Gohr $this->stripDirLangs(realpath(__DIR__ . '/../inc/lang'), $keep); 64b1f206e1SAndreas Gohr $this->processExtensions(realpath(__DIR__ . '/../lib/plugins'), $keep); 65b1f206e1SAndreas Gohr $this->processExtensions(realpath(__DIR__ . '/../lib/tpl'), $keep); 66352ec8ceSAndreas Gohr } 67352ec8ceSAndreas Gohr 68352ec8ceSAndreas Gohr /** 69352ec8ceSAndreas Gohr * Strip languages from extensions 70352ec8ceSAndreas Gohr * 71352ec8ceSAndreas Gohr * @param string $path path to plugin or template dir 72352ec8ceSAndreas Gohr * @param array $keep_langs languages to keep 73352ec8ceSAndreas Gohr */ 74352ec8ceSAndreas Gohr protected function processExtensions($path, $keep_langs) { 75c0470665SMartin 'E.T.' Misuth if(is_dir($path)) { 76c0470665SMartin 'E.T.' Misuth $entries = scandir($path); 77c0470665SMartin 'E.T.' Misuth 78c0470665SMartin 'E.T.' Misuth foreach($entries as $entry) { 79c0470665SMartin 'E.T.' Misuth if($entry != "." && $entry != "..") { 8079e197efSAndreas Gohr if(is_dir($path . '/' . $entry)) { 81c0470665SMartin 'E.T.' Misuth 8279e197efSAndreas Gohr $plugin_langs = $path . '/' . $entry . '/lang'; 83c0470665SMartin 'E.T.' Misuth 84c0470665SMartin 'E.T.' Misuth if(is_dir($plugin_langs)) { 85352ec8ceSAndreas Gohr $this->stripDirLangs($plugin_langs, $keep_langs); 86c0470665SMartin 'E.T.' Misuth } 87c0470665SMartin 'E.T.' Misuth } 88c0470665SMartin 'E.T.' Misuth } 89c0470665SMartin 'E.T.' Misuth } 90c0470665SMartin 'E.T.' Misuth } 91c0470665SMartin 'E.T.' Misuth } 92c0470665SMartin 'E.T.' Misuth 93352ec8ceSAndreas Gohr /** 94352ec8ceSAndreas Gohr * Strip languages from path 95352ec8ceSAndreas Gohr * 96352ec8ceSAndreas Gohr * @param string $path path to lang dir 97352ec8ceSAndreas Gohr * @param array $keep_langs languages to keep 98352ec8ceSAndreas Gohr */ 99352ec8ceSAndreas Gohr protected function stripDirLangs($path, $keep_langs) { 100c0470665SMartin 'E.T.' Misuth $dir = dir($path); 101c0470665SMartin 'E.T.' Misuth 102c0470665SMartin 'E.T.' Misuth while(($cur_dir = $dir->read()) !== false) { 103b1f206e1SAndreas Gohr if($cur_dir != '.' && $cur_dir != '..' && is_dir($path . '/' . $cur_dir)) { 104c0470665SMartin 'E.T.' Misuth 105c0470665SMartin 'E.T.' Misuth if(!in_array($cur_dir, $keep_langs, true)) { 106352ec8ceSAndreas Gohr io_rmdir($path . '/' . $cur_dir, true); 107c0470665SMartin 'E.T.' Misuth } 108c0470665SMartin 'E.T.' Misuth } 109c0470665SMartin 'E.T.' Misuth } 110c0470665SMartin 'E.T.' Misuth $dir->close(); 111c0470665SMartin 'E.T.' Misuth } 112c0470665SMartin 'E.T.' Misuth} 113c0470665SMartin 'E.T.' Misuth 114352ec8ceSAndreas Gohr$cli = new StripLangsCLI(); 115352ec8ceSAndreas Gohr$cli->run(); 116