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