4 use splitbrain\phpcli\CLI
;
5 use splitbrain\phpcli\Options
;
6 use dokuwiki\Utf8\PhpString
;
8 if (!defined('DOKU_INC')) define('DOKU_INC', realpath(__DIR__
. '/../') . '/');
9 define('NOSESSION', 1);
10 require_once(DOKU_INC
. 'inc/init.php');
13 * Checkout and commit pages from the command line while maintaining the history
15 class PageCLI
extends CLI
17 protected $force = false;
18 protected $username = '';
21 * Register options and arguments on the given $options object
23 * @param Options $options
26 protected function setup(Options
$options)
29 $options->registerOption(
31 'force obtaining a lock for the page (generally bad idea)',
34 $options->registerOption(
36 'work as this user. defaults to current CLI user',
41 'Utility to help command line Dokuwiki page editing, allow ' .
42 'pages to be checked out for editing then committed after changes'
45 /* checkout command */
46 $options->registerCommand(
48 'Checks out a file from the repository, using the wiki id and obtaining ' .
49 'a lock for the page. ' . "\n" .
50 'If a working_file is specified, this is where the page is copied to. ' .
51 'Otherwise defaults to the same as the wiki page in the current ' .
54 $options->registerArgument(
56 'The wiki page to checkout',
60 $options->registerArgument(
62 'How to name the local checkout',
68 $options->registerCommand(
70 'Checks in the working_file into the repository using the specified ' .
71 'wiki id, archiving the previous version.'
73 $options->registerArgument(
75 'The local file to commit',
79 $options->registerArgument(
81 'The wiki page to create or update',
85 $options->registerOption(
87 'Summary describing the change (required)',
92 $options->registerOption(
101 $options->registerCommand(
103 'Obtains or updates a lock for a wiki page'
105 $options->registerArgument(
107 'The wiki page to lock',
113 $options->registerCommand(
115 'Removes a lock for a wiki page.'
117 $options->registerArgument(
119 'The wiki page to unlock',
125 $options->registerCommand(
127 'Prints metadata value for a page to stdout.'
129 $options->registerArgument(
131 'The wiki page to get the metadata for',
135 $options->registerArgument(
137 'The name of the metadata item to be retrieved.' . "\n" .
138 'If empty, an array of all the metadata items is returned.' . "\n" .
139 'For retrieving items that are stored in sub-arrays, separate the ' .
140 'keys of the different levels by spaces, in quotes, eg "date modified".',
149 * Arguments and options have been parsed when this is run
151 * @param Options $options
154 protected function main(Options
$options)
156 $this->force
= $options->getOpt('force', false);
157 $this->username
= $options->getOpt('user', $this->getUser());
159 $command = $options->getCmd();
160 $args = $options->getArgs();
163 $wiki_id = array_shift($args);
164 $localfile = array_shift($args);
165 $this->commandCheckout($wiki_id, $localfile);
168 $localfile = array_shift($args);
169 $wiki_id = array_shift($args);
170 $this->commandCommit(
173 $options->getOpt('message', ''),
174 $options->getOpt('trivial', false)
178 $wiki_id = array_shift($args);
179 $this->obtainLock($wiki_id);
180 $this->success("$wiki_id locked");
183 $wiki_id = array_shift($args);
184 $this->clearLock($wiki_id);
185 $this->success("$wiki_id unlocked");
188 $wiki_id = array_shift($args);
189 $key = trim(array_shift($args));
190 $meta = p_get_metadata($wiki_id, $key, METADATA_RENDER_UNLIMITED
);
191 echo trim(json_encode($meta, JSON_PRETTY_PRINT
));
195 echo $options->help();
202 * @param string $wiki_id
203 * @param string $localfile
205 protected function commandCheckout($wiki_id, $localfile)
209 $wiki_id = cleanID($wiki_id);
210 $wiki_fn = wikiFN($wiki_id);
212 if (!file_exists($wiki_fn)) {
213 $this->fatal("$wiki_id does not yet exist");
216 if (empty($localfile)) {
217 $localfile = getcwd() . '/' . PhpString
::basename($wiki_fn);
220 if (!file_exists(dirname($localfile))) {
221 $this->fatal("Directory " . dirname($localfile) . " does not exist");
224 if (stristr(realpath(dirname($localfile)), (string) realpath($conf['datadir'])) !== false) {
225 $this->fatal("Attempt to check out file into data directory - not allowed");
228 $this->obtainLock($wiki_id);
230 if (!copy($wiki_fn, $localfile)) {
231 $this->clearLock($wiki_id);
232 $this->fatal("Unable to copy $wiki_fn to $localfile");
235 $this->success("$wiki_id > $localfile");
239 * Save a file as a new page revision
241 * @param string $localfile
242 * @param string $wiki_id
243 * @param string $message
246 protected function commandCommit($localfile, $wiki_id, $message, $minor)
248 $wiki_id = cleanID($wiki_id);
249 $message = trim($message);
251 if (!file_exists($localfile)) {
252 $this->fatal("$localfile does not exist");
255 if (!is_readable($localfile)) {
256 $this->fatal("Cannot read from $localfile");
260 $this->fatal("Summary message required");
263 $this->obtainLock($wiki_id);
265 saveWikiText($wiki_id, file_get_contents($localfile), $message, $minor);
267 $this->clearLock($wiki_id);
269 $this->success("$localfile > $wiki_id");
273 * Lock the given page or exit
275 * @param string $wiki_id
277 protected function obtainLock($wiki_id)
279 if ($this->force
) $this->deleteLock($wiki_id);
281 $_SERVER['REMOTE_USER'] = $this->username
;
283 if (checklock($wiki_id)) {
284 $this->error("Page $wiki_id is already locked by another user");
290 if (checklock($wiki_id)) {
291 $this->error("Unable to obtain lock for $wiki_id ");
292 var_dump(checklock($wiki_id));
298 * Clear the lock on the given page
300 * @param string $wiki_id
302 protected function clearLock($wiki_id)
304 if ($this->force
) $this->deleteLock($wiki_id);
306 $_SERVER['REMOTE_USER'] = $this->username
;
307 if (checklock($wiki_id)) {
308 $this->error("Page $wiki_id is locked by another user");
314 if (file_exists(wikiLockFN($wiki_id))) {
315 $this->error("Unable to clear lock for $wiki_id");
321 * Forcefully remove a lock on the page given
323 * @param string $wiki_id
325 protected function deleteLock($wiki_id)
327 $wikiLockFN = wikiLockFN($wiki_id);
329 if (file_exists($wikiLockFN)) {
330 if (!unlink($wikiLockFN)) {
331 $this->error("Unable to delete $wikiLockFN");
338 * Get the current user's username from the environment
342 protected function getUser()
344 $user = getenv('USER');
346 $user = getenv('USERNAME');
358 $cli = new PageCLI();