/wp-content/plugins/postie/util/wp-i18n/cron-svn-pots.php

https://bitbucket.org/rchlmrtn/chiari · PHP · 117 lines · 65 code · 3 blank · 49 comment · 4 complexity · de19a99d0ffa77485f9b1cbce86dde5f MD5 · raw file

  1. <?php
  2. require_once dirname( __FILE__ ) . '/makepot.php';
  3. function silent_system( $command ) {
  4. global $at_least_one_error;
  5. ob_start();
  6. system( "$command 2>&1", $exit_code );
  7. $output = ob_get_contents();
  8. ob_end_clean();
  9. if ( $exit_code != 0 ) {
  10. echo "ERROR:\t$command\nCODE:\t$exit_code\nOUTPUT:\n";
  11. echo $output."\n";
  12. } else {
  13. echo "OK:\t$command\n";
  14. }
  15. return $exit_code;
  16. }
  17. $options = getopt( 'c:p:m:n:sa:b:u:w:df' );
  18. if ( empty( $options ) ) {
  19. ?>
  20. -s No branch/version directories, it's all flat
  21. -c Application svn checkout
  22. -p POT svn checkout
  23. -m MakePOT project
  24. -n POT filename
  25. -a Relative path of application inside version dir in -c
  26. -b Relative patch of POT dir inside version dir in -p
  27. -u SVN username (optional)
  28. -w SVN password (optional)
  29. -d Dry-run
  30. -f Fast - do not update checkouts
  31. <?php
  32. die;
  33. }
  34. $application_svn_checkout = realpath( $options['c'] );
  35. $pot_svn_checkout = realpath( $options['p'] );
  36. $makepot_project = str_replace( '-', '_', $options['m'] );
  37. $pot_name = $options['n'];
  38. $no_branch_dirs = isset( $options['s'] );
  39. $relative_application_path = isset( $options['a'] )? '/'.$options['a'] : '';
  40. $relative_pot_path = isset( $options['b'] )? '/'.$options['b'] : '';
  41. $dry_run = isset( $options['d'] );
  42. $makepot = new MakePOT;
  43. $svn_args = array('--non-interactive');
  44. if ( isset( $options['u'] ) && isset( $options['w'] ) ) {
  45. $svn_args[] = '--username='.$options['u'];
  46. $svn_args[] = '--password='.$options['w'];
  47. $svn_args[] = '--no-auth-cache';
  48. }
  49. $svn_args_str = implode( ' ', array_map( 'escapeshellarg', $svn_args ) );
  50. $svn = 'svn '.$svn_args_str;
  51. $versions = array();
  52. chdir( $application_svn_checkout );
  53. if ( ! isset( $options['f'] ) ) {
  54. $exit = silent_system( "$svn cleanup" );
  55. if ( 0 != $exit ) die();
  56. $exit = silent_system( "$svn up" );
  57. if ( 0 != $exit ) die();
  58. }
  59. if ( is_dir( 'trunk' ) ) $versions[] = 'trunk';
  60. $branches = glob( 'branches/*' );
  61. if ( false !== $branches ) $versions = array_merge( $versions, $branches );
  62. $tags = glob( 'tags/*' );
  63. if ( false !== $tags ) $versions = array_merge( $versions, $tags );
  64. if ( $no_branch_dirs ) {
  65. $versions = array( '.' );
  66. }
  67. chdir( $pot_svn_checkout );
  68. if ( $application_svn_checkout != $pot_svn_checkout && ! isset( $options['f'] ) ) {
  69. $exit = silent_system( "$svn cleanup" );
  70. if ( 0 != $exit ) die();
  71. $exit = silent_system( "$svn up" );
  72. if ( 0 != $exit ) die();
  73. }
  74. $real_application_svn_checkout = realpath( $application_svn_checkout );
  75. foreach( $versions as $version ) {
  76. $application_path = "$real_application_svn_checkout/$version{$relative_application_path}";
  77. if ( !is_dir( $application_path ) ) continue;
  78. $pot = "$version{$relative_pot_path}/$pot_name";
  79. $exists = is_file( $pot );
  80. // do not update old tag pots
  81. if ( 'tags/' == substr( $version, 0, 5 ) && $exists ) continue;
  82. if ( !is_dir( $version ) ) {
  83. $exit = silent_system( "$svn mkdir $version" );
  84. if ( 0 != $exit ) continue;
  85. }
  86. if ( !is_dir(dirname("$pot_svn_checkout/$pot")) ) continue;
  87. if ( !call_user_func( array( &$makepot, $makepot_project ), $application_path, "$pot_svn_checkout/$pot" ) ) continue;
  88. if ( !file_exists( "$pot_svn_checkout/$pot" ) ) continue;
  89. if ( !$exists ) {
  90. $exit = silent_system( "$svn add $pot" );
  91. if ( 0 != $exit ) continue;
  92. }
  93. // do not commit if the difference is only in the header, but always commit a new file
  94. $real_differences = `svn diff $pot | wc -l` > 16;
  95. $target = $exists ? $pot : $version;
  96. if ( !$exists || $real_differences ) {
  97. preg_match( '/Revision:\s+(\d+)/', `svn info $application_path`, $matches );
  98. $logmsg = isset( $matches[1] ) && intval( $matches[1] )? "POT, generated from r".intval( $matches[1] ) : 'Automatic POT update';
  99. $command = "$svn ci $target --non-interactive --message='$logmsg'";
  100. if ( !$dry_run )
  101. silent_system( $command );
  102. else
  103. echo "CMD:\t$command\n";
  104. } else {
  105. silent_system( "$svn revert $target" );
  106. }
  107. }