3 #------------------------------------------------------------------------------
4 if ('cli' != php_sapi_name()) die();
6 ini_set('memory_limit','128M');
7 if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__
).'/../').'/');
8 require_once DOKU_INC
.'inc/init.php';
9 require_once DOKU_INC
.'inc/common.php';
10 require_once DOKU_INC
.'inc/cliopts.php';
12 #------------------------------------------------------------------------------
13 function usage($action) {
16 print "Usage: dwpage.php [opts] checkout <wiki:page> [working_file]
18 Checks out a file from the repository, using the wiki id and obtaining
20 If a working_file is specified, this is where the page is copied to.
21 Otherwise defaults to the same as the wiki page in the current
25 $ ./dwpage.php checkout wiki:syntax ./new_syntax.txt
28 -h, --help=<action>: get help
29 -f: force obtaining a lock for the page (generally bad idea)
33 print "Usage: dwpage.php [opts] -m \"Msg\" commit <working_file> <wiki:page>
35 Checks in the working_file into the repository using the specified
36 wiki id, archiving the previous version.
39 $ ./dwpage.php -m \"Some message\" commit ./new_syntax.txt wiki:syntax
42 -h, --help=<action>: get help
43 -f: force obtaining a lock for the page (generally bad idea)
44 -t, trivial: minor change
45 -m (required): Summary message describing the change
49 print "Usage: dwpage.php [opts] lock <wiki:page>
51 Obtains or updates a lock for a wiki page
54 $ ./dwpage.php lock wiki:syntax
57 -h, --help=<action>: get help
58 -f: force obtaining a lock for the page (generally bad idea)
62 print "Usage: dwpage.php [opts] unlock <wiki:page>
64 Removes a lock for a wiki page.
67 $ ./dwpage.php unlock wiki:syntax
70 -h, --help=<action>: get help
71 -f: force obtaining a lock for the page (generally bad idea)
75 print "Usage: dwpage.php [opts] <action>
77 Utility to help command line Dokuwiki page editing, allow
78 pages to be checked out for editing then committed after changes
80 Normal operation would be;
85 checkout: see $ dwpage.php --help=checkout
86 commit: see $ dwpage.php --help=commit
87 lock: see $ dwpage.php --help=lock
90 -h, --help=<action>: get help
91 e.g. $ ./dwpage.php -hcommit
92 e.g. $ ./dwpage.php --help=commit
98 #------------------------------------------------------------------------------
100 $user = getenv('USER');
101 if (empty ($username)) {
102 $user = getenv('USERNAME');
106 if (empty ($username)) {
112 #------------------------------------------------------------------------------
113 function getSuppliedArgument($OPTS, $short, $long) {
114 $arg = $OPTS->get($short);
115 if ( is_null($arg) ) {
116 $arg = $OPTS->get($long);
121 #------------------------------------------------------------------------------
122 function obtainLock($WIKI_ID) {
126 if ( !file_exists(wikiFN($WIKI_ID)) ) {
127 fwrite( STDERR
, "$WIKI_ID does not yet exist\n");
130 $_SERVER['REMOTE_USER'] = $USERNAME;
131 if ( checklock($WIKI_ID) ) {
132 fwrite( STDERR
, "Page $WIKI_ID is already locked by another user\n");
138 $_SERVER['REMOTE_USER'] = '_'.$USERNAME.'_';
140 if ( checklock($WIKI_ID) != $USERNAME ) {
142 fwrite( STDERR
, "Unable to obtain lock for $WIKI_ID\n" );
148 #------------------------------------------------------------------------------
149 function clearLock($WIKI_ID) {
153 if ( !file_exists(wikiFN($WIKI_ID)) ) {
154 fwrite( STDERR
, "$WIKI_ID does not yet exist\n");
157 $_SERVER['REMOTE_USER'] = $USERNAME;
158 if ( checklock($WIKI_ID) ) {
159 fwrite( STDERR
, "Page $WIKI_ID is locked by another user\n");
165 if ( file_exists(wikiLockFN($WIKI_ID)) ) {
166 fwrite( STDERR
, "Unable to clear lock for $WIKI_ID\n" );
172 #------------------------------------------------------------------------------
173 function deleteLock($WIKI_ID) {
175 $wikiLockFN = wikiLockFN($WIKI_ID);
177 if ( file_exists($wikiLockFN) ) {
178 if ( !unlink($wikiLockFN) ) {
179 fwrite( STDERR
, "Unable to delete $wikiLockFN\n" );
186 #------------------------------------------------------------------------------
187 $USERNAME = getUser();
189 $SYSTEM_ID = '127.0.0.1';
191 #------------------------------------------------------------------------------
192 $OPTS = Doku_Cli_Opts
::getOptions(
203 if ( $OPTS->isError() ) {
204 print $OPTS->getMessage()."\n";
208 if ( $OPTS->has('h') or $OPTS->has('help') or !$OPTS->hasArgs() ) {
209 usage(getSuppliedArgument($OPTS,'h','help'));
213 if ( $OPTS->has('u') or $OPTS->has('user') ) {
214 $USERNAME = getSuppliedArgument($OPTS,'u','user');
217 if ( $OPTS->has('s') or $OPTS->has('system') ) {
218 $SYSTEM_ID = getSuppliedArgument($OPTS,'s','system');
221 #------------------------------------------------------------------------------
222 switch ( $OPTS->arg(0) ) {
224 #----------------------------------------------------------------------
227 $WIKI_ID = $OPTS->arg(1);
230 fwrite( STDERR
, "Wiki page ID required\n");
234 $WIKI_FN = wikiFN($WIKI_ID);
236 if ( !file_exists($WIKI_FN) ) {
237 fwrite( STDERR
, "$WIKI_ID does not yet exist\n");
241 $TARGET_FN = $OPTS->arg(2);
243 if ( empty($TARGET_FN) ) {
244 $TARGET_FN = getcwd().'/'.basename($WIKI_FN);
247 if ( !file_exists(dirname($TARGET_FN)) ) {
248 fwrite( STDERR
, "Directory ".dirname($TARGET_FN)." does not exist\n");
252 if ( stristr( realpath(dirname($TARGET_FN)), realpath($conf['datadir']) ) !== false ) {
253 fwrite( STDERR
, "Attempt to check out file into data directory - not allowed\n");
257 if ( $OPTS->has('f') ) {
258 deleteLock($WIKI_ID);
261 obtainLock($WIKI_ID);
263 # Need to lock the file first?
264 if ( !copy($WIKI_FN, $TARGET_FN) ) {
265 fwrite( STDERR
, "Unable to copy $WIKI_FN to $TARGET_FN\n");
270 print "$WIKI_ID > $TARGET_FN\n";
275 #----------------------------------------------------------------------
278 $TARGET_FN = $OPTS->arg(1);
281 fwrite( STDERR
, "Target filename required\n");
285 if ( !file_exists($TARGET_FN) ) {
286 fwrite( STDERR
, "$TARGET_FN does not exist\n");
290 if ( !is_readable($TARGET_FN) ) {
291 fwrite( STDERR
, "Cannot read from $TARGET_FN\n");
295 $WIKI_ID = $OPTS->arg(2);
298 fwrite( STDERR
, "Wiki page ID required\n");
302 if ( !$OPTS->has('m') ) {
303 fwrite( STDERR
, "Summary message required\n");
307 if ( $OPTS->has('f') ) {
308 deleteLock($WIKI_ID);
311 $_SERVER['REMOTE_USER'] = $USERNAME;
312 if ( checklock($WIKI_ID) ) {
313 fwrite( STDERR
, "$WIKI_ID is locked by another user\n");
317 obtainLock($WIKI_ID);
319 saveWikiText($WIKI_ID, file_get_contents($TARGET_FN), $OPTS->get('m'), $OPTS->has('t'));
327 #----------------------------------------------------------------------
330 $WIKI_ID = $OPTS->arg(1);
333 fwrite( STDERR
, "Wiki page ID required\n");
337 if ( $OPTS->has('f') ) {
338 deleteLock($WIKI_ID);
341 obtainLock($WIKI_ID);
343 print "Locked : $WIKI_ID\n";
348 #----------------------------------------------------------------------
351 $WIKI_ID = $OPTS->arg(1);
354 fwrite( STDERR
, "Wiki page ID required\n");
358 if ( $OPTS->has('f') ) {
359 deleteLock($WIKI_ID);
364 print "Unlocked : $WIKI_ID\n";
369 #----------------------------------------------------------------------
372 fwrite( STDERR
, "Invalid action ".$OPTS->arg(0)."\n" );