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( 124*3fe01844SAndreas 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, 131*3fe01844SAndreas 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, 140*3fe01844SAndreas Gohr 'getmeta' 1412c23659dSRobin Getz ); 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; 185*3fe01844SAndreas Gohr case 'getmeta': 1862c23659dSRobin Getz $wiki_id = array_shift($args); 1872c23659dSRobin Getz $key = trim(array_shift($args)); 1882c23659dSRobin Getz $meta=print_r(p_get_metadata($wiki_id, $key), true); 1892c23659dSRobin Getz print($meta); 1902c23659dSRobin Getz if (strcmp(substr($meta, -1), "\n")) 1912c23659dSRobin Getz print("\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)) { 2158cbc5ee8SAndreas Gohr $localfile = getcwd() . '/' . \dokuwiki\Utf8\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 222e82b082eSAndreas Gohr if(stristr(realpath(dirname($localfile)), 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