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 */ 158c7c53b0SAndreas Gohrclass PageCLI extends CLI 168c7c53b0SAndreas 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 */ 27*d868eb89SAndreas Gohr protected function setup(Options $options) 28*d868eb89SAndreas Gohr { 29e82b082eSAndreas Gohr /* global */ 30e82b082eSAndreas Gohr $options->registerOption( 31e82b082eSAndreas Gohr 'force', 32e82b082eSAndreas Gohr 'force obtaining a lock for the page (generally bad idea)', 33e82b082eSAndreas Gohr 'f' 34e82b082eSAndreas Gohr ); 35e82b082eSAndreas Gohr $options->registerOption( 36e82b082eSAndreas Gohr 'user', 37e82b082eSAndreas Gohr 'work as this user. defaults to current CLI user', 38bf00b396SAndreas Gohr 'u', 39bf00b396SAndreas Gohr 'username' 40e82b082eSAndreas Gohr ); 41e82b082eSAndreas Gohr $options->setHelp( 42e82b082eSAndreas Gohr 'Utility to help command line Dokuwiki page editing, allow ' . 43e82b082eSAndreas Gohr 'pages to be checked out for editing then committed after changes' 44e82b082eSAndreas Gohr ); 45e82b082eSAndreas Gohr 46e82b082eSAndreas Gohr /* checkout command */ 47e82b082eSAndreas Gohr $options->registerCommand( 48e82b082eSAndreas Gohr 'checkout', 49e82b082eSAndreas Gohr 'Checks out a file from the repository, using the wiki id and obtaining ' . 50e82b082eSAndreas Gohr 'a lock for the page. ' . "\n" . 51e82b082eSAndreas Gohr 'If a working_file is specified, this is where the page is copied to. ' . 52e82b082eSAndreas Gohr 'Otherwise defaults to the same as the wiki page in the current ' . 53e82b082eSAndreas Gohr 'working directory.' 54e82b082eSAndreas Gohr ); 55e82b082eSAndreas Gohr $options->registerArgument( 56e82b082eSAndreas Gohr 'wikipage', 57e82b082eSAndreas Gohr 'The wiki page to checkout', 58e82b082eSAndreas Gohr true, 59e82b082eSAndreas Gohr 'checkout' 60e82b082eSAndreas Gohr ); 61e82b082eSAndreas Gohr $options->registerArgument( 62e82b082eSAndreas Gohr 'workingfile', 63e82b082eSAndreas Gohr 'How to name the local checkout', 64e82b082eSAndreas Gohr false, 65e82b082eSAndreas Gohr 'checkout' 66e82b082eSAndreas Gohr ); 67e82b082eSAndreas Gohr 68e82b082eSAndreas Gohr /* commit command */ 69e82b082eSAndreas Gohr $options->registerCommand( 70e82b082eSAndreas Gohr 'commit', 71e82b082eSAndreas Gohr 'Checks in the working_file into the repository using the specified ' . 72e82b082eSAndreas Gohr 'wiki id, archiving the previous version.' 73e82b082eSAndreas Gohr ); 74e82b082eSAndreas Gohr $options->registerArgument( 75e82b082eSAndreas Gohr 'workingfile', 76e82b082eSAndreas Gohr 'The local file to commit', 77e82b082eSAndreas Gohr true, 78e82b082eSAndreas Gohr 'commit' 79e82b082eSAndreas Gohr ); 80e82b082eSAndreas Gohr $options->registerArgument( 81e82b082eSAndreas Gohr 'wikipage', 82e82b082eSAndreas Gohr 'The wiki page to create or update', 83e82b082eSAndreas Gohr true, 84e82b082eSAndreas Gohr 'commit' 85e82b082eSAndreas Gohr ); 86e82b082eSAndreas Gohr $options->registerOption( 87e82b082eSAndreas Gohr 'message', 88e82b082eSAndreas Gohr 'Summary describing the change (required)', 89e82b082eSAndreas Gohr 'm', 90e82b082eSAndreas Gohr 'summary', 91e82b082eSAndreas Gohr 'commit' 92e82b082eSAndreas Gohr ); 93e82b082eSAndreas Gohr $options->registerOption( 94e82b082eSAndreas Gohr 'trivial', 95e82b082eSAndreas Gohr 'minor change', 96e82b082eSAndreas Gohr 't', 97e82b082eSAndreas Gohr false, 98e82b082eSAndreas Gohr 'commit' 99e82b082eSAndreas Gohr ); 100e82b082eSAndreas Gohr 101e82b082eSAndreas Gohr /* lock command */ 102e82b082eSAndreas Gohr $options->registerCommand( 103e82b082eSAndreas Gohr 'lock', 104e82b082eSAndreas Gohr 'Obtains or updates a lock for a wiki page' 105e82b082eSAndreas Gohr ); 106e82b082eSAndreas Gohr $options->registerArgument( 107e82b082eSAndreas Gohr 'wikipage', 108e82b082eSAndreas Gohr 'The wiki page to lock', 109e82b082eSAndreas Gohr true, 110e82b082eSAndreas Gohr 'lock' 111e82b082eSAndreas Gohr ); 112e82b082eSAndreas Gohr 113e82b082eSAndreas Gohr /* unlock command */ 114e82b082eSAndreas Gohr $options->registerCommand( 115e82b082eSAndreas Gohr 'unlock', 116e82b082eSAndreas Gohr 'Removes a lock for a wiki page.' 117e82b082eSAndreas Gohr ); 118e82b082eSAndreas Gohr $options->registerArgument( 119e82b082eSAndreas Gohr 'wikipage', 120e82b082eSAndreas Gohr 'The wiki page to unlock', 121e82b082eSAndreas Gohr true, 122e82b082eSAndreas Gohr 'unlock' 123e82b082eSAndreas Gohr ); 1242c23659dSRobin Getz 1252c23659dSRobin Getz /* gmeta command */ 1262c23659dSRobin Getz $options->registerCommand( 1273fe01844SAndreas Gohr 'getmeta', 1282c23659dSRobin Getz 'Prints metadata value for a page to stdout.' 1292c23659dSRobin Getz ); 1302c23659dSRobin Getz $options->registerArgument( 1312c23659dSRobin Getz 'wikipage', 1322c23659dSRobin Getz 'The wiki page to get the metadata for', 1332c23659dSRobin Getz true, 1343fe01844SAndreas Gohr 'getmeta' 1352c23659dSRobin Getz ); 1362c23659dSRobin Getz $options->registerArgument( 1372c23659dSRobin Getz 'key', 1382c23659dSRobin Getz 'The name of the metadata item to be retrieved.' . "\n" . 1392c23659dSRobin Getz 'If empty, an array of all the metadata items is returned.' ."\n" . 1402c23659dSRobin Getz 'For retrieving items that are stored in sub-arrays, separate the ' . 1412c23659dSRobin Getz 'keys of the different levels by spaces, in quotes, eg "date modified".', 1422c23659dSRobin Getz false, 1433fe01844SAndreas Gohr 'getmeta' 1442c23659dSRobin Getz ); 145e82b082eSAndreas Gohr } 146e82b082eSAndreas Gohr 147e82b082eSAndreas Gohr /** 148e82b082eSAndreas Gohr * Your main program 149e82b082eSAndreas Gohr * 150e82b082eSAndreas Gohr * Arguments and options have been parsed when this is run 151e82b082eSAndreas Gohr * 152cbeaa4a0SAndreas Gohr * @param Options $options 153e82b082eSAndreas Gohr * @return void 154e82b082eSAndreas Gohr */ 155*d868eb89SAndreas Gohr protected function main(Options $options) 156*d868eb89SAndreas Gohr { 157e82b082eSAndreas Gohr $this->force = $options->getOpt('force', false); 158e82b082eSAndreas Gohr $this->username = $options->getOpt('user', $this->getUser()); 159e82b082eSAndreas Gohr 160e82b082eSAndreas Gohr $command = $options->getCmd(); 161cbeaa4a0SAndreas Gohr $args = $options->getArgs(); 162e82b082eSAndreas Gohr switch($command) { 1631caeb00aSHarry Fuecks case 'checkout': 164cbeaa4a0SAndreas Gohr $wiki_id = array_shift($args); 165cbeaa4a0SAndreas Gohr $localfile = array_shift($args); 166e82b082eSAndreas Gohr $this->commandCheckout($wiki_id, $localfile); 1671caeb00aSHarry Fuecks break; 1681caeb00aSHarry Fuecks case 'commit': 169cbeaa4a0SAndreas Gohr $localfile = array_shift($args); 170cbeaa4a0SAndreas Gohr $wiki_id = array_shift($args); 171e82b082eSAndreas Gohr $this->commandCommit( 172e82b082eSAndreas Gohr $localfile, 173e82b082eSAndreas Gohr $wiki_id, 174e82b082eSAndreas Gohr $options->getOpt('message', ''), 175e82b082eSAndreas Gohr $options->getOpt('trivial', false) 176e82b082eSAndreas Gohr ); 1771caeb00aSHarry Fuecks break; 1781caeb00aSHarry Fuecks case 'lock': 179cbeaa4a0SAndreas Gohr $wiki_id = array_shift($args); 180e82b082eSAndreas Gohr $this->obtainLock($wiki_id); 181e82b082eSAndreas Gohr $this->success("$wiki_id locked"); 1821caeb00aSHarry Fuecks break; 1831caeb00aSHarry Fuecks case 'unlock': 184cbeaa4a0SAndreas Gohr $wiki_id = array_shift($args); 185e82b082eSAndreas Gohr $this->clearLock($wiki_id); 186e82b082eSAndreas Gohr $this->success("$wiki_id unlocked"); 1871caeb00aSHarry Fuecks break; 1883fe01844SAndreas Gohr case 'getmeta': 1892c23659dSRobin Getz $wiki_id = array_shift($args); 1902c23659dSRobin Getz $key = trim(array_shift($args)); 1918d670f35SAndreas Gohr $meta = p_get_metadata($wiki_id, $key, METADATA_RENDER_UNLIMITED); 1928d670f35SAndreas Gohr echo trim(json_encode($meta, JSON_PRETTY_PRINT)); 1938d670f35SAndreas Gohr echo "\n"; 1942c23659dSRobin Getz break; 1951caeb00aSHarry Fuecks default: 196e82b082eSAndreas Gohr echo $options->help(); 1971caeb00aSHarry Fuecks } 1981caeb00aSHarry Fuecks } 1991caeb00aSHarry Fuecks 200e82b082eSAndreas Gohr /** 201e82b082eSAndreas Gohr * Check out a file 202e82b082eSAndreas Gohr * 203e82b082eSAndreas Gohr * @param string $wiki_id 204e82b082eSAndreas Gohr * @param string $localfile 205e82b082eSAndreas Gohr */ 206*d868eb89SAndreas Gohr protected function commandCheckout($wiki_id, $localfile) 207*d868eb89SAndreas Gohr { 208e82b082eSAndreas Gohr global $conf; 209e82b082eSAndreas Gohr 210e82b082eSAndreas Gohr $wiki_id = cleanID($wiki_id); 211e82b082eSAndreas Gohr $wiki_fn = wikiFN($wiki_id); 212e82b082eSAndreas Gohr 213e82b082eSAndreas Gohr if(!file_exists($wiki_fn)) { 214e82b082eSAndreas Gohr $this->fatal("$wiki_id does not yet exist"); 215e82b082eSAndreas Gohr } 216e82b082eSAndreas Gohr 217e82b082eSAndreas Gohr if(empty($localfile)) { 218b1f206e1SAndreas Gohr $localfile = getcwd() . '/' . PhpString::basename($wiki_fn); 219e82b082eSAndreas Gohr } 220e82b082eSAndreas Gohr 221e82b082eSAndreas Gohr if(!file_exists(dirname($localfile))) { 222e82b082eSAndreas Gohr $this->fatal("Directory " . dirname($localfile) . " does not exist"); 223e82b082eSAndreas Gohr } 224e82b082eSAndreas Gohr 225b1f206e1SAndreas Gohr if(stristr(realpath(dirname($localfile)), (string) realpath($conf['datadir'])) !== false) { 226e82b082eSAndreas Gohr $this->fatal("Attempt to check out file into data directory - not allowed"); 227e82b082eSAndreas Gohr } 228e82b082eSAndreas Gohr 229e82b082eSAndreas Gohr $this->obtainLock($wiki_id); 230e82b082eSAndreas Gohr 231e82b082eSAndreas Gohr if(!copy($wiki_fn, $localfile)) { 232e82b082eSAndreas Gohr $this->clearLock($wiki_id); 233e82b082eSAndreas Gohr $this->fatal("Unable to copy $wiki_fn to $localfile"); 234e82b082eSAndreas Gohr } 235e82b082eSAndreas Gohr 236e82b082eSAndreas Gohr $this->success("$wiki_id > $localfile"); 237e82b082eSAndreas Gohr } 238e82b082eSAndreas Gohr 239e82b082eSAndreas Gohr /** 240e82b082eSAndreas Gohr * Save a file as a new page revision 241e82b082eSAndreas Gohr * 242e82b082eSAndreas Gohr * @param string $localfile 243e82b082eSAndreas Gohr * @param string $wiki_id 244e82b082eSAndreas Gohr * @param string $message 245e82b082eSAndreas Gohr * @param bool $minor 246e82b082eSAndreas Gohr */ 247*d868eb89SAndreas Gohr protected function commandCommit($localfile, $wiki_id, $message, $minor) 248*d868eb89SAndreas Gohr { 249e82b082eSAndreas Gohr $wiki_id = cleanID($wiki_id); 250e82b082eSAndreas Gohr $message = trim($message); 251e82b082eSAndreas Gohr 252e82b082eSAndreas Gohr if(!file_exists($localfile)) { 253e82b082eSAndreas Gohr $this->fatal("$localfile does not exist"); 254e82b082eSAndreas Gohr } 255e82b082eSAndreas Gohr 256e82b082eSAndreas Gohr if(!is_readable($localfile)) { 257e82b082eSAndreas Gohr $this->fatal("Cannot read from $localfile"); 258e82b082eSAndreas Gohr } 259e82b082eSAndreas Gohr 260e82b082eSAndreas Gohr if(!$message) { 261e82b082eSAndreas Gohr $this->fatal("Summary message required"); 262e82b082eSAndreas Gohr } 263e82b082eSAndreas Gohr 264e82b082eSAndreas Gohr $this->obtainLock($wiki_id); 265e82b082eSAndreas Gohr 266e82b082eSAndreas Gohr saveWikiText($wiki_id, file_get_contents($localfile), $message, $minor); 267e82b082eSAndreas Gohr 268e82b082eSAndreas Gohr $this->clearLock($wiki_id); 269e82b082eSAndreas Gohr 270e82b082eSAndreas Gohr $this->success("$localfile > $wiki_id"); 271e82b082eSAndreas Gohr } 272e82b082eSAndreas Gohr 273e82b082eSAndreas Gohr /** 274e82b082eSAndreas Gohr * Lock the given page or exit 275e82b082eSAndreas Gohr * 276e82b082eSAndreas Gohr * @param string $wiki_id 277e82b082eSAndreas Gohr */ 278*d868eb89SAndreas Gohr protected function obtainLock($wiki_id) 279*d868eb89SAndreas Gohr { 280e82b082eSAndreas Gohr if($this->force) $this->deleteLock($wiki_id); 281e82b082eSAndreas Gohr 282e82b082eSAndreas Gohr $_SERVER['REMOTE_USER'] = $this->username; 283bf00b396SAndreas Gohr 284e82b082eSAndreas Gohr if(checklock($wiki_id)) { 285e82b082eSAndreas Gohr $this->error("Page $wiki_id is already locked by another user"); 286e82b082eSAndreas Gohr exit(1); 287e82b082eSAndreas Gohr } 288e82b082eSAndreas Gohr 289e82b082eSAndreas Gohr lock($wiki_id); 290e82b082eSAndreas Gohr 291cb62a664SJustinTrouble if(checklock($wiki_id)) { 292e82b082eSAndreas Gohr $this->error("Unable to obtain lock for $wiki_id "); 293e82b082eSAndreas Gohr var_dump(checklock($wiki_id)); 294e82b082eSAndreas Gohr exit(1); 295e82b082eSAndreas Gohr } 296e82b082eSAndreas Gohr } 297e82b082eSAndreas Gohr 298e82b082eSAndreas Gohr /** 299e82b082eSAndreas Gohr * Clear the lock on the given page 300e82b082eSAndreas Gohr * 301e82b082eSAndreas Gohr * @param string $wiki_id 302e82b082eSAndreas Gohr */ 303*d868eb89SAndreas Gohr protected function clearLock($wiki_id) 304*d868eb89SAndreas Gohr { 305e82b082eSAndreas Gohr if($this->force) $this->deleteLock($wiki_id); 306e82b082eSAndreas Gohr 307e82b082eSAndreas Gohr $_SERVER['REMOTE_USER'] = $this->username; 308e82b082eSAndreas Gohr if(checklock($wiki_id)) { 309e82b082eSAndreas Gohr $this->error("Page $wiki_id is locked by another user"); 310e82b082eSAndreas Gohr exit(1); 311e82b082eSAndreas Gohr } 312e82b082eSAndreas Gohr 313e82b082eSAndreas Gohr unlock($wiki_id); 314e82b082eSAndreas Gohr 315e82b082eSAndreas Gohr if(file_exists(wikiLockFN($wiki_id))) { 316e82b082eSAndreas Gohr $this->error("Unable to clear lock for $wiki_id"); 317e82b082eSAndreas Gohr exit(1); 318e82b082eSAndreas Gohr } 319e82b082eSAndreas Gohr } 320e82b082eSAndreas Gohr 321e82b082eSAndreas Gohr /** 322e82b082eSAndreas Gohr * Forcefully remove a lock on the page given 323e82b082eSAndreas Gohr * 324e82b082eSAndreas Gohr * @param string $wiki_id 325e82b082eSAndreas Gohr */ 326*d868eb89SAndreas Gohr protected function deleteLock($wiki_id) 327*d868eb89SAndreas Gohr { 328e82b082eSAndreas Gohr $wikiLockFN = wikiLockFN($wiki_id); 329e82b082eSAndreas Gohr 330e82b082eSAndreas Gohr if(file_exists($wikiLockFN)) { 331e82b082eSAndreas Gohr if(!unlink($wikiLockFN)) { 332e82b082eSAndreas Gohr $this->error("Unable to delete $wikiLockFN"); 333e82b082eSAndreas Gohr exit(1); 334e82b082eSAndreas Gohr } 335e82b082eSAndreas Gohr } 336e82b082eSAndreas Gohr } 337e82b082eSAndreas Gohr 338e82b082eSAndreas Gohr /** 339e82b082eSAndreas Gohr * Get the current user's username from the environment 340e82b082eSAndreas Gohr * 341e82b082eSAndreas Gohr * @return string 342e82b082eSAndreas Gohr */ 343*d868eb89SAndreas Gohr protected function getUser() 344*d868eb89SAndreas Gohr { 3451caeb00aSHarry Fuecks $user = getenv('USER'); 346feec2ab4SAndreas Gohr if(empty ($user)) { 3471caeb00aSHarry Fuecks $user = getenv('USERNAME'); 3481caeb00aSHarry Fuecks } else { 3491caeb00aSHarry Fuecks return $user; 3501caeb00aSHarry Fuecks } 351feec2ab4SAndreas Gohr if(empty ($user)) { 3521caeb00aSHarry Fuecks $user = 'admin'; 3531caeb00aSHarry Fuecks } 3541caeb00aSHarry Fuecks return $user; 3551caeb00aSHarry Fuecks } 3561caeb00aSHarry Fuecks} 3571caeb00aSHarry Fuecks 358e82b082eSAndreas Gohr// Main 359e82b082eSAndreas Gohr$cli = new PageCLI(); 360e82b082eSAndreas Gohr$cli->run(); 361