Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dokuwiki
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
BRIC
dokuwiki
Commits
352ec8ce
Commit
352ec8ce
authored
11 years ago
by
Andreas Gohr
Browse files
Options
Downloads
Patches
Plain Diff
refactored bin/striplangs.php to new CLI interface
parent
d48bcdb4
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
bin/striplangs.php
+87
-128
87 additions, 128 deletions
bin/striplangs.php
with
87 additions
and
128 deletions
bin/striplangs.php
+
87
−
128
View file @
352ec8ce
#!/usr/bin/php
<?php
/**
* Strip unwanted languages from the DokuWiki install
*
* @author Martin 'E.T.' Misuth <et.github@ethome.sk>
*/
if
(
'cli'
!=
php_sapi_name
())
die
();
#------------------------------------------------------------------------------
if
(
!
defined
(
'DOKU_INC'
))
define
(
'DOKU_INC'
,
realpath
(
dirname
(
__FILE__
)
.
'/../'
)
.
'/'
);
require_once
DOKU_INC
.
'inc/cliopts.php'
;
#------------------------------------------------------------------------------
function
usage
(
$show_examples
=
false
)
{
print
"Usage: striplangs.php [-h [-x]] [-e] [-k lang1[,lang2]..[,langN]]
Removes all languages from the installation, besides the ones
after the -k option. English language is never removed!
OPTIONS
-h, --help get this help
-x, --examples get also usage examples
-k, --keep comma separated list of languages, -e is always implied
-e, --english keeps english, dummy to use without -k
\n
"
;
if
(
$show_examples
)
{
print
"
\n
EXAMPLES
Strips all languages, but keeps 'en' and 'de':
striplangs -k de
Strips all but 'en','ca-valencia','cs','de','is','sk':
striplangs --keep ca-valencia,cs,de,is,sk
Strips all but 'en':
striplangs -e
No option specified, prints usage and throws error:
striplangs
\n
"
;
}
}
function
getSuppliedArgument
(
$OPTS
,
$short
,
$long
)
{
$arg
=
$OPTS
->
get
(
$short
);
if
(
is_null
(
$arg
)
)
{
$arg
=
$OPTS
->
get
(
$long
);
if
(
!
defined
(
'DOKU_INC'
))
define
(
'DOKU_INC'
,
realpath
(
dirname
(
__FILE__
)
.
'/../'
)
.
'/'
);
define
(
'NOSESSION'
,
1
);
require_once
(
DOKU_INC
.
'inc/init.php'
);
class
StripLangsCLI
extends
DokuCLI
{
/**
* Register options and arguments on the given $options object
*
* @param DokuCLI_Options $options
* @return void
*/
protected
function
setup
(
DokuCLI_Options
$options
)
{
$options
->
setHelp
(
'Remove all languages from the installation, besides the ones specified. English language '
.
'is never removed!'
);
$options
->
registerOption
(
'keep'
,
'Comma separated list of languages to keep in addition to English.'
,
'k'
);
$options
->
registerOption
(
'english-only'
,
'Remove all languages except English'
,
'e'
);
}
return
$arg
;
}
function
processPlugins
(
$path
,
$keep_langs
)
{
if
(
is_dir
(
$path
))
{
$entries
=
scandir
(
$path
);
foreach
(
$entries
as
$entry
)
{
if
(
$entry
!=
"."
&&
$entry
!=
".."
)
{
if
(
is_dir
(
$path
.
'/'
.
$entry
)
)
{
/**
* Your main program
*
* Arguments and options have been parsed when this is run
*
* @param DokuCLI_Options $options
* @return void
*/
protected
function
main
(
DokuCLI_Options
$options
)
{
if
(
$options
->
getOpt
(
'keep'
))
{
$keep
=
explode
(
','
,
$options
->
getOpt
(
'keep'
));
if
(
!
in_array
(
'en'
,
$keep
))
$keep
[]
=
'en'
;
}
elseif
(
$options
->
getOpt
(
'english-only'
))
{
$keep
=
array
(
'en'
);
}
else
{
echo
$options
->
help
();
exit
(
0
);
}
$plugin_langs
=
$path
.
'/'
.
$entry
.
'/lang'
;
// Kill all language directories in /inc/lang and /lib/plugins besides those in $langs array
$this
->
stripDirLangs
(
realpath
(
dirname
(
__FILE__
)
.
'/../inc/lang'
),
$keep
);
$this
->
processExtensions
(
realpath
(
dirname
(
__FILE__
)
.
'/../lib/plugins'
),
$keep
);
$this
->
processExtensions
(
realpath
(
dirname
(
__FILE__
)
.
'/../lib/tpl'
),
$keep
);
}
if
(
is_dir
(
$plugin_langs
)
)
{
stripDirLangs
(
$plugin_langs
,
$keep_langs
);
/**
* Strip languages from extensions
*
* @param string $path path to plugin or template dir
* @param array $keep_langs languages to keep
*/
protected
function
processExtensions
(
$path
,
$keep_langs
)
{
if
(
is_dir
(
$path
))
{
$entries
=
scandir
(
$path
);
foreach
(
$entries
as
$entry
)
{
if
(
$entry
!=
"."
&&
$entry
!=
".."
)
{
if
(
is_dir
(
$path
.
'/'
.
$entry
))
{
$plugin_langs
=
$path
.
'/'
.
$entry
.
'/lang'
;
if
(
is_dir
(
$plugin_langs
))
{
$this
->
stripDirLangs
(
$plugin_langs
,
$keep_langs
);
}
}
}
}
}
}
}
function
stripDirLangs
(
$path
,
$keep_langs
)
{
$dir
=
dir
(
$path
);
while
((
$cur_dir
=
$dir
->
read
())
!==
false
)
{
if
(
$cur_dir
!=
'.'
and
$cur_dir
!=
'..'
and
is_dir
(
$path
.
'/'
.
$cur_dir
))
{
/**
* Strip languages from path
*
* @param string $path path to lang dir
* @param array $keep_langs languages to keep
*/
protected
function
stripDirLangs
(
$path
,
$keep_langs
)
{
$dir
=
dir
(
$path
);
if
(
!
in_array
(
$cur_dir
,
$keep_langs
,
true
)
)
{
killDir
(
$path
.
'/'
.
$cur_dir
);
}
}
}
$dir
->
close
();
}
while
((
$cur_dir
=
$dir
->
read
())
!==
false
)
{
if
(
$cur_dir
!=
'.'
and
$cur_dir
!=
'..'
and
is_dir
(
$path
.
'/'
.
$cur_dir
))
{
function
killDir
(
$dir
)
{
if
(
is_dir
(
$dir
))
{
$entries
=
scandir
(
$dir
);
foreach
(
$entries
as
$entry
)
{
if
(
$entry
!=
"."
&&
$entry
!=
".."
)
{
if
(
is_dir
(
$dir
.
'/'
.
$entry
)
)
{
killDir
(
$dir
.
'/'
.
$entry
);
}
else
{
unlink
(
$dir
.
'/'
.
$entry
);
if
(
!
in_array
(
$cur_dir
,
$keep_langs
,
true
))
{
io_rmdir
(
$path
.
'/'
.
$cur_dir
,
true
);
}
}
}
reset
(
$entries
);
rmdir
(
$dir
);
}
}
#------------------------------------------------------------------------------
// handle options
$short_opts
=
'hxk:e'
;
$long_opts
=
array
(
'help'
,
'examples'
,
'keep='
,
'english'
);
$OPTS
=
Doku_Cli_Opts
::
getOptions
(
__FILE__
,
$short_opts
,
$long_opts
);
if
(
$OPTS
->
isError
()
)
{
fwrite
(
STDERR
,
$OPTS
->
getMessage
()
.
"
\n
"
);
exit
(
1
);
}
// handle '--examples' option
$show_examples
=
(
$OPTS
->
has
(
'x'
)
or
$OPTS
->
has
(
'examples'
)
)
?
true
:
false
;
// handle '--help' option
if
(
$OPTS
->
has
(
'h'
)
or
$OPTS
->
has
(
'help'
)
)
{
usage
(
$show_examples
);
exit
(
0
);
}
// handle both '--keep' and '--english' options
if
(
$OPTS
->
has
(
'k'
)
or
$OPTS
->
has
(
'keep'
)
)
{
$preserved_langs
=
getSuppliedArgument
(
$OPTS
,
'k'
,
'keep'
);
$langs
=
explode
(
','
,
$preserved_langs
);
// ! always enforce 'en' lang when using '--keep' (DW relies on it)
if
(
!
isset
(
$langs
[
'en'
])
)
{
$langs
[]
=
'en'
;
$dir
->
close
();
}
}
elseif
(
$OPTS
->
has
(
'e'
)
or
$OPTS
->
has
(
'english'
)
)
{
// '--english' was specified strip everything besides 'en'
$langs
=
array
(
'en'
);
}
else
{
// no option was specified, print usage but don't do anything as
// this run might not be intented
usage
();
print
"
\n
ERROR
No option specified, use either -h -x to get more info,
or -e to strip every language besides english.
\n
"
;
exit
(
1
);
}
// Kill all language directories in /inc/lang and /lib/plugins besides those in $langs array
stripDirLangs
(
realpath
(
dirname
(
__FILE__
)
.
'/../inc/lang'
),
$langs
);
processPlugins
(
realpath
(
dirname
(
__FILE__
)
.
'/../lib/plugins'
),
$langs
);
$cli
=
new
StripLangsCLI
();
$cli
->
run
();
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment