1
#!/usr/bin/php -d short_open_tag=on
3
#------------------------------------------------------------------------------
4
if ('cli' != php_sapi_name()) die();
6
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
7
require_once DOKU_INC.'inc/init.php';
8
require_once DOKU_INC.'inc/common.php';
9
require_once DOKU_INC.'inc/cliopts.php';
11
#------------------------------------------------------------------------------
12
function usage($action) {
15
print "Usage: dwpage.php [opts] checkout <wiki:page> [working_file]
17
Checks out a file from the repository, using the wiki id and obtaining
19
If a working_file is specified, this is where the page is copied to.
20
Otherwise defaults to the same as the wiki page in the current
24
$ ./dwpage.php checkout wiki:syntax ./new_syntax.txt
27
-h, --help=<action>: get help
28
-f: force obtaining a lock for the page (generally bad idea)
32
print "Usage: dwpage.php [opts] -m \"Msg\" commit <working_file> <wiki:page>
34
Checks in the working_file into the repository using the specified
35
wiki id, archiving the previous version.
38
$ ./dwpage.php -m \"Some message\" commit ./new_syntax.txt wiki:syntax
41
-h, --help=<action>: get help
42
-f: force obtaining a lock for the page (generally bad idea)
43
-m (required): Summary message describing the change
47
print "Usage: dwpage.php [opts] lock <wiki:page>
49
Obtains or updates a lock for a wiki page
52
$ ./dwpage.php lock wiki:syntax
55
-h, --help=<action>: get help
56
-f: force obtaining a lock for the page (generally bad idea)
60
print "Usage: dwpage.php [opts] unlock <wiki:page>
62
Removes a lock for a wiki page.
65
$ ./dwpage.php unlock wiki:syntax
68
-h, --help=<action>: get help
69
-f: force obtaining a lock for the page (generally bad idea)
73
print "Usage: dwpage.php [opts] <action>
75
Utility to help command line Dokuwiki page editing, allow
76
pages to be checked out for editing then committed after changes
78
Normal operation would be;
83
checkout: see $ dwpage.php --help=checkout
84
commit: see $ dwpage.php --help=commit
85
lock: see $ dwpage.php --help=lock
88
-h, --help=<action>: get help
89
e.g. $ ./dwpage.php -hcommit
90
e.g. $ ./dwpage.php --help=commit
96
#------------------------------------------------------------------------------
98
$user = getenv('USER');
99
if (empty ($username)) {
100
$user = getenv('USERNAME');
104
if (empty ($username)) {
110
#------------------------------------------------------------------------------
111
function getSuppliedArgument($OPTS, $short, $long) {
112
$arg = $OPTS->get($short);
113
if ( is_null($arg) ) {
114
$arg = $OPTS->get($long);
119
#------------------------------------------------------------------------------
120
function obtainLock($WIKI_ID) {
124
if ( !file_exists(wikiFN($WIKI_ID)) ) {
125
fwrite( STDERR, "$WIKI_ID does not yet exist\n");
128
$_SERVER['REMOTE_USER'] = $USERNAME;
129
if ( checklock($WIKI_ID) ) {
130
fwrite( STDERR, "Page $WIKI_ID is already locked by another user\n");
136
$_SERVER['REMOTE_USER'] = '_'.$USERNAME.'_';
138
if ( checklock($WIKI_ID) != $USERNAME ) {
140
fwrite( STDERR, "Unable to obtain lock for $WIKI_ID\n" );
146
#------------------------------------------------------------------------------
147
function clearLock($WIKI_ID) {
151
if ( !file_exists(wikiFN($WIKI_ID)) ) {
152
fwrite( STDERR, "$WIKI_ID does not yet exist\n");
155
$_SERVER['REMOTE_USER'] = $USERNAME;
156
if ( checklock($WIKI_ID) ) {
157
fwrite( STDERR, "Page $WIKI_ID is locked by another user\n");
163
if ( file_exists(wikiLockFN($WIKI_ID)) ) {
164
fwrite( STDERR, "Unable to clear lock for $WIKI_ID\n" );
170
#------------------------------------------------------------------------------
171
function deleteLock($WIKI_ID) {
173
$wikiLockFN = wikiLockFN($WIKI_ID);
175
if ( file_exists($wikiLockFN) ) {
176
if ( !unlink($wikiLockFN) ) {
177
fwrite( STDERR, "Unable to delete $wikiLockFN\n" );
184
#------------------------------------------------------------------------------
185
$USERNAME = getUser();
187
$SYSTEM_ID = '127.0.0.1';
189
#------------------------------------------------------------------------------
190
$OPTS = Doku_Cli_Opts::getOptions(
200
if ( $OPTS->isError() ) {
201
print $OPTS->getMessage()."\n";
205
if ( $OPTS->has('h') or $OPTS->has('help') or !$OPTS->hasArgs() ) {
206
usage(getSuppliedArgument($OPTS,'h','help'));
210
if ( $OPTS->has('u') or $OPTS->has('user') ) {
211
$USERNAME = getSuppliedArgument($OPTS,'u','user');
214
if ( $OPTS->has('s') or $OPTS->has('system') ) {
215
$SYSTEM_ID = getSuppliedArgument($OPTS,'s','system');
218
#------------------------------------------------------------------------------
219
switch ( $OPTS->arg(0) ) {
221
#----------------------------------------------------------------------
224
$WIKI_ID = $OPTS->arg(1);
227
fwrite( STDERR, "Wiki page ID required\n");
231
$WIKI_FN = wikiFN($WIKI_ID);
233
if ( !file_exists($WIKI_FN) ) {
234
fwrite( STDERR, "$WIKI_ID does not yet exist\n");
238
$TARGET_FN = $OPTS->arg(2);
240
if ( empty($TARGET_FN) ) {
241
$TARGET_FN = getcwd().'/'.basename($WIKI_FN);
244
if ( !file_exists(dirname($TARGET_FN)) ) {
245
fwrite( STDERR, "Directory ".dirname($TARGET_FN)." does not exist\n");
249
if ( stristr( realpath(dirname($TARGET_FN)), realpath($conf['datadir']) ) !== FALSE ) {
250
fwrite( STDERR, "Attempt to check out file into data directory - not allowed\n");
254
if ( $OPTS->has('f') ) {
255
deleteLock($WIKI_ID);
258
obtainLock($WIKI_ID);
260
# Need to lock the file first?
261
if ( !copy($WIKI_FN, $TARGET_FN) ) {
262
fwrite( STDERR, "Unable to copy $WIKI_FN to $TARGET_FN\n");
267
print "$WIKI_ID > $TARGET_FN\n";
272
#----------------------------------------------------------------------
275
$TARGET_FN = $OPTS->arg(1);
278
fwrite( STDERR, "Target filename required\n");
282
if ( !file_exists($TARGET_FN) ) {
283
fwrite( STDERR, "$TARGET_FN does not exist\n");
287
if ( !is_readable($TARGET_FN) ) {
288
fwrite( STDERR, "Cannot read from $TARGET_FN\n");
292
$WIKI_ID = $OPTS->arg(2);
295
fwrite( STDERR, "Wiki page ID required\n");
299
if ( !$OPTS->has('m') ) {
300
fwrite( STDERR, "Summary message required\n");
304
if ( $OPTS->has('f') ) {
305
deleteLock($WIKI_ID);
308
$_SERVER['REMOTE_USER'] = $USERNAME;
309
if ( checklock($WIKI_ID) ) {
310
fwrite( STDERR, "$WIKI_ID is locked by another user\n");
314
obtainLock($WIKI_ID);
316
saveWikiText($WIKI_ID, file_get_contents($TARGET_FN), $OPTS->get('m'));
324
#----------------------------------------------------------------------
327
$WIKI_ID = $OPTS->arg(1);
330
fwrite( STDERR, "Wiki page ID required\n");
334
if ( $OPTS->has('f') ) {
335
deleteLock($WIKI_ID);
338
obtainLock($WIKI_ID);
340
print "Locked : $WIKI_ID\n";
345
#----------------------------------------------------------------------
348
$WIKI_ID = $OPTS->arg(1);
351
fwrite( STDERR, "Wiki page ID required\n");
355
if ( $OPTS->has('f') ) {
356
deleteLock($WIKI_ID);
361
print "Unlocked : $WIKI_ID\n";
366
#----------------------------------------------------------------------
369
fwrite( STDERR, "Invalid action ".$OPTS->arg(0)."\n" );