Portuguese language update
[dokuwiki/radio.git] / bin / dwpage.php
blobf664770bfabdf733ad9547b19918bb3b20ee78d6
1 #!/usr/bin/php
2 <?php
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) {
14 switch ( $action ) {
15 case 'checkout':
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
19 a lock for the page.
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
22 working directory.
24 EXAMPLE
25 $ ./dwpage.php checkout wiki:syntax ./new_syntax.txt
27 OPTIONS
28 -h, --help=<action>: get help
29 -f: force obtaining a lock for the page (generally bad idea)
31 break;
32 case 'commit':
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.
38 EXAMPLE
39 $ ./dwpage.php -m \"Some message\" commit ./new_syntax.txt wiki:syntax
41 OPTIONS
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
47 break;
48 case 'lock':
49 print "Usage: dwpage.php [opts] lock <wiki:page>
51 Obtains or updates a lock for a wiki page
53 EXAMPLE
54 $ ./dwpage.php lock wiki:syntax
56 OPTIONS
57 -h, --help=<action>: get help
58 -f: force obtaining a lock for the page (generally bad idea)
60 break;
61 case 'unlock':
62 print "Usage: dwpage.php [opts] unlock <wiki:page>
64 Removes a lock for a wiki page.
66 EXAMPLE
67 $ ./dwpage.php unlock wiki:syntax
69 OPTIONS
70 -h, --help=<action>: get help
71 -f: force obtaining a lock for the page (generally bad idea)
73 break;
74 default:
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;
84 ACTIONS
85 checkout: see $ dwpage.php --help=checkout
86 commit: see $ dwpage.php --help=commit
87 lock: see $ dwpage.php --help=lock
89 OPTIONS
90 -h, --help=<action>: get help
91 e.g. $ ./dwpage.php -hcommit
92 e.g. $ ./dwpage.php --help=commit
94 break;
98 #------------------------------------------------------------------------------
99 function getUser() {
100 $user = getenv('USER');
101 if (empty ($username)) {
102 $user = getenv('USERNAME');
103 } else {
104 return $user;
106 if (empty ($username)) {
107 $user = 'admin';
109 return $user;
112 #------------------------------------------------------------------------------
113 function getSuppliedArgument($OPTS, $short, $long) {
114 $arg = $OPTS->get($short);
115 if ( is_null($arg) ) {
116 $arg = $OPTS->get($long);
118 return $arg;
121 #------------------------------------------------------------------------------
122 function obtainLock($WIKI_ID) {
124 global $USERNAME;
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");
133 exit(1);
136 lock($WIKI_ID);
138 $_SERVER['REMOTE_USER'] = '_'.$USERNAME.'_';
140 if ( checklock($WIKI_ID) != $USERNAME ) {
142 fwrite( STDERR, "Unable to obtain lock for $WIKI_ID\n" );
143 exit(1);
148 #------------------------------------------------------------------------------
149 function clearLock($WIKI_ID) {
151 global $USERNAME ;
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");
160 exit(1);
163 unlock($WIKI_ID);
165 if ( file_exists(wikiLockFN($WIKI_ID)) ) {
166 fwrite( STDERR, "Unable to clear lock for $WIKI_ID\n" );
167 exit(1);
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" );
180 exit(1);
186 #------------------------------------------------------------------------------
187 $USERNAME = getUser();
188 $CWD = getcwd();
189 $SYSTEM_ID = '127.0.0.1';
191 #------------------------------------------------------------------------------
192 $OPTS = Doku_Cli_Opts::getOptions(
193 __FILE__,
194 'h::fm:u:s:t',
195 array(
196 'help==',
197 'user=',
198 'system=',
199 'trivial',
203 if ( $OPTS->isError() ) {
204 print $OPTS->getMessage()."\n";
205 exit(1);
208 if ( $OPTS->has('h') or $OPTS->has('help') or !$OPTS->hasArgs() ) {
209 usage(getSuppliedArgument($OPTS,'h','help'));
210 exit(0);
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 #----------------------------------------------------------------------
225 case 'checkout':
227 $WIKI_ID = $OPTS->arg(1);
229 if ( !$WIKI_ID ) {
230 fwrite( STDERR, "Wiki page ID required\n");
231 exit(1);
234 $WIKI_FN = wikiFN($WIKI_ID);
236 if ( !file_exists($WIKI_FN) ) {
237 fwrite( STDERR, "$WIKI_ID does not yet exist\n");
238 exit(1);
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");
249 exit(1);
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");
254 exit(1);
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");
266 clearLock($WIKI_ID);
267 exit(1);
270 print "$WIKI_ID > $TARGET_FN\n";
271 exit(0);
273 break;
275 #----------------------------------------------------------------------
276 case 'commit':
278 $TARGET_FN = $OPTS->arg(1);
280 if ( !$TARGET_FN ) {
281 fwrite( STDERR, "Target filename required\n");
282 exit(1);
285 if ( !file_exists($TARGET_FN) ) {
286 fwrite( STDERR, "$TARGET_FN does not exist\n");
287 exit(1);
290 if ( !is_readable($TARGET_FN) ) {
291 fwrite( STDERR, "Cannot read from $TARGET_FN\n");
292 exit(1);
295 $WIKI_ID = $OPTS->arg(2);
297 if ( !$WIKI_ID ) {
298 fwrite( STDERR, "Wiki page ID required\n");
299 exit(1);
302 if ( !$OPTS->has('m') ) {
303 fwrite( STDERR, "Summary message required\n");
304 exit(1);
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");
314 exit(1);
317 obtainLock($WIKI_ID);
319 saveWikiText($WIKI_ID, file_get_contents($TARGET_FN), $OPTS->get('m'), $OPTS->has('t'));
321 clearLock($WIKI_ID);
323 exit(0);
325 break;
327 #----------------------------------------------------------------------
328 case 'lock':
330 $WIKI_ID = $OPTS->arg(1);
332 if ( !$WIKI_ID ) {
333 fwrite( STDERR, "Wiki page ID required\n");
334 exit(1);
337 if ( $OPTS->has('f') ) {
338 deleteLock($WIKI_ID);
341 obtainLock($WIKI_ID);
343 print "Locked : $WIKI_ID\n";
344 exit(0);
346 break;
348 #----------------------------------------------------------------------
349 case 'unlock':
351 $WIKI_ID = $OPTS->arg(1);
353 if ( !$WIKI_ID ) {
354 fwrite( STDERR, "Wiki page ID required\n");
355 exit(1);
358 if ( $OPTS->has('f') ) {
359 deleteLock($WIKI_ID);
360 } else {
361 clearLock($WIKI_ID);
364 print "Unlocked : $WIKI_ID\n";
365 exit(0);
367 break;
369 #----------------------------------------------------------------------
370 default:
372 fwrite( STDERR, "Invalid action ".$OPTS->arg(0)."\n" );
373 exit(1);
375 break;