xref: /dokuwiki/bin/dwpage.php (revision 8c7c53b0321a3cd3116b8d3b2ad27863a38dece7)
1cbfa4829SPhy#!/usr/bin/env php
21caeb00aSHarry Fuecks<?php
3cbeaa4a0SAndreas Gohr
4cbeaa4a0SAndreas Gohruse splitbrain\phpcli\CLI;
5cbeaa4a0SAndreas Gohruse splitbrain\phpcli\Options;
6b1f206e1SAndreas Gohruse dokuwiki\Utf8\PhpString;
7cbeaa4a0SAndreas Gohr
8b1f206e1SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC', realpath(__DIR__ . '/../') . '/');
9e82b082eSAndreas Gohrdefine('NOSESSION', 1);
10e82b082eSAndreas Gohrrequire_once(DOKU_INC . 'inc/init.php');
111caeb00aSHarry Fuecks
12e82b082eSAndreas Gohr/**
13e82b082eSAndreas Gohr * Checkout and commit pages from the command line while maintaining the history
14e82b082eSAndreas Gohr */
15*8c7c53b0SAndreas Gohrclass PageCLI extends CLI
16*8c7c53b0SAndreas Gohr{
17e82b082eSAndreas Gohr
18e82b082eSAndreas Gohr    protected $force = false;
19e82b082eSAndreas Gohr    protected $username = '';
20e82b082eSAndreas Gohr
21e82b082eSAndreas Gohr    /**
22e82b082eSAndreas Gohr     * Register options and arguments on the given $options object
23e82b082eSAndreas Gohr     *
24cbeaa4a0SAndreas Gohr     * @param Options $options
25e82b082eSAndreas Gohr     * @return void
26e82b082eSAndreas Gohr     */
27cbeaa4a0SAndreas Gohr    protected function setup(Options $options) {
28e82b082eSAndreas Gohr        /* global */
29e82b082eSAndreas Gohr        $options->registerOption(
30e82b082eSAndreas Gohr            'force',
31e82b082eSAndreas Gohr            'force obtaining a lock for the page (generally bad idea)',
32e82b082eSAndreas Gohr            'f'
33e82b082eSAndreas Gohr        );
34e82b082eSAndreas Gohr        $options->registerOption(
35e82b082eSAndreas Gohr            'user',
36e82b082eSAndreas Gohr            'work as this user. defaults to current CLI user',
37bf00b396SAndreas Gohr            'u',
38bf00b396SAndreas Gohr            'username'
39e82b082eSAndreas Gohr        );
40e82b082eSAndreas Gohr        $options->setHelp(
41e82b082eSAndreas Gohr            'Utility to help command line Dokuwiki page editing, allow ' .
42e82b082eSAndreas Gohr            'pages to be checked out for editing then committed after changes'
43e82b082eSAndreas Gohr        );
44e82b082eSAndreas Gohr
45e82b082eSAndreas Gohr        /* checkout command */
46e82b082eSAndreas Gohr        $options->registerCommand(
47e82b082eSAndreas Gohr            'checkout',
48e82b082eSAndreas Gohr            'Checks out a file from the repository, using the wiki id and obtaining ' .
49e82b082eSAndreas Gohr            'a lock for the page. ' . "\n" .
50e82b082eSAndreas Gohr            'If a working_file is specified, this is where the page is copied to. ' .
51e82b082eSAndreas Gohr            'Otherwise defaults to the same as the wiki page in the current ' .
52e82b082eSAndreas Gohr            'working directory.'
53e82b082eSAndreas Gohr        );
54e82b082eSAndreas Gohr        $options->registerArgument(
55e82b082eSAndreas Gohr            'wikipage',
56e82b082eSAndreas Gohr            'The wiki page to checkout',
57e82b082eSAndreas Gohr            true,
58e82b082eSAndreas Gohr            'checkout'
59e82b082eSAndreas Gohr        );
60e82b082eSAndreas Gohr        $options->registerArgument(
61e82b082eSAndreas Gohr            'workingfile',
62e82b082eSAndreas Gohr            'How to name the local checkout',
63e82b082eSAndreas Gohr            false,
64e82b082eSAndreas Gohr            'checkout'
65e82b082eSAndreas Gohr        );
66e82b082eSAndreas Gohr
67e82b082eSAndreas Gohr        /* commit command */
68e82b082eSAndreas Gohr        $options->registerCommand(
69e82b082eSAndreas Gohr            'commit',
70e82b082eSAndreas Gohr            'Checks in the working_file into the repository using the specified ' .
71e82b082eSAndreas Gohr            'wiki id, archiving the previous version.'
72e82b082eSAndreas Gohr        );
73e82b082eSAndreas Gohr        $options->registerArgument(
74e82b082eSAndreas Gohr            'workingfile',
75e82b082eSAndreas Gohr            'The local file to commit',
76e82b082eSAndreas Gohr            true,
77e82b082eSAndreas Gohr            'commit'
78e82b082eSAndreas Gohr        );
79e82b082eSAndreas Gohr        $options->registerArgument(
80e82b082eSAndreas Gohr            'wikipage',
81e82b082eSAndreas Gohr            'The wiki page to create or update',
82e82b082eSAndreas Gohr            true,
83e82b082eSAndreas Gohr            'commit'
84e82b082eSAndreas Gohr        );
85e82b082eSAndreas Gohr        $options->registerOption(
86e82b082eSAndreas Gohr            'message',
87e82b082eSAndreas Gohr            'Summary describing the change (required)',
88e82b082eSAndreas Gohr            'm',
89e82b082eSAndreas Gohr            'summary',
90e82b082eSAndreas Gohr            'commit'
91e82b082eSAndreas Gohr        );
92e82b082eSAndreas Gohr        $options->registerOption(
93e82b082eSAndreas Gohr            'trivial',
94e82b082eSAndreas Gohr            'minor change',
95e82b082eSAndreas Gohr            't',
96e82b082eSAndreas Gohr            false,
97e82b082eSAndreas Gohr            'commit'
98e82b082eSAndreas Gohr        );
99e82b082eSAndreas Gohr
100e82b082eSAndreas Gohr        /* lock command */
101e82b082eSAndreas Gohr        $options->registerCommand(
102e82b082eSAndreas Gohr            'lock',
103e82b082eSAndreas Gohr            'Obtains or updates a lock for a wiki page'
104e82b082eSAndreas Gohr        );
105e82b082eSAndreas Gohr        $options->registerArgument(
106e82b082eSAndreas Gohr            'wikipage',
107e82b082eSAndreas Gohr            'The wiki page to lock',
108e82b082eSAndreas Gohr            true,
109e82b082eSAndreas Gohr            'lock'
110e82b082eSAndreas Gohr        );
111e82b082eSAndreas Gohr
112e82b082eSAndreas Gohr        /* unlock command */
113e82b082eSAndreas Gohr        $options->registerCommand(
114e82b082eSAndreas Gohr            'unlock',
115e82b082eSAndreas Gohr            'Removes a lock for a wiki page.'
116e82b082eSAndreas Gohr        );
117e82b082eSAndreas Gohr        $options->registerArgument(
118e82b082eSAndreas Gohr            'wikipage',
119e82b082eSAndreas Gohr            'The wiki page to unlock',
120e82b082eSAndreas Gohr            true,
121e82b082eSAndreas Gohr            'unlock'
122e82b082eSAndreas Gohr        );
1232c23659dSRobin Getz
1242c23659dSRobin Getz        /* gmeta command */
1252c23659dSRobin Getz        $options->registerCommand(
1263fe01844SAndreas Gohr            'getmeta',
1272c23659dSRobin Getz            'Prints metadata value for a page to stdout.'
1282c23659dSRobin Getz        );
1292c23659dSRobin Getz        $options->registerArgument(
1302c23659dSRobin Getz            'wikipage',
1312c23659dSRobin Getz            'The wiki page to get the metadata for',
1322c23659dSRobin Getz            true,
1333fe01844SAndreas Gohr            'getmeta'
1342c23659dSRobin Getz        );
1352c23659dSRobin Getz        $options->registerArgument(
1362c23659dSRobin Getz            'key',
1372c23659dSRobin Getz            'The name of the metadata item to be retrieved.' . "\n" .
1382c23659dSRobin Getz            'If empty, an array of all the metadata items is returned.' ."\n" .
1392c23659dSRobin Getz            'For retrieving items that are stored in sub-arrays, separate the ' .
1402c23659dSRobin Getz            'keys of the different levels by spaces, in quotes, eg "date modified".',
1412c23659dSRobin Getz            false,
1423fe01844SAndreas Gohr            'getmeta'
1432c23659dSRobin Getz        );
144e82b082eSAndreas Gohr    }
145e82b082eSAndreas Gohr
146e82b082eSAndreas Gohr    /**
147e82b082eSAndreas Gohr     * Your main program
148e82b082eSAndreas Gohr     *
149e82b082eSAndreas Gohr     * Arguments and options have been parsed when this is run
150e82b082eSAndreas Gohr     *
151cbeaa4a0SAndreas Gohr     * @param Options $options
152e82b082eSAndreas Gohr     * @return void
153e82b082eSAndreas Gohr     */
154cbeaa4a0SAndreas Gohr    protected function main(Options $options) {
155e82b082eSAndreas Gohr        $this->force = $options->getOpt('force', false);
156e82b082eSAndreas Gohr        $this->username = $options->getOpt('user', $this->getUser());
157e82b082eSAndreas Gohr
158e82b082eSAndreas Gohr        $command = $options->getCmd();
159cbeaa4a0SAndreas Gohr        $args = $options->getArgs();
160e82b082eSAndreas Gohr        switch($command) {
1611caeb00aSHarry Fuecks            case 'checkout':
162cbeaa4a0SAndreas Gohr                $wiki_id = array_shift($args);
163cbeaa4a0SAndreas Gohr                $localfile = array_shift($args);
164e82b082eSAndreas Gohr                $this->commandCheckout($wiki_id, $localfile);
1651caeb00aSHarry Fuecks                break;
1661caeb00aSHarry Fuecks            case 'commit':
167cbeaa4a0SAndreas Gohr                $localfile = array_shift($args);
168cbeaa4a0SAndreas Gohr                $wiki_id = array_shift($args);
169e82b082eSAndreas Gohr                $this->commandCommit(
170e82b082eSAndreas Gohr                    $localfile,
171e82b082eSAndreas Gohr                    $wiki_id,
172e82b082eSAndreas Gohr                    $options->getOpt('message', ''),
173e82b082eSAndreas Gohr                    $options->getOpt('trivial', false)
174e82b082eSAndreas Gohr                );
1751caeb00aSHarry Fuecks                break;
1761caeb00aSHarry Fuecks            case 'lock':
177cbeaa4a0SAndreas Gohr                $wiki_id = array_shift($args);
178e82b082eSAndreas Gohr                $this->obtainLock($wiki_id);
179e82b082eSAndreas Gohr                $this->success("$wiki_id locked");
1801caeb00aSHarry Fuecks                break;
1811caeb00aSHarry Fuecks            case 'unlock':
182cbeaa4a0SAndreas Gohr                $wiki_id = array_shift($args);
183e82b082eSAndreas Gohr                $this->clearLock($wiki_id);
184e82b082eSAndreas Gohr                $this->success("$wiki_id unlocked");
1851caeb00aSHarry Fuecks                break;
1863fe01844SAndreas Gohr            case 'getmeta':
1872c23659dSRobin Getz                $wiki_id = array_shift($args);
1882c23659dSRobin Getz                $key = trim(array_shift($args));
1898d670f35SAndreas Gohr                $meta = p_get_metadata($wiki_id, $key, METADATA_RENDER_UNLIMITED);
1908d670f35SAndreas Gohr                echo trim(json_encode($meta, JSON_PRETTY_PRINT));
1918d670f35SAndreas Gohr                echo "\n";
1922c23659dSRobin Getz                break;
1931caeb00aSHarry Fuecks            default:
194e82b082eSAndreas Gohr                echo $options->help();
1951caeb00aSHarry Fuecks        }
1961caeb00aSHarry Fuecks    }
1971caeb00aSHarry Fuecks
198e82b082eSAndreas Gohr    /**
199e82b082eSAndreas Gohr     * Check out a file
200e82b082eSAndreas Gohr     *
201e82b082eSAndreas Gohr     * @param string $wiki_id
202e82b082eSAndreas Gohr     * @param string $localfile
203e82b082eSAndreas Gohr     */
204e82b082eSAndreas Gohr    protected function commandCheckout($wiki_id, $localfile) {
205e82b082eSAndreas Gohr        global $conf;
206e82b082eSAndreas Gohr
207e82b082eSAndreas Gohr        $wiki_id = cleanID($wiki_id);
208e82b082eSAndreas Gohr        $wiki_fn = wikiFN($wiki_id);
209e82b082eSAndreas Gohr
210e82b082eSAndreas Gohr        if(!file_exists($wiki_fn)) {
211e82b082eSAndreas Gohr            $this->fatal("$wiki_id does not yet exist");
212e82b082eSAndreas Gohr        }
213e82b082eSAndreas Gohr
214e82b082eSAndreas Gohr        if(empty($localfile)) {
215b1f206e1SAndreas Gohr            $localfile = getcwd() . '/' . PhpString::basename($wiki_fn);
216e82b082eSAndreas Gohr        }
217e82b082eSAndreas Gohr
218e82b082eSAndreas Gohr        if(!file_exists(dirname($localfile))) {
219e82b082eSAndreas Gohr            $this->fatal("Directory " . dirname($localfile) . " does not exist");
220e82b082eSAndreas Gohr        }
221e82b082eSAndreas Gohr
222b1f206e1SAndreas Gohr        if(stristr(realpath(dirname($localfile)), (string) realpath($conf['datadir'])) !== false) {
223e82b082eSAndreas Gohr            $this->fatal("Attempt to check out file into data directory - not allowed");
224e82b082eSAndreas Gohr        }
225e82b082eSAndreas Gohr
226e82b082eSAndreas Gohr        $this->obtainLock($wiki_id);
227e82b082eSAndreas Gohr
228e82b082eSAndreas Gohr        if(!copy($wiki_fn, $localfile)) {
229e82b082eSAndreas Gohr            $this->clearLock($wiki_id);
230e82b082eSAndreas Gohr            $this->fatal("Unable to copy $wiki_fn to $localfile");
231e82b082eSAndreas Gohr        }
232e82b082eSAndreas Gohr
233e82b082eSAndreas Gohr        $this->success("$wiki_id > $localfile");
234e82b082eSAndreas Gohr    }
235e82b082eSAndreas Gohr
236e82b082eSAndreas Gohr    /**
237e82b082eSAndreas Gohr     * Save a file as a new page revision
238e82b082eSAndreas Gohr     *
239e82b082eSAndreas Gohr     * @param string $localfile
240e82b082eSAndreas Gohr     * @param string $wiki_id
241e82b082eSAndreas Gohr     * @param string $message
242e82b082eSAndreas Gohr     * @param bool $minor
243e82b082eSAndreas Gohr     */
244e82b082eSAndreas Gohr    protected function commandCommit($localfile, $wiki_id, $message, $minor) {
245e82b082eSAndreas Gohr        $wiki_id = cleanID($wiki_id);
246e82b082eSAndreas Gohr        $message = trim($message);
247e82b082eSAndreas Gohr
248e82b082eSAndreas Gohr        if(!file_exists($localfile)) {
249e82b082eSAndreas Gohr            $this->fatal("$localfile does not exist");
250e82b082eSAndreas Gohr        }
251e82b082eSAndreas Gohr
252e82b082eSAndreas Gohr        if(!is_readable($localfile)) {
253e82b082eSAndreas Gohr            $this->fatal("Cannot read from $localfile");
254e82b082eSAndreas Gohr        }
255e82b082eSAndreas Gohr
256e82b082eSAndreas Gohr        if(!$message) {
257e82b082eSAndreas Gohr            $this->fatal("Summary message required");
258e82b082eSAndreas Gohr        }
259e82b082eSAndreas Gohr
260e82b082eSAndreas Gohr        $this->obtainLock($wiki_id);
261e82b082eSAndreas Gohr
262e82b082eSAndreas Gohr        saveWikiText($wiki_id, file_get_contents($localfile), $message, $minor);
263e82b082eSAndreas Gohr
264e82b082eSAndreas Gohr        $this->clearLock($wiki_id);
265e82b082eSAndreas Gohr
266e82b082eSAndreas Gohr        $this->success("$localfile > $wiki_id");
267e82b082eSAndreas Gohr    }
268e82b082eSAndreas Gohr
269e82b082eSAndreas Gohr    /**
270e82b082eSAndreas Gohr     * Lock the given page or exit
271e82b082eSAndreas Gohr     *
272e82b082eSAndreas Gohr     * @param string $wiki_id
273e82b082eSAndreas Gohr     */
274e82b082eSAndreas Gohr    protected function obtainLock($wiki_id) {
275e82b082eSAndreas Gohr        if($this->force) $this->deleteLock($wiki_id);
276e82b082eSAndreas Gohr
277e82b082eSAndreas Gohr        $_SERVER['REMOTE_USER'] = $this->username;
278bf00b396SAndreas Gohr
279e82b082eSAndreas Gohr        if(checklock($wiki_id)) {
280e82b082eSAndreas Gohr            $this->error("Page $wiki_id is already locked by another user");
281e82b082eSAndreas Gohr            exit(1);
282e82b082eSAndreas Gohr        }
283e82b082eSAndreas Gohr
284e82b082eSAndreas Gohr        lock($wiki_id);
285e82b082eSAndreas Gohr
286cb62a664SJustinTrouble        if(checklock($wiki_id)) {
287e82b082eSAndreas Gohr            $this->error("Unable to obtain lock for $wiki_id ");
288e82b082eSAndreas Gohr            var_dump(checklock($wiki_id));
289e82b082eSAndreas Gohr            exit(1);
290e82b082eSAndreas Gohr        }
291e82b082eSAndreas Gohr    }
292e82b082eSAndreas Gohr
293e82b082eSAndreas Gohr    /**
294e82b082eSAndreas Gohr     * Clear the lock on the given page
295e82b082eSAndreas Gohr     *
296e82b082eSAndreas Gohr     * @param string $wiki_id
297e82b082eSAndreas Gohr     */
298e82b082eSAndreas Gohr    protected function clearLock($wiki_id) {
299e82b082eSAndreas Gohr        if($this->force) $this->deleteLock($wiki_id);
300e82b082eSAndreas Gohr
301e82b082eSAndreas Gohr        $_SERVER['REMOTE_USER'] = $this->username;
302e82b082eSAndreas Gohr        if(checklock($wiki_id)) {
303e82b082eSAndreas Gohr            $this->error("Page $wiki_id is locked by another user");
304e82b082eSAndreas Gohr            exit(1);
305e82b082eSAndreas Gohr        }
306e82b082eSAndreas Gohr
307e82b082eSAndreas Gohr        unlock($wiki_id);
308e82b082eSAndreas Gohr
309e82b082eSAndreas Gohr        if(file_exists(wikiLockFN($wiki_id))) {
310e82b082eSAndreas Gohr            $this->error("Unable to clear lock for $wiki_id");
311e82b082eSAndreas Gohr            exit(1);
312e82b082eSAndreas Gohr        }
313e82b082eSAndreas Gohr    }
314e82b082eSAndreas Gohr
315e82b082eSAndreas Gohr    /**
316e82b082eSAndreas Gohr     * Forcefully remove a lock on the page given
317e82b082eSAndreas Gohr     *
318e82b082eSAndreas Gohr     * @param string $wiki_id
319e82b082eSAndreas Gohr     */
320e82b082eSAndreas Gohr    protected function deleteLock($wiki_id) {
321e82b082eSAndreas Gohr        $wikiLockFN = wikiLockFN($wiki_id);
322e82b082eSAndreas Gohr
323e82b082eSAndreas Gohr        if(file_exists($wikiLockFN)) {
324e82b082eSAndreas Gohr            if(!unlink($wikiLockFN)) {
325e82b082eSAndreas Gohr                $this->error("Unable to delete $wikiLockFN");
326e82b082eSAndreas Gohr                exit(1);
327e82b082eSAndreas Gohr            }
328e82b082eSAndreas Gohr        }
329e82b082eSAndreas Gohr    }
330e82b082eSAndreas Gohr
331e82b082eSAndreas Gohr    /**
332e82b082eSAndreas Gohr     * Get the current user's username from the environment
333e82b082eSAndreas Gohr     *
334e82b082eSAndreas Gohr     * @return string
335e82b082eSAndreas Gohr     */
336e82b082eSAndreas Gohr    protected function getUser() {
3371caeb00aSHarry Fuecks        $user = getenv('USER');
338feec2ab4SAndreas Gohr        if(empty ($user)) {
3391caeb00aSHarry Fuecks            $user = getenv('USERNAME');
3401caeb00aSHarry Fuecks        } else {
3411caeb00aSHarry Fuecks            return $user;
3421caeb00aSHarry Fuecks        }
343feec2ab4SAndreas Gohr        if(empty ($user)) {
3441caeb00aSHarry Fuecks            $user = 'admin';
3451caeb00aSHarry Fuecks        }
3461caeb00aSHarry Fuecks        return $user;
3471caeb00aSHarry Fuecks    }
3481caeb00aSHarry Fuecks}
3491caeb00aSHarry Fuecks
350e82b082eSAndreas Gohr// Main
351e82b082eSAndreas Gohr$cli = new PageCLI();
352e82b082eSAndreas Gohr$cli->run();
353