diff options
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/dwpage.php | 597 | ||||
-rwxr-xr-x | bin/gittool.php | 216 | ||||
-rwxr-xr-x | bin/indexer.php | 175 | ||||
-rwxr-xr-x | bin/render.php | 94 | ||||
-rwxr-xr-x | bin/striplangs.php | 214 | ||||
-rwxr-xr-x | bin/wantedpages.php | 223 |
6 files changed, 706 insertions, 813 deletions
diff --git a/bin/dwpage.php b/bin/dwpage.php index 96f6d3ef9..a777fd3e1 100755 --- a/bin/dwpage.php +++ b/bin/dwpage.php @@ -1,378 +1,317 @@ #!/usr/bin/php <?php -#------------------------------------------------------------------------------ -if ('cli' != php_sapi_name()) die(); - -ini_set('memory_limit','128M'); -if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/'); -require_once DOKU_INC.'inc/init.php'; -require_once DOKU_INC.'inc/common.php'; -require_once DOKU_INC.'inc/cliopts.php'; - -#------------------------------------------------------------------------------ -function usage($action) { - switch ( $action ) { - case 'checkout': - print "Usage: dwpage.php [opts] checkout <wiki:page> [working_file] - - Checks out a file from the repository, using the wiki id and obtaining - a lock for the page. - If a working_file is specified, this is where the page is copied to. - Otherwise defaults to the same as the wiki page in the current - working directory. - - EXAMPLE - $ ./dwpage.php checkout wiki:syntax ./new_syntax.txt - - OPTIONS - -h, --help=<action>: get help - -f: force obtaining a lock for the page (generally bad idea) -"; - break; - case 'commit': - print "Usage: dwpage.php [opts] -m \"Msg\" commit <working_file> <wiki:page> - - Checks in the working_file into the repository using the specified - wiki id, archiving the previous version. - - EXAMPLE - $ ./dwpage.php -m \"Some message\" commit ./new_syntax.txt wiki:syntax - - OPTIONS - -h, --help=<action>: get help - -f: force obtaining a lock for the page (generally bad idea) - -t, trivial: minor change - -m (required): Summary message describing the change -"; - break; - case 'lock': - print "Usage: dwpage.php [opts] lock <wiki:page> - - Obtains or updates a lock for a wiki page - - EXAMPLE - $ ./dwpage.php lock wiki:syntax - - OPTIONS - -h, --help=<action>: get help - -f: force obtaining a lock for the page (generally bad idea) -"; - break; - case 'unlock': - print "Usage: dwpage.php [opts] unlock <wiki:page> - - Removes a lock for a wiki page. - - EXAMPLE - $ ./dwpage.php unlock wiki:syntax - - OPTIONS - -h, --help=<action>: get help - -f: force obtaining a lock for the page (generally bad idea) -"; - break; - default: - print "Usage: dwpage.php [opts] <action> - - Utility to help command line Dokuwiki page editing, allow - pages to be checked out for editing then committed after changes - - Normal operation would be; - - - - ACTIONS - checkout: see $ dwpage.php --help=checkout - commit: see $ dwpage.php --help=commit - lock: see $ dwpage.php --help=lock - - OPTIONS - -h, --help=<action>: get help - e.g. $ ./dwpage.php -hcommit - e.g. $ ./dwpage.php --help=commit -"; - break; +if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__).'/../').'/'); +define('NOSESSION', 1); +require_once(DOKU_INC.'inc/init.php'); + +/** + * Checkout and commit pages from the command line while maintaining the history + */ +class PageCLI extends DokuCLI { + + protected $force = false; + protected $username = ''; + + /** + * Register options and arguments on the given $options object + * + * @param DokuCLI_Options $options + * @return void + */ + protected function setup(DokuCLI_Options $options) { + /* global */ + $options->registerOption( + 'force', + 'force obtaining a lock for the page (generally bad idea)', + 'f' + ); + $options->registerOption( + 'user', + 'work as this user. defaults to current CLI user', + 'u' + ); + $options->setHelp( + 'Utility to help command line Dokuwiki page editing, allow '. + 'pages to be checked out for editing then committed after changes' + ); + + /* checkout command */ + $options->registerCommand( + 'checkout', + 'Checks out a file from the repository, using the wiki id and obtaining '. + 'a lock for the page. '."\n". + 'If a working_file is specified, this is where the page is copied to. '. + 'Otherwise defaults to the same as the wiki page in the current '. + 'working directory.' + ); + $options->registerArgument( + 'wikipage', + 'The wiki page to checkout', + true, + 'checkout' + ); + $options->registerArgument( + 'workingfile', + 'How to name the local checkout', + false, + 'checkout' + ); + + /* commit command */ + $options->registerCommand( + 'commit', + 'Checks in the working_file into the repository using the specified '. + 'wiki id, archiving the previous version.' + ); + $options->registerArgument( + 'workingfile', + 'The local file to commit', + true, + 'commit' + ); + $options->registerArgument( + 'wikipage', + 'The wiki page to create or update', + true, + 'commit' + ); + $options->registerOption( + 'message', + 'Summary describing the change (required)', + 'm', + 'summary', + 'commit' + ); + $options->registerOption( + 'trivial', + 'minor change', + 't', + false, + 'commit' + ); + + /* lock command */ + $options->registerCommand( + 'lock', + 'Obtains or updates a lock for a wiki page' + ); + $options->registerArgument( + 'wikipage', + 'The wiki page to lock', + true, + 'lock' + ); + + /* unlock command */ + $options->registerCommand( + 'unlock', + 'Removes a lock for a wiki page.' + ); + $options->registerArgument( + 'wikipage', + 'The wiki page to unlock', + true, + 'unlock' + ); } -} - -#------------------------------------------------------------------------------ -function getUser() { - $user = getenv('USER'); - if (empty ($user)) { - $user = getenv('USERNAME'); - } else { - return $user; - } - if (empty ($user)) { - $user = 'admin'; - } - return $user; -} - -#------------------------------------------------------------------------------ -function getSuppliedArgument($OPTS, $short, $long) { - $arg = $OPTS->get($short); - if ( is_null($arg) ) { - $arg = $OPTS->get($long); - } - return $arg; -} - -#------------------------------------------------------------------------------ -function obtainLock($WIKI_ID) { - - global $USERNAME; - - if ( !file_exists(wikiFN($WIKI_ID)) ) { - fwrite( STDERR, "$WIKI_ID does not yet exist\n"); - } - - $_SERVER['REMOTE_USER'] = $USERNAME; - if ( checklock($WIKI_ID) ) { - fwrite( STDERR, "Page $WIKI_ID is already locked by another user\n"); - exit(1); - } - - lock($WIKI_ID); - - $_SERVER['REMOTE_USER'] = '_'.$USERNAME.'_'; - - if ( checklock($WIKI_ID) != $USERNAME ) { - - fwrite( STDERR, "Unable to obtain lock for $WIKI_ID\n" ); - exit(1); - - } -} - -#------------------------------------------------------------------------------ -function clearLock($WIKI_ID) { - - global $USERNAME ; - if ( !file_exists(wikiFN($WIKI_ID)) ) { - fwrite( STDERR, "$WIKI_ID does not yet exist\n"); - } - - $_SERVER['REMOTE_USER'] = $USERNAME; - if ( checklock($WIKI_ID) ) { - fwrite( STDERR, "Page $WIKI_ID is locked by another user\n"); - exit(1); - } - - unlock($WIKI_ID); - - if ( file_exists(wikiLockFN($WIKI_ID)) ) { - fwrite( STDERR, "Unable to clear lock for $WIKI_ID\n" ); - exit(1); - } - -} - -#------------------------------------------------------------------------------ -function deleteLock($WIKI_ID) { - - $wikiLockFN = wikiLockFN($WIKI_ID); - - if ( file_exists($wikiLockFN) ) { - if ( !unlink($wikiLockFN) ) { - fwrite( STDERR, "Unable to delete $wikiLockFN\n" ); - exit(1); + /** + * 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) { + $this->force = $options->getOpt('force', false); + $this->username = $options->getOpt('user', $this->getUser()); + + $command = $options->getCmd(); + switch($command) { + case 'checkout': + $wiki_id = array_shift($options->args); + $localfile = array_shift($options->args); + $this->commandCheckout($wiki_id, $localfile); + break; + case 'commit': + $localfile = array_shift($options->args); + $wiki_id = array_shift($options->args); + $this->commandCommit( + $localfile, + $wiki_id, + $options->getOpt('message', ''), + $options->getOpt('trivial', false) + ); + break; + case 'lock': + $wiki_id = array_shift($options->args); + $this->obtainLock($wiki_id); + $this->success("$wiki_id locked"); + break; + case 'unlock': + $wiki_id = array_shift($options->args); + $this->clearLock($wiki_id); + $this->success("$wiki_id unlocked"); + break; + default: + echo $options->help(); } } -} - -#------------------------------------------------------------------------------ -$USERNAME = getUser(); -$CWD = getcwd(); -$SYSTEM_ID = '127.0.0.1'; - -#------------------------------------------------------------------------------ -$OPTS = Doku_Cli_Opts::getOptions( - __FILE__, - 'h::fm:u:s:t', - array( - 'help==', - 'user=', - 'system=', - 'trivial', - ) -); - -if ( $OPTS->isError() ) { - print $OPTS->getMessage()."\n"; - exit(1); -} - -if ( $OPTS->has('h') or $OPTS->has('help') or !$OPTS->hasArgs() ) { - usage(getSuppliedArgument($OPTS,'h','help')); - exit(0); -} - -if ( $OPTS->has('u') or $OPTS->has('user') ) { - $USERNAME = getSuppliedArgument($OPTS,'u','user'); -} - -if ( $OPTS->has('s') or $OPTS->has('system') ) { - $SYSTEM_ID = getSuppliedArgument($OPTS,'s','system'); -} + /** + * Check out a file + * + * @param string $wiki_id + * @param string $localfile + */ + protected function commandCheckout($wiki_id, $localfile) { + global $conf; -#------------------------------------------------------------------------------ -switch ( $OPTS->arg(0) ) { + $wiki_id = cleanID($wiki_id); + $wiki_fn = wikiFN($wiki_id); - #---------------------------------------------------------------------- - case 'checkout': - - $WIKI_ID = $OPTS->arg(1); - - if ( !$WIKI_ID ) { - fwrite( STDERR, "Wiki page ID required\n"); - exit(1); + if(!file_exists($wiki_fn)) { + $this->fatal("$wiki_id does not yet exist"); } - $WIKI_FN = wikiFN($WIKI_ID); - - if ( !file_exists($WIKI_FN) ) { - fwrite( STDERR, "$WIKI_ID does not yet exist\n"); - exit(1); + if(empty($localfile)) { + $localfile = getcwd().'/'.utf8_basename($wiki_fn); } - $TARGET_FN = $OPTS->arg(2); - - if ( empty($TARGET_FN) ) { - $TARGET_FN = getcwd().'/'.utf8_basename($WIKI_FN); + if(!file_exists(dirname($localfile))) { + $this->fatal("Directory ".dirname($localfile)." does not exist"); } - if ( !file_exists(dirname($TARGET_FN)) ) { - fwrite( STDERR, "Directory ".dirname($TARGET_FN)." does not exist\n"); - exit(1); + if(stristr(realpath(dirname($localfile)), realpath($conf['datadir'])) !== false) { + $this->fatal("Attempt to check out file into data directory - not allowed"); } - if ( stristr( realpath(dirname($TARGET_FN)), realpath($conf['datadir']) ) !== false ) { - fwrite( STDERR, "Attempt to check out file into data directory - not allowed\n"); - exit(1); - } + $this->obtainLock($wiki_id); - if ( $OPTS->has('f') ) { - deleteLock($WIKI_ID); + if(!copy($wiki_fn, $localfile)) { + $this->clearLock($wiki_id); + $this->fatal("Unable to copy $wiki_fn to $localfile"); } - obtainLock($WIKI_ID); + $this->success("$wiki_id > $localfile"); + } - # Need to lock the file first? - if ( !copy($WIKI_FN, $TARGET_FN) ) { - fwrite( STDERR, "Unable to copy $WIKI_FN to $TARGET_FN\n"); - clearLock($WIKI_ID); - exit(1); + /** + * Save a file as a new page revision + * + * @param string $localfile + * @param string $wiki_id + * @param string $message + * @param bool $minor + */ + protected function commandCommit($localfile, $wiki_id, $message, $minor) { + $wiki_id = cleanID($wiki_id); + $message = trim($message); + + if(!file_exists($localfile)) { + $this->fatal("$localfile does not exist"); } - print "$WIKI_ID > $TARGET_FN\n"; - exit(0); + if(!is_readable($localfile)) { + $this->fatal("Cannot read from $localfile"); + } - break; + if(!$message) { + $this->fatal("Summary message required"); + } - #---------------------------------------------------------------------- - case 'commit': + $this->obtainLock($wiki_id); - $TARGET_FN = $OPTS->arg(1); + saveWikiText($wiki_id, file_get_contents($localfile), $message, $minor); - if ( !$TARGET_FN ) { - fwrite( STDERR, "Target filename required\n"); - exit(1); - } + $this->clearLock($wiki_id); - if ( !file_exists($TARGET_FN) ) { - fwrite( STDERR, "$TARGET_FN does not exist\n"); - exit(1); - } + $this->success("$localfile > $wiki_id"); + } - if ( !is_readable($TARGET_FN) ) { - fwrite( STDERR, "Cannot read from $TARGET_FN\n"); + /** + * Lock the given page or exit + * + * @param string $wiki_id + */ + protected function obtainLock($wiki_id) { + if($this->force) $this->deleteLock($wiki_id); + + $_SERVER['REMOTE_USER'] = $this->username; + if(checklock($wiki_id)) { + $this->error("Page $wiki_id is already locked by another user"); exit(1); } - $WIKI_ID = $OPTS->arg(2); + lock($wiki_id); - if ( !$WIKI_ID ) { - fwrite( STDERR, "Wiki page ID required\n"); + $_SERVER['REMOTE_USER'] = '_'.$this->username.'_'; + if(checklock($wiki_id) != $this->username) { + $this->error("Unable to obtain lock for $wiki_id "); + var_dump(checklock($wiki_id)); exit(1); } + } - if ( !$OPTS->has('m') ) { - fwrite( STDERR, "Summary message required\n"); + /** + * Clear the lock on the given page + * + * @param string $wiki_id + */ + protected function clearLock($wiki_id) { + if($this->force) $this->deleteLock($wiki_id); + + $_SERVER['REMOTE_USER'] = $this->username; + if(checklock($wiki_id)) { + $this->error("Page $wiki_id is locked by another user"); exit(1); } - if ( $OPTS->has('f') ) { - deleteLock($WIKI_ID); - } + unlock($wiki_id); - $_SERVER['REMOTE_USER'] = $USERNAME; - if ( checklock($WIKI_ID) ) { - fwrite( STDERR, "$WIKI_ID is locked by another user\n"); + if(file_exists(wikiLockFN($wiki_id))) { + $this->error("Unable to clear lock for $wiki_id"); exit(1); } + } - obtainLock($WIKI_ID); - - saveWikiText($WIKI_ID, file_get_contents($TARGET_FN), $OPTS->get('m'), $OPTS->has('t')); - - clearLock($WIKI_ID); - - exit(0); - - break; - - #---------------------------------------------------------------------- - case 'lock': - - $WIKI_ID = $OPTS->arg(1); - - if ( !$WIKI_ID ) { - fwrite( STDERR, "Wiki page ID required\n"); - exit(1); - } - - if ( $OPTS->has('f') ) { - deleteLock($WIKI_ID); - } - - obtainLock($WIKI_ID); - - print "Locked : $WIKI_ID\n"; - exit(0); - - break; - - #---------------------------------------------------------------------- - case 'unlock': - - $WIKI_ID = $OPTS->arg(1); - - if ( !$WIKI_ID ) { - fwrite( STDERR, "Wiki page ID required\n"); - exit(1); + /** + * Forcefully remove a lock on the page given + * + * @param string $wiki_id + */ + protected function deleteLock($wiki_id) { + $wikiLockFN = wikiLockFN($wiki_id); + + if(file_exists($wikiLockFN)) { + if(!unlink($wikiLockFN)) { + $this->error("Unable to delete $wikiLockFN"); + exit(1); + } } + } - if ( $OPTS->has('f') ) { - deleteLock($WIKI_ID); + /** + * Get the current user's username from the environment + * + * @return string + */ + protected function getUser() { + $user = getenv('USER'); + if(empty ($user)) { + $user = getenv('USERNAME'); } else { - clearLock($WIKI_ID); + return $user; } - - print "Unlocked : $WIKI_ID\n"; - exit(0); - - break; - - #---------------------------------------------------------------------- - default: - - fwrite( STDERR, "Invalid action ".$OPTS->arg(0)."\n" ); - exit(1); - - break; - + if(empty ($user)) { + $user = 'admin'; + } + return $user; + } } + +// Main +$cli = new PageCLI(); +$cli->run();
\ No newline at end of file diff --git a/bin/gittool.php b/bin/gittool.php index f9f68ac94..6944dde57 100755 --- a/bin/gittool.php +++ b/bin/gittool.php @@ -1,78 +1,101 @@ #!/usr/bin/php <?php - -if('cli' != php_sapi_name()) die(); -ini_set('memory_limit', '128M'); if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__).'/../').'/'); define('NOSESSION', 1); require_once(DOKU_INC.'inc/init.php'); -$GitToolCLI = new GitToolCLI(); - -array_shift($argv); -$command = array_shift($argv); - -switch($command) { - case '': - case 'help': - $GitToolCLI->cmd_help(); - break; - case 'clone': - $GitToolCLI->cmd_clone($argv); - break; - case 'install': - $GitToolCLI->cmd_install($argv); - break; - case 'repo': - case 'repos': - $GitToolCLI->cmd_repos(); - break; - default: - $GitToolCLI->cmd_git($command, $argv); -} - /** * Easily manage DokuWiki git repositories * * @author Andreas Gohr <andi@splitbrain.org> */ -class GitToolCLI { - private $color = true; - - public function cmd_help() { - echo <<<EOF -Usage: gittool.php <command> [parameters] - -Manage git repositories for DokuWiki and its plugins and templates. +class GitToolCLI extends DokuCLI { -EXAMPLE - -$> ./bin/gittool.php clone gallery template:ach -$> ./bin/gittool.php repos -$> ./bin/gittool.php origin -v - -COMMANDS + /** + * Register options and arguments on the given $options object + * + * @param DokuCLI_Options $options + * @return void + */ + protected function setup(DokuCLI_Options $options) { + $options->setHelp( + "Manage git repositories for DokuWiki and its plugins and templates.\n\n". + "$> ./bin/gittool.php clone gallery template:ach\n". + "$> ./bin/gittool.php repos\n". + "$> ./bin/gittool.php origin -v" + ); -help - This help screen + $options->registerArgument( + 'command', + 'Command to execute. See below', + true + ); -clone <extensions> - Tries to install a known plugin or template (prefix with template:) via - git. Uses the DokuWiki.org plugin repository to find the proper git - repository. Multiple extensions can be given as parameters + $options->registerCommand( + 'clone', + 'Tries to install a known plugin or template (prefix with template:) via git. Uses the DokuWiki.org '. + 'plugin repository to find the proper git repository. Multiple extensions can be given as parameters' + ); + $options->registerArgument( + 'extension', + 'name of the extension to install, prefix with \'template:\' for templates', + true, + 'clone' + ); -install <extensions> - The same as clone, but when no git source repository can be found, the - extension is installed via download + $options->registerCommand( + 'install', + 'The same as clone, but when no git source repository can be found, the extension is installed via '. + 'download' + ); + $options->registerArgument( + 'extension', + 'name of the extension to install, prefix with \'template:\' for templates', + true, + 'install' + ); -repos - Lists all git repositories found in this DokuWiki installation + $options->registerCommand( + 'repos', + 'Lists all git repositories found in this DokuWiki installation' + ); -<any> - Any unknown commands are assumed to be arguments to git and will be - executed in all repositories found within this DokuWiki installation + $options->registerCommand( + '*', + 'Any unknown commands are assumed to be arguments to git and will be executed in all repositories '. + 'found within this DokuWiki installation' + ); + } -EOF; + /** + * 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) { + $command = $options->getCmd(); + if(!$command) $command = array_shift($options->args); + + switch($command) { + case '': + echo $options->help(); + break; + case 'clone': + $this->cmd_clone($options->args); + break; + case 'install': + $this->cmd_install($options->args); + break; + case 'repo': + case 'repos': + $this->cmd_repos(); + break; + default: + $this->cmd_git($command, $options->args); + } } /** @@ -88,7 +111,7 @@ EOF; $repo = $this->getSourceRepo($ext); if(!$repo) { - $this->msg_error("could not find a repository for $ext"); + $this->error("could not find a repository for $ext"); $errors[] = $ext; } else { if($this->cloneExtension($ext, $repo)) { @@ -100,8 +123,8 @@ EOF; } echo "\n"; - if($succeeded) $this->msg_success('successfully cloned the following extensions: '.join(', ', $succeeded)); - if($errors) $this->msg_error('failed to clone the following extensions: '.join(', ', $errors)); + if($succeeded) $this->success('successfully cloned the following extensions: '.join(', ', $succeeded)); + if($errors) $this->error('failed to clone the following extensions: '.join(', ', $errors)); } /** @@ -117,7 +140,7 @@ EOF; $repo = $this->getSourceRepo($ext); if(!$repo) { - $this->msg_info("could not find a repository for $ext"); + $this->info("could not find a repository for $ext"); if($this->downloadExtension($ext)) { $succeeded[] = $ext; } else { @@ -133,8 +156,8 @@ EOF; } echo "\n"; - if($succeeded) $this->msg_success('successfully installed the following extensions: '.join(', ', $succeeded)); - if($errors) $this->msg_error('failed to install the following extensions: '.join(', ', $errors)); + if($succeeded) $this->success('successfully installed the following extensions: '.join(', ', $succeeded)); + if($errors) $this->error('failed to install the following extensions: '.join(', ', $errors)); } /** @@ -152,19 +175,19 @@ EOF; foreach($repos as $repo) { if(!@chdir($repo)) { - $this->msg_error("Could not change into $repo"); + $this->error("Could not change into $repo"); continue; } echo "\n"; - $this->msg_info("executing $shell in $repo"); + $this->info("executing $shell in $repo"); $ret = 0; system($shell, $ret); if($ret == 0) { - $this->msg_success("git succeeded in $repo"); + $this->success("git succeeded in $repo"); } else { - $this->msg_error("git failed in $repo"); + $this->error("git failed in $repo"); } } } @@ -193,23 +216,23 @@ EOF; $url = $plugin->getDownloadURL(); if(!$url) { - $this->msg_error("no download URL for $ext"); + $this->error("no download URL for $ext"); return false; } $ok = false; try { - $this->msg_info("installing $ext via download from $url"); + $this->info("installing $ext via download from $url"); $ok = $plugin->installFromURL($url); } catch(Exception $e) { - $this->msg_error($e->getMessage()); + $this->error($e->getMessage()); } if($ok) { - $this->msg_success("installed $ext via download"); + $this->success("installed $ext via download"); return true; } else { - $this->msg_success("failed to install $ext via download"); + $this->success("failed to install $ext via download"); return false; } } @@ -228,14 +251,14 @@ EOF; $target = DOKU_PLUGIN.$ext; } - $this->msg_info("cloning $ext from $repo to $target"); + $this->info("cloning $ext from $repo to $target"); $ret = 0; system("git clone $repo $target", $ret); if($ret === 0) { - $this->msg_success("cloning of $ext succeeded"); + $this->success("cloning of $ext succeeded"); return true; } else { - $this->msg_error("cloning of $ext failed"); + $this->error("cloning of $ext failed"); return false; } } @@ -248,7 +271,7 @@ EOF; * @return array */ private function findRepos() { - $this->msg_info('Looking for .git directories'); + $this->info('Looking for .git directories'); $data = array_merge( glob(DOKU_INC.'.git', GLOB_ONLYDIR), glob(DOKU_PLUGIN.'*/.git', GLOB_ONLYDIR), @@ -256,9 +279,9 @@ EOF; ); if(!$data) { - $this->msg_error('Found no .git directories'); + $this->error('Found no .git directories'); } else { - $this->msg_success('Found '.count($data).' .git directories'); + $this->success('Found '.count($data).' .git directories'); } $data = array_map('fullpath', array_map('dirname', $data)); return $data; @@ -304,37 +327,8 @@ EOF; return false; } +} - /** - * Print an error message - * - * @param $string - */ - private function msg_error($string) { - if($this->color) echo "\033[31m"; // red - echo "E: $string\n"; - if($this->color) echo "\033[37m"; // reset - } - - /** - * Print a success message - * - * @param $string - */ - private function msg_success($string) { - if($this->color) echo "\033[32m"; // green - echo "S: $string\n"; - if($this->color) echo "\033[37m"; // reset - } - - /** - * Print an info message - * - * @param $string - */ - private function msg_info($string) { - if($this->color) echo "\033[36m"; // cyan - echo "I: $string\n"; - if($this->color) echo "\033[37m"; // reset - } -}
\ No newline at end of file +// Main +$cli = new GitToolCLI(); +$cli->run();
\ No newline at end of file diff --git a/bin/indexer.php b/bin/indexer.php index 6f6b5d9fa..13895c36a 100755 --- a/bin/indexer.php +++ b/bin/indexer.php @@ -1,98 +1,103 @@ #!/usr/bin/php <?php -if ('cli' != php_sapi_name()) die(); - -ini_set('memory_limit','128M'); -if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/'); +if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__).'/../').'/'); +define('NOSESSION', 1); require_once(DOKU_INC.'inc/init.php'); -require_once(DOKU_INC.'inc/cliopts.php'); -session_write_close(); -// handle options -$short_opts = 'hcuq'; -$long_opts = array('help', 'clear', 'update', 'quiet'); -$OPTS = Doku_Cli_Opts::getOptions(__FILE__,$short_opts,$long_opts); -if ( $OPTS->isError() ) { - fwrite( STDERR, $OPTS->getMessage() . "\n"); - _usage(); - exit(1); -} -$CLEAR = false; -$QUIET = false; -$INDEXER = null; -foreach ($OPTS->options as $key => $val) { - switch ($key) { - case 'h': - case 'help': - _usage(); - exit; - case 'c': - case 'clear': - $CLEAR = true; - break; - case 'q': - case 'quiet': - $QUIET = true; - break; +/** + * Update the Search Index from command line + */ +class IndexerCLI extends DokuCLI { + + private $quiet = false; + private $clear = false; + + /** + * Register options and arguments on the given $options object + * + * @param DokuCLI_Options $options + * @return void + */ + protected function setup(DokuCLI_Options $options) { + $options->setHelp( + 'Updates the searchindex by indexing all new or changed pages. When the -c option is '. + 'given the index is cleared first.' + ); + + $options->registerOption( + 'clear', + 'clear the index before updating', + 'c' + ); + $options->registerOption( + 'quiet', + 'don\'t produce any output', + 'q' + ); } -} - -#------------------------------------------------------------------------------ -# Action - -if($CLEAR) _clearindex(); -_update(); - - - -#------------------------------------------------------------------------------ - -function _usage() { - print "Usage: indexer.php <options> - - Updates the searchindex by indexing all new or changed pages - when the -c option is given the index is cleared first. - OPTIONS - -h, --help show this help and exit - -c, --clear clear the index before updating - -q, --quiet don't produce any output -"; -} - -function _update(){ - global $conf; - $data = array(); - _quietecho("Searching pages... "); - search($data,$conf['datadir'],'search_allpages',array('skipacl' => true)); - _quietecho(count($data)." pages found.\n"); - - foreach($data as $val){ - _index($val['id']); + /** + * 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) { + $this->clear = $options->getOpt('clear'); + $this->quiet = $options->getOpt('quiet'); + + if($this->clear) $this->clearindex(); + + $this->update(); } -} -function _index($id){ - global $CLEAR; - global $QUIET; + /** + * Update the index + */ + function update() { + global $conf; + $data = array(); + $this->quietecho("Searching pages... "); + search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true)); + $this->quietecho(count($data)." pages found.\n"); + + foreach($data as $val) { + $this->index($val['id']); + } + } - _quietecho("$id... "); - idx_addPage($id, !$QUIET, $CLEAR); - _quietecho("done.\n"); -} + /** + * Index the given page + * + * @param string $id + */ + function index($id) { + $this->quietecho("$id... "); + idx_addPage($id, !$this->quiet, $this->clear); + $this->quietecho("done.\n"); + } -/** - * Clear all index files - */ -function _clearindex(){ - _quietecho("Clearing index... "); - idx_get_indexer()->clear(); - _quietecho("done.\n"); -} + /** + * Clear all index files + */ + function clearindex() { + $this->quietecho("Clearing index... "); + idx_get_indexer()->clear(); + $this->quietecho("done.\n"); + } -function _quietecho($msg) { - global $QUIET; - if(!$QUIET) echo $msg; + /** + * Print message if not supressed + * + * @param string $msg + */ + function quietecho($msg) { + if(!$this->quiet) echo $msg; + } } -//Setup VIM: ex: et ts=2 : +// Main +$cli = new IndexerCLI(); +$cli->run();
\ No newline at end of file diff --git a/bin/render.php b/bin/render.php index d30ef2958..672993223 100755 --- a/bin/render.php +++ b/bin/render.php @@ -1,5 +1,10 @@ #!/usr/bin/php <?php +if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__).'/../').'/'); +define('NOSESSION', 1); +require_once(DOKU_INC.'inc/init.php'); + + /** * A simple commandline tool to render some DokuWiki syntax with a given * renderer. @@ -9,59 +14,48 @@ * DokuWiki markup * * @license GPL2 - * @author Andreas Gohr <andi@splitbrain.org> + * @author Andreas Gohr <andi@splitbrain.org> */ -if ('cli' != php_sapi_name()) die(); +class RenderCLI extends DokuCLI { -ini_set('memory_limit','128M'); -if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/'); -define('NOSESSION',1); -require_once(DOKU_INC.'inc/init.php'); -require_once(DOKU_INC.'inc/common.php'); -require_once(DOKU_INC.'inc/parserutils.php'); -require_once(DOKU_INC.'inc/cliopts.php'); - -// handle options -$short_opts = 'hr:'; -$long_opts = array('help','renderer:'); -$OPTS = Doku_Cli_Opts::getOptions(__FILE__,$short_opts,$long_opts); -if ( $OPTS->isError() ) { - fwrite( STDERR, $OPTS->getMessage() . "\n"); - _usage(); - exit(1); -} -$RENDERER = 'xhtml'; -foreach ($OPTS->options as $key => $val) { - switch ($key) { - case 'h': - case 'help': - _usage(); - exit; - case 'r': - case 'renderer': - $RENDERER = $val; + /** + * Register options and arguments on the given $options object + * + * @param DokuCLI_Options $options + * @return void + */ + protected function setup(DokuCLI_Options $options) { + $options->setHelp( + 'A simple commandline tool to render some DokuWiki syntax with a given renderer.'. + "\n\n". + 'This may not work for plugins that expect a certain environment to be '. + 'set up before rendering, but should work for most or even all standard '. + 'DokuWiki markup' + ); + $options->registerOption('renderer', 'The renderer mode to use. Defaults to xhtml', 'r', 'mode'); } -} + /** + * Your main program + * + * Arguments and options have been parsed when this is run + * + * @param DokuCLI_Options $options + * @throws DokuCLI_Exception + * @return void + */ + protected function main(DokuCLI_Options $options) { + $renderer = $options->getOpt('renderer', 'xhtml'); -// do the action -$source = stream_get_contents(STDIN); -$info = array(); -$result = p_render($RENDERER,p_get_instructions($source),$info); -if(is_null($result)) die("No such renderer $RENDERER\n"); -echo $result; - -/** - * Print usage info - */ -function _usage(){ - print "Usage: render.php <options> - - Reads DokuWiki syntax from STDIN and renders it with the given renderer - to STDOUT - - OPTIONS - -h, --help show this help and exit - -r, --renderer <renderer> the render mode (default: xhtml) -"; + // do the action + $source = stream_get_contents(STDIN); + $info = array(); + $result = p_render($renderer, p_get_instructions($source), $info); + if(is_null($result)) throw new DokuCLI_Exception("No such renderer $renderer"); + echo $result; + } } + +// Main +$cli = new RenderCLI(); +$cli->run();
\ No newline at end of file diff --git a/bin/striplangs.php b/bin/striplangs.php index 2bfddcea4..6335bc84c 100755 --- a/bin/striplangs.php +++ b/bin/striplangs.php @@ -1,148 +1,110 @@ #!/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! +if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__).'/../').'/'); +define('NOSESSION', 1); +require_once(DOKU_INC.'inc/init.php'); - 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); +/** + * Remove unwanted languages from a DokuWiki install + */ +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); + /** + * 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); - while(($cur_dir = $dir->read()) !== false) { - if( $cur_dir != '.' and $cur_dir != '..' and is_dir($path.'/'.$cur_dir)) { + while(($cur_dir = $dir->read()) !== false) { + if($cur_dir != '.' and $cur_dir != '..' and is_dir($path.'/'.$cur_dir)) { - if ( !in_array($cur_dir, $keep_langs, true ) ) { - killDir($path.'/'.$cur_dir); - } - } - } - $dir->close(); -} - -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 diff --git a/bin/wantedpages.php b/bin/wantedpages.php index afcb6b271..8fc4ba74f 100755 --- a/bin/wantedpages.php +++ b/bin/wantedpages.php @@ -1,134 +1,133 @@ #!/usr/bin/php <?php -if ('cli' != php_sapi_name()) die(); - -#------------------------------------------------------------------------------ -ini_set('memory_limit','128M'); -if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/'); -require_once DOKU_INC.'inc/init.php'; -require_once DOKU_INC.'inc/common.php'; -require_once DOKU_INC.'inc/search.php'; -require_once DOKU_INC.'inc/cliopts.php'; +if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__).'/../').'/'); +define('NOSESSION', 1); +require_once(DOKU_INC.'inc/init.php'); + +/** + * Find wanted pages + */ +class WantedPagesCLI extends DokuCLI { + + const DIR_CONTINUE = 1; + const DIR_NS = 2; + const DIR_PAGE = 3; + + /** + * Register options and arguments on the given $options object + * + * @param DokuCLI_Options $options + * @return void + */ + protected function setup(DokuCLI_Options $options) { + $options->setHelp( + 'Outputs a list of wanted pages (pages which have internal links but do not yet exist).' + ); + $options->registerArgument( + 'namespace', + 'The namespace to lookup. Defaults to root namespace', + false + ); + } -#------------------------------------------------------------------------------ -function usage() { - print "Usage: wantedpages.php [wiki:namespace] + /** + * 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->args) { + $startdir = dirname(wikiFN($options->args[0].':xxx')); + } else { + $startdir = dirname(wikiFN('xxx')); + } - Outputs a list of wanted pages (pages which have - internal links but do not yet exist). + $this->info("searching $startdir"); - If the optional [wiki:namespace] is not provided, - defaults to the root wiki namespace + $wanted_pages = array(); - OPTIONS - -h, --help get help -"; -} - -#------------------------------------------------------------------------------ -define ('DW_DIR_CONTINUE',1); -define ('DW_DIR_NS',2); -define ('DW_DIR_PAGE',3); + foreach($this->get_pages($startdir) as $page) { + $wanted_pages = array_merge($wanted_pages, $this->internal_links($page)); + } + $wanted_pages = array_unique($wanted_pages); + sort($wanted_pages); -#------------------------------------------------------------------------------ -function dw_dir_filter($entry, $basepath) { - if ($entry == '.' || $entry == '..' ) { - return DW_DIR_CONTINUE; - } - if ( is_dir($basepath . '/' . $entry) ) { - if ( strpos($entry, '_') === 0 ) { - return DW_DIR_CONTINUE; + foreach($wanted_pages as $page) { + print $page."\n"; } - return DW_DIR_NS; - } - if ( preg_match('/\.txt$/',$entry) ) { - return DW_DIR_PAGE; } - return DW_DIR_CONTINUE; -} -#------------------------------------------------------------------------------ -function dw_get_pages($dir) { - static $trunclen = null; - if ( !$trunclen ) { - global $conf; - $trunclen = strlen($conf['datadir'].':'); + protected function dir_filter($entry, $basepath) { + if($entry == '.' || $entry == '..') { + return WantedPagesCLI::DIR_CONTINUE; + } + if(is_dir($basepath.'/'.$entry)) { + if(strpos($entry, '_') === 0) { + return WantedPagesCLI::DIR_CONTINUE; + } + return WantedPagesCLI::DIR_NS; + } + if(preg_match('/\.txt$/', $entry)) { + return WantedPagesCLI::DIR_PAGE; + } + return WantedPagesCLI::DIR_CONTINUE; } - if ( !is_dir($dir) ) { - fwrite( STDERR, "Unable to read directory $dir\n"); - exit(1); - } + protected function get_pages($dir) { + static $trunclen = null; + if(!$trunclen) { + global $conf; + $trunclen = strlen($conf['datadir'].':'); + } - $pages = array(); - $dh = opendir($dir); - while ( false !== ( $entry = readdir($dh) ) ) { - $status = dw_dir_filter($entry, $dir); - if ( $status == DW_DIR_CONTINUE ) { - continue; - } else if ( $status == DW_DIR_NS ) { - $pages = array_merge($pages, dw_get_pages($dir . '/' . $entry)); - } else { - $page = array( - 'id' => pathID(substr($dir.'/'.$entry,$trunclen)), - 'file'=> $dir.'/'.$entry, + if(!is_dir($dir)) { + throw new DokuCLI_Exception("Unable to read directory $dir"); + } + + $pages = array(); + $dh = opendir($dir); + while(false !== ($entry = readdir($dh))) { + $status = $this->dir_filter($entry, $dir); + if($status == WantedPagesCLI::DIR_CONTINUE) { + continue; + } else if($status == WantedPagesCLI::DIR_NS) { + $pages = array_merge($pages, $this->get_pages($dir.'/'.$entry)); + } else { + $page = array( + 'id' => pathID(substr($dir.'/'.$entry, $trunclen)), + 'file' => $dir.'/'.$entry, ); - $pages[] = $page; + $pages[] = $page; + } } + closedir($dh); + return $pages; } - closedir($dh); - return $pages; -} -#------------------------------------------------------------------------------ -function dw_internal_links($page) { - global $conf; - $instructions = p_get_instructions(file_get_contents($page['file'])); - $links = array(); - $cns = getNS($page['id']); - $exists = false; - foreach($instructions as $ins){ - if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink') ){ - $mid = $ins[1][0]; - resolve_pageid($cns,$mid,$exists); - if ( !$exists ) { - list($mid) = explode('#',$mid); //record pages without hashs - $links[] = $mid; + function internal_links($page) { + global $conf; + $instructions = p_get_instructions(file_get_contents($page['file'])); + $links = array(); + $cns = getNS($page['id']); + $exists = false; + foreach($instructions as $ins) { + if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink')) { + $mid = $ins[1][0]; + resolve_pageid($cns, $mid, $exists); + if(!$exists) { + list($mid) = explode('#', $mid); //record pages without hashs + $links[] = $mid; + } } } + return $links; } - return $links; } -#------------------------------------------------------------------------------ -$OPTS = Doku_Cli_Opts::getOptions(__FILE__,'h',array('help')); - -if ( $OPTS->isError() ) { - fwrite( STDERR, $OPTS->getMessage() . "\n"); - exit(1); -} - -if ( $OPTS->has('h') or $OPTS->has('help') ) { - usage(); - exit(0); -} - -$START_DIR = $conf['datadir']; - -if ( $OPTS->numArgs() == 1 ) { - $START_DIR .= '/' . $OPTS->arg(0); -} - -#------------------------------------------------------------------------------ -$WANTED_PAGES = array(); - -foreach ( dw_get_pages($START_DIR) as $WIKI_PAGE ) { - $WANTED_PAGES = array_merge($WANTED_PAGES,dw_internal_links($WIKI_PAGE)); -} -$WANTED_PAGES = array_unique($WANTED_PAGES); -sort($WANTED_PAGES); - -foreach ( $WANTED_PAGES as $WANTED_PAGE ) { - print $WANTED_PAGE."\n"; -} -exit(0); +// Main +$cli = new WantedPagesCLI(); +$cli->run();
\ No newline at end of file |