summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbin/dwpage.php373
-rwxr-xr-xbin/wantedpages.php129
-rw-r--r--inc/cliopts.php362
3 files changed, 864 insertions, 0 deletions
diff --git a/bin/dwpage.php b/bin/dwpage.php
new file mode 100755
index 000000000..35c094b74
--- /dev/null
+++ b/bin/dwpage.php
@@ -0,0 +1,373 @@
+#!/usr/bin/php -d short_open_tag=on
+<?php
+#------------------------------------------------------------------------------
+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)
+ -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;
+ }
+}
+
+#------------------------------------------------------------------------------
+function getUser() {
+ $user = getenv('USER');
+ if (empty ($username)) {
+ $user = getenv('USERNAME');
+ } else {
+ return $user;
+ }
+ if (empty ($username)) {
+ $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(wikiFN($WIKI_ID).'.lock') ) {
+ fwrite( STDERR, "Unable to clear lock for $WIKI_ID\n" );
+ exit(1);
+ }
+
+}
+
+#------------------------------------------------------------------------------
+function deleteLock($WIKI_ID) {
+
+ $wikiLockFN = wikiFN($WIKI_ID).'.lock';
+
+ if ( file_exists($wikiLockFN) ) {
+ if ( !unlink($wikiLockFN) ) {
+ fwrite( STDERR, "Unable to delete $wikiLockFN\n" );
+ exit(1);
+ }
+ }
+
+}
+
+#------------------------------------------------------------------------------
+$USERNAME = getUser();
+$CWD = getcwd();
+$SYSTEM_ID = '127.0.0.1';
+
+#------------------------------------------------------------------------------
+$OPTS = Doku_Cli_Opts::getOptions(
+ __FILE__,
+ 'h::fm:u:s:',
+ array(
+ 'help==',
+ 'user=',
+ 'system=',
+ )
+);
+
+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');
+}
+
+#------------------------------------------------------------------------------
+switch ( $OPTS->arg(0) ) {
+
+ #----------------------------------------------------------------------
+ case 'checkout':
+
+ $WIKI_ID = $OPTS->arg(1);
+
+ if ( !$WIKI_ID ) {
+ fwrite( STDERR, "Wiki page ID required\n");
+ exit(1);
+ }
+
+ $WIKI_FN = wikiFN($WIKI_ID);
+
+ if ( !file_exists($WIKI_FN) ) {
+ fwrite( STDERR, "$WIKI_ID does not yet exist\n");
+ exit(1);
+ }
+
+ $TARGET_FN = $OPTS->arg(2);
+
+ if ( empty($TARGET_FN) ) {
+ $TARGET_FN = getcwd().'/'.basename($WIKI_FN);
+ }
+
+ if ( !file_exists(dirname($TARGET_FN)) ) {
+ fwrite( STDERR, "Directory ".dirname($TARGET_FN)." does not exist\n");
+ exit(1);
+ }
+
+ 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);
+ }
+
+ if ( $OPTS->has('f') ) {
+ deleteLock($WIKI_ID);
+ }
+
+ obtainLock($WIKI_ID);
+
+ # 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);
+ }
+
+ print "$WIKI_ID > $TARGET_FN\n";
+ exit(0);
+
+ break;
+
+ #----------------------------------------------------------------------
+ case 'commit':
+
+ $TARGET_FN = $OPTS->arg(1);
+
+ if ( !$TARGET_FN ) {
+ fwrite( STDERR, "Target filename required\n");
+ exit(1);
+ }
+
+ if ( !file_exists($TARGET_FN) ) {
+ fwrite( STDERR, "$TARGET_FN does not exist\n");
+ exit(1);
+ }
+
+ if ( !is_readable($TARGET_FN) ) {
+ fwrite( STDERR, "Cannot read from $TARGET_FN\n");
+ exit(1);
+ }
+
+ $WIKI_ID = $OPTS->arg(2);
+
+ if ( !$WIKI_ID ) {
+ fwrite( STDERR, "Wiki page ID required\n");
+ exit(1);
+ }
+
+ if ( !$OPTS->has('m') ) {
+ fwrite( STDERR, "Summary message required\n");
+ exit(1);
+ }
+
+ if ( $OPTS->has('f') ) {
+ deleteLock($WIKI_ID);
+ }
+
+ $_SERVER['REMOTE_USER'] = $USERNAME;
+ if ( checklock($WIKI_ID) ) {
+ fwrite( STDERR, "$WIKI_ID is locked by another user\n");
+ exit(1);
+ }
+
+ obtainLock($WIKI_ID);
+
+ saveWikiText($WIKI_ID, file_get_contents($TARGET_FN), $OPTS->get('m'));
+
+ 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);
+ }
+
+ if ( $OPTS->has('f') ) {
+ deleteLock($WIKI_ID);
+ } else {
+ clearLock($WIKI_ID);
+ }
+
+ print "Unlocked : $WIKI_ID\n";
+ exit(0);
+
+ break;
+
+ #----------------------------------------------------------------------
+ default:
+
+ fwrite( STDERR, "Invalid action ".$OPTS->arg(0)."\n" );
+ exit(1);
+
+ break;
+
+}
+
diff --git a/bin/wantedpages.php b/bin/wantedpages.php
new file mode 100755
index 000000000..caa88dc78
--- /dev/null
+++ b/bin/wantedpages.php
@@ -0,0 +1,129 @@
+#!/usr/bin/php -d short_open_tag=on
+<?php
+#------------------------------------------------------------------------------
+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';
+
+#------------------------------------------------------------------------------
+function usage() {
+ print "Usage: wantedpages.php [wiki:namespace]
+
+ Outputs a list of wanted pages (pages which have
+ internal links but do not yet exist).
+
+ If the optional [wiki:namespace] is not provided,
+ defaults to the root wiki namespace
+
+ OPTIONS
+ -h, --help=<action>: get help
+";
+}
+
+#------------------------------------------------------------------------------
+define ('DW_DIR_CONTINUE',1);
+define ('DW_DIR_NS',2);
+define ('DW_DIR_PAGE',3);
+
+#------------------------------------------------------------------------------
+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;
+ }
+ 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'].':');
+ }
+
+ if ( !is_dir($dir) ) {
+ fwrite( STDERR, "Unable to read directory $dir\n");
+ exit(1);
+ }
+
+ $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'=>substr(pathID($dir . '/' . $entry),$trunclen),
+ 'file'=>$dir . '/' . $entry,
+ );
+ $pages[] = $page;
+ }
+ }
+ 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 ) {
+ $links[] = $mid;
+ }
+ }
+ }
+ 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);
diff --git a/inc/cliopts.php b/inc/cliopts.php
new file mode 100644
index 000000000..592b529a0
--- /dev/null
+++ b/inc/cliopts.php
@@ -0,0 +1,362 @@
+<?php
+/**
+* Brutally chopped and modified from http://pear.php.net/package/Console_Getopts
+*/
+// +----------------------------------------------------------------------+
+// | PHP Version 4 |
+// +----------------------------------------------------------------------+
+// | Copyright (c) 1997-2003 The PHP Group |
+// +----------------------------------------------------------------------+
+// | This source file is subject to version 3.0 of the PHP license, |
+// | that is bundled with this package in the file LICENSE, and is |
+// | available through the world-wide-web at the following url: |
+// | http://www.php.net/license/3_0.txt. |
+// | If you did not receive a copy of the PHP license and are unable to |
+// | obtain it through the world-wide-web, please send a note to |
+// | license@php.net so we can mail you a copy immediately. |
+// +----------------------------------------------------------------------+
+// | Author: Andrei Zmievski <andrei@php.net> |
+// | Modified: Harry Fuecks hfuecks gmail.com |
+// +----------------------------------------------------------------------+
+//
+
+if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
+
+//------------------------------------------------------------------------------
+/**
+* Sets up CLI environment based on SAPI and PHP version
+* Helps resolve some issues between the CGI and CLI SAPIs
+* as well is inconsistencies between PHP 4.3+ and older versions
+*/
+if (version_compare(phpversion(), '4.3.0', '<') || php_sapi_name() == 'cgi') {
+ // Handle output buffering
+ @ob_end_flush();
+ ob_implicit_flush(TRUE);
+
+ // PHP ini settings
+ set_time_limit(0);
+ ini_set('track_errors', TRUE);
+ ini_set('html_errors', FALSE);
+ ini_set('magic_quotes_runtime', FALSE);
+
+ // Define stream constants
+ define('STDIN', fopen('php://stdin', 'r'));
+ define('STDOUT', fopen('php://stdout', 'w'));
+ define('STDERR', fopen('php://stderr', 'w'));
+
+ // Close the streams on script termination
+ register_shutdown_function(
+ create_function('',
+ 'fclose(STDIN); fclose(STDOUT); fclose(STDERR); return true;')
+ );
+}
+
+//------------------------------------------------------------------------------
+/**
+* Error codes
+*/
+define('DOKU_CLI_OPTS_UNKNOWN_OPT',1); //Unrecognized option
+define('DOKU_CLI_OPTS_OPT_ARG_REQUIRED',2); //Option requires argument
+define('DOKU_CLI_OPTS_OPT_ARG_DENIED',3); //Option not allowed argument
+define('DOKU_CLI_OPTS_OPT_ABIGUOUS',4);//Option abiguous
+define('DOKU_CLI_OPTS_ARG_READ',5);//Could not read argv
+
+//------------------------------------------------------------------------------
+/**
+ * Command-line options parsing class.
+ *
+ * @author Andrei Zmievski <andrei@php.net>
+ *
+ */
+ class Doku_Cli_Opts {
+
+ /**
+ * <?php ?>
+ * @see http://www.sitepoint.com/article/php-command-line-1/3
+ * @param string executing file name - this MUST be passed the __FILE__ constant
+ * @param string short options
+ * @param array (optional) long options
+ * @return Doku_Cli_Opts_Container or Doku_Cli_Opts_Error
+ */
+ function & getOptions($bin_file, $short_options, $long_options = null) {
+ $args = Doku_Cli_Opts::readPHPArgv();
+
+ if ( Doku_Cli_Opts::isError($args) ) {
+ return $args;
+ }
+
+ // Compatibility between "php extensions.php" and "./extensions.php"
+ if ( realpath($_SERVER['argv'][0]) == $bin_file ) {
+ $options = Doku_Cli_Opts::getOpt($args,$short_options,$long_options);
+ } else {
+ $options = Doku_Cli_Opts::getOpt2($args,$short_options,$long_options);
+ }
+
+ if ( Doku_Cli_Opts::isError($options) ) {
+ return $options;
+ }
+
+ $container = new Doku_Cli_Opts_Container($options);
+ return $container;
+ }
+
+ function getopt2($args, $short_options, $long_options = null) {
+ return Doku_Cli_Opts::doGetopt(
+ 2, $args, $short_options, $long_options
+ );
+ }
+
+ function getopt($args, $short_options, $long_options = null) {
+ return Doku_Cli_Opts::doGetopt(
+ 1, $args, $short_options, $long_options
+ );
+ }
+
+ function doGetopt($version, $args, $short_options, $long_options = null) {
+
+ // in case you pass directly readPHPArgv() as the first arg
+ if (Doku_Cli_Opts::isError($args)) {
+ return $args;
+ }
+ if (empty($args)) {
+ return array(array(), array());
+ }
+ $opts = array();
+ $non_opts = array();
+
+ settype($args, 'array');
+
+ if ($long_options && is_array($long_options)) {
+ sort($long_options);
+ }
+
+ /*
+ * Preserve backwards compatibility with callers that relied on
+ * erroneous POSIX fix.
+ */
+ if ($version < 2) {
+ if (isset($args[0]{0}) && $args[0]{0} != '-') {
+ array_shift($args);
+ }
+ }
+
+ reset($args);
+ while (list($i, $arg) = each($args)) {
+
+ /* The special element '--' means explicit end of
+ options. Treat the rest of the arguments as non-options
+ and end the loop. */
+ if ($arg == '--') {
+ $non_opts = array_merge($non_opts, array_slice($args, $i + 1));
+ break;
+ }
+
+ if ($arg{0} != '-' || (strlen($arg) > 1 && $arg{1} == '-' && !$long_options)) {
+ $non_opts = array_merge($non_opts, array_slice($args, $i));
+ break;
+ } elseif (strlen($arg) > 1 && $arg{1} == '-') {
+ $error = Doku_Cli_Opts::_parseLongOption(substr($arg, 2), $long_options, $opts, $args);
+ if (Doku_Cli_Opts::isError($error))
+ return $error;
+ } else {
+ $error = Doku_Cli_Opts::_parseShortOption(substr($arg, 1), $short_options, $opts, $args);
+ if (Doku_Cli_Opts::isError($error))
+ return $error;
+ }
+ }
+
+ return array($opts, $non_opts);
+ }
+
+ function _parseShortOption($arg, $short_options, &$opts, &$args) {
+ for ($i = 0; $i < strlen($arg); $i++) {
+ $opt = $arg{$i};
+ $opt_arg = null;
+
+ /* Try to find the short option in the specifier string. */
+ if (($spec = strstr($short_options, $opt)) === false || $arg{$i} == ':')
+ {
+ return Doku_Cli_Opts::raiseError(
+ DOKU_CLI_OPTS_UNKNOWN_OPT,
+ "Unrecognized option -- $opt"
+ );
+ }
+
+ if (strlen($spec) > 1 && $spec{1} == ':') {
+ if (strlen($spec) > 2 && $spec{2} == ':') {
+ if ($i + 1 < strlen($arg)) {
+ /* Option takes an optional argument. Use the remainder of
+ the arg string if there is anything left. */
+ $opts[] = array($opt, substr($arg, $i + 1));
+ break;
+ }
+ } else {
+ /* Option requires an argument. Use the remainder of the arg
+ string if there is anything left. */
+ if ($i + 1 < strlen($arg)) {
+ $opts[] = array($opt, substr($arg, $i + 1));
+ break;
+ } else if (list(, $opt_arg) = each($args))
+ /* Else use the next argument. */;
+ else
+ return Doku_Cli_Opts::raiseError(
+ DOKU_CLI_OPTS_OPT_ARG_REQUIRED,
+ "Option requires an argument -- $opt"
+ );
+ }
+ }
+
+ $opts[] = array($opt, $opt_arg);
+ }
+ }
+
+ function _parseLongOption($arg, $long_options, &$opts, &$args) {
+ @list($opt, $opt_arg) = explode('=', $arg);
+ $opt_len = strlen($opt);
+
+ for ($i = 0; $i < count($long_options); $i++) {
+ $long_opt = $long_options[$i];
+ $opt_start = substr($long_opt, 0, $opt_len);
+
+ /* Option doesn't match. Go on to the next one. */
+ if ($opt_start != $opt)
+ continue;
+
+ $opt_rest = substr($long_opt, $opt_len);
+
+ /* Check that the options uniquely matches one of the allowed
+ options. */
+ if ($opt_rest != '' && $opt{0} != '=' &&
+ $i + 1 < count($long_options) &&
+ $opt == substr($long_options[$i+1], 0, $opt_len)) {
+ return Doku_Cli_Opts::raiseError(
+ DOKU_CLI_OPTS_OPT_ABIGUOUS,
+ "Option --$opt is ambiguous"
+ );
+ }
+
+ if (substr($long_opt, -1) == '=') {
+ if (substr($long_opt, -2) != '==') {
+ /* Long option requires an argument.
+ Take the next argument if one wasn't specified. */;
+ if (!strlen($opt_arg) && !(list(, $opt_arg) = each($args))) {
+ return Doku_Cli_Opts::raiseError(
+ DOKU_CLI_OPTS_OPT_ARG_REQUIRED,
+ "Option --$opt requires an argument"
+ );
+ }
+ }
+ } else if ($opt_arg) {
+ return Doku_Cli_Opts::raiseError(
+ DOKU_CLI_OPTS_OPT_ARG_DENIED,
+ "Option --$opt doesn't allow an argument"
+ );
+ }
+
+ $opts[] = array('--' . $opt, $opt_arg);
+ return;
+ }
+
+ return Doku_Cli_Opts::raiseError(
+ DOKU_CLI_OPTS_UNKNOWN_OPT,
+ "Unrecognized option --$opt"
+ );
+ }
+
+ function readPHPArgv() {
+ global $argv;
+ if (!is_array($argv)) {
+ if (!@is_array($_SERVER['argv'])) {
+ if (!@is_array($GLOBALS['HTTP_SERVER_VARS']['argv'])) {
+ return Doku_Cli_Opts::raiseError(
+ DOKU_CLI_OPTS_ARG_READ,
+ "Could not read cmd args (register_argc_argv=Off?)"
+ );
+ }
+ return $GLOBALS['HTTP_SERVER_VARS']['argv'];
+ }
+ return $_SERVER['argv'];
+ }
+ return $argv;
+ }
+
+ function raiseError($code, $msg) {
+ return new Doku_Cli_Opts_Error($code, $msg);
+ }
+
+ function isError($obj) {
+ return is_a($obj, 'Doku_Cli_Opts_Error');
+ }
+
+}
+
+//------------------------------------------------------------------------------
+class Doku_Cli_Opts_Error {
+
+ var $code;
+ var $msg;
+
+ function Doku_Cli_Opts_Error($code, $msg) {
+ $this->code = $code;
+ $this->msg = $msg;
+ }
+
+ function getMessage() {
+ return $this->msg;
+ }
+
+ function isError() {
+ return TRUE;
+ }
+
+}
+
+//------------------------------------------------------------------------------
+class Doku_Cli_Opts_Container {
+
+ var $options = array();
+ var $args = array();
+
+ function Doku_Cli_Opts_Container($options) {
+ foreach ( $options[0] as $option ) {
+ if ( FALSE !== ( strpos($option[0], '--') ) ) {
+ $opt_name = substr($option[0], 2);
+ } else {
+ $opt_name = $option[0];
+ }
+ $this->options[$opt_name] = $option[1];
+ }
+
+
+ $this->args = $options[1];
+ }
+
+ function has($option) {
+ return array_key_exists($option, $this->options);
+ }
+
+ function get($option) {
+ if ( isset($this->options[$option]) ) {
+ return ( $this->options[$option] ) ;
+ }
+ }
+
+ function arg($index) {
+ if ( isset($this->args[$index]) ) {
+ return $this->args[$index];
+ }
+ }
+
+ function numArgs() {
+ return count($this->args);
+ }
+
+ function hasArgs() {
+ return count($this->args) !== 0;
+ }
+
+ function isError() {
+ return FALSE;
+ }
+
+}