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 */ 148c7c53b0SAndreas Gohrclass StripLangsCLI extends CLI 158c7c53b0SAndreas 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 */ 23*d868eb89SAndreas Gohr protected function setup(Options $options) 24*d868eb89SAndreas Gohr { 25c0470665SMartin 'E.T.' Misuth 26352ec8ceSAndreas Gohr $options->setHelp( 27352ec8ceSAndreas Gohr 'Remove all languages from the installation, besides the ones specified. English language ' . 28352ec8ceSAndreas Gohr 'is never removed!' 29352ec8ceSAndreas Gohr ); 30c0470665SMartin 'E.T.' Misuth 31352ec8ceSAndreas Gohr $options->registerOption( 32352ec8ceSAndreas Gohr 'keep', 33352ec8ceSAndreas Gohr 'Comma separated list of languages to keep in addition to English.', 3447914fdfSAndreas Gohr 'k', 3547914fdfSAndreas Gohr 'langcodes' 36352ec8ceSAndreas Gohr ); 37352ec8ceSAndreas Gohr $options->registerOption( 38352ec8ceSAndreas Gohr 'english-only', 39352ec8ceSAndreas Gohr 'Remove all languages except English', 40352ec8ceSAndreas Gohr 'e' 41352ec8ceSAndreas Gohr ); 42c0470665SMartin 'E.T.' Misuth } 43c0470665SMartin 'E.T.' Misuth 44352ec8ceSAndreas Gohr /** 45352ec8ceSAndreas Gohr * Your main program 46352ec8ceSAndreas Gohr * 47352ec8ceSAndreas Gohr * Arguments and options have been parsed when this is run 48352ec8ceSAndreas Gohr * 49cbeaa4a0SAndreas Gohr * @param Options $options 50352ec8ceSAndreas Gohr * @return void 51352ec8ceSAndreas Gohr */ 52*d868eb89SAndreas Gohr protected function main(Options $options) 53*d868eb89SAndreas Gohr { 54352ec8ceSAndreas Gohr if($options->getOpt('keep')) { 55352ec8ceSAndreas Gohr $keep = explode(',', $options->getOpt('keep')); 56352ec8ceSAndreas Gohr if(!in_array('en', $keep)) $keep[] = 'en'; 57352ec8ceSAndreas Gohr } elseif($options->getOpt('english-only')) { 58b1f206e1SAndreas Gohr $keep = ['en']; 59352ec8ceSAndreas Gohr } else { 60352ec8ceSAndreas Gohr echo $options->help(); 61352ec8ceSAndreas Gohr exit(0); 62c0470665SMartin 'E.T.' Misuth } 63c0470665SMartin 'E.T.' Misuth 64352ec8ceSAndreas Gohr // Kill all language directories in /inc/lang and /lib/plugins besides those in $langs array 65b1f206e1SAndreas Gohr $this->stripDirLangs(realpath(__DIR__ . '/../inc/lang'), $keep); 66b1f206e1SAndreas Gohr $this->processExtensions(realpath(__DIR__ . '/../lib/plugins'), $keep); 67b1f206e1SAndreas Gohr $this->processExtensions(realpath(__DIR__ . '/../lib/tpl'), $keep); 68352ec8ceSAndreas Gohr } 69352ec8ceSAndreas Gohr 70352ec8ceSAndreas Gohr /** 71352ec8ceSAndreas Gohr * Strip languages from extensions 72352ec8ceSAndreas Gohr * 73352ec8ceSAndreas Gohr * @param string $path path to plugin or template dir 74352ec8ceSAndreas Gohr * @param array $keep_langs languages to keep 75352ec8ceSAndreas Gohr */ 76*d868eb89SAndreas Gohr protected function processExtensions($path, $keep_langs) 77*d868eb89SAndreas Gohr { 78c0470665SMartin 'E.T.' Misuth if(is_dir($path)) { 79c0470665SMartin 'E.T.' Misuth $entries = scandir($path); 80c0470665SMartin 'E.T.' Misuth 81c0470665SMartin 'E.T.' Misuth foreach($entries as $entry) { 82c0470665SMartin 'E.T.' Misuth if($entry != "." && $entry != "..") { 8379e197efSAndreas Gohr if(is_dir($path . '/' . $entry)) { 84c0470665SMartin 'E.T.' Misuth 8579e197efSAndreas Gohr $plugin_langs = $path . '/' . $entry . '/lang'; 86c0470665SMartin 'E.T.' Misuth 87c0470665SMartin 'E.T.' Misuth if(is_dir($plugin_langs)) { 88352ec8ceSAndreas Gohr $this->stripDirLangs($plugin_langs, $keep_langs); 89c0470665SMartin 'E.T.' Misuth } 90c0470665SMartin 'E.T.' Misuth } 91c0470665SMartin 'E.T.' Misuth } 92c0470665SMartin 'E.T.' Misuth } 93c0470665SMartin 'E.T.' Misuth } 94c0470665SMartin 'E.T.' Misuth } 95c0470665SMartin 'E.T.' Misuth 96352ec8ceSAndreas Gohr /** 97352ec8ceSAndreas Gohr * Strip languages from path 98352ec8ceSAndreas Gohr * 99352ec8ceSAndreas Gohr * @param string $path path to lang dir 100352ec8ceSAndreas Gohr * @param array $keep_langs languages to keep 101352ec8ceSAndreas Gohr */ 102*d868eb89SAndreas Gohr protected function stripDirLangs($path, $keep_langs) 103*d868eb89SAndreas Gohr { 104c0470665SMartin 'E.T.' Misuth $dir = dir($path); 105c0470665SMartin 'E.T.' Misuth 106c0470665SMartin 'E.T.' Misuth while(($cur_dir = $dir->read()) !== false) { 107b1f206e1SAndreas Gohr if($cur_dir != '.' && $cur_dir != '..' && is_dir($path . '/' . $cur_dir)) { 108c0470665SMartin 'E.T.' Misuth 109c0470665SMartin 'E.T.' Misuth if(!in_array($cur_dir, $keep_langs, true)) { 110352ec8ceSAndreas Gohr io_rmdir($path . '/' . $cur_dir, true); 111c0470665SMartin 'E.T.' Misuth } 112c0470665SMartin 'E.T.' Misuth } 113c0470665SMartin 'E.T.' Misuth } 114c0470665SMartin 'E.T.' Misuth $dir->close(); 115c0470665SMartin 'E.T.' Misuth } 116c0470665SMartin 'E.T.' Misuth} 117c0470665SMartin 'E.T.' Misuth 118352ec8ceSAndreas Gohr$cli = new StripLangsCLI(); 119352ec8ceSAndreas Gohr$cli->run(); 120