fix missing return
[dokuwiki.git] / bin / dwpage.php
blobdee8039eba0aadb3ef49697e2ffad1b1d0d386c4
1 #!/usr/bin/env php
2 <?php
4 use splitbrain\phpcli\CLI;
5 use splitbrain\phpcli\Options;
7 if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../') . '/');
8 define('NOSESSION', 1);
9 require_once(DOKU_INC . 'inc/init.php');
11 /**
12 * Checkout and commit pages from the command line while maintaining the history
14 class PageCLI extends CLI {
16 protected $force = false;
17 protected $username = '';
19 /**
20 * Register options and arguments on the given $options object
22 * @param Options $options
23 * @return void
25 protected function setup(Options $options) {
26 /* global */
27 $options->registerOption(
28 'force',
29 'force obtaining a lock for the page (generally bad idea)',
30 'f'
32 $options->registerOption(
33 'user',
34 'work as this user. defaults to current CLI user',
35 'u',
36 'username'
38 $options->setHelp(
39 'Utility to help command line Dokuwiki page editing, allow ' .
40 'pages to be checked out for editing then committed after changes'
43 /* checkout command */
44 $options->registerCommand(
45 'checkout',
46 'Checks out a file from the repository, using the wiki id and obtaining ' .
47 'a lock for the page. ' . "\n" .
48 'If a working_file is specified, this is where the page is copied to. ' .
49 'Otherwise defaults to the same as the wiki page in the current ' .
50 'working directory.'
52 $options->registerArgument(
53 'wikipage',
54 'The wiki page to checkout',
55 true,
56 'checkout'
58 $options->registerArgument(
59 'workingfile',
60 'How to name the local checkout',
61 false,
62 'checkout'
65 /* commit command */
66 $options->registerCommand(
67 'commit',
68 'Checks in the working_file into the repository using the specified ' .
69 'wiki id, archiving the previous version.'
71 $options->registerArgument(
72 'workingfile',
73 'The local file to commit',
74 true,
75 'commit'
77 $options->registerArgument(
78 'wikipage',
79 'The wiki page to create or update',
80 true,
81 'commit'
83 $options->registerOption(
84 'message',
85 'Summary describing the change (required)',
86 'm',
87 'summary',
88 'commit'
90 $options->registerOption(
91 'trivial',
92 'minor change',
93 't',
94 false,
95 'commit'
98 /* lock command */
99 $options->registerCommand(
100 'lock',
101 'Obtains or updates a lock for a wiki page'
103 $options->registerArgument(
104 'wikipage',
105 'The wiki page to lock',
106 true,
107 'lock'
110 /* unlock command */
111 $options->registerCommand(
112 'unlock',
113 'Removes a lock for a wiki page.'
115 $options->registerArgument(
116 'wikipage',
117 'The wiki page to unlock',
118 true,
119 'unlock'
124 * Your main program
126 * Arguments and options have been parsed when this is run
128 * @param Options $options
129 * @return void
131 protected function main(Options $options) {
132 $this->force = $options->getOpt('force', false);
133 $this->username = $options->getOpt('user', $this->getUser());
135 $command = $options->getCmd();
136 $args = $options->getArgs();
137 switch($command) {
138 case 'checkout':
139 $wiki_id = array_shift($args);
140 $localfile = array_shift($args);
141 $this->commandCheckout($wiki_id, $localfile);
142 break;
143 case 'commit':
144 $localfile = array_shift($args);
145 $wiki_id = array_shift($args);
146 $this->commandCommit(
147 $localfile,
148 $wiki_id,
149 $options->getOpt('message', ''),
150 $options->getOpt('trivial', false)
152 break;
153 case 'lock':
154 $wiki_id = array_shift($args);
155 $this->obtainLock($wiki_id);
156 $this->success("$wiki_id locked");
157 break;
158 case 'unlock':
159 $wiki_id = array_shift($args);
160 $this->clearLock($wiki_id);
161 $this->success("$wiki_id unlocked");
162 break;
163 default:
164 echo $options->help();
169 * Check out a file
171 * @param string $wiki_id
172 * @param string $localfile
174 protected function commandCheckout($wiki_id, $localfile) {
175 global $conf;
177 $wiki_id = cleanID($wiki_id);
178 $wiki_fn = wikiFN($wiki_id);
180 if(!file_exists($wiki_fn)) {
181 $this->fatal("$wiki_id does not yet exist");
184 if(empty($localfile)) {
185 $localfile = getcwd() . '/' . \dokuwiki\Utf8\PhpString::basename($wiki_fn);
188 if(!file_exists(dirname($localfile))) {
189 $this->fatal("Directory " . dirname($localfile) . " does not exist");
192 if(stristr(realpath(dirname($localfile)), realpath($conf['datadir'])) !== false) {
193 $this->fatal("Attempt to check out file into data directory - not allowed");
196 $this->obtainLock($wiki_id);
198 if(!copy($wiki_fn, $localfile)) {
199 $this->clearLock($wiki_id);
200 $this->fatal("Unable to copy $wiki_fn to $localfile");
203 $this->success("$wiki_id > $localfile");
207 * Save a file as a new page revision
209 * @param string $localfile
210 * @param string $wiki_id
211 * @param string $message
212 * @param bool $minor
214 protected function commandCommit($localfile, $wiki_id, $message, $minor) {
215 $wiki_id = cleanID($wiki_id);
216 $message = trim($message);
218 if(!file_exists($localfile)) {
219 $this->fatal("$localfile does not exist");
222 if(!is_readable($localfile)) {
223 $this->fatal("Cannot read from $localfile");
226 if(!$message) {
227 $this->fatal("Summary message required");
230 $this->obtainLock($wiki_id);
232 saveWikiText($wiki_id, file_get_contents($localfile), $message, $minor);
234 $this->clearLock($wiki_id);
236 $this->success("$localfile > $wiki_id");
240 * Lock the given page or exit
242 * @param string $wiki_id
244 protected function obtainLock($wiki_id) {
245 if($this->force) $this->deleteLock($wiki_id);
247 $_SERVER['REMOTE_USER'] = $this->username;
249 if(checklock($wiki_id)) {
250 $this->error("Page $wiki_id is already locked by another user");
251 exit(1);
254 lock($wiki_id);
256 if(checklock($wiki_id)) {
257 $this->error("Unable to obtain lock for $wiki_id ");
258 var_dump(checklock($wiki_id));
259 exit(1);
264 * Clear the lock on the given page
266 * @param string $wiki_id
268 protected function clearLock($wiki_id) {
269 if($this->force) $this->deleteLock($wiki_id);
271 $_SERVER['REMOTE_USER'] = $this->username;
272 if(checklock($wiki_id)) {
273 $this->error("Page $wiki_id is locked by another user");
274 exit(1);
277 unlock($wiki_id);
279 if(file_exists(wikiLockFN($wiki_id))) {
280 $this->error("Unable to clear lock for $wiki_id");
281 exit(1);
286 * Forcefully remove a lock on the page given
288 * @param string $wiki_id
290 protected function deleteLock($wiki_id) {
291 $wikiLockFN = wikiLockFN($wiki_id);
293 if(file_exists($wikiLockFN)) {
294 if(!unlink($wikiLockFN)) {
295 $this->error("Unable to delete $wikiLockFN");
296 exit(1);
302 * Get the current user's username from the environment
304 * @return string
306 protected function getUser() {
307 $user = getenv('USER');
308 if(empty ($user)) {
309 $user = getenv('USERNAME');
310 } else {
311 return $user;
313 if(empty ($user)) {
314 $user = 'admin';
316 return $user;
320 // Main
321 $cli = new PageCLI();
322 $cli->run();