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