vertrad.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/bin/php
  2. <?php
  3. /*
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. const N="\n";
  16. const SNAME='vertrad.php';
  17. const SVERS='0.0.1';
  18. const RED="\33[1;31m";
  19. const NORM="\033[0m";
  20. $opts=[
  21. 'files'=>[],
  22. 'prompt'=>false
  23. ];
  24. $help=
  25. 'SYNOPSIS
  26. '.SNAME.' [options] <file[s]>
  27. DESCRIPTION
  28. This is '.SNAME.' v'.SVERS.', a CLI php script that ...
  29. OPTIONS
  30. -p, --prompt
  31. Prompt for editing on each suspect pair of msgid-msgstr.
  32. -h, --help
  33. Show this help text and exit.
  34. ...
  35. NOTES
  36. ...
  37. EXIT VALUES
  38. 0: regular run
  39. 1: fatal error
  40. DISCLAIMER AND LICENSE
  41. This program comes with ABSOLUTELY NO WARRANTY; for details see the source.
  42. This is free software, and you are welcome to redistribute it under certain
  43. conditions; see <http://www.gnu.org/licenses/> for details.'.N;
  44. for ($i=1; $i<$argc; $i++) {
  45. if ($argv[$i][0]=='-') {
  46. switch ($argv[$i]) {
  47. case '--makereadme':
  48. file_put_contents(__DIR__.'/README.md','```text'.N.$help.'```'.N);
  49. exit(0);
  50. break;
  51. case '-p':
  52. case '--prompt':
  53. $opts['prompt']=true;
  54. break;
  55. case '-h':
  56. case '--help':
  57. echo($help);
  58. exit(0);
  59. break;
  60. default:
  61. fwrite(STDERR,'Error: «'.$argv[$i].'» is not a valid option.'.N);
  62. exit(1);
  63. }
  64. } elseif (is_file($argv[$i]) && is_readable($argv[$i])) {
  65. $opts['files'][]=$argv[$i];
  66. } else {
  67. fwrite(STDERR,'Error: «'.$argv[$i].'» is not a readable file.'.N);
  68. exit(1);
  69. }
  70. }
  71. //print_r($opts); exit(0);
  72. if (count($opts['files'])==0) {
  73. fwrite(STDERR,'Error: you have to specify at least one file.'.N);
  74. exit(1);
  75. }
  76. $ts=0;
  77. foreach ($opts['files'] as $file) {
  78. $cont=@file($file,FILE_IGNORE_NEW_LINES);
  79. if ($cont!==false) {
  80. $s=0;
  81. $i=0;
  82. $c=count($cont);
  83. while ($i<$c) {
  84. if (preg_match('/^msgid "/u',$cont[$i])===1) {
  85. $msgid=$cont[$i];
  86. $i++;
  87. $msgstr=$cont[$i];
  88. if ($msgstr!='msgstr ""') {
  89. $show=false;
  90. if (preg_match_all('/\\\"/u',$msgid)!=preg_match_all('/\\\"/u',$msgstr)) $show=true;
  91. if (preg_match_all('/</u',$msgid)!=preg_match_all('/</u',$msgstr)) $show=true;
  92. if (preg_match_all('/>/u',$msgid)!=preg_match_all('/>/u',$msgstr)) $show=true;
  93. if (preg_match_all('/\'/u',$msgid)!=preg_match_all('/\'/u',$msgstr)) $show=true;
  94. if ($show) {
  95. $s++;
  96. echo('«'.$file.'»:'.$i.N.hil($msgid).N.hil($msgstr).N);
  97. if ($opts['prompt']) {
  98. $inp='+';
  99. while (!in_array($inp,['y','n','e','']))
  100. $inp=strtolower(prompt('Do you want to edit it? [(y)es/(N)o/(e)xit] '));
  101. if ($inp=='y')
  102. //system('nano +'.($i+1).' '.escapeshellarg($file).' > `tty`');
  103. system('kate -l '.($i+1).' '.escapeshellarg($file));
  104. elseif ($inp=='e')
  105. exit(0);
  106. }
  107. echo(N);
  108. }
  109. }
  110. }
  111. $i++;
  112. }
  113. //echo('«'.$file.'» total suspects: '.$s.N);
  114. $summ[]='«'.$file.'» total suspects: '.$s;
  115. $ts+=$s;
  116. } else {
  117. fwrite(STDERR,'Error: could not open «'.$file.'».'.N);
  118. exit(1);
  119. }
  120. }
  121. foreach ($summ as $row)
  122. echo($row.N);
  123. echo('Grand total of suspects: '.$ts.N);
  124. exit(0);
  125. function hil($msg) {
  126. $msg=preg_replace('/\\\"/u',RED.'\\\"'.NORM,$msg);
  127. $msg=preg_replace('/</u',RED.'<'.NORM,$msg);
  128. $msg=preg_replace('/>/u',RED.'>'.NORM,$msg);
  129. $msg=preg_replace('/\'/u',RED.'\''.NORM,$msg);
  130. return($msg);
  131. }
  132. function prompt($prompt) {
  133. echo($prompt);
  134. $line=stream_get_line(STDIN, 1024, PHP_EOL);
  135. return(trim($line));
  136. }
  137. ?>