PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/bin/dwpage.php

https://gitlab.com/michield/dokuwiki
PHP | 378 lines | 265 code | 96 blank | 17 comment | 51 complexity | 65da5623d16429de71c7570d0072249c MD5 | raw file
  1. #!/usr/bin/php
  2. <?php
  3. #------------------------------------------------------------------------------
  4. if ('cli' != php_sapi_name()) die();
  5. ini_set('memory_limit','128M');
  6. if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
  7. require_once DOKU_INC.'inc/init.php';
  8. require_once DOKU_INC.'inc/common.php';
  9. require_once DOKU_INC.'inc/cliopts.php';
  10. #------------------------------------------------------------------------------
  11. function usage($action) {
  12. switch ( $action ) {
  13. case 'checkout':
  14. print "Usage: dwpage.php [opts] checkout <wiki:page> [working_file]
  15. Checks out a file from the repository, using the wiki id and obtaining
  16. a lock for the page.
  17. If a working_file is specified, this is where the page is copied to.
  18. Otherwise defaults to the same as the wiki page in the current
  19. working directory.
  20. EXAMPLE
  21. $ ./dwpage.php checkout wiki:syntax ./new_syntax.txt
  22. OPTIONS
  23. -h, --help=<action>: get help
  24. -f: force obtaining a lock for the page (generally bad idea)
  25. ";
  26. break;
  27. case 'commit':
  28. print "Usage: dwpage.php [opts] -m \"Msg\" commit <working_file> <wiki:page>
  29. Checks in the working_file into the repository using the specified
  30. wiki id, archiving the previous version.
  31. EXAMPLE
  32. $ ./dwpage.php -m \"Some message\" commit ./new_syntax.txt wiki:syntax
  33. OPTIONS
  34. -h, --help=<action>: get help
  35. -f: force obtaining a lock for the page (generally bad idea)
  36. -t, trivial: minor change
  37. -m (required): Summary message describing the change
  38. ";
  39. break;
  40. case 'lock':
  41. print "Usage: dwpage.php [opts] lock <wiki:page>
  42. Obtains or updates a lock for a wiki page
  43. EXAMPLE
  44. $ ./dwpage.php lock wiki:syntax
  45. OPTIONS
  46. -h, --help=<action>: get help
  47. -f: force obtaining a lock for the page (generally bad idea)
  48. ";
  49. break;
  50. case 'unlock':
  51. print "Usage: dwpage.php [opts] unlock <wiki:page>
  52. Removes a lock for a wiki page.
  53. EXAMPLE
  54. $ ./dwpage.php unlock wiki:syntax
  55. OPTIONS
  56. -h, --help=<action>: get help
  57. -f: force obtaining a lock for the page (generally bad idea)
  58. ";
  59. break;
  60. default:
  61. print "Usage: dwpage.php [opts] <action>
  62. Utility to help command line Dokuwiki page editing, allow
  63. pages to be checked out for editing then committed after changes
  64. Normal operation would be;
  65. ACTIONS
  66. checkout: see $ dwpage.php --help=checkout
  67. commit: see $ dwpage.php --help=commit
  68. lock: see $ dwpage.php --help=lock
  69. OPTIONS
  70. -h, --help=<action>: get help
  71. e.g. $ ./dwpage.php -hcommit
  72. e.g. $ ./dwpage.php --help=commit
  73. ";
  74. break;
  75. }
  76. }
  77. #------------------------------------------------------------------------------
  78. function getUser() {
  79. $user = getenv('USER');
  80. if (empty ($user)) {
  81. $user = getenv('USERNAME');
  82. } else {
  83. return $user;
  84. }
  85. if (empty ($user)) {
  86. $user = 'admin';
  87. }
  88. return $user;
  89. }
  90. #------------------------------------------------------------------------------
  91. function getSuppliedArgument($OPTS, $short, $long) {
  92. $arg = $OPTS->get($short);
  93. if ( is_null($arg) ) {
  94. $arg = $OPTS->get($long);
  95. }
  96. return $arg;
  97. }
  98. #------------------------------------------------------------------------------
  99. function obtainLock($WIKI_ID) {
  100. global $USERNAME;
  101. if ( !file_exists(wikiFN($WIKI_ID)) ) {
  102. fwrite( STDERR, "$WIKI_ID does not yet exist\n");
  103. }
  104. $_SERVER['REMOTE_USER'] = $USERNAME;
  105. if ( checklock($WIKI_ID) ) {
  106. fwrite( STDERR, "Page $WIKI_ID is already locked by another user\n");
  107. exit(1);
  108. }
  109. lock($WIKI_ID);
  110. $_SERVER['REMOTE_USER'] = '_'.$USERNAME.'_';
  111. if ( checklock($WIKI_ID) != $USERNAME ) {
  112. fwrite( STDERR, "Unable to obtain lock for $WIKI_ID\n" );
  113. exit(1);
  114. }
  115. }
  116. #------------------------------------------------------------------------------
  117. function clearLock($WIKI_ID) {
  118. global $USERNAME ;
  119. if ( !file_exists(wikiFN($WIKI_ID)) ) {
  120. fwrite( STDERR, "$WIKI_ID does not yet exist\n");
  121. }
  122. $_SERVER['REMOTE_USER'] = $USERNAME;
  123. if ( checklock($WIKI_ID) ) {
  124. fwrite( STDERR, "Page $WIKI_ID is locked by another user\n");
  125. exit(1);
  126. }
  127. unlock($WIKI_ID);
  128. if ( file_exists(wikiLockFN($WIKI_ID)) ) {
  129. fwrite( STDERR, "Unable to clear lock for $WIKI_ID\n" );
  130. exit(1);
  131. }
  132. }
  133. #------------------------------------------------------------------------------
  134. function deleteLock($WIKI_ID) {
  135. $wikiLockFN = wikiLockFN($WIKI_ID);
  136. if ( file_exists($wikiLockFN) ) {
  137. if ( !unlink($wikiLockFN) ) {
  138. fwrite( STDERR, "Unable to delete $wikiLockFN\n" );
  139. exit(1);
  140. }
  141. }
  142. }
  143. #------------------------------------------------------------------------------
  144. $USERNAME = getUser();
  145. $CWD = getcwd();
  146. $SYSTEM_ID = '127.0.0.1';
  147. #------------------------------------------------------------------------------
  148. $OPTS = Doku_Cli_Opts::getOptions(
  149. __FILE__,
  150. 'h::fm:u:s:t',
  151. array(
  152. 'help==',
  153. 'user=',
  154. 'system=',
  155. 'trivial',
  156. )
  157. );
  158. if ( $OPTS->isError() ) {
  159. print $OPTS->getMessage()."\n";
  160. exit(1);
  161. }
  162. if ( $OPTS->has('h') or $OPTS->has('help') or !$OPTS->hasArgs() ) {
  163. usage(getSuppliedArgument($OPTS,'h','help'));
  164. exit(0);
  165. }
  166. if ( $OPTS->has('u') or $OPTS->has('user') ) {
  167. $USERNAME = getSuppliedArgument($OPTS,'u','user');
  168. }
  169. if ( $OPTS->has('s') or $OPTS->has('system') ) {
  170. $SYSTEM_ID = getSuppliedArgument($OPTS,'s','system');
  171. }
  172. #------------------------------------------------------------------------------
  173. switch ( $OPTS->arg(0) ) {
  174. #----------------------------------------------------------------------
  175. case 'checkout':
  176. $WIKI_ID = $OPTS->arg(1);
  177. if ( !$WIKI_ID ) {
  178. fwrite( STDERR, "Wiki page ID required\n");
  179. exit(1);
  180. }
  181. $WIKI_FN = wikiFN($WIKI_ID);
  182. if ( !file_exists($WIKI_FN) ) {
  183. fwrite( STDERR, "$WIKI_ID does not yet exist\n");
  184. exit(1);
  185. }
  186. $TARGET_FN = $OPTS->arg(2);
  187. if ( empty($TARGET_FN) ) {
  188. $TARGET_FN = getcwd().'/'.utf8_basename($WIKI_FN);
  189. }
  190. if ( !file_exists(dirname($TARGET_FN)) ) {
  191. fwrite( STDERR, "Directory ".dirname($TARGET_FN)." does not exist\n");
  192. exit(1);
  193. }
  194. if ( stristr( realpath(dirname($TARGET_FN)), realpath($conf['datadir']) ) !== false ) {
  195. fwrite( STDERR, "Attempt to check out file into data directory - not allowed\n");
  196. exit(1);
  197. }
  198. if ( $OPTS->has('f') ) {
  199. deleteLock($WIKI_ID);
  200. }
  201. obtainLock($WIKI_ID);
  202. # Need to lock the file first?
  203. if ( !copy($WIKI_FN, $TARGET_FN) ) {
  204. fwrite( STDERR, "Unable to copy $WIKI_FN to $TARGET_FN\n");
  205. clearLock($WIKI_ID);
  206. exit(1);
  207. }
  208. print "$WIKI_ID > $TARGET_FN\n";
  209. exit(0);
  210. break;
  211. #----------------------------------------------------------------------
  212. case 'commit':
  213. $TARGET_FN = $OPTS->arg(1);
  214. if ( !$TARGET_FN ) {
  215. fwrite( STDERR, "Target filename required\n");
  216. exit(1);
  217. }
  218. if ( !file_exists($TARGET_FN) ) {
  219. fwrite( STDERR, "$TARGET_FN does not exist\n");
  220. exit(1);
  221. }
  222. if ( !is_readable($TARGET_FN) ) {
  223. fwrite( STDERR, "Cannot read from $TARGET_FN\n");
  224. exit(1);
  225. }
  226. $WIKI_ID = $OPTS->arg(2);
  227. if ( !$WIKI_ID ) {
  228. fwrite( STDERR, "Wiki page ID required\n");
  229. exit(1);
  230. }
  231. if ( !$OPTS->has('m') ) {
  232. fwrite( STDERR, "Summary message required\n");
  233. exit(1);
  234. }
  235. if ( $OPTS->has('f') ) {
  236. deleteLock($WIKI_ID);
  237. }
  238. $_SERVER['REMOTE_USER'] = $USERNAME;
  239. if ( checklock($WIKI_ID) ) {
  240. fwrite( STDERR, "$WIKI_ID is locked by another user\n");
  241. exit(1);
  242. }
  243. obtainLock($WIKI_ID);
  244. saveWikiText($WIKI_ID, file_get_contents($TARGET_FN), $OPTS->get('m'), $OPTS->has('t'));
  245. clearLock($WIKI_ID);
  246. exit(0);
  247. break;
  248. #----------------------------------------------------------------------
  249. case 'lock':
  250. $WIKI_ID = $OPTS->arg(1);
  251. if ( !$WIKI_ID ) {
  252. fwrite( STDERR, "Wiki page ID required\n");
  253. exit(1);
  254. }
  255. if ( $OPTS->has('f') ) {
  256. deleteLock($WIKI_ID);
  257. }
  258. obtainLock($WIKI_ID);
  259. print "Locked : $WIKI_ID\n";
  260. exit(0);
  261. break;
  262. #----------------------------------------------------------------------
  263. case 'unlock':
  264. $WIKI_ID = $OPTS->arg(1);
  265. if ( !$WIKI_ID ) {
  266. fwrite( STDERR, "Wiki page ID required\n");
  267. exit(1);
  268. }
  269. if ( $OPTS->has('f') ) {
  270. deleteLock($WIKI_ID);
  271. } else {
  272. clearLock($WIKI_ID);
  273. }
  274. print "Unlocked : $WIKI_ID\n";
  275. exit(0);
  276. break;
  277. #----------------------------------------------------------------------
  278. default:
  279. fwrite( STDERR, "Invalid action ".$OPTS->arg(0)."\n" );
  280. exit(1);
  281. break;
  282. }